sacvf
sacvf

Reputation: 2533

How to return predicted values, residuals, R square from lm()?

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

If I type:

   > res[[1]]

I get:

      (Intercept)          x1          x2 
     -0.44803887  0.06398476 -0.62798646 

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

I need something general to extract whatever I need from the summary?

Upvotes: 11

Views: 48118

Answers (3)

jlhoward
jlhoward

Reputation: 59355

There are a couple of things going on here.

First, you are better off combining your variables into a data.frame:

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

If you do this, using you model for prediction with a new dataset will be much easier.

Second, some of the statistics of the fit are accessible from the model itself, and some are accessible 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 the statistics of the coefficients, you need to use 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

Upvotes: 15

TheComeOnMan
TheComeOnMan

Reputation: 12875

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

Upvotes: 1

nograpes
nograpes

Reputation: 18323

In your function, you return just the coefficients. Try returning 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

Upvotes: 1

Related Questions