This is a function to model simple linear regression.

Right way to use the function: slr_function(dataset, yvar = , xvar= ,…) [Note: argument 1 is the dataset name, argument 2 is the response variable, argument 3 is the predictor]

slr_function <- function(variables, yvar, xvar, ...) {
  
  
  attach(variables)
  
  # Independent variable(x)
  x <- model.matrix(yvar ~ xvar)
  
  # Dependent variable(y)
  y <- yvar
  
  detach(variables)
  
  #calculate the co-efficients(beta0 and beta1)
  b <- solve(crossprod(x))%*%crossprod(x,y)
  
  
  # calculate predicted values(yhat)
  yhat_dummy <- x%*%b
  
  #calculate the normal equations (e and xe), whose  indivaidual sum is equal to zero
  e <- y - yhat_dummy
  
  xe <- e*x[,2]
  
  slr_data <- as.matrix(cbind(x[,2], y, yhat_dummy, e, xe))
  
  colnames(slr_data) <- c("x", "y","yhat", "e", "xe")
  
  print(cat("Regression Equation: Y =",b[1],"+",b[2],"*","x"))
  
  return(slr_data)
}

Example: Lets model cars data

slr_function(cars, yvar = dist, xvar = speed)
## Regression Equation: Y = -17.57909 + 3.932409 * xNULL
##     x   y      yhat          e          xe
## 1   4   2 -1.849460   3.849460   15.397839
## 2   4  10 -1.849460  11.849460   47.397839
## 3   7   4  9.947766  -5.947766  -41.634365
## 4   7  22  9.947766  12.052234   84.365635
## 5   8  16 13.880175   2.119825   16.958599
## 6   9  10 17.812584  -7.812584  -70.313255
## 7  10  18 21.744993  -3.744993  -37.449927
## 8  10  26 21.744993   4.255007   42.550073
## 9  10  34 21.744993  12.255007  122.550073
## 10 11  17 25.677401  -8.677401  -95.451416
## 11 11  28 25.677401   2.322599   25.548584
## 12 12  14 29.609810 -15.609810 -187.317723
## 13 12  20 29.609810  -9.609810 -115.317723
## 14 12  24 29.609810  -5.609810  -67.317723
## 15 12  28 29.609810  -1.609810  -19.317723
## 16 13  26 33.542219  -7.542219  -98.048847
## 17 13  34 33.542219   0.457781    5.951153
## 18 13  34 33.542219   0.457781    5.951153
## 19 13  46 33.542219  12.457781  161.951153
## 20 14  26 37.474628 -11.474628 -160.644788
## 21 14  36 37.474628  -1.474628  -20.644788
## 22 14  60 37.474628  22.525372  315.355212
## 23 14  80 37.474628  42.525372  595.355212
## 24 15  20 41.407036 -21.407036 -321.105547
## 25 15  26 41.407036 -15.407036 -231.105547
## 26 15  54 41.407036  12.592964  188.894453
## 27 16  32 45.339445 -13.339445 -213.431124
## 28 16  40 45.339445  -5.339445  -85.431124
## 29 17  32 49.271854 -17.271854 -293.621518
## 30 17  40 49.271854  -9.271854 -157.621518
## 31 17  50 49.271854   0.728146   12.378482
## 32 18  42 53.204263 -11.204263 -201.676730
## 33 18  56 53.204263   2.795737   50.323270
## 34 18  76 53.204263  22.795737  410.323270
## 35 18  84 53.204263  30.795737  554.323270
## 36 19  36 57.136672 -21.136672 -401.596759
## 37 19  46 57.136672 -11.136672 -211.596759
## 38 19  68 57.136672  10.863328  206.403241
## 39 20  32 61.069080 -29.069080 -581.381606
## 40 20  48 61.069080 -13.069080 -261.381606
## 41 20  52 61.069080  -9.069080 -181.381606
## 42 20  56 61.069080  -5.069080 -101.381606
## 43 20  64 61.069080   2.930920   58.618394
## 44 22  66 68.933898  -2.933898  -64.545752
## 45 23  54 72.866307 -18.866307 -433.925051
## 46 24  70 76.798715  -6.798715 -163.169168
## 47 24  92 76.798715  15.201285  364.830832
## 48 24  93 76.798715  16.201285  388.830832
## 49 24 120 76.798715  43.201285 1036.830832
## 50 25  85 80.731124   4.268876  106.721898