-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.R
283 lines (204 loc) · 9.84 KB
/
main.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
#ucitavanje biblioteka
library('ggplot2')
library('car')
library('MASS')
# ucitavanje podataka
DepressionDataSet <- read.delim('./depression-and-the-internet.txt')
# Izbacivanje NA vrijednosti iz podataka (izgubljeno je 14 redova)
DepressionDataSet = na.omit(DepressionDataSet)
# promjena imena stupaca koji imaju losa imena
names(DepressionDataSet)[names(DepressionDataSet) == 'Household.income...000.'] = 'HouseholdIncome'
names(DepressionDataSet)[names(DepressionDataSet) == 'Household.size'] = 'HouseholdSize'
names(DepressionDataSet)[names(DepressionDataSet) == 'Internet.use..mean.hours.per.week.'] = 'InternetUse'
names(DepressionDataSet)[names(DepressionDataSet) == 'Race..white...1..minority...0.'] = 'Race'
# promjena rase u factor
DepressionDataSet$Race<- as.factor( DepressionDataSet$Race )
levels(DepressionDataSet$Race) = c('nonWhite', 'white')
#############################
##### Ovo je zadatak a) #####
#############################
summary(DepressionDataSet)
ggplot(DepressionDataSet, aes(HouseholdIncome) ) +
geom_histogram(bins = 20) +
geom_vline(aes(xintercept=mean(HouseholdIncome)), color='blue', linetype='dashed', size=1) +
xlab('Household income (in 1000)') +
ylab('')
ggplot(DepressionDataSet, aes(InternetUse) ) +
geom_histogram(bins = 20) +
geom_vline(aes(xintercept=mean(InternetUse)), color='blue', linetype='dashed', size=1) +
xlab('Internet use') +
ylab('')
ggplot(DepressionDataSet, aes(DepressionBefore) ) +
geom_histogram(bins = 20) +
geom_vline(aes(xintercept=mean(DepressionBefore)), color='blue', linetype='dashed', size=1) +
xlab('Depression before') +
ylab('')
ggplot(DepressionDataSet, aes(DepressionAfter) ) +
geom_histogram(bins = 20) +
geom_vline(aes(xintercept=mean(DepressionAfter)), color='blue', linetype='dashed', size=1) +
xlab('Depression after') +
ylab('')
ggplot(DepressionDataSet, aes(HouseholdIncome) ) +
geom_histogram(bins = 20) +
geom_vline(aes(xintercept=mean(HouseholdIncome)), color='blue', linetype='dashed', size=1) +
xlab('Household income (in 1000)') +
ylab('')
ggplot(DepressionDataSet, aes(HouseholdSize) ) +
geom_bar() +
xlab('Household size') +
ylab('')
#############################
##### Ovo je zadatak b) #####
#############################
cor(DepressionDataSet[, c('InternetUse', 'DepressionBefore', 'DepressionAfter', 'HouseholdIncome', 'HouseholdSize')])
ggplot(DepressionDataSet, aes(x = InternetUse, y = DepressionAfter)) + geom_point()
ggplot(DepressionDataSet, aes(x = DepressionBefore, y = DepressionAfter)) + geom_point()
ggplot(DepressionDataSet, aes(x = HouseholdSize, y = HouseholdIncome)) + geom_point()
#############################
##### Ovo je zadatak c) #####
#############################
# u isti dataframe je dodana stupac Difference
DepressionDataSet$Difference = DepressionDataSet$DepressionAfter - DepressionDataSet$DepressionBefore
# opis dobivene varijable
summary(DepressionDataSet$Difference)
ggplot(DepressionDataSet, aes(Difference) ) +
geom_histogram(bins = 20) +
geom_vline(aes(xintercept=mean(Difference)), color='blue', linetype='dashed', size=1) +
xlab('Depression after - Depression before') +
ylab('')
#############################
##### Ovo je zadatak d) #####
#############################
#prema Shapiro testu uzorci nisu iz normalne distribucije
shapiro.test(DepressionDataSet$DepressionBefore)
shapiro.test(DepressionDataSet$DepressionAfter)
shapiro.test(DepressionDataSet$InternetUse)
shapiro.test(DepressionDataSet$HouseholdIncome)
shapiro.test(DepressionDataSet$HouseholdSize)
shapiro.test(DepressionDataSet$Difference)
#iz grafova mozemo zakljuciti da Difference blizu normalne distribucije
ggplot(DepressionDataSet, aes(Difference) ) + geom_density()
ggplot(DepressionDataSet, aes(sample = Difference) ) + stat_qq() + stat_qq_line()
#############################
##### Ovo je zadatak e) #####
#############################
ggplot(data = DepressionDataSet, aes(x = Gender, y = Difference)) +
geom_boxplot()
#testiranje normalnosti po grupama
#Difference nije normalna po grupama Gender
tapply(DepressionDataSet$Difference, DepressionDataSet$Gender, shapiro.test)
ggplot(DepressionDataSet, aes(sample = Difference, colour = Gender)) +
stat_qq() +
stat_qq_line()
ggplot(DepressionDataSet, aes(x=Difference, color = Gender) ) + geom_density()
#parametarski test
tapply(DepressionDataSet$Difference, DepressionDataSet$Gender, var)
#Brown–Forsythe test
leveneTest(DepressionDataSet$Difference, DepressionDataSet$Gender, data = DepressionDataSet)
#prihvacamo hipotezu o jednakosti varijanca, ali nisu bili zadovoljeni uvjeti za provodenje testa
#neparametarski test
tapply(DepressionDataSet$Difference, DepressionDataSet$Gender, median)
wilcox.test(DepressionDataSet$Difference~DepressionDataSet$Gender)
#prihvacamo hipotezu da su medijani jednaki
#############################
##### Ovo je zadatak f) #####
#############################
ggplot(data = DepressionDataSet, aes(x = Race, y = Difference)) +
geom_boxplot()
#testiranje normalnosti po grupama
ggplot(DepressionDataSet, aes(sample = Difference, colour = Race)) +
stat_qq() +
stat_qq_line()
ggplot(DepressionDataSet, aes(x=Difference, color = Race) ) + geom_density()
tapply(DepressionDataSet$Difference, DepressionDataSet$Race, shapiro.test)
#Difference nije normalna po grupama Race
#parametarski test
tapply(DepressionDataSet$Difference, DepressionDataSet$Race, var)
#Brown–Forsythe test
leveneTest(DepressionDataSet$Difference, DepressionDataSet$Race, data = DepressionDataSet)
#prihvacamo hipotezu o jednakosti varijanca, ali nisu bili zadovoljeni uvjeti za provodenje testa
#neparametarski test
tapply(DepressionDataSet$Difference, DepressionDataSet$Race, median)
wilcox.test(DepressionDataSet$Difference~DepressionDataSet$Race)
#prihvacamo hipotezu da su medijani jednaki
#############################
##### Ovo je zadatak g) #####
#############################
ggplot(data = DepressionDataSet, aes(x = Age, y = Difference)) +
geom_boxplot()
#testiranje normalnosti po grupama
ggplot(DepressionDataSet, aes(sample = Difference, colour = Age)) +
stat_qq() +
stat_qq_line()
ggplot(DepressionDataSet, aes(x=Difference, color = Age) ) + geom_density()
tapply(DepressionDataSet$Difference, DepressionDataSet$Age, shapiro.test)
#parametarski test
tapply(DepressionDataSet$Difference, DepressionDataSet$Age, var)
#Brown–Forsythe test
leveneTest(DepressionDataSet$Difference, DepressionDataSet$Age, data = DepressionDataSet)
#prihvacamo hipotezu o jednakosti varijanca, ali nisu bili zadovoljeni uvjeti za provodenje testa
#neparametarski test
tapply(DepressionDataSet$Difference, DepressionDataSet$Age, median)
wilcox.test(DepressionDataSet$Difference~DepressionDataSet$Age)
# postoji signifikantna razlika, pa prihvacamo da medijani nisu jednaki
#############################
##### Ovo je zadatak h) #####
#############################
ggplot(data = DepressionDataSet, aes(x = interaction(Age, Gender), y = InternetUse)) +
geom_boxplot()
tapply(DepressionDataSet$InternetUse, list(DepressionDataSet$Age, DepressionDataSet$Gender), median)
tapply(DepressionDataSet$InternetUse, list(DepressionDataSet$Age, DepressionDataSet$Gender), var)
ggplot(DepressionDataSet, aes(sample=InternetUse, color = interaction(Age, Gender)) ) +
stat_qq() +
stat_qq_line()
tapply(DepressionDataSet$InternetUse, interaction(DepressionDataSet$Age, DepressionDataSet$Gender), shapiro.test)
#nisu zadovoljene pretpostavke za provodenje anove
#ANOVA
ANOVA.model = aov(InternetUse ~ Age * Gender, data = DepressionDataSet)
summary(ANOVA.model)
#postoji signifikantna razlika po grupi godine
TukeyHSD(ANOVA.model)
#postoji signifikantnarazlika izmedu grupa Teen:male i Adult:male, kao i grupa Teen:male i Adult:female
kruskal.test(InternetUse ~ interaction(Age, Gender), data=DepressionDataSet)
#postoji signifikantna razlika izmdeu grupa
#############################
##### Ovo je zadatak i) #####
#############################
# modificirano grupiranje spremljeno u ModifiedHouseholdSize
DepressionDataSet$ModifiedHouseholdSize = as.factor(DepressionDataSet$HouseholdSize)
levels( DepressionDataSet$ModifiedHouseholdSize )= c("1","2","3","4","5","5")
DepressionDataSet$ModifiedHouseholdSize = as.integer(DepressionDataSet$ModifiedHouseholdSize)
ggplot(data = DepressionDataSet, aes(group = ModifiedHouseholdSize, y=Difference)) +
geom_boxplot()
tapply(DepressionDataSet$Difference, DepressionDataSet$ModifiedHouseholdSize, shapiro.test)
#nisu zadovoljene pretpostavke parametarskog testa (samo je grupa '2' normalna)
kruskal.test(Difference ~ ModifiedHouseholdSize, data=DepressionDataSet)
# ne postoji signifikantna razlika
#############################
##### Ovo je zadatak j) #####
#############################
#izrada dummy varijabla
ModelsDataSet = subset(DepressionDataSet, select = c(Difference, InternetUse, HouseholdIncome, HouseholdSize))
ModelsDataSet$IsFemale = as.integer(DepressionDataSet$Gender == 'female')
ModelsDataSet$IsWhite = as.integer(DepressionDataSet$Race == 'white')
ModelsDataSet$IsAdult = as.integer(DepressionDataSet$Age == 'Adult')
#pregled korelacije izmedu varijabli
cor(ModelsDataSet)
#linearni model sa svim varijablama
Linear.model.all = lm(Difference ~ ., ModelsDataSet)
summary(Linear.model.all)
#prazan linearni model
Linear.model.empty = lm(Difference ~ 1, ModelsDataSet)
#metode izbora varijabli
stepAIC(Linear.model.empty, direction = 'forward', scope=list(upper=Linear.model.all,lower=Linear.model.empty))
stepAIC(Linear.model.all, direction = 'backward')
stepAIC(Linear.model.empty, direction = 'both', scope=list(upper=Linear.model.all,lower=Linear.model.empty))
#sve metode izbora varijabli daju isti rezultat
Linear.model = lm(formula = Difference ~ HouseholdSize + IsAdult + InternetUse, data = ModelsDataSet)
summary(Linear.model)
#############################
##### Ovo je zadatak k) #####
#############################
ggplot(fortify(Linear.model), aes(sample = .resid)) +
stat_qq() +
stat_qq_line()