[R] predict
Phil Spector
spector at stat.berkeley.edu
Fri May 21 23:53:42 CEST 2010
Try this:
mydata = as.data.frame(cbind(PrinComp[,1:5],x1=X[,1]))
net5 = lm(x1~.,data=mydata)
y=predict(net5, newdata=data.frame(PrinCompPredict)[,1:5])
(I can't test it, since you didn't provide a reproducible
example.)
Basically your problem is that when you pass a *matrix*
to the modeling functions, it *doesn't* use the dimnames
as it would use the names of a data frame. If you look
at the names of the coefficients from your net5 object,
you'll see they look like
PrinComp[,1:5]PC1, PrinComp[,1:5]PC2, etc.
If you place all your data in a properly named data frame,
you'll find the modeling functions work much more smoothly.
- Phil Spector
Statistical Computing Facility
Department of Statistics
UC Berkeley
spector at stat.berkeley.edu
On Fri, 21 May 2010, Luis Felipe Parra wrote:
> Hello I am creating a linear model with the command
>
> net5 = lm( X[,1] ~ PrinComp[,1:5]) where my vector PrinComp looks like this
>
>> head(PrinComp[,1:5])
> PC1 PC2 PC3 PC4 PC5
> [1,] 1.8626055 -3.34190998 -0.5448889 2.8296751 0.3994096
> [2,] 3.1124144 -1.68113572 1.7187314 -2.0162583 -0.2935675
> [3,] 3.3538049 -0.05471002 -2.9385065 0.6921495 -2.2743761
> [4,] 2.3491111 1.46433537 3.2600091 0.5121311 -2.2212727
> [5,] 1.4467501 2.64829347 -2.4559811 -2.4745585 -2.3537081
> [6,] 0.1575151 3.35242794 1.9367978 3.0293779 -1.4176263
> now I am trying to use the predict command like this
>
> y=predict(net5, newdata=data.frame(PrinCompPredict)[,1:5]) where
>
>> data.frame(PrinCompPredict)[,1:5]
> PC1 PC2 PC3 PC4 PC5
> 1 6.473546 7.529725 1.192588 0.1401967 10.81945
>
> and I am getting the following error message,
> Mensajes de aviso perdidos
> 'newdata' had 1 rows but variable(s) found have 164 rows
>
> Does anybody know what can be wrong with this? thank you
>
> Felipe Parra
>
> [[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.
>
More information about the R-help
mailing list