[R] predict.lm with matrix as newdata
Uwe Ligges
ligges at statistik.tu-dortmund.de
Mon Feb 18 18:14:09 CET 2008
Marlin Keith Cox wrote:
> Thank you in advance for helping me with this.
> I included a part of the actual data. The result of pred.est and
> pred.est1are different, but they should be identical. For
> pred.est, I entered the slope and y intercept and received a value for each
> individual number in the matrix (Z). For pred.est1, I was wanting R to do
> this for me and it seems that it combined values within each row of the
> matrix (Z), this was indicated by the both the row numbers and pred.est1 =
> 30.
>
> rm(list=ls())
>
> #Water
> w = c(9.037929, 8.273594, 9.253578, 8.039490, 8.381091, 11.610205,
> 11.261445, 11.185045, 9.903959, 12.307910, 10.307602, 13.950863,
> 13.028331, 13.677622, 13.051269, 13.289698, 14.391914, 21.411512,
> 75.568802, 78.168239, 60.829664, 112.558057, 127.296835, 108.739532,
> 133.516712, 130.492614, 129.783800, 168.289704, 152.732359, 168.405646)
>
> #Resistance
> R=c(660, 548, 676, 763, 768, 692, 657, 630, 748, 680, 786, 645, 710, 677,
> 692, 732, 737, 651, 396,
> 601, 640, 448, 464, 472, 434, 487, 495, 426, 429, 456)
>
> #Detector length
> Lend=c(37.0, 39.0, 39.0, 39.0, 40.0, 41.5, 44.0, 45.0, 46.0, 47.0,
> 47.0, 48.0,
> 48.5, 49.0, 51.0, 53.0, 53.0, 60.0, 89.0, 103.0, 108.5, 118.0, 118.0,
> 123.0,
> 126.0, 138.0, 139.0, 141.0, 141.0, 151.0)
>
> #Errors to be multiplied by Restistance
> x=c(0,.05,.10,.15,.20,.25)
>
> #Errors to be multiplied by Detector length
> y=c(0,.01,.02,.03,.04,.05)
>
>
> #equation to predict water weight in grams
> Rs<-(Lend^2)/R
> model.lm<-lm(w~Rs)
> a=predict.lm(model.lm)
>
>
>
> X=(R%o%x+R)
>
> Y=((Lend%o%y+Lend)^2)
> X
> Y
> num.x.col <- length(X[1,])
> num.y.col <- length(Y[1,])
> num.rows <- length(X[,1])
>
> Z <- matrix(nrow=num.rows, ncol=num.x.col*num.y.col)
>
> for( i in 1:num.rows) {
> Z[i,] <- as.vector( Y[i,] %*% t(X[i,])^-1 )
> }
> Z
> pred.est <- 3.453*Z+1.994
What did you try above? What has this Z to do with the linear model?
Why does Z correspond to the former Rs - it is really far off the
original values from Rs? Pay attention here.
I fear you are confusing some things here. You might want to explain
what X, Y and Z are good for.
> pred.est1 <- predict.lm(model.lm, data.frame(Rs=Z))
If you want to apply predict() [please, not predict.lm()] on all columns
of Z, try:
apply(Z, 2, function(x) predict(model.lm, newdata = data.frame(Rs=x)))
Uwe Ligges
> pred.est
> pred.est1
>
> detach(z)
> detach(sen)
>
> On Sat, Feb 16, 2008 at 2:10 AM, Uwe Ligges <ligges at statistik.tu-dortmund.de>
> wrote:
>
>>
>> Marlin Keith Cox wrote:
>>> Z is a matrix and when I run the following line, it creates a prediction
>>> estimate using each column, how can I get it an estimate for each
>> individual
>>> number. I have tried changing Z to a data.frame, but this does not do
>> it
>>> either.
>>>
>>> model.lm<-lm(w~x)
>>>
>>> pred.est <- predict.lm(model.lm, data.frame(x=Z))
>> That's what I do and it always worked for me. Can you please specify a
>> reproducible example that shows what you got and tells us exactly what
>> you expected?
>>
>> BTW: It is expected that you call the generic predict() rather than its
>> particular method.
>>
>> Uwe Ligges
>>
>>
>>> Thanks in advance,
>>> keith
>>>
>
>
>
More information about the R-help
mailing list