[R] Using lm's subset parameter results in Error in xj[i] : invalid subscript type 'list'
Jorge Cimentada
cimentadaj at gmail.com
Tue Sep 20 10:14:24 CEST 2016
By subsetting the rows in the firstkids data frame, you've already
subsetted your data.
Try specifying firstkids as your data instead of a subset in the lm call.
Also, eliminate the kidmomhsage prefix from all of your variables since
you're running the linear model on a different data frame(firstkids)
Something along this line:
lm(kid_score ~ mom_age ...., data = firstkids)
*Jorge Cimentada*
*Ph.D. Candidate*
Dpt. Ciències Polítiques i Socials
Ramon Trias Fargas, 25-27 | 08005 Barcelona
Office 24.331
[Tel.] 697 382 009
jorge.cimentada at upf.edu
http://www.upf.edu/dcpis/
On Tue, Sep 20, 2016 at 10:00 AM, mviljamaa <mviljamaa at kapsi.fi> wrote:
> I'm trying to take lm on a subset of my dataset and to do this I believe I
> need to pass my subset of the data as the subset parameter of lm.
>
> So I do my subsetting:
>
> firstkids <- kidmomhsage[0:234,], i.e. the first 234 rows of the data
> frame.
>
> Then construct the model:
>
> fit4 <- lm(kidmomhsage$kid_score ~ kidmomhsage$mom_age +
> kidmomhsage$mom_hs + kidmomhsage$mom_age * kidmomhsage$mom_hs,
> subset=firstkids)
>
> which results in:
>
> Error in xj[i] : invalid subscript type 'list'
>
> I read somewhere a recommendation to use "unlist":
>
> fit4 <- lm(kidmomhsage$kid_score ~ kidmomhsage$mom_age +
> kidmomhsage$mom_hs + kidmomhsage$mom_age * kidmomhsage$mom_hs,
> subset=unlist(firstkids))
>
> which seems to not produce the error and results in some sort of model,
> but is this model the correct one (i.e. for the data set firstkids, just as
> it originally appears)? How does unlist change the data?
>
> ______________________________________________
> R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posti
> ng-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signatura-correu.png
Type: image/png
Size: 3020 bytes
Desc: not available
URL: <https://stat.ethz.ch/pipermail/r-help/attachments/20160920/282edeae/attachment.png>
More information about the R-help
mailing list