如何从R中的lm.fit返回预测值,残差,R平方?

sac*_*cvf 7 regression r linear-regression

这段代码将返回系数:intercept,slop1,slop2

set.seed(1)
n=10

y=rnorm(n)
x1=rnorm(n)
x2=rnorm(n)

lm.ft=function(y,x1,x2)
  return(lm(y~x1+x2)$coef)

res=list();
for(i in 1:n){
  x1.bar=x1-x1[i]
  x2.bar=x2-x2[i]
  res[[i]]=lm.ft(y,x1.bar,x2.bar)
}
Run Code Online (Sandbox Code Playgroud)

如果我输入:

   > res[[1]]
Run Code Online (Sandbox Code Playgroud)

我明白了:

      (Intercept)          x1          x2 
     -0.44803887  0.06398476 -0.62798646 
Run Code Online (Sandbox Code Playgroud)

我们如何返回预测值,残差,R square,..等?

我需要一些通用的东西从摘要中提取我需要的东西?

jlh*_*ard 12

这里有几件事情.

首先,最好将变量组合到data.frame中:

df  <- data.frame(y=rnorm(10), x1=rnorm(10), x2 = rnorm(10))
fit <- lm(y~x1+x2, data=df)
Run Code Online (Sandbox Code Playgroud)

如果这样做,那么使用模型进行预测并使用新数据集会更容易.

其次,可以从模型本身访问一些拟合的统计数据,有些可以从中获取summary(fit).

coef  <- coefficients(fit)       # coefficients
resid <- residuals(fit)          # residuals
pred  <- predict(fit)            # fitted values
rsq   <- summary(fit)$r.squared  # R-sq for the fit
se    <- summary(fit)$sigma      # se of the fit
Run Code Online (Sandbox Code Playgroud)

要获得系数的统计信息,您需要使用摘要:

stat.coef  <- summary(fit)$coefficients
coef    <- stat.coef[,1]    # 1st column: coefficients (same as above)
se.coef <- stat.coef[,2]    # 2nd column: se for each coef
t.coef  <- stat.coef[,3]    # 3rd column: t-value for each coef
p.coef  <- stat.coef[,4]    # 4th column: p-value for each coefficient
Run Code Online (Sandbox Code Playgroud)


nog*_*pes 1

在您的函数中,您仅返回系数。尝试返回整个模型:

\n\n
lm.ft=function(y,x1,x2) lm(y~x1+x2) # You don't need the return statement.\n
Run Code Online (Sandbox Code Playgroud)\n\n

现在尝试您的代码,然后运行:

\n\n
summary(res[[1]])\n\n# Call:\n#   lm(formula = y ~ x1 + x2)\n# \n# Residuals:\n#   Min       1Q   Median       3Q      Max \n# -0.88518 -0.25311  0.03868  0.43110  0.61753 \n# \n# Coefficients:\n#   Estimate Std. Error t value Pr(>|t|)  \n# (Intercept) -0.44804    0.32615  -1.374   0.2119  \n# x1           0.06398    0.24048   0.266   0.7979  \n# x2          -0.62799    0.26915  -2.333   0.0524 .\n# ---\n#   Signif. codes:  0 \xe2\x80\x98***\xe2\x80\x99 0.001 \xe2\x80\x98**\xe2\x80\x99 0.01 \xe2\x80\x98*\xe2\x80\x99 0.05 \xe2\x80\x98.\xe2\x80\x99 0.1 \xe2\x80\x98 \xe2\x80\x99 1\n# \n# Residual standard error: 0.6149 on 7 degrees of freedom\n# Multiple R-squared:  0.5173,  Adjusted R-squared:  0.3794 \n# F-statistic: 3.751 on 2 and 7 DF,  p-value: 0.07814\n
Run Code Online (Sandbox Code Playgroud)\n