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

后端 未结 3 1077
挽巷
挽巷 2020-12-09 12:29

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         


        
相关标签:
3条回答
  • 2020-12-09 13:10

    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
    
    0 讨论(0)
  • 2020-12-09 13:25

    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
    
    0 讨论(0)
  • 2020-12-09 13:32

    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
    
    0 讨论(0)
提交回复
热议问题