简体   繁体   English

使用新数据评估线性 model 返回拟合值

[英]Evaluating linear model with new data returns fitted values

I am constructing and evaluating my model as shown below.我正在构建和评估我的 model,如下所示。

yData <- rnorm(10)
xData <- matrix(rnorm(20), 10, 2)
polyModel <- lm(yData~polym(xData, degree=2, raw=T))
newData <- matrix(rnorm(100), 50, 2)
yPredicted <- predict(polyModel, polym(newData, degree=2, raw=T))

However, the model evaluation yPredicted just equals the fitted values polyModel$fitted.values , a vector of length 10. I was expecting yPredicted to be a vector of length 50 in this case.但是,model 评估yPredicted恰好等于拟合值polyModel$fitted.values ,一个长度为 10 的向量。在这种情况下,我期望yPredicted是一个长度为 50 的向量。 Some help would be much appreciated.一些帮助将不胜感激。

predict() doesn't work very well unless the data is specified in the data argument. predict()不能很好地工作,除非在data参数中指定了数据。 This appears to work:这似乎有效:

polyModel <- lm(yData~poly(V1, V2, degree=2, raw=TRUE),
                data=as.data.frame(xData))
length(fitted(polyModel))  ## 10
newData <- matrix(rnorm(100), 50, 2)
yPredicted <- predict(polyModel, newdata=as.data.frame(newData))
length(yPredicted) ## 50
  • V1 and V2 are the default column names assigned when you convert a matrix into a data frame. V1V2是将矩阵转换为数据框时分配的默认列名。
  • this specification wouldn't work wello if you had an unknown and/or large number of columns to put into the polynomial (eg poly(V1, ..., V1000, degree=2, raw=TRUE) )如果您将未知和/或大量列放入多项式(例如poly(V1, ..., V1000, degree=2, raw=TRUE) ),则此规范将无法正常工作

If you don't know the number of columns in advance, a slightly hacky solution would be:如果您事先不知道列数,则稍微有点hacky 的解决方案是:

f <- as.formula(sprintf("yData~poly(%s, degree=2, raw=TRUE)",
           paste("V", seq(ncol(xData)), sep="", collapse=", "))
polyModel <- lm(f, data=as.frame(xData))

(untested) (未经测试)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM