简体   繁体   English

如何使用r中的拟合模型预测相关值?

[英]How to predict dependent values using fitted model in r?

I am fitting a model with:我正在拟合一个模型:

var4pca <- lm(lg[5:415,1] ~ pcalg1$x[, 1:8] + pcalg2$x[, 1:8] + pcalg3$x[, 1:8] + pcalg4$x[, 1:8])

I now want to predict values for a validation set(83 rows).我现在想预测验证集的值(83 行)。 How can I do this?我怎样才能做到这一点?

I am trying to use:我正在尝试使用:

pred_pca<-predict(var4pca, va)

where va is my validation set.其中 va 是我的验证集。 But this is returning me a vector with length 411, whereas I only want length 83但这给我返回了一个长度为 411 的向量,而我只想要长度为 83

In my experience, lm is very fussy about prediction.根据我的经验,lm 对预测非常挑剔。 It demands that the new data look exactly like the data used to create the model.它要求新数据与用于创建模型的数据完全一样。 By that I mean things like col names have to match.我的意思是像 col 名称必须匹配。 What typically will work is to create a data frame of all the data and then create df.train and df.test as the correct rows of the data frame.通常会起作用的是创建所有数据的数据框,然后创建 df.train 和 df.test 作为数据框的正确行。 That should do the trick.这应该够了吧。 As joran says be careful with formulas.正如乔兰所说,小心使用公式。 One advantage of putting all the data into a df with named cols is that then one can use the formula depvar ~.将所有数据放入具有命名 cols 的 df 的一个优点是,可以使用公式 depvar ~。 - typically much easier to write. - 通常更容易编写。

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

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