+ Reply to Thread
Results 1 to 3 of 3

Thread: Writing a function?

Hybrid View

  1. #1
    Points: 1,412, Level: 21
    Level completed: 12%, Points required for next Level: 88

    Location
    Canada
    Posts
    36
    Thanks
    0
    Thanked 0 Times in 0 Posts

    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!!
    Last edited by mogetons; 04-01-2011 at 02:40 PM. Reason: changed title to be more appropriate

  2. #2
    TS Contributor
    Points: 5,565, Level: 48
    Level completed: 8%, Points required for next Level: 185

    Location
    St Albans, UK
    Posts
    257
    Thanks
    0
    Thanked 7 Times in 5 Posts

    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. #3
    Points: 1,412, Level: 21
    Level completed: 12%, Points required for next Level: 88

    Location
    Canada
    Posts
    36
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Writing a function?

    Quote Originally Posted by Mike White View Post
    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.
    Thanks for your thoughts Mike!

    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!

+ Reply to Thread

Similar Threads

  1. [Solved] Script writing for PCA in R
    By Emma2415 in forum R
    Replies: 4
    Last Post: 01-05-2011, 09:49 AM
  2. Can Anyone Help Writing in Statistical Language
    By seanbella in forum Statistics
    Replies: 2
    Last Post: 12-30-2010, 11:23 PM
  3. Writing a book - need help!
    By rakbeater in forum Probability
    Replies: 8
    Last Post: 08-05-2010, 12:21 AM
  4. P values in Medical Writing
    By medwriter in forum Biostatistics
    Replies: 3
    Last Post: 02-10-2010, 09:58 AM
  5. Writing out stats
    By xjennax in forum Statistics
    Replies: 1
    Last Post: 03-23-2006, 11:36 PM

Posting Permissions

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








Advertise on Talk Stats