预测系数()

时间:2017-09-08 10:17:41

标签: r regression linear predict

我试图通过使用predict()函数来验证我的线性回归模型。我使用校准和验证数据集。有没有办法让R ^ 2作为predict()的输出?我想将它与校准集的摘要进行比较。

目前我不理解predict()的输出。

我的代码的一部分:

model<-lm(y~x, data=daten)
model

model2<-predict(model,daten2)
model2

summary(model)
Call:
lm(formula = y ~ x, data = daten)

 Residuals:
 Min      1Q  Median      3Q     Max 
-5.0347 -1.4576 -0.7656  1.4046  5.5095 

Coefficients:
Estimate Std. Error t value Pr(>|t|)    
(Intercept) -44.03468    7.40057  -5.950 1.58e-05 ***
x             0.39646    0.04928   8.045 3.38e-07 ***
 ---
Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1

Residual standard error: 2.728 on 17 degrees of freedom
Multiple R-squared:  0.792, Adjusted R-squared:  0.7798 
F-statistic: 64.73 on 1 and 17 DF,  p-value: 3.379e-07

summary(model2)
Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
4.719  12.820  15.350  15.650  20.180  27.370 

1 个答案:

答案 0 :(得分:0)

RMSE <- function(fitted, true){
  sqrt(mean((fitted - true)^2))
}

R2 <- function(fitted, true){
 1 - (sum((true - fitted)^2)/sum((true - mean(true))^2))
}

拟合值在model2中,而true值可能是来自daten2的响应变量

RMSE(model2, daten2$y)

R2(model2, daten2$y)

重点是:要计算拟合度度量,您需要提供真实结果和预测值。 “predict.lm”只提供预测值

相关问题