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

后端 未结 3 1084
挽巷
挽巷 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
    

提交回复
热议问题