[Rd] strange bahaviour of predict.lm

Rui Barradas ru|pb@rr@d@@ @end|ng |rom @@po@pt
Tue Mar 17 07:46:57 CET 2020


Hello,

The problem seems to be that A is a matrix. The following solves the error.

# create A and b as in your code then run
A <- as.data.frame(A)
df1 <- cbind(A, b)
reg <- lm(b ~ ., df1)

# etc


Hope this helps,

Rui Barradas

Às 04:36 de 17/03/20, Moshe Olshansky via R-devel escreveu:
> Hello,
> Below is my code:
>> A <- matrix(rnorm(10*3),ncol=3)
>> b <- runif(10)
>> reg <- lm(b ~ A)
>> A1 <- matrix(rnorm(5*3),ncol=3)
>> A1 <- as.data.frame(A1)
>> b1 <- predict(reg,A1)
> Warning message:
> 'newdata' had 5 rows but variables found have 10 rows
> 
>    And instead of being an array of length 5, b1 is of length 10 and is identical to reg$fitted.values
> I think that it should not be like this.
> Let me note that for lm I do not care about this as much since I can use reg$coefficients, but unfortunately this behaviour is "inherited" by other methods. When I am trying to fit a regression tree, predicting from the object without using 'predict' method is less trivial.
> Thank you,Moshe.
> P.S. just in case:> sessionInfo()
> R version 3.6.2 (2019-12-12)
> Platform: x86_64-pc-linux-gnu (64-bit)
> Running under: Linux Mint 19.1
> 
> Matrix products: default
> BLAS:   /usr/lib/x86_64-linux-gnu/blas/libblas.so.3.7.1
> LAPACK: /usr/lib/x86_64-linux-gnu/lapack/liblapack.so.3.7.1
> 
> locale:
> [1] C
> 
> attached base packages:
> [1] stats     graphics  grDevices utils     datasets  methods   base
> 
> loaded via a namespace (and not attached):
> [1] compiler_3.6.2 tools_3.6.2
> 
> 
> 
> 
> 
> 	[[alternative HTML version deleted]]
> 
> ______________________________________________
> R-devel using r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-devel
>



More information about the R-devel mailing list