如何计算R中的预测R Sq

如何计算R中的预测R Sq,r,R,我试图用R编程语言计算多元线性回归模型的预测R平方,就像我们在Minitab中得到的那样 浏览这个链接,它给出了如何在Minitab中完成它。但我不知道在R是怎么做到的 线性模型具有汇总功能,可按如下方式使用: set.seed(0) y <- 1:10 x <- runif(10) summary(lm(y~x)) #Call: #lm(formula = y ~ x) #Residuals: # Min 1Q Median 3Q Max

我试图用R编程语言计算多元线性回归模型的预测R平方,就像我们在Minitab中得到的那样

浏览这个链接,它给出了如何在Minitab中完成它。但我不知道在R是怎么做到的


线性模型具有汇总功能,可按如下方式使用:

set.seed(0)
y <- 1:10
x <- runif(10)
summary(lm(y~x))

#Call:
#lm(formula = y ~ x)

#Residuals:
#    Min      1Q  Median      3Q     Max 
#-5.1468 -1.7243 -0.1631  1.6937  4.5146 

#Coefficients:
#            Estimate Std. Error t value Pr(>|t|)
#(Intercept)    3.931      2.561   1.535    0.163
#x              2.472      3.720   0.664    0.525

#Residual standard error: 3.126 on 8 degrees of freedom
#Multiple R-squared:  0.0523,   Adjusted R-squared:  -0.06616 
#F-statistic: 0.4415 on 1 and 8 DF,  p-value: 0.5251

请检查:

#按-预测残差平方和

非常感谢。。我想我需要更好地搜索stackoverflow。。再次感谢
summary(lm(y~x))$r.squared
summary(lm(y~x))$adj.r.squared
#PRESS - predicted residual sums of squares

PRESS <- function(linear.model) {
  #' calculate the predictive residuals
  pr <- residuals(linear.model)/(1-lm.influence(linear.model)$hat)
  #' calculate the PRESS
  PRESS <- sum(pr^2)
  
  return(PRESS)
}

pred_r_squared <- function(linear.model) {
  #' Use anova() to get the sum of squares for the linear model
  lm.anova <- anova(linear.model)
  #' Calculate the total sum of squares
  tss <- sum(lm.anova$'Sum Sq')
  # Calculate the predictive R^2
  pred.r.squared <- 1-PRESS(linear.model)/(tss)
  
  return(pred.r.squared)
}
model <- lm(disp ~ mpg, mtcars)    
pred_r_squared(model)
#0.6815513