我试图通过将变量传递到模型中来预测R使用predict()函数的值.
我收到以下错误:
Error in eval(predvars, data, env) :
numeric 'envir' arg not of length one
Run Code Online (Sandbox Code Playgroud)
这是我的data frame名字df:
df <- read.table(text = '
Quarter Coupon Total
1 "Dec 06" 25027.072 132450574
2 "Dec 07" 76386.820 194154767
3 "Dec 08" 79622.147 221571135
4 "Dec 09" 74114.416 205880072
5 "Dec 10" 70993.058 188666980
6 "Jun 06" 12048.162 139137919
7 "Jun 07" 46889.369 165276325
8 "Jun 08" 84732.537 207074374
9 "Jun 09" 83240.084 221945162
10 "Jun 10" 81970.143 …Run Code Online (Sandbox Code Playgroud) > age <- c(23,19,25,10,9,12,11,8)
> steroid <- c(27.1,22.1,21.9,10.7,7.4,18.8,14.7,5.7)
> sample <- data.frame(age,steroid)
> fit2 <- lm(sample$steroid~poly(sample$age,2,raw=TRUE))
> fit2
Call:
lm(formula = sample$steroid ~ poly(sample$age, 2, raw = TRUE))
Coefficients:
(Intercept) -27.7225
poly(sample$age, 2, raw = TRUE)1 5.1819
poly(sample$age, 2, raw = TRUE)2 -0.1265
> (newdata=data.frame(age=15))
age
1 15
> predict(fit2,newdata,interval="predict")
fit lwr upr
1 24.558395 17.841337 31.27545
2 25.077825 17.945550 32.21010
3 22.781034 15.235782 30.32628
4 11.449490 5.130638 17.76834
5 8.670526 2.152853 15.18820
6 16.248596 9.708411 22.78878
7 13.975514 7.616779 …Run Code Online (Sandbox Code Playgroud) 我在这里读到的答案很少,但我恐怕无法找到答案.
我的R代码是:
colors <- bmw[bmw$Channel=="Colors" & bmw$Hour=20,]
colors_test <- tail(colors, 89)
colors_train <- head(colors, 810)
colors_train_agg <- aggregate(colors_train$Impressions, list(colors_train$`Position of Ad in Break`), FUN=mean, na.rm=TRUE)
colnames(colors_train_agg) <- c("ad_position", "avg_impressions")
lm_colors <- lm(colors_train_agg$avg_impressions ~ poly(colors_train_agg$ad_position, 12))
summary(lm_colors)
colors_test_agg <- aggregate(colors_test$Impressions, list(colors_test$`Position of Ad in Break`), FUN=mean, na.rm=TRUE)
colnames(colors_test_agg) <- c("ad_position", "avg_impressions")
new.df <- data.frame(colors_test_agg$ad_position)
colnames(new.df) <- c("ad_position")
colors_test_test <- predict(lm_colors, newdata=new.df)
Run Code Online (Sandbox Code Playgroud)
所以我对训练和测试数据都有完全相同的列名.我仍然收到警告:
Warning message:
'newdata' had 15 rows but variables found have 22 rows
有人可以提出什么是错的吗?另外,我想知道我是否以正确的方式做到了.
此外,将非常感谢关于如何计算模型精度的一些指示.谢谢!