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

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

提交回复
热议问题