[R] Using lm's subset parameter results in Error in xj[i] : invalid subscript type 'list'

mviljamaa mviljamaa at kapsi.fi
Tue Sep 20 10:00:51 CEST 2016


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?



More information about the R-help mailing list