Finding y-hat in R [closed]

Assuming you want the fitted values and the residuals of a simple linear regression model, you can get these as follows:

mod <- lm(y~x, data = df)
data.frame(df, y_hat = fitted(mod), e = residuals(mod))
   y x    y_hat          e
1 17 1 17.67857 -0.6785714
2 22 2 22.21429 -0.2142857
3 29 3 26.75000  2.2500000
4 29 4 31.28571 -2.2857143
5 38 5 35.82143  2.1785714
6 39 6 40.35714 -1.3571429
7 45 7 44.89286  0.1071429

Leave a Comment