10

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?

jay.sf
  • 60,139
  • 8
  • 53
  • 110
sacvf
  • 2,463
  • 5
  • 36
  • 54
  • 1
    I don't understand why you're looping over `1:n` – TheComeOnMan Jan 03 '14 at 16:10
  • possible duplicate of [Outputting Regression results into a data frame in R](http://stackoverflow.com/questions/20838596/outputting-regression-results-into-a-data-frame-in-r) – Tomas Jan 03 '14 at 18:40

3 Answers3

15

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
hyiltiz
  • 1,158
  • 14
  • 25
jlhoward
  • 58,004
  • 7
  • 97
  • 140
1

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
nograpes
  • 18,623
  • 1
  • 44
  • 67
  • Then, instead of returning just `coef`, return what you need, you can even return just the `summary`, or you could make a list of the coefficients and the residuals and other statistics you want. If you just have the coefficients, you can just matrix multiply (`%*%`) the data. – nograpes Jan 03 '14 at 16:20
1

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
TheComeOnMan
  • 12,535
  • 8
  • 39
  • 54