How to return the predicted values, residuals, R squared from lm.fit to R?

this piece of code will return the coefficients: interception, 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) } 

If I print:

  > res[[1]] 

I get:

  (Intercept) x1 x2 -0.44803887 0.06398476 -0.62798646 

How can we return the predicted values, residuals, R square, .. etc?

Do I need something in common to extract everything I need from a resume?

+6
source share
3 answers

There are a few things here.

First, you better combine variables with data.frame:

 df <- data.frame(y=rnorm(10), x1=rnorm(10), x2 = rnorm(10)) fit <- lm(y~x1+x2, data=df) 

If you do, then using the prediction model with the new dataset is much easier.

Secondly, some of the fitness statistics are available from the model itself, and some of them are available from 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 

To get odds statistics, you need to use a summary:

 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 
+8
source

In your function, you return only coefficients. Try to return the whole model:

 lm.ft=function(y,x1,x2) lm(y~x1+x2) # You don't need the return statement. 

Now try your code and then run:

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

You need predict -

 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) return(lm(y~x1+x2)) res=lm.ft(y,x1,x2) ypredicted <- predict(res) residuals <- y - ypredicted 
+1
source

All Articles