-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathweek_02_assign_02.Rmd
193 lines (144 loc) · 4.5 KB
/
week_02_assign_02.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
---
title: "week_02_assign_02"
author: "[email protected]"
date: "`r Sys.Date()`"
output: html_document
---
```{r}
lc2 <- read.table("C:\\Users\\JEmre\\Downloads\\LastCall2.txt",header=TRUE)
head(lc2)
```
```{r}
m = lc2$male
f = lc2$female
```
```{r}
sort(f)
n <- length(f)
mean <- sum(f)/length(f)
mean(f)
# calculating the median by working out the rank first
0.5 * (n-1) + 1
sort(f)[13]
median(f)
# 25-th percentile
0.25 * (n-1) + 1
sort(f)[7]
quantile(f,0.25)
# 75-th percentile
0.75 * (n-1) + 1
sort(f)[19]
#CHECK
quantile(f,0.75)
#var
sum((f-(sum(f)/length(f)))^2)/(n-1)
#CHECK
var(f)
IQR(f)
boxplot(f) # we can see that it is overly skewed to the right i.e. median will be lower than the mean but it will have a higher probability of occurrence or density , that is is usually the case of returns of financial assets traded on organized exchanges , and in such situation we will need way higher sample size to validate our conclusions statistically speaking #
```
Question: can we subset multiple indices from the data?
Yes, you can do this by first creating a vector of indices. Remember that you can do this using the function `c()`, which stands for *concatenate* or in other words, *merge together*.
```{r}
sort(f)[c(19,22,23)]
```
```{r}
head(m)
```
```{r}
data.frame(mean(m),var(m),median(m),sd(m))
```
```{r}
mean.f <- mean(f)
mean.m <- mean(m)
```
let's us combine some of the statistics above in one data frame
```{r}
data.frame(mean(f),mean(m),var(f),var(m))
```
```{r}
gardasil <- read.table("C:\\Users\\JEmre\\Downloads\\GardasilStudy.csv",header = T, sep = ";")
head(gardasil)
```
Checking that the variable `Race` in the Gardasil dataset is a factor.
```{r}
is.factor(gardasil$Race)
```
```{r}
is.factor(gardasil$Completed)
```
Given that answer is false , let us add these factors as numerical
```{r}
gardasil$Completed <- factor(gardasil$Completed,
levels=c(1,0),
labels=c("Completed","Uncompleted"))
is.factor(gardasil$Completed)
```
let us construct some tables and check the distribution of some of the commands operating on bivariate , n-variate data sets
```{r}
table(gardasil$Race)
table(gardasil$Completed)
table(gardasil$InsuranceType)
table(gardasil$LocationType)
```
```{r}
table(gardasil)
```
Now, we transform variable frequencies of "InsuranceType" in to proportions
```{r}
ins.tab <- table(gardasil$InsuranceType)
ins.tab <- ins.tab/sum(ins.tab)
ins.tab
```
The function `xtabs()` allow to construct contingency tables by specifying which variables to use. Build a contingency table to represent the type of insurance according to the location of the clinics.
```{r}
xtabs(~InsuranceType+LocationType,data=gardasil)
nrow(gardasil)
```
Plotting
```{r}
mosaicplot(~gardasil$Completed+gardasil$LocationType)
#corresponding contingency table
xtabs(~Completed+LocationType,data=gardasil)
```
Now we change the order of the variables being plotted within the
contingency table
```{r}
mosaicplot(~gardasil$LocationType+gardasil$Completed)
#corresponding contingency table
xtabs(~LocationType+Completed,data=gardasil)
```
To add labels on the x-axis and y-axis
```{r}
mosaicplot(~gardasil$Completed+gardasil$LocationType,xlab = "Completed",ylab="Location Type")
```
Question;If you were in an urban area, would you have a smaller or greater probability to complete the vaccine study?
```{r}
mosaicplot(~gardasil$Completed+gardasil$LocationType,xlab = "Completed",ylab="Location Type")
# we can see that rectangle corresponding to the urban is smaller hence lower probability to complete the vaccine
```
```{r}
mosaicplot(~gardasil$LocationType+gardasil$Completed+gardasil$InsuranceType, xlab="Location Type",ylab="Completed", las=2,main="Location vs Completed according to insurance")
```
```{r}
# angles for each slice corresponding to each category of the `Race` variable
race.freq = table(gardasil$Race)/sum(table(gardasil$Race))
race.freq*360
```
```{r}
pie(table(gardasil$InsuranceType),radius = 1 , init.angle = 90)
```
```{r}
pie(table(gardasil$Race), col = sample(colours(distinct = TRUE), 4))
```
```{r}
barplot(
table(gardasil$InsuranceType),
main="Patients by type of insurance",
col=c("red","green","blue","black")
)
```
```{r}
barplot(table(gardasil$LocationType), horiz=T,col=sample(colours(distinct = TRUE)))
title("Patients by type of location")
```