I am learning to work with apply family functions and R loops.
I am working with a basic data set table that has y (outcome variable) column and x (predictor variable) column with 100 rows.
I have already used the lm()
function to run a regression for the data.
Model.1<-lm(y~x, data = data)
Coefficients:
(Intercept) x
13.87 4.89
summary(Model.1)
Residuals:
Min 1Q Median 3Q Max
-4.1770 -1.7005 -0.0011 1.5625 6.4893
Coefficients:
Estimate Std. Error t value Pr(>|t|)
(Intercept) 13.87039 0.95625 14.51 <2e-16 ***
x 4.88956 0.09339 52.35 <2e-16 ***
---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
Residual standard error: 2.195 on 98 degrees of freedom
Multiple R-squared: 0.9655, Adjusted R-squared: 0.9651
F-statistic: 2741 on 1 and 98 DF, p-value: < 2.2e-16
anova(Model.1)
Analysis of Variance Table
Response: y
Df Sum Sq Mean Sq F value Pr(>F)
x 1 13202 13202.5 2740.9 < 2.2e-16 ***
Residuals 98 472 4.8
---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
attributes(Model.1)
$names
[1] "coefficients" "residuals" "effects" "rank" "fitted.values" "assign" "qr" "df.residual"
[9] "xlevels" "call" "terms" "model"
$class
[1] "lm"
I know want to randomly sample 100 observations from my table "y" and "x" table. This is the function I created to run the random sample with replacement
draw_100<-function(){
random_100=sample(data, 100, replace = TRUE)
}
Running random_100 gives me these outputs
random_100
x x.1 y
1 8.112187 8.112187 53.69602
2 8.403589 8.403589 53.79438
3 9.541786 9.541786 58.48542
4 8.989281 8.989281 57.08601
5 6.965905 6.965905 46.62331
6 10.167800 10.167800 63.91487
7 10.683152 10.683152 65.84915
8 10.703093 10.703093 66.24738
9 8.337231 8.337231 51.87687
10 13.106177 13.106177 75.94588
11 10.726036 10.726036 65.19384
12 8.601641 8.601641 51.95095
13 10.338696 10.338696 62.92599
14 5.771682 5.771682 42.14190
15 6.161545 6.161545 46.36998
16 9.874543 9.874543 63.67148
17 8.540996 8.540996 58.85341
18 9.866002 9.866002 63.26319
19 8.622546 8.622546 57.05820
20 9.539929 9.539929 64.76654
21 9.498090 9.498090 61.38521
22 8.206142 8.206142 53.43508
23 8.245825 8.245825 58.29646
24 12.192542 12.192542 76.17440
25 6.955028 6.955028 49.73094
26 10.237639 10.237639 65.71210
27 10.927818 10.927818 67.18048
28 8.536011 8.536011 52.97402
29 9.574403 9.574403 60.53908
30 9.507752 9.507752 58.40020
31 5.838214 5.838214 41.93612
32 10.702791 10.702791 64.54986
33 6.704084 6.704084 46.88057
34 12.914798 12.914798 78.99422
35 16.607947 16.607947 96.60247
36 8.334241 8.334241 55.32263
37 12.287914 12.287914 71.46411
38 11.214098 11.214098 68.53254
39 7.722161 7.722161 50.81632
40 14.065276 14.065276 80.31033
41 10.402173 10.402173 64.36506
42 10.984727 10.984727 64.25032
43 8.491214 8.491214 58.36475
44 9.120864 9.120864 61.24240
45 10.251654 10.251654 60.56177
46 4.497277 4.497277 33.20243
47 11.384417 11.384417 68.61502
48 14.033980 14.033980 83.95417
49 9.909422 9.909422 62.27733
50 8.692219 8.692219 55.73567
51 12.864750 12.864750 79.08818
52 9.886267 9.886267 65.87693
53 10.457541 10.457541 61.36505
54 13.395296 13.395296 76.01832
55 10.343134 10.343134 60.84247
56 10.233329 10.233329 65.12074
57 10.756491 10.756491 70.05930
58 9.287774 9.287774 57.65071
59 11.704419 11.704419 72.65211
60 13.075236 13.075236 77.87956
61 12.066161 12.066161 69.34647
62 10.044714 10.044714 65.80648
63 13.331926 13.331926 80.72634
64 10.816099 10.816099 67.11356
65 10.377846 10.377846 63.14035
66 11.824583 11.824583 67.51041
67 7.114326 7.114326 51.80456
68 9.752344 9.752344 59.36107
69 10.869720 10.869720 67.97186
70 10.366262 10.366262 66.28012
71 10.656127 10.656127 67.86625
72 6.246312 6.246312 45.95457
73 8.003875 8.003875 49.29802
74 11.541176 11.541176 67.89918
75 11.799510 11.799510 73.15802
76 9.787112 9.787112 62.90187
77 13.187445 13.187445 80.26162
78 13.019787 13.019787 75.69156
79 3.854378 3.854378 35.82556
80 11.724234 11.724234 71.79034
81 6.953864 6.953864 45.72355
82 12.822231 12.822231 76.93698
83 9.285428 9.285428 59.61610
84 10.259240 10.259240 62.37958
85 10.613086 10.613086 63.91694
86 8.547155 8.547155 54.72216
87 15.069100 15.069100 86.23767
88 7.816772 7.816772 51.41676
89 13.854272 13.854272 88.10100
90 9.495968 9.495968 61.61393
91 9.881453 9.881453 65.24259
92 7.475875 7.475875 50.80777
93 13.286219 13.286219 81.15708
94 9.703433 9.703433 60.75532
95 5.415999 5.415999 42.55981
96 12.997555 12.997555 78.12987
97 11.893787 11.893787 68.97691
98 5.228217 5.228217 37.38417
99 8.392504 8.392504 54.81151
100 8.077527 8.077527 51.47045
I am having an road block, using this new random sample of 100 values and fitting a regression model to it to extract the coefficient and standard error?
I thought I may need to use the supply()
function but I truly believe I am overthinking this. Because when I the regression model with my R object with the store random sample and it was identical to Model.1. I am off.
Model.2<-lm(y~x, data = random_100)
Call:
lm(formula = y ~ x, data = random_100)
Coefficients:
(Intercept) x
13.87 4.89
Coefficient and slop were identical to Model.1
Call:
lm(formula = y ~ x, data = random_100)
Residuals:
Min 1Q Median 3Q Max
-4.1770 -1.7005 -0.0011 1.5625 6.4893
Coefficients:
Estimate Std. Error t value Pr(>|t|)
(Intercept) 13.87039 0.95625 14.51 <2e-16 ***
x 4.88956 0.09339 52.35 <2e-16 ***
---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
Residual standard error: 2.195 on 98 degrees of freedom
Multiple R-squared: 0.9655, Adjusted R-squared: 0.9651
F-statistic: 2741 on 1 and 98 DF, p-value: < 2.2e-16