1. Formula vs. selecting rows when using functions?

Hi everyone,

I've been learning R by going through "The R book" by Michael Crawley, and by reading and searching through the various excellent help files from R and around the internet. I was playing with what I thought was equivalent code to make sure I was understanding what I was asking R to do before I move into more difficult stuff, and came across something that I really don't understand and can't figure out.

I have a dataframe that consists of 2 factors (Nest and Egg), and one continuous variable (Incubation period). Incubation period is paired by Nest, and each Nest has an A and a B egg. I had the file sorted so within the dataframe, the Nest and Egg columns looked roughly like this:

1 a
1 b
2 a
2 b
3 a
3 b
4 a
4 b
...
and there was a number for Incubation for each row.

I ran a t.test on it in 2 ways like this:

with(ABeggs08nona,t.test(Incubation~Egg,paired=T))

and

with(ABeggs08nona,t.test(Incubation[Egg == "a"], Incubation[Egg == "b"],paired=T))

I'd also unstacked the data and ran a paired t.test on it that way to make sure I'd gotten it right; I got the same result from each, and it seemed to me that the syntax from the 2 codes above was equivalent.

In doing more searching on testing variances however, I've done the same thing for fligner.test:

with(ABeggs08nona,fligner.test(Incubation~Egg))

and

with(ABeggs08nona,fligner.test(Incubation[Egg == "a"], Incubation[Egg == "b"]))

In this case, I got RADICALLY different results.

So, what am I missing? It looks to me that I've asked it, in the latter case, to pick out the rows that contain "a"s in the Egg column, and compare their Incubation periods to those in Egg's "b" column. Is that not what, in the first case, Incubation~Egg (Incubation as a function of Egg) is saying too?

Thanks for any tips you can provide!!

2. Re: Writing a function?

I have not used this function, but I think that you will find the reason for the differences by comparing the help files for the fligner.test and t.test. When 2 terms are use in the fligner.test (x, and g), x is a numeric vector of data values, or a list of numeric data vectors, and g is a vector or factor object giving the group for the corresponding elements of x. Whereas the function parameters for the t.test (x and y) are both numeric vectors of data values.

3. Re: Writing a function?

Originally Posted by Mike White
I have not used this function, but I think that you will find the reason for the differences by comparing the help files for the fligner.test and t.test. When 2 terms are use in the fligner.test (x, and g), x is a numeric vector of data values, or a list of numeric data vectors, and g is a vector or factor object giving the group for the corresponding elements of x. Whereas the function parameters for the t.test (x and y) are both numeric vectors of data values.

I was trying to decipher the help file earlier, and came to the conclusion that it must be something to do with that g. The help file also has some examples which I've run; they are along the lines of my
with(ABeggs08nona,fligner.test(Incubation~Egg))
which is apparently the same as:
fligner.test(ABeggs08nona\$Incubation, ABeggs08nona\$Egg)
In these cases, I see x=Incubation and g=Egg

So, in the case below, wouldn't everything inside the brackets be considered a list of numeric data vectors specified by the Egg==""? (that is, a series of x's, in which case it wouldn't look for a g since g is ignored if x is a list?)
with(ABeggs08nona,fligner.test(Incubation[Egg == "a"], Incubation[Egg == "b"])
I guess I'm confused about what that last syntax is actually saying. Is it saying, "test these data by comparing values of Incubation that correspond to values of Egg that are "a" to values of Incubation that correspond to values of Egg that are "b""?

Thanks again for anyone's take on this! This is really helping me get a grasp on R!

Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts