[R] NA confusion (length question)
Joshua Wiley
jwiley.psych at gmail.com
Tue Sep 14 15:50:25 CEST 2010
You're quite welcome. If you've a hankering to learn more you can see
the documentation for ?residuals.lm (note the .lm addition, which
designates the particular method that gets dispatched since your
object class was "lm"). For even more fun: ?naresid (which
residuals() calls) and to see how it knew where to add the NAs:
mylm$na.action
On Tue, Sep 14, 2010 at 6:36 AM, Stratford, Jeffrey
<jeffrey.stratford at wilkes.edu> wrote:
> Josh,
>
> I am raising my cup of coffee to you - that worked perfectly.
>
> Cheers and thanks again!
>
> -----Original Message-----
> From: Joshua Wiley [mailto:jwiley.psych at gmail.com]
> Sent: Tuesday, September 14, 2010 9:29 AM
> To: Stratford, Jeffrey
> Cc: r-help at r-project.org
> Subject: Re: [R] NA confusion (length question)
>
> Hi Jeffrey,
>
> As a bit of a side note, data copied in html emails tends to show up
> poorly (since emails to the list are converted to plain text).
> Anyways, note the difference between:
>
> length(mylm$residuals)
> length(residuals(mylm))
>
> Accessing the residuals value from mylm is *not* quite the same as
> calling the residuals() function on mylm. So, if you use:
>
> cbind(residuals(mylm), mydata)
>
> you should be good.
>
> Hope that helps,
>
> Josh
>
> On Tue, Sep 14, 2010 at 6:20 AM, Stratford, Jeffrey
> <jeffrey.stratford at wilkes.edu> wrote:
>> Hi folks,
>>
>>
>>
>> I am running a very simple regression using
>>
>>
>>
>> mylm <- lm(mass ~ tarsus, na.action=na.exclude)
>>
>>
>>
>> I would like the use the residuals from this analysis for more
>> regression but I'm running into a snag when I try
>>
>>
>>
>> cbind(mylm$residuals, mydata) # where my data is the original data set
>>
>>
>>
>> The error tells me that it cannot use cbind because the length of
>> mylm$residuals is 50 and mydata is 52. This makes sense except I
>> thought that na.exclude preserves length by inserting NA in residuals
>> where there were NA's in the orginal data ( i = 3, 20). When I ask to
>> see the residuals( mylm$residuals) I see 52 numbers. How can this be?
>>
>>
>>
>> I am just looking to see how I can produce a third vector with length =
>> 52 and NA's for i=3,20.
>>
>>
>>
>> I'm using R 2.11.1 on Windows XP and the data are below
>>
>>
>>
>> Many thanks,
>>
>>
>>
>> Jeff
>>
>>
>>
>>
>>
>> tarsus
>>
>> mass
>>
>>
>>
>> 21.5
>>
>> 23.2
>>
>>
>> 21.8
>>
>> 22.6
>>
>>
>> 20.9
>>
>> NA
>>
>>
>> 21.6
>>
>> 20.8
>>
>>
>> 21.5
>>
>> 21.5
>>
>>
>> 22
>>
>> 23.2
>>
>>
>> 21.6
>>
>> 23
>>
>>
>> 22.1
>>
>> 21
>>
>>
>> 21.1
>>
>> 21
>>
>>
>> 20.6
>>
>> 22.9
>>
>>
>> 20.1
>>
>> 21.8
>>
>>
>> 22.2
>>
>> 20.4
>>
>>
>> 21.9
>>
>> 21.5
>>
>>
>> 21.1
>>
>> 21.3
>>
>>
>> 21.5
>>
>> 20.1
>>
>>
>> 19.9
>>
>> 21.4
>>
>>
>> 22.1
>>
>> 27.3
>>
>>
>> 20.1
>>
>> 19.7
>>
>>
>> 19.6
>>
>> 16.7
>>
>>
>> 20.9
>>
>> NA
>>
>>
>> 20.1
>>
>> 21.7
>>
>>
>> 21.8
>>
>> 22.3
>>
>>
>> 20.9
>>
>> 21.1
>>
>>
>> 21.6
>>
>> 20.8
>>
>>
>> 22.4
>>
>> 20.5
>>
>>
>> 21.4
>>
>> 20.4
>>
>>
>> 21.4
>>
>> 21
>>
>>
>> 21.6
>>
>> 21
>>
>>
>> 21.2
>>
>> 23.3
>>
>>
>> 21.1
>>
>> 21.9
>>
>>
>> 22
>>
>> 21
>>
>>
>> 22.2
>>
>> 21.5
>>
>>
>> 20.6
>>
>> 20.6
>>
>>
>> 21.3
>>
>> 20.5
>>
>>
>> 20.6
>>
>> 20.5
>>
>>
>> 21.4
>>
>> 20.8
>>
>>
>> 21.8
>>
>> 21.4
>>
>>
>> 21.6
>>
>> 19.6
>>
>>
>> 22.4
>>
>> 24.3
>>
>>
>> 21.7
>>
>> 20.3
>>
>>
>> 21.4
>>
>> 21.3
>>
>>
>> 20.7
>>
>> 18.2
>>
>>
>> 21.3
>>
>> 20.9
>>
>>
>> 21.7
>>
>> 20.7
>>
>>
>> 22.6
>>
>> 20
>>
>>
>> 22
>>
>> 23
>>
>>
>> 18.4
>>
>> 20
>>
>>
>> 20.1
>>
>> 20.6
>>
>>
>> 19.8
>>
>> 19.1
>>
>>
>> 21.1
>>
>> 26.3
>>
>>
>> 19.8
>>
>> 22.3
>>
>>
>> 21.2
>>
>> 22.2
>>
>>
>> 19.5
>>
>> 20.6
>>
>>
>> 19.7
>>
>> 22.7
>>
>>
>>
>>
>>
>>
>>
>> ************************************
>>
>> Jeffrey A. Stratford
>>
>> Department of Health and Biological Sciences
>>
>> 84 W. South Street
>>
>> Wilkes University, PA 18766
>>
>> jeffrey.stratford at wilkes.edu
>>
>> 570-408-4761 (office)
>>
>> 570-332-2942 (cell)
>>
>> http://web.wilkes.edu/jeffrey.stratford/
>>
>> ************************************
>>
>>
>>
>>
>> [[alternative HTML version deleted]]
>>
>> ______________________________________________
>> R-help at r-project.org mailing list
>> https://stat.ethz.ch/mailman/listinfo/r-help
>> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
>> and provide commented, minimal, self-contained, reproducible code.
>>
>
>
>
> --
> Joshua Wiley
> Ph.D. Student, Health Psychology
> University of California, Los Angeles
> http://www.joshuawiley.com/
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
More information about the R-help
mailing list