我尝试使用计算R平方的公式,但是它们都给了我无效的R平方,为负或大于1。
我正在尝试使用R平方来评估我的模型拟合度。这样就可以根据预测值和实际值进行计算。
这是我的实际数据:
lasso_pred <- c(13.61946, 13.91595, 13.55052, 13.68006, 13.85825, 13.61735, 13.51712, 13.49727, 13.53181, 13.02560, 13.68658, 13.75073, 13.81856, 13.37798, 13.82558, 13.86672, 13.30965, 13.85373, 13.89834, 13.61562, 13.86353, 13.99774, 13.64185, 13.85063, 13.89282, 13.95036)
actual <- c(13.88986, 14.01917, 14.08782, 13.79533, 14.21647 ,14.18666, 13.84793, 14.02716 ,13.72207, 13.31198 ,13.99843,13.68277 ,13.81584, 13.81574, 14.24260 ,13.75368 ,13.71866 ,13.69617, 14.10187, 13.48185, 14.19855, 13.32519,13.61845, 13.81680, 13.94318, 14.09442)
SSE <- sum((lasso_pred - actual)^2) #sum of squares residual
SST <- sum((actual - mean(actual))^2) #total sum of squares
r2 <- 1-SSE/SST
SSR <- sum((lasso_pred- mean(actual))^2) #sum of squares regression
SST <- sum((actual - mean(actual))^2) #total sum of squares
r2 <- SSR/SST
计算出的R平方不在0到1的范围内。