Advertisement
Guest User

Simple linear regression with prediction

a guest
Nov 20th, 2019
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
R 2.47 KB | None | 0 0
  1. > # Input data
  2.  
  3. ## Example input data
  4. > head(my_df)
  5.                Y            X
  6. [1,] -0.39643897  0.014797410
  7. [2,] -1.11042940 -1.634525956
  8. [3,] -0.02086076 -0.340575102
  9. [4,]  1.90382401  0.117504381
  10. [5,]  0.62557591  1.042435648
  11. [6,] -1.40269719 -0.006455178
  12.  
  13. ## Data has 50 rows, two columns
  14. > dim(my_df)
  15. [1] 50  2
  16.  
  17. ######
  18.  
  19. ## Model formula here is Y depends on X
  20. > lm(data=my_df, " Y ~ X ")
  21.  
  22. Call:
  23. lm(formula = " Y ~ X ", data = my_df)
  24.  
  25. Coefficients:
  26. (Intercept)            X  
  27.  -1.076e-17    5.600e-01  
  28.  
  29. > model = lm(data=my_df, " Y ~ X ")
  30.  
  31. ## You can get more information using the `summary()` function
  32. > summary(model)
  33.  
  34. Call:
  35. lm(formula = " Y ~ X ", data = my_df)
  36.  
  37. Residuals:
  38.      Min       1Q   Median       3Q      Max
  39. -1.65443 -0.57053 -0.06958  0.47720  1.83802
  40.  
  41. Coefficients:
  42.               Estimate Std. Error t value Pr(>|t|)    
  43. (Intercept) -1.076e-17  1.184e-01   0.000        1    
  44. X            5.600e-01  1.196e-01   4.683 2.35e-05 ***
  45. ---
  46. Signif. codes:  0***0.001**0.01*0.05 ‘.’ 0.1 ‘ ’ 1
  47.  
  48. Residual standard error: 0.8371 on 48 degrees of freedom
  49. Multiple R-squared:  0.3136,    Adjusted R-squared:  0.2993
  50. F-statistic: 21.93 on 1 and 48 DF,  p-value: 2.352e-05
  51.  
  52. ### The adjusted R-squared and p-values of X indicate that overal the model seems moderately correlated and the association between X and Y is significant.
  53.  
  54.  
  55. ### Predicting new observations
  56.  
  57. ## Get some new data
  58. > new_data = data.frame(X = seq(-3, 3, 0.5) )
  59. > new_data
  60.       X
  61. 1  -3.0
  62. 2  -2.5
  63. 3  -2.0
  64. 4  -1.5
  65. 5  -1.0
  66. 6  -0.5
  67. 7   0.0
  68. 8   0.5
  69. 9   1.0
  70. 10  1.5
  71. 11  2.0
  72. 12  2.5
  73. 13  3.0
  74.  
  75. ## Predict using existing model
  76.  
  77. ## $fit shows you the values of your prediction for your new input Xs
  78. > predict(model, new_data, se.fit=TRUE)
  79. $fit
  80.             1             2             3             4             5
  81. -1.680000e+00 -1.400000e+00 -1.120000e+00 -8.400000e-01 -5.600000e-01
  82.             6             7             8             9            10
  83. -2.800000e-01 -1.075513e-17  2.800000e-01  5.600000e-01  8.400000e-01
  84.            11            12            13
  85.  1.120000e+00  1.400000e+00  1.680000e+00
  86.  
  87. $se.fit
  88.         1         2         3         4         5         6         7         8
  89. 0.3777751 0.3215416 0.2668595 0.2149163 0.1682676 0.1326235 0.1183807 0.1326235
  90.         9        10        11        12        13
  91. 0.1682676 0.2149163 0.2668595 0.3215416 0.3777751
  92.  
  93. $df
  94. [1] 48
  95.  
  96. $residual.scale
  97. [1] 0.8370783
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement