-
Notifications
You must be signed in to change notification settings - Fork 0
/
Statistical Theory and Methods - Assignment.R
352 lines (345 loc) · 10.7 KB
/
Statistical Theory and Methods - Assignment.R
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
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
### Statistical Theory and Methods
### Coursework 1
rm(list=ls())
load("rehoming.Rdata")
createsample("201749908")
save(mysample,file="mysample.RData")
mysample
### The dataset I am working with is mysample
## Column Names in the dataset
colnames(mysample)
## Data type for all the columns in the dataset
str(mysample)
### Getting the breeds in the samples
unique(mysample$Breed)
#---------------------------------------------------------#
#--------------------- Data Cleaning----------------------#
# Unique values in all the columns
unique(mysample$Visited)#None
unique(mysample$Rehomed)### Has 99999 and -1 present
unique(mysample$Health)#None
unique(mysample$Breed)### Has NA present
unique(mysample$Age)#None
unique(mysample$Reason)### Has NA values present
unique(mysample$Returned)### Has NA values present
### Getting the percentage of observations removed
## Breed
len_breed=length(mysample$Breed)
len_breed###no. of samples in breed
len_na_breed=sum(is.na(mysample$Breed))
len_na_breed
per_rem_breed=(len_na_breed/len_breed)*100
per_rem_breed
## Rehoming
len_rehoming=length(mysample$Rehomed)
len_rehoming
len_na_rehoming=nrow(mysample[mysample$Rehomed=='99999',])
len_na_rehoming
per_rem_rehoming=(len_na_rehoming/len_rehoming)*100
per_rem_rehoming
### Both together
a=mysample[is.na(mysample$Breed),]
a
# no common values - so basically the sum of two
### Droping rows based on the Rehoming column
new_samples<-mysample[mysample$Rehomed!='99999',]
length(new_samples$Visited)
### Dropping rows based on the Breed column
new_samp<-mysample[!is.na(mysample$Breed),]
new_samp<-new_samp[new_samp$Rehomed!='99999',]
length(new_samp$Visited)
#---------------------------------------------------------#
#-------------------Data Exploration----------------------#
new_data_grouped<-split(new_samp, new_samp$Breed)
new_data_grouped
summary(new_data_grouped$`Border Collie`)
summary(new_data_grouped$Greyhound)
summary(new_data_grouped$`Labrador Retriever`)
### Get box plot based on Health
boxplot(new_data_grouped$`Border Collie`$Health)
boxplot(new_data_grouped$Greyhound$Health)
boxplot(new_data_grouped$`Labrador Retriever`$Health)
### Bar graph based on age
par(mfrow=c(3,1))
A<-new_data_grouped$`Border Collie`$Age
a<-table(A)
barplot(a, main="Age Distribution of Border Collies", xlab="Age",ylab="Total Number", horiz=TRUE)
B<-new_data_grouped$Greyhound$Age
b<-table(B)
barplot(b,main="Age Distribution of Greyhound", xlab="Age",ylab="Total Number", horiz=TRUE)
C<-new_data_grouped$`Labrador Retriever`$Age
c<-table(C)
barplot(c,main="Age Distribution of Labrador Retrievers", xlab="Age",ylab="Total Number", horiz=TRUE)
D<-new_data_grouped$`Labrador Retriever`$Visited
d<-table(D)
barplot(d,main="Age Distribution of Greyhound", xlab="Age",ylab="Total Number")
# Got to remove the -1 values because visiting cant be negative
colnames(new_data_grouped$`Border Collie`)
#---------------------------------------------------------#
#-----------------Modelling and estimation----------------#
# Rehoming plot
par(mfrow=c(3,1))
new_data_grouped<-split(new_samples, new_samples$Breed)
A<-new_data_grouped$`Border Collie`
B<-new_data_grouped$Greyhound
C<-new_data_grouped$`Labrador Retriever`
BC_R<-A$Rehomed
B$Rehomed### Removing the -1 as well
B<-B[B$Rehomed!='-1',]
G_R<-B$Rehomed
LR_R<-C$Rehome
range(BC_R)
range(G_R)
range(LR_R)
hist(BC_R, breaks=c(0,5,10,15,20,25,30,35,40,45,50,55,60),right=FALSE, freq=TRUE, main="Histogram of Border Collies rehomed", xlab="Week Ranges")
hist(G_R,breaks=c(0,15,30,45,60,75,90),right=FALSE, freq=TRUE, main="Histogram of Greyhounds rehomed", xlab="Week Ranges")
hist(LR_R, breaks=c(0,5,10,15,20,25,30,35,40,45,50,55),right=FALSE,freq=TRUE, main="Histogram of Labrador Retrievers rehomed", xlab="Week Ranges")
## They are discrete Variables - as it is in terms of whole weeks and not with decimal places
####################
# Binomial Distribuiton
f = function(x, n, p){
return( (p^x)*((1-p)^(n-x))*factorial(n)/(factorial(x)*factorial(n-x)))
}
####################
# Poissons Distribution
f1 = function (l, x){
return (((l^x)*exp(-1*l))/factorial(x))
}
####################
#Geometric Distribution
f2=function(p,x){
return (p*((1-p)^x))
}
#######################################
#BORDER COLLIE
hist(BC_R, breaks=c(0,5,10,15,20,25,30,35,40,45,50,55,60),right=FALSE, freq=TRUE)
BC_R<-c(BC_R)
BC_R
x=BC_R
curve(dnorm(x, mean=mean(x), sd=sd(x)),
col="darkblue", lwd=2, add=TRUE, yaxt="n")
plotNormalHistogram( x, prob = FALSE,
main = "Normal Distribution overlay on Histogram",
length = 60 )
pmf <- dpois(x, lambda=mean(x))
plot(
x,
pmf,
type = "h",
lwd = 3,
main = "Poisson PMF",
xlab = "Number of events",
ylab = "Probability"
)
### Best fit is Binomial Distribution
########################################
#GREYHOUND
hist(G_R,breaks=c(0,15,30,45,60,75),right=FALSE, freq=FALSE)
q<-mean(G_R)
p<-1/q
x<-c(G_R)
curve(dexp(x, rate=1/mean(x)),
col="darkblue", lwd=2, add=TRUE, yaxt="n")
curve(dgeom(x, p=1/mean(x)),
col="red", lwd=2, add=TRUE, yaxt="n")
### best fit is Geometric Distribution
########################################
#LAB RETRIEVER
hist(LR_R, breaks=c(0,10,20,30,40,50,60),right=FALSE,freq=TRUE)
LR_R<-c(LR_R)
LR_R
x=LR_R
curve(dnorm(x, mean=mean(x), sd=sd(x)),
col="darkblue", lwd=2, add=TRUE, yaxt="n")
plotNormalHistogram( x, prob = FALSE,
main = "Normal Distribution overlay on Histogram",
length = 60 )
pmf <- dpois(x, lambda=mean(x))
plot(
x,
pmf,
type = "h",
lwd = 3,
main = "Poisson PMF",
xlab = "Number of events",
ylab = "Probability"
)
l<-mean(BC_R)
l
n<-60
p<-l/n
x<-seq(from=0,to=60,by=1)
plot(x,f1(l,x),col="blue", type='l')
l<-mean(LR_R)
n<-50
p<-l/n
x<-seq(from=0,to=50,by=1)
plot(x,f1(l,x),col="blue", type='l')
plot(x,f(x,n,p),col="red",type='l')
### best fit is Poissons Distribution
########################################
#Checking the Suitability of the proposed model
# Checking for QQ plots
# Border Collie
par(mfrow=c(1,1))
BC_R
BC_R<-BC_R[order(BC_R)]
BC_R
mu<-mean(BC_R)
sigma<-sd(BC_R)
sort(BC_R)
z<-(sort(BC_R)-mu)/sigma
n<-length(BC_R)
r<-(1:n)
q<-qnorm(p=r/(n+1),mean=0,sd=1)
plot(q,z)
abline(a=0, b=1, col="red")
qqnorm(BC_R)
abline(a=0, b=1, col="red")
#--------------------------------------------------#
#----------------Inference------------------------#
# What to check the mean rehoming time for the three breeds is 27 weeks
# We use confidence interval to do this
# Its like guessing ranges, how likely it is that the mean rehoming time
# is within a certain range. That range is called as confidence interval
# 2 types - z intervals and T intervals
# 1.z intervals - when the population sd is known
# 2. t intervals - when the population sd is not known
################################################
# Using z intervals
# 1. Border Collie
mu<-mean(BC_R)
sigma<-sd(BC_R)
mu
sigma
z<-1.96
CI_1 <-(mu-(z*sigma))
CI_2 <-(mu+(z*sigma))
CI_1
CI_2
# 2. Greyhound
mu<-mean(G_R)
sigma<-sd(G_R)
mu
sigma
z<-1.96
CI_1 <-(mu-(z*sigma))
CI_2 <-(mu+(z*sigma))
CI_1
CI_2
# 3. Lab Retriever
mu<-mean(LR_R)
sigma<-sd(LR_R)
mu
sigma
z<-1.96
CI_1 <-(mu-(z*sigma))
CI_2 <-(mu+(z*sigma))
CI_1
CI_2
### uSING T - TEST
# 1. Border Collie
mu<-mean(BC_R)
sigma<-sd(BC_R)
n<-length(BC_R)
standard_error = sigma / sqrt(n)
degrees_of_freedom = n - 1
t_value = qt(0.975, degrees_of_freedom) # t-value for 95% confidence interval
margin_of_error = t_value * (standard_error)
lower_ci = mu - margin_of_error
upper_ci = mu + margin_of_error
print( c("border collie",mu, lower_ci, upper_ci))
# 2. Grey hound
mu<-mean(G_R)
sigma<-sd(G_R)
n<-length(G_R)
standard_error = sigma / sqrt(n)
degrees_of_freedom = n - 1
t_value = qt(0.975, degrees_of_freedom) # t-value for 95% confidence interval
margin_of_error = t_value * (standard_error)
lower_ci = mu - margin_of_error
upper_ci = mu + margin_of_error
print( c("Grey hound",mu, lower_ci, upper_ci))
# 3. Lab Ret
mu<-mean(LR_R)
sigma<-sd(LR_R)
n<-length(LR_R)
standard_error = sigma / sqrt(n)
degrees_of_freedom = n - 1
t_value = qt(0.975, degrees_of_freedom) # t-value for 95% confidence interval
margin_of_error = t_value * (standard_error)
lower_ci = mu - margin_of_error
upper_ci = mu + margin_of_error
print( c("Lab ret",mu, lower_ci, upper_ci))
#-------------------------------------------------------------#
#-------------------------Discussions-------------------------#
#Will directly write in Word
#-------------------------------------------------------------#
#------------------------Comparison---------------------------#
# A, B combo
breed1 <- A
breed2 <- B
breed1
breed2
breed1_R<-breed1$Rehomed
breed2_R<-breed2$Rehomed
mean_breed1 <- mean(breed1_R)
mean_breed2 <- mean(breed2_R)
sd_breed1 <- sd(breed1_R)
sd_breed2 <- sd(breed2_R)
n_breed1 <- length(breed1_R)
n_breed2 <- length(breed2_R)
standard_error_breed1 <- sd_breed1 / sqrt(n_breed1)
standard_error_breed2 <- sd_breed2 / sqrt(n_breed2)
difference_means <- mean_breed1 - mean_breed2
margin_of_error <- 1.96 * sqrt(standard_error_breed1^2 + standard_error_breed2^2)
lower_ci <- difference_means - margin_of_error
upper_ci <- difference_means + margin_of_error
print( c("Border Collie','Greyhound','Breed 1 mean",
mean_breed1, 'Breed 2 mean',mean_breed2,
'lower _ci',lower_ci,"Upper_ci", upper_ci))
# A, C Combo
breed1 <- A
breed2 <- C
breed1
breed2
breed1_R<-breed1$Rehomed
breed2_R<-breed2$Rehomed
mean_breed1 <- mean(breed1_R)
mean_breed2 <- mean(breed2_R)
sd_breed1 <- sd(breed1_R)
sd_breed2 <- sd(breed2_R)
n_breed1 <- length(breed1_R)
n_breed2 <- length(breed2_R)
standard_error_breed1 <- sd_breed1 / sqrt(n_breed1)
standard_error_breed2 <- sd_breed2 / sqrt(n_breed2)
difference_means <- mean_breed1 - mean_breed2
margin_of_error <- 1.96 * sqrt(standard_error_breed1^2 + standard_error_breed2^2)
lower_ci <- difference_means - margin_of_error
upper_ci <- difference_means + margin_of_error
print( c("Border Collie','Lab','Breed 1 mean",
mean_breed1, 'Breed 2 mean',mean_breed2,
'lower _ci',lower_ci,"Upper_ci", upper_ci))
# B,C combo
breed1 <- B
breed2 <- C
breed1
breed2
breed1_R<-breed1$Rehomed
breed2_R<-breed2$Rehomed
mean_breed1 <- mean(breed1_R)
mean_breed2 <- mean(breed2_R)
sd_breed1 <- sd(breed1_R)
sd_breed2 <- sd(breed2_R)
n_breed1 <- length(breed1_R)
n_breed2 <- length(breed2_R)
standard_error_breed1 <- sd_breed1 / sqrt(n_breed1)
standard_error_breed2 <- sd_breed2 / sqrt(n_breed2)
difference_means <- mean_breed1 - mean_breed2
margin_of_error <- 1.96 * sqrt(standard_error_breed1^2 + standard_error_breed2^2)
lower_ci <- difference_means - margin_of_error
upper_ci <- difference_means + margin_of_error
print( c('Greyhound','Lab ret','Breed 1 mean',
mean_breed1, 'Breed 2 mean',mean_breed2,
'lower _ci',lower_ci,"Upper_ci", upper_ci))
###############################################################