predict() 中的决定系数

Coefficient of determination in 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 
RMSE <- function(fitted, true){
  sqrt(mean((fitted - true)^2))
}

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

拟合值在 model2 中,而真实值可能在 daten2 的响应变量中

RMSE(model2, daten2$y)

R2(model2, daten2$y)

要点是:要计算拟合优度指标,您需要提供真实结果和预测值。 "predict.lm" 只提供预测值