Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- > # Input data
- ## Example input data
- > head(my_df)
- Y X
- [1,] -0.39643897 0.014797410
- [2,] -1.11042940 -1.634525956
- [3,] -0.02086076 -0.340575102
- [4,] 1.90382401 0.117504381
- [5,] 0.62557591 1.042435648
- [6,] -1.40269719 -0.006455178
- ## Data has 50 rows, two columns
- > dim(my_df)
- [1] 50 2
- ######
- ## Model formula here is Y depends on X
- > lm(data=my_df, " Y ~ X ")
- Call:
- lm(formula = " Y ~ X ", data = my_df)
- Coefficients:
- (Intercept) X
- -1.076e-17 5.600e-01
- > model = lm(data=my_df, " Y ~ X ")
- ## You can get more information using the `summary()` function
- > summary(model)
- Call:
- lm(formula = " Y ~ X ", data = my_df)
- Residuals:
- Min 1Q Median 3Q Max
- -1.65443 -0.57053 -0.06958 0.47720 1.83802
- Coefficients:
- Estimate Std. Error t value Pr(>|t|)
- (Intercept) -1.076e-17 1.184e-01 0.000 1
- X 5.600e-01 1.196e-01 4.683 2.35e-05 ***
- ---
- Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
- Residual standard error: 0.8371 on 48 degrees of freedom
- Multiple R-squared: 0.3136, Adjusted R-squared: 0.2993
- F-statistic: 21.93 on 1 and 48 DF, p-value: 2.352e-05
- ### 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.
- ### Predicting new observations
- ## Get some new data
- > new_data = data.frame(X = seq(-3, 3, 0.5) )
- > new_data
- X
- 1 -3.0
- 2 -2.5
- 3 -2.0
- 4 -1.5
- 5 -1.0
- 6 -0.5
- 7 0.0
- 8 0.5
- 9 1.0
- 10 1.5
- 11 2.0
- 12 2.5
- 13 3.0
- ## Predict using existing model
- ## $fit shows you the values of your prediction for your new input Xs
- > predict(model, new_data, se.fit=TRUE)
- $fit
- 1 2 3 4 5
- -1.680000e+00 -1.400000e+00 -1.120000e+00 -8.400000e-01 -5.600000e-01
- 6 7 8 9 10
- -2.800000e-01 -1.075513e-17 2.800000e-01 5.600000e-01 8.400000e-01
- 11 12 13
- 1.120000e+00 1.400000e+00 1.680000e+00
- $se.fit
- 1 2 3 4 5 6 7 8
- 0.3777751 0.3215416 0.2668595 0.2149163 0.1682676 0.1326235 0.1183807 0.1326235
- 9 10 11 12 13
- 0.1682676 0.2149163 0.2668595 0.3215416 0.3777751
- $df
- [1] 48
- $residual.scale
- [1] 0.8370783
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement