-
Notifications
You must be signed in to change notification settings - Fork 0
/
Ngare_lab7_correlatio.Rmd
308 lines (213 loc) · 6.47 KB
/
Ngare_lab7_correlatio.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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
---
title: "Geog533 Lab 7 - Correlation"
author: "Ngare Winnie"
output:
html_document:
df_print: paged
toc: yes
html_notebook:
toc: yes
toc_float: yes
---
## Question 1
This is Exercise 1 in Chapter 7 of the Textbook [R].
### Problem 1a
Find the correlation coefficient, *r*, for the following sample data on income and education:
```{r}
library(knitr)
obs <- seq(1:5)
income <- c(30,28,52,40,35)
edu <- c(12,13,18,16,17)
df <- data.frame(obs,income,edu)
names(df) <- c("Observation","Income ($*1000)","Education (Years)")
kable(df)
```
Solution 1a
```{r}
cor(income,edu)
```
### Problem 1b
Test the null hypothesis ρ = 0.
Solution 1b
```{r}
cor.test(income,edu)
2/sqrt(5)
print("we do not reject the null hypothesis.Therefore there is no correlation.")
```
### Problem 1c
Find Spearman’s rank correlation coefficient for these data.
Solution 1c
```{r}
cor(income,edu,method = "spearman")
```
### Problem 1d
Test whether the observed value of rs from part (c) is significantly different from zero.
Solution 1d
```{r}
cor.test(income,edu,method = "spearman")
2/sqrt(5)
print("we do not reject the null hypothesis")
print("correlation is equal to zero.")
```
## Question 2
This is Exercise 3 in Chapter 7 of the Textbook [R].
### Problem
The distribution of the t-statistic for testing the significance of a correlation coefficient has n – 2 degrees of freedom. If the sample size is 36 and α = 0.05, what is the smallest absolute value a correlation coefficient must have to be significant? What if the sample size is 80?
### Solution
```{r}
sample36 <- 2/sqrt(36)
sample80 <- 2/sqrt(80)
sample36
sample80
```
## Question 3
This is Exercise 4 in Chapter 7 of the Textbook [R].
### Problem
Find the correlation coefficient for the following data:
```{r}
library(knitr)
Obs <- seq(1:4)
X <- c(2,8,9,7)
Y <- c(6,6,10,4)
df <- data.frame(Obs,X,Y)
kable(df)
```
### Solution
```{r}
cor.test(X,Y)
print("Accept the null.True correlation is equal to zero.")
```
## Question 4
This is Exercise 6 in Chapter 7 of the Textbook [R].
### Problem
Find the correlation coefficient between median annual income in the United States and the number of horse races won by the leading jockey, for the period 1984–1995. Test the hypothesis that the true correlation coefficient is equal to zero. Interpret your results.
```{r}
year <- 1984:1994
income <- c(35165,35778,37027,37256,37512,37997,37343,36054,35593,35241,35486)
races <- c(399,469,429,450,474,598,364,430,433,410,317)
df <- data.frame(year,income,races)
names(df) <- c("Year","Median income","Number of races won by leading jockey")
kable(df)
```
### Solution
```{r}
cor.test(income,races)
2/sqrt(11)
print("We do not reject the null:Therefore true correlation is equal to zero.")
```
## Question 5
This is Exercise 7 in Chapter 7 of the Textbook [R].
### Problem
For the following ranked data, find Spearman’s r, and then test the null hypothesis (using a Type I error probability of 0.10) that the true correlation is equal to zero.
```{r}
library(knitr)
obs <- 1:6
x <- c(1,2,5,6,11,12)
y <- c(8,4,12,3,10,7)
df <- data.frame(obs,x,y)
names(df) <- c("Observation","Rank of x","Rank of y")
kable(df)
```
### Solution
```{r}
cor.test(x,y,method = "spearman",use="complete.obs",conf.level = 0.90)
2/sqrt(6)
print("accept the null:Therefore true correlation is equal to zero.")
```
## Question 6
This is Exercise 8 in Chapter 7 of the Textbook [R].
### Problem
Find Pearson’s r for the following data, and then test the null hypothesis that the correlation coefficient is equal to zero. Use a Type I error probability of 0.05.
```{r}
library(knitr)
obs <- 1:6
x <- c(3.2,2.4,1.6,8.3,7.2,5.1)
y <- c(6.2,7.3,8.1,2.6,6.3,4.3)
df <- data.frame(obs,x,y)
names(df) <- c("Observation","x","y")
kable(df)
```
### Solution
```{r}
cor.test(x,y,method = "pearson")
2/sqrt(6)
print("We do not reject the null hypothesis:Therefore true correlation is equal to 0")
```
## Question 7
This is Exercise 9 in Chapter 7 of the Textbook [R].
### Problem
Using R and the Milwaukee dataset, find the correlation between number of bedrooms and lot size.
### Solution
```{r}
dset<- read.csv("Milwaukee_Sales.csv",header = T)
cor.test(dset$Bedrms,dset$LotSize,use = "complete.obs")
print("We do not reject the null hypothesis.Therefore correlation is equal to zero.")
```
## Question 8
This is Exercise 10 in Chapter 7 of the Textbook [R].
### Problem
Using R and the Hypothetical UK Housing Prices dataset, find the correlation between floor area and number of bedrooms.
### Solution
```{r}
uk <- read.csv("UK_Housing.csv",header = T)
cor.test(uk$floorarea,uk$bedrooms)
print("Reject the null hypothesis:Therefore floor area and bedrooms have a correlation.")
```
## Question 9
Use the **cars** data frame in the **datasets** package to perform the following tasks:
### Problem 9a
Plot a scatterplot for the data frame (x: speed, y: dist)
Solution 9a
```{r}
library(datasets)
df <- cars
plot(df$speed,df$dist,main = "Scatter plot of speed and distance.")
```
### Problem 9b
How many rows in the data frame?
Solution 9b
```{r}
nrow(df)
```
### Problem 9c
Calculate Pearson’s correlation coefficient using the equation below:
$$r=\frac{\sum_{n}^{i=1}(x_i-\bar{x})(y_i-\bar{y})}{(n-1)s_{x}s_{y}}$$
Solution 9c
```{r}
car.cov <- cov(df$speed,df$dist)
n <- 50
degree <- n-1
num <- car.cov*degree
num
denom <- degree*sd(df$speed)*sd(df$dist)
denom
r <- num/denom
r
```
### Problem 9d
Use the cor.test() function to find Pearson’s correlation coefficient and compare it to the one from part (c)
Solution 9d
```{r}
cor.test(df$speed,df$dist,method = "pearson")
print("They are the same")
```
### Problem 9e
Calculate Spearman’s rank correlation coefficient using the equation below:
$$r_{S} = 1 - \frac{6\sum_{i=1}^{n}d_{i}^{2}}{n^3-n}$$
Solution 9e
```{r}
df$ranksp <- rank(df$speed)
df$rankdist <- rank(df$dist)
df$d <- df$ranksp-df$rankdist
df$sq.d <- {df$d}^2
summation <- sum(df$sq.d)
spear.rank <- 1-{6*summation/{50^3-50}}
spear.rank
```
### Problem 9f
Use the cor.test() function to find Spearman’s rank correlation coefficient and compare it to the one from part (e)
Solution 9f
```{r}
cor.test(df$speed,df$dist,method = "spearman")
print("They are the same.")
```