gpt4 book ai didi

r - 如何使用 R 动态回归和预测多个项目?

转载 作者:行者123 更新时间:2023-12-01 23:34:52 25 4
gpt4 key购买 nike

我正在尝试编写一个函数来回归多个项目,然后尝试根据模型预测数据:

"tnt" <- function(train_dep, train_indep, test_dep, test_indep) 
{
y <- train_dep
x <- train_indep
mod <- lm (y ~ x)
estimate <- predict(mod, data.frame(x=test_indep))
rmse <- sqrt(sum((test_dep-estimate)^2)/length(test_dep))
print(summary(mod))
print(paste("RMSE: ", rmse))
}

如果我通过上面这个,它就失败了:

train_dep = vector1
train_indep <- cbind(vector2, vector3)
test_dep = vector4
test_indep <- cbind(vector5, vector6)
tnt(train_dep, train_indep, test_dep, test_indep)

将上面的内容更改为类似以下的内容,但我希望动态完成此操作,以便我可以将任意数量的列矩阵传递给它:

x1 = x[,1]
x2 = x[,2]
mod <- lm(y ~ x1+x2)
estimate <- predict(mod, data.frame(x1=test_indep[,1], x2=test_indep[,2]))

看起来这可能有所帮助,但我仍然对过程的其余部分感到困惑:http://finzi.psych.upenn.edu/R/Rhelp02a/archive/70843.html

最佳答案

试试这个:

tnt <- function(train_dep, train_indep, test_dep, test_indep) 
{ dat<- as.data.frame(cbind(y=train_dep, train_indep))
mod <- lm (y ~ . , data=dat )
newdat <- as.data.frame(test_indep)
names(newdat) <- names(dat)[2:length(dat)]

estimate <- predict(mod, newdata=newdat )
rmse <- sqrt(sum((test_dep-estimate)^2)/length(test_dep))
print(summary(mod))
print(paste("RMSE: ", rmse))
}


Call:
lm(formula = y ~ ., data = dat)

Residuals:
1 2 3
0 0 0

Coefficients: (1 not defined because of singularities)
Estimate Std. Error t value Pr(>|t|)
(Intercept) 0 0 NA NA
V2 1 0 Inf <2e-16 ***
V3 NA NA NA NA
---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1

Residual standard error: 0 on 1 degrees of freedom
Multiple R-squared: 1, Adjusted R-squared: 1
F-statistic: Inf on 1 and 1 DF, p-value: < 2.2e-16

[1] "RMSE: 0"
Warning message:
In predict.lm(mod, newdata = newdat) :
prediction from a rank-deficient fit may be misleading
>

警告是因为您提供的完全适合

关于r - 如何使用 R 动态回归和预测多个项目?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6968127/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com