-
Notifications
You must be signed in to change notification settings - Fork 0
/
Handout5Code_RMarkdown_students.Rmd
244 lines (159 loc) · 3.78 KB
/
Handout5Code_RMarkdown_students.Rmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
---
title: "Handout5Code_RMarkdown_students"
output: html_document
editor_options:
chunk_output_type: console
---
# Example 1:
ARTIFICIAL POPULATION DATA
```{r}
# generate artifical population of size N=5000 based on characteristics
# of the real data in "bodyfat.csv" focusing on height vs. weight
# 178.9244=mean(bodyfatData$Weight), 29.38916=sd(bodyfatData$Weight)
# 3.491=s, ,63.270=beta0hat, 0.0384=beta1hat from lm(Height~Weight,data=bodyfatData)
set.seed(11)
Weight=rnorm(5000,178.9244,29.38916)
Error=rnorm(5000,0,3.491)
Height=63.270+0.0384*Weight+Error
set.seed(NULL)
```
Scatterplot of artificial population data
```{r}
par(mfrow=c(1,1))
plot(Weight, Height, cex=0.4)
abline(63.270,0.0384)
```
Draw sample 1 of size 60 from artificial population
```{r}
set.seed(15)
index1=sample(1:5000,60)
weight1=Weight[index1]
height1=Height[index1]
```
### 1(f)
Fit LS line
```{r}
weight=weight1
height=height1
fit=lm(height~weight)
#fitted line plot and model summary for our "real" sample
par(mfrow=c(1,1))
plot(weight,height,col='black',pch=19,cex=1,ylab='Height',xlab='Weight')
abline(fit, col='blue',lwd=2)
#model summary
summary(fit)
```
Compute test statistic "by-hand".
```{r}
(t<-(0.04418-0)/0.01271)
```
### 1(h)
Model summary
```{r}
summary(fit)
```
Confirm calculation (there will be some discrepancies just because of rounding)
```{r}
#RMSE
summary(fit)$sigma
#MSE
summary(fit)$sigma^2
```
### 1(i)
```{r}
#X=model.matrix( lm(height ~ weight) )
X=model.matrix(fit)
X
```
```{r}
solve(t(X) %*% X)
```
### 1(j)
Check your work:
```{r}
3.024*sqrt(1.766444e-05)
```
### 1(l)
Manual p-value computation
```{r}
(t=(0.04418-0.02)/0.01271)
pt(t,df=60-1-1,lower.tail=FALSE)
#or
pt(1.9, df=58, lower.tail=FALSE)
```
# Example 2: UOPgpa
### 2(b-c)
Import Data from .txt\
Save data file to same location as R script;
```{r}
setwd(dirname(rstudioapi::getActiveDocumentContext()$path))
UOPgpaData = read.table('UOPgpa.txt', header=TRUE, sep="\t")
```
Fit LS Regression Line\
-*-*-*-* IMPORTANT: Make sure you substitute "???" by the correct code.-*-*-*-*
```{r}
par(mfrow=c(1,1))
plot(GPA~StudyHours,data=UOPgpaData,pch=19,col='black')
UOPgpa.fit=lm(GPA~StudyHours,data=UOPgpaData)
abline(UOPgpa.fit,col='blue')
summary(UOPgpa.fit)
```
Correlation
```{r}
#Correlation
cor(UOPgpaData$StudyHours, UOPgpaData$GPA)
#Correlation squared
cor(UOPgpaData$StudyHours, UOPgpaData$GPA)^2
#this is the same values of R-squared in the output from the model
```
### 2(d)
Compute t.\
-*-*-*-* IMPORTANT: Make sure you substitute "???" by the correct code.-*-*-*-*
```{r}
t = (0.08938/0.02771)
t
```
### 2(f-g)
t critical value
```{r}
tstar=qt(p=0.975, df=80-2);tstar
```
CI calculation by hand. -*-*-*-* IMPORTANT: Write the coded needed to answer the question.-*-*-*-*
```{r}
0.08938 + 1.990847*(0.02771)
0.08938 - 1.990847*(0.02771)
```
### 2(h)
Confidence interval for pop. coeff.
```{r}
confint(UOPgpa.fit,level=0.95)
```
# Example 3: Advertising.txt \#
Import Data Save data file to same location as R script; then,
```{r}
#setwd(dirname(rstudioapi::getActiveDocumentContext()$path))
AdvData=read.table("Advertising.txt",header=T, sep='\t')
str(AdvData)
```
### 3(a)
\-*-*-*-* IMPORTANT: Write the coded needed to answer the question.-*-*-*-*
Fit a linear model
```{r}
Adv.fit = lm(Sales ~ TV + Radio, data = AdvData)
```
Find the Confidence Intervals for coefficients
```{r}
confint(Adv.fit, level = 0.95)
```
### 3(d-e)
CI's for coefficients w/ Bonferonni Adjustment -*-*-*-* IMPORTANT: Make sure you substitute "???" by the correct code.-*-*-*-*
```{r}
confint(Adv.fit, level = (1 - ((1 - 0.95)/3)))
```
### 3(f)
Model summary: Sales\~TV+Radio\
-*-*-*-* IMPORTANT: Write the coded needed to answer the question.-*-*-*-*\
Tip: Look back to your code from 3a.
```{r}
summary(Adv.fit)
```