-
Notifications
You must be signed in to change notification settings - Fork 0
/
.Rhistory
512 lines (512 loc) · 53.2 KB
/
.Rhistory
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
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
#making sure that the value of perceived support stays in a range from 0 to 10
ifelse(AgentPop$Perceived_Support[AgentPop$AgentNo[i]] >= 10 ,AgentPop$Perceived_Support[AgentPop$AgentNo[i]] <- 10, ifelse(AgentPop$Perceived_Support[AgentPop$AgentNo[i]] <= 0, AgentPop$Perceived_Support[AgentPop$AgentNo[i]] <- 0, AgentPop$Perceived_Support[AgentPop$AgentNo[i]] <- AgentPop$Perceived_Support[AgentPop$AgentNo[i]]))
#finding out the changed perceived support level
Perceived_Support_update <- AgentPop$Perceived_Support[AgentPop$AgentNo[i]]
#PERCEIVED SUPPORT BUFFERS AGAINST DISTRESS
#calculating the individual buffer (%), which is determined by the perceived support level, and indicates by how much perceived support buffers against distress
individual_buffer <- Perceived_Support_update / 11
#individual buffer together with the set impact value of the event determines the amount of distress that is going to be subtracted from the distress level after the event has happened
AgentPop$Distress[AgentPop$AgentNo[i]] <- AgentPop$Distress[AgentPop$AgentNo[i]] - (Happened_Events$Impact[k] * individual_buffer)
#LIKELIHOOD OF MAJOR DEPRESSIVE DISORDER ONSET
#setting different distress thresholds for Major Depressive Disorder onset in low and high vulnerability groups
#I grab those in low vulnerability group with distress level bigger than 40
Low_Vulnerability <- (1:nrow(AgentPop))[AgentPop$Vulnerability_Group == "L" & AgentPop$Distress >= 40]
#I set their MDD onset to 1 (meaning it is very likely that Major Depressive Disorder might have started)
AgentPop$MDD_onset[Low_Vulnerability] <- 1
#I grab those in high vulnerability group with distress level bigger than 13.33 (there is a threefold increased risk of MDD onset in a high vulnerability group)
High_Vulnerability <- (1:nrow(AgentPop))[AgentPop$Vulnerability_Group == "H" & AgentPop$Distress >= 13.33]
#I set their MDD onset to 1 (meaning it is very likely that Major Depressive Disorder might have started)
AgentPop$MDD_onset[High_Vulnerability] <- 1
}
}
######## Simulation Ends ########
#Count of MDD onset in Low and High Vulnerability groups
c <- AgentPop %>% group_by(Vulnerability_Group) %>% count(MDD_onset) %>% na.omit()
#visualize MDD onset count for different intervals of perceived support in both vulnerability groups
plot(Distress ~ Perceived_Support, data = AgentPop)
ggplot(AgentPop, aes(x = Perceived_Support, y = Distress, colour = Vulnerability_Group)) +
geom_smooth(method = lm) + geom_hline(yintercept = 13.33, color='coral', linetype = "dashed")+ geom_hline(yintercept = 40, color='#00AFBB', linetype = "dashed") + geom_point() + ggtitle("Relationship between Perceived support and Distress level", "Received inappropriate support") + theme_classic()
#visualize how many of MDD onset in total are in L and H groups
ggplot(AgentPop, aes(x=Vulnerability_Group, y=MDD_onset)) + geom_bar(stat="identity", fill = "#00AFBB") + ggtitle("Count of Major Depressive Disorder onset in Low and High Vulnerability groups") + ggtitle("Count of Major Depressive Disorder onset in Low and High Vulnerability groups", "Received inappropriate support") + theme_classic()
AgentPop %>% group_by(Vulnerability_Group) %>% summarise(mean = mean(Perceived_Support), sd = sd(Perceived_Support))
AgentPop %>% group_by(Vulnerability_Group) %>% summarise(mean = mean(Distress), sd = sd(Distress), max = max(Distress), min = min(Distress))
AgentPop %>% group_by(Vulnerability_Group) %>% summarise(sum(Inappropriate_Received_support_count))
View(c)
AgentPop$MDD_onset <- as.numeric(AgentPop$MDD_onset)
class(AgentPop$MDD_onset)
#Count of MDD onset in Low and High Vulnerability groups
c <- AgentPop %>% group_by(Vulnerability_Group) %>% count(MDD_onset) %>% na.omit()
#visualize MDD onset count for different intervals of perceived support in both vulnerability groups
plot(Distress ~ Perceived_Support, data = AgentPop)
ggplot(AgentPop, aes(x = Perceived_Support, y = Distress, colour = Vulnerability_Group)) +
geom_smooth(method = lm) + geom_hline(yintercept = 13.33, color='coral', linetype = "dashed")+ geom_hline(yintercept = 40, color='#00AFBB', linetype = "dashed") + geom_point() + ggtitle("Relationship between Perceived support and Distress level", "Received inappropriate support") + theme_classic()
#visualize how many of MDD onset in total are in L and H groups
ggplot(AgentPop, aes(x=Vulnerability_Group, y=MDD_onset)) + geom_bar(stat="identity", fill = "#00AFBB") + ggtitle("Count of Major Depressive Disorder onset in Low and High Vulnerability groups") + ggtitle("Count of Major Depressive Disorder onset in Low and High Vulnerability groups", "Received inappropriate support") + theme_classic()
AgentPop %>% group_by(Vulnerability_Group) %>% summarise(mean = mean(Perceived_Support), sd = sd(Perceived_Support))
AgentPop %>% group_by(Vulnerability_Group) %>% summarise(mean = mean(Distress), sd = sd(Distress), max = max(Distress), min = min(Distress))
AgentPop %>% group_by(Vulnerability_Group) %>% summarise(sum(Inappropriate_Received_support_count))
View(c)
AgentPop <- read.csv(file = "~/Rutos SmuTkes/Cognitive Science/4th Semester/Social and cultural dynamics/ABM models/Agent_Population.csv")
View(AgentPop)
######## Simulation Starts ########
#creating a for loop to go through all the events
for (k in 1:nrow(Happened_Events)){
#creating a for loop to go through all the agents in a populations
for (i in 1:nrow(AgentPop)){
#STRESSFUL EVENT INCREASE DISTRESS LEVEL
#determine which event with what valence happens
Event <- Happened_Events$Valence[k]
#events increase the distress level of agents
ifelse(Event >= -13 & Event <= -12 ,AgentPop$Distress[AgentPop$AgentNo[i]] <- AgentPop$Distress[AgentPop$AgentNo[i]] + 14, ifelse(Event > -12 & Event <= -8 ,AgentPop$Distress[AgentPop$AgentNo[i]] <- AgentPop$Distress[AgentPop$AgentNo[i]] + 7, ifelse(Event > -8 & Event <= -4 ,AgentPop$Distress[AgentPop$AgentNo[i]] <- AgentPop$Distress[AgentPop$AgentNo[i]] + 3, ifelse(Event > -4 & Event <= 0 ,AgentPop$Distress[AgentPop$AgentNo[i]] <- AgentPop$Distress[AgentPop$AgentNo[i]] + 1))))
#RECEIVED and PERCEIVED SUPPORT INTERACTION
#received support happens randomly to each agent during each event. There is a 33.33% chance for each scenario to happen: a) agent receives appropriate support (1), b) agent receives inappropriate support (2), c) agent does not receive any support (3)
#how to keep track whether agent got or did not get received support (see how to save this kind of data)? Then I can run both ifelse at the same time and keep track of which scenario a-c happened
#determining which received support scenario (a-c) happens to the agent
Received_support <- data.frame(Received_Support = 1:3)
Received_support_sample <- sample_n(Received_support, 1, replace = TRUE)
#keeping track of how many times each agent received support
#ifelse(Received_support_sample == 1, AgentPop$Appropriate_Received_support_count[AgentPop$AgentNo[i]] <- AgentPop$Appropriate_Received_support_count[AgentPop$AgentNo[i]] + 1, AgentPop$Appropriate_Received_support_count[AgentPop$AgentNo[i]] <- AgentPop$Appropriate_Received_support_count[AgentPop$AgentNo[i]])
ifelse(Received_support_sample == 2, AgentPop$Inappropriate_Received_support_count[AgentPop$AgentNo[i]] <- AgentPop$Inappropriate_Received_support_count[AgentPop$AgentNo[i]] + 1, AgentPop$Inappropriate_Received_support_count[AgentPop$AgentNo[i]] <- AgentPop$Inappropriate_Received_support_count[AgentPop$AgentNo[i]])
#received support increases/decreases perceived support level depending on its appropriateness
#ifelse(Received_support_sample == 1, AgentPop$Perceived_Support[AgentPop$AgentNo[i]] <- AgentPop$Perceived_Support[AgentPop$AgentNo[i]] + 0.7, AgentPop$Perceived_Support[AgentPop$AgentNo[i]] <- AgentPop$Perceived_Support[AgentPop$AgentNo[i]])
ifelse(Received_support_sample == 2, AgentPop$Perceived_Support[AgentPop$AgentNo[i]] <- AgentPop$Perceived_Support[AgentPop$AgentNo[i]] - 0.7, AgentPop$Perceived_Support[AgentPop$AgentNo[i]] <- AgentPop$Perceived_Support[AgentPop$AgentNo[i]])
#making sure that the value of perceived support stays in a range from 0 to 10
ifelse(AgentPop$Perceived_Support[AgentPop$AgentNo[i]] >= 10 ,AgentPop$Perceived_Support[AgentPop$AgentNo[i]] <- 10, ifelse(AgentPop$Perceived_Support[AgentPop$AgentNo[i]] <= 0, AgentPop$Perceived_Support[AgentPop$AgentNo[i]] <- 0, AgentPop$Perceived_Support[AgentPop$AgentNo[i]] <- AgentPop$Perceived_Support[AgentPop$AgentNo[i]]))
#finding out the changed perceived support level
Perceived_Support_update <- AgentPop$Perceived_Support[AgentPop$AgentNo[i]]
#PERCEIVED SUPPORT BUFFERS AGAINST DISTRESS
#calculating the individual buffer (%), which is determined by the perceived support level, and indicates by how much perceived support buffers against distress
individual_buffer <- Perceived_Support_update / 11
#individual buffer together with the set impact value of the event determines the amount of distress that is going to be subtracted from the distress level after the event has happened
AgentPop$Distress[AgentPop$AgentNo[i]] <- AgentPop$Distress[AgentPop$AgentNo[i]] - (Happened_Events$Impact[k] * individual_buffer)
#LIKELIHOOD OF MAJOR DEPRESSIVE DISORDER ONSET
#setting different distress thresholds for Major Depressive Disorder onset in low and high vulnerability groups
#I grab those in low vulnerability group with distress level bigger than 40
Low_Vulnerability <- (1:nrow(AgentPop))[AgentPop$Vulnerability_Group == "L" & AgentPop$Distress >= 40]
#I set their MDD onset to 1 (meaning it is very likely that Major Depressive Disorder might have started)
AgentPop$MDD_onset[Low_Vulnerability] <- 1
#I grab those in high vulnerability group with distress level bigger than 13.33 (there is a threefold increased risk of MDD onset in a high vulnerability group)
High_Vulnerability <- (1:nrow(AgentPop))[AgentPop$Vulnerability_Group == "H" & AgentPop$Distress >= 13.33]
#I set their MDD onset to 1 (meaning it is very likely that Major Depressive Disorder might have started)
AgentPop$MDD_onset[High_Vulnerability] <- 1
}
}
######## Simulation Ends ########
AgentPop$MDD_onset <- as.numeric(AgentPop$MDD_onset)
class(AgentPop$MDD_onset)
c <- AgentPop2 %>% group_by(Vulnerability_Group) %>% count(MDD_onset) %>% na.omit()
ggplot(AgentPop2, aes(x = Perceived_Support, y = Distress, colour = Vulnerability_Group)) +
geom_smooth(method = lm) + geom_hline(yintercept = 13.33, color='coral', linetype = "dashed")+ geom_hline(yintercept = 40, color='#00AFBB', linetype = "dashed") + geom_point() + ggtitle("Relationship between Perceived support and Distress level", "Received appropriate support") + theme_classic()
#visualize how many of MDD onset in total are in L and H groups
ggplot(AgentPop2, aes(x=Vulnerability_Group, y=MDD_onset)) + geom_bar(stat="identity", fill = "#00AFBB") + ggtitle("Count of Major Depressive Disorder onset in Low and High Vulnerability groups") + ggtitle("Count of Major Depressive Disorder onset in Low and High Vulnerability groups", "Received appropriate support") + theme_classic()
AgentPop2 %>% group_by(Vulnerability_Group) %>% summarise(mean = mean(Perceived_Support), sd = sd(Perceived_Support))
AgentPop2 %>% group_by(Vulnerability_Group) %>% summarise(mean = mean(Distress), sd = sd(Distress), max = max(Distress), min = min(Distress))
AgentPop2 %>% group_by(Vulnerability_Group) %>% summarise(sum(Appropriate_Received_support_count))
View(c)
#Count of MDD onset in Low and High Vulnerability groups
c <- AgentPop %>% group_by(Vulnerability_Group) %>% count(MDD_onset) %>% na.omit()
#visualize MDD onset count for different intervals of perceived support in both vulnerability groups
plot(Distress ~ Perceived_Support, data = AgentPop)
ggplot(AgentPop, aes(x = Perceived_Support, y = Distress, colour = Vulnerability_Group)) +
geom_smooth(method = lm) + geom_hline(yintercept = 13.33, color='coral', linetype = "dashed")+ geom_hline(yintercept = 40, color='#00AFBB', linetype = "dashed") + geom_point() + ggtitle("Relationship between Perceived support and Distress level", "Received inappropriate support") + theme_classic()
#visualize how many of MDD onset in total are in L and H groups
ggplot(AgentPop, aes(x=Vulnerability_Group, y=MDD_onset)) + geom_bar(stat="identity", fill = "#00AFBB") + ggtitle("Count of Major Depressive Disorder onset in Low and High Vulnerability groups") + ggtitle("Count of Major Depressive Disorder onset in Low and High Vulnerability groups", "Received inappropriate support") + theme_classic()
AgentPop %>% group_by(Vulnerability_Group) %>% summarise(mean = mean(Perceived_Support), sd = sd(Perceived_Support))
AgentPop %>% group_by(Vulnerability_Group) %>% summarise(mean = mean(Distress), sd = sd(Distress), max = max(Distress), min = min(Distress))
AgentPop %>% group_by(Vulnerability_Group) %>% summarise(sum(Inappropriate_Received_support_count))
View(c)
AgentPop <- read.csv(file = "~/Rutos SmuTkes/Cognitive Science/4th Semester/Social and cultural dynamics/ABM models/Agent_Population.csv")
#creating a for loop to go through all the events
for (k in 1:nrow(Happened_Events)){
#creating a for loop to go through all the agents in a populations
for (i in 1:nrow(AgentPop)){
#STRESSFUL EVENT INCREASE DISTRESS LEVEL
#determine which event with what valence happens
Event <- Happened_Events$Valence[k]
#events increase the distress level of agents
ifelse(Event >= -13 & Event <= -12 ,AgentPop$Distress[AgentPop$AgentNo[i]] <- AgentPop$Distress[AgentPop$AgentNo[i]] + 14, ifelse(Event > -12 & Event <= -8 ,AgentPop$Distress[AgentPop$AgentNo[i]] <- AgentPop$Distress[AgentPop$AgentNo[i]] + 7, ifelse(Event > -8 & Event <= -4 ,AgentPop$Distress[AgentPop$AgentNo[i]] <- AgentPop$Distress[AgentPop$AgentNo[i]] + 3, ifelse(Event > -4 & Event <= 0 ,AgentPop$Distress[AgentPop$AgentNo[i]] <- AgentPop$Distress[AgentPop$AgentNo[i]] + 1))))
#RECEIVED and PERCEIVED SUPPORT INTERACTION
#received support happens randomly to each agent during each event. There is a 33.33% chance for each scenario to happen: a) agent receives appropriate support (1), b) agent receives inappropriate support (2), c) agent does not receive any support (3)
#how to keep track whether agent got or did not get received support (see how to save this kind of data)? Then I can run both ifelse at the same time and keep track of which scenario a-c happened
#determining which received support scenario (a-c) happens to the agent
Received_support <- data.frame(Received_Support = 1:3)
Received_support_sample <- sample_n(Received_support, 1, replace = TRUE)
#keeping track of how many times each agent received support
#ifelse(Received_support_sample == 1, AgentPop$Appropriate_Received_support_count[AgentPop$AgentNo[i]] <- AgentPop$Appropriate_Received_support_count[AgentPop$AgentNo[i]] + 1, AgentPop$Appropriate_Received_support_count[AgentPop$AgentNo[i]] <- AgentPop$Appropriate_Received_support_count[AgentPop$AgentNo[i]])
#ifelse(Received_support_sample == 2, AgentPop$Inappropriate_Received_support_count[AgentPop$AgentNo[i]] <- AgentPop$Inappropriate_Received_support_count[AgentPop$AgentNo[i]] + 1, AgentPop$Inappropriate_Received_support_count[AgentPop$AgentNo[i]] <- AgentPop$Inappropriate_Received_support_count[AgentPop$AgentNo[i]])
#received support increases/decreases perceived support level depending on its appropriateness
#ifelse(Received_support_sample == 1, AgentPop$Perceived_Support[AgentPop$AgentNo[i]] <- AgentPop$Perceived_Support[AgentPop$AgentNo[i]] + 0.7, AgentPop$Perceived_Support[AgentPop$AgentNo[i]] <- AgentPop$Perceived_Support[AgentPop$AgentNo[i]])
#ifelse(Received_support_sample == 2, AgentPop$Perceived_Support[AgentPop$AgentNo[i]] <- AgentPop$Perceived_Support[AgentPop$AgentNo[i]] - 0.7, AgentPop$Perceived_Support[AgentPop$AgentNo[i]] <- AgentPop$Perceived_Support[AgentPop$AgentNo[i]])
#making sure that the value of perceived support stays in a range from 0 to 10
ifelse(AgentPop$Perceived_Support[AgentPop$AgentNo[i]] >= 10 ,AgentPop$Perceived_Support[AgentPop$AgentNo[i]] <- 10, ifelse(AgentPop$Perceived_Support[AgentPop$AgentNo[i]] <= 0, AgentPop$Perceived_Support[AgentPop$AgentNo[i]] <- 0, AgentPop$Perceived_Support[AgentPop$AgentNo[i]] <- AgentPop$Perceived_Support[AgentPop$AgentNo[i]]))
#finding out the changed perceived support level
Perceived_Support_update <- AgentPop$Perceived_Support[AgentPop$AgentNo[i]]
#PERCEIVED SUPPORT BUFFERS AGAINST DISTRESS
#calculating the individual buffer (%), which is determined by the perceived support level, and indicates by how much perceived support buffers against distress
individual_buffer <- Perceived_Support_update / 11
#individual buffer together with the set impact value of the event determines the amount of distress that is going to be subtracted from the distress level after the event has happened
AgentPop$Distress[AgentPop$AgentNo[i]] <- AgentPop$Distress[AgentPop$AgentNo[i]] - (Happened_Events$Impact[k] * individual_buffer)
#LIKELIHOOD OF MAJOR DEPRESSIVE DISORDER ONSET
#setting different distress thresholds for Major Depressive Disorder onset in low and high vulnerability groups
#I grab those in low vulnerability group with distress level bigger than 40
Low_Vulnerability <- (1:nrow(AgentPop))[AgentPop$Vulnerability_Group == "L" & AgentPop$Distress >= 40]
#I set their MDD onset to 1 (meaning it is very likely that Major Depressive Disorder might have started)
AgentPop$MDD_onset[Low_Vulnerability] <- 1
#I grab those in high vulnerability group with distress level bigger than 13.33 (there is a threefold increased risk of MDD onset in a high vulnerability group)
High_Vulnerability <- (1:nrow(AgentPop))[AgentPop$Vulnerability_Group == "H" & AgentPop$Distress >= 13.33]
#I set their MDD onset to 1 (meaning it is very likely that Major Depressive Disorder might have started)
AgentPop$MDD_onset[High_Vulnerability] <- 1
}
}
######## Simulation Ends ########
AgentPop$MDD_onset <- as.numeric(AgentPop$MDD_onset)
class(AgentPop$MDD_onset)
#Count of MDD onset in Low and High Vulnerability groups
c <- AgentPop %>% group_by(Vulnerability_Group) %>% count(MDD_onset) %>% na.omit()
#visualize MDD onset count for different intervals of perceived support in both vulnerability groups
plot(Distress ~ Perceived_Support, data = AgentPop)
ggplot(AgentPop, aes(x = Perceived_Support, y = Distress, colour = Vulnerability_Group)) +
geom_smooth(method = lm) + geom_hline(yintercept = 13.33, color='coral', linetype = "dashed")+ geom_hline(yintercept = 40, color='#00AFBB', linetype = "dashed") + geom_point() + ggtitle("Relationship between Perceived support and Distress level", "No Received support") + theme_classic()
#visualize how many of MDD onset in total are in L and H groups
ggplot(AgentPop, aes(x=Vulnerability_Group, y=MDD_onset)) + geom_bar(stat="identity", fill = "#00AFBB") + ggtitle("Count of Major Depressive Disorder onset in Low and High Vulnerability groups") + ggtitle("Count of Major Depressive Disorder onset in Low and High Vulnerability groups", "No Received support") + theme_classic()
AgentPop %>% group_by(Vulnerability_Group) %>% summarise(mean = mean(Perceived_Support), sd = sd(Perceived_Support))
AgentPop %>% group_by(Vulnerability_Group) %>% summarise(mean = mean(Distress), sd = sd(Distress), max = max(Distress), min = min(Distress))
AgentPop %>% group_by(Vulnerability_Group) %>% summarise(sum(Appropriate_Received_support_count))
View(c)
write.csv(AgentPop, "4.csv")
View(AgentPop)
AgentPop <- read.csv(file = "~/Rutos SmuTkes/Cognitive Science/4th Semester/Social and cultural dynamics/ABM models/Agent_Population.csv")
View(AgentPop)
AgentPop %>% group_by(Vulnerability_Group) %>% summarise(mean = mean(Perceived_Support), sd = sd(Perceived_Support))
AgentPop <- read.csv(file = "~/Rutos SmuTkes/Cognitive Science/4th Semester/Social and cultural dynamics/ABM models/Agent_Population.csv")
######## Simulation Starts ########
#creating a for loop to go through all the events
for (k in 1:nrow(Happened_Events)){
#creating a for loop to go through all the agents in a populations
for (i in 1:nrow(AgentPop)){
#STRESSFUL EVENT INCREASE DISTRESS LEVEL
#determine which event with what valence happens
Event <- Happened_Events$Valence[k]
#events increase the distress level of agents
ifelse(Event >= -13 & Event <= -12 ,AgentPop$Distress[AgentPop$AgentNo[i]] <- AgentPop$Distress[AgentPop$AgentNo[i]] + 14, ifelse(Event > -12 & Event <= -8 ,AgentPop$Distress[AgentPop$AgentNo[i]] <- AgentPop$Distress[AgentPop$AgentNo[i]] + 7, ifelse(Event > -8 & Event <= -4 ,AgentPop$Distress[AgentPop$AgentNo[i]] <- AgentPop$Distress[AgentPop$AgentNo[i]] + 3, ifelse(Event > -4 & Event <= 0 ,AgentPop$Distress[AgentPop$AgentNo[i]] <- AgentPop$Distress[AgentPop$AgentNo[i]] + 1))))
#RECEIVED and PERCEIVED SUPPORT INTERACTION
#received support happens randomly to each agent during each event. There is a 33.33% chance for each scenario to happen: a) agent receives appropriate support (1), b) agent receives inappropriate support (2), c) agent does not receive any support (3)
#how to keep track whether agent got or did not get received support (see how to save this kind of data)? Then I can run both ifelse at the same time and keep track of which scenario a-c happened
#determining which received support scenario (a-c) happens to the agent
Received_support <- data.frame(Received_Support = 1:3)
Received_support_sample <- sample_n(Received_support, 1, replace = TRUE)
#keeping track of how many times each agent received support
ifelse(Received_support_sample == 1, AgentPop$Appropriate_Received_support_count[AgentPop$AgentNo[i]] <- AgentPop$Appropriate_Received_support_count[AgentPop$AgentNo[i]] + 1, AgentPop$Appropriate_Received_support_count[AgentPop$AgentNo[i]] <- AgentPop$Appropriate_Received_support_count[AgentPop$AgentNo[i]])
ifelse(Received_support_sample == 2, AgentPop$Inappropriate_Received_support_count[AgentPop$AgentNo[i]] <- AgentPop$Inappropriate_Received_support_count[AgentPop$AgentNo[i]] + 1, AgentPop$Inappropriate_Received_support_count[AgentPop$AgentNo[i]] <- AgentPop$Inappropriate_Received_support_count[AgentPop$AgentNo[i]])
#received support increases/decreases perceived support level depending on its appropriateness
ifelse(Received_support_sample == 1, AgentPop$Perceived_Support[AgentPop$AgentNo[i]] <- AgentPop$Perceived_Support[AgentPop$AgentNo[i]] + 0.7, AgentPop$Perceived_Support[AgentPop$AgentNo[i]] <- AgentPop$Perceived_Support[AgentPop$AgentNo[i]])
ifelse(Received_support_sample == 2, AgentPop$Perceived_Support[AgentPop$AgentNo[i]] <- AgentPop$Perceived_Support[AgentPop$AgentNo[i]] - 0.7, AgentPop$Perceived_Support[AgentPop$AgentNo[i]] <- AgentPop$Perceived_Support[AgentPop$AgentNo[i]])
#making sure that the value of perceived support stays in a range from 0 to 10
ifelse(AgentPop$Perceived_Support[AgentPop$AgentNo[i]] >= 10 ,AgentPop$Perceived_Support[AgentPop$AgentNo[i]] <- 10, ifelse(AgentPop$Perceived_Support[AgentPop$AgentNo[i]] <= 0, AgentPop$Perceived_Support[AgentPop$AgentNo[i]] <- 0, AgentPop$Perceived_Support[AgentPop$AgentNo[i]] <- AgentPop$Perceived_Support[AgentPop$AgentNo[i]]))
#finding out the changed perceived support level
Perceived_Support_update <- AgentPop$Perceived_Support[AgentPop$AgentNo[i]]
#PERCEIVED SUPPORT BUFFERS AGAINST DISTRESS
#calculating the individual buffer (%), which is determined by the perceived support level, and indicates by how much perceived support buffers against distress
individual_buffer <- Perceived_Support_update / 11
#individual buffer together with the set impact value of the event determines the amount of distress that is going to be subtracted from the distress level after the event has happened
AgentPop$Distress[AgentPop$AgentNo[i]] <- AgentPop$Distress[AgentPop$AgentNo[i]] - (Happened_Events$Impact[k] * individual_buffer)
#LIKELIHOOD OF MAJOR DEPRESSIVE DISORDER ONSET
#setting different distress thresholds for Major Depressive Disorder onset in low and high vulnerability groups
#I grab those in low vulnerability group with distress level bigger than 40
Low_Vulnerability <- (1:nrow(AgentPop))[AgentPop$Vulnerability_Group == "L" & AgentPop$Distress >= 40]
#I set their MDD onset to 1 (meaning it is very likely that Major Depressive Disorder might have started)
AgentPop$MDD_onset[Low_Vulnerability] <- 1
#I grab those in high vulnerability group with distress level bigger than 13.33 (there is a threefold increased risk of MDD onset in a high vulnerability group)
High_Vulnerability <- (1:nrow(AgentPop))[AgentPop$Vulnerability_Group == "H" & AgentPop$Distress >= 13.33]
#I set their MDD onset to 1 (meaning it is very likely that Major Depressive Disorder might have started)
AgentPop$MDD_onset[High_Vulnerability] <- 1
}
}
######## Simulation Ends ########
View(AgentPop)
write.csv(AgentPop, "1.csv")
AgentPop$MDD_onset <- as.numeric(AgentPop$MDD_onset)
class(AgentPop$MDD_onset)
#Count of MDD onset in Low and High Vulnerability groups
c <- AgentPop %>% group_by(Vulnerability_Group) %>% count(MDD_onset) %>% na.omit()
#visualize MDD onset count for different intervals of perceived support in both vulnerability groups
plot(Distress ~ Perceived_Support, data = AgentPop)
ggplot(AgentPop, aes(x = Perceived_Support, y = Distress, colour = Vulnerability_Group)) +
geom_smooth(method = lm) + geom_hline(yintercept = 13.33, color='coral', linetype = "dashed")+ geom_hline(yintercept = 40, color='#00AFBB', linetype = "dashed") + geom_point() + ggtitle("Relationship between Perceived support and Distress level", "Received appropriate and inappropriate support") + theme_classic()
#visualize how many of MDD onset in total are in L and H groups
ggplot(AgentPop, aes(x=Vulnerability_Group, y=MDD_onset)) + geom_bar(stat="identity", fill = "#00AFBB") + ggtitle("Count of Major Depressive Disorder onset in Low and High Vulnerability groups") + ggtitle("Count of Major Depressive Disorder onset in Low and High Vulnerability groups", "Received appropriate and inappropriate support") + theme_classic()
AgentPop %>% group_by(Vulnerability_Group) %>% summarise(mean = mean(Perceived_Support), sd = sd(Perceived_Support))
AgentPop %>% group_by(Vulnerability_Group) %>% summarise(mean = mean(Distress), sd = sd(Distress), max = max(Distress), min = min(Distress))
AgentPop %>% group_by(Vulnerability_Group) %>% summarise(sum(Appropriate_Received_support_count))
AgentPop %>% group_by(Vulnerability_Group) %>% summarise(sum(Inappropriate_Received_support_count))
View(c)
#with the function created above I generate a dataset with desired number of events with desired valence
Happened_Events <- Events_f(0, 2, 4, 8)
Happened_Events$Impact <- as.numeric(Happened_Events$Impact)
class(Happened_Events$Impact)
AgentPop <- read.csv(file = "~/Rutos SmuTkes/Cognitive Science/4th Semester/Social and cultural dynamics/ABM models/Agent_Population.csv")
######## Simulation Starts ########
#creating a for loop to go through all the events
for (k in 1:nrow(Happened_Events)){
#creating a for loop to go through all the agents in a populations
for (i in 1:nrow(AgentPop)){
#STRESSFUL EVENT INCREASE DISTRESS LEVEL
#determine which event with what valence happens
Event <- Happened_Events$Valence[k]
#events increase the distress level of agents
ifelse(Event >= -13 & Event <= -12 ,AgentPop$Distress[AgentPop$AgentNo[i]] <- AgentPop$Distress[AgentPop$AgentNo[i]] + 14, ifelse(Event > -12 & Event <= -8 ,AgentPop$Distress[AgentPop$AgentNo[i]] <- AgentPop$Distress[AgentPop$AgentNo[i]] + 7, ifelse(Event > -8 & Event <= -4 ,AgentPop$Distress[AgentPop$AgentNo[i]] <- AgentPop$Distress[AgentPop$AgentNo[i]] + 3, ifelse(Event > -4 & Event <= 0 ,AgentPop$Distress[AgentPop$AgentNo[i]] <- AgentPop$Distress[AgentPop$AgentNo[i]] + 1))))
#RECEIVED and PERCEIVED SUPPORT INTERACTION
#received support happens randomly to each agent during each event. There is a 33.33% chance for each scenario to happen: a) agent receives appropriate support (1), b) agent receives inappropriate support (2), c) agent does not receive any support (3)
#how to keep track whether agent got or did not get received support (see how to save this kind of data)? Then I can run both ifelse at the same time and keep track of which scenario a-c happened
#determining which received support scenario (a-c) happens to the agent
Received_support <- data.frame(Received_Support = 1:3)
Received_support_sample <- sample_n(Received_support, 1, replace = TRUE)
#keeping track of how many times each agent received support
ifelse(Received_support_sample == 1, AgentPop$Appropriate_Received_support_count[AgentPop$AgentNo[i]] <- AgentPop$Appropriate_Received_support_count[AgentPop$AgentNo[i]] + 1, AgentPop$Appropriate_Received_support_count[AgentPop$AgentNo[i]] <- AgentPop$Appropriate_Received_support_count[AgentPop$AgentNo[i]])
ifelse(Received_support_sample == 2, AgentPop$Inappropriate_Received_support_count[AgentPop$AgentNo[i]] <- AgentPop$Inappropriate_Received_support_count[AgentPop$AgentNo[i]] + 1, AgentPop$Inappropriate_Received_support_count[AgentPop$AgentNo[i]] <- AgentPop$Inappropriate_Received_support_count[AgentPop$AgentNo[i]])
#received support increases/decreases perceived support level depending on its appropriateness
ifelse(Received_support_sample == 1, AgentPop$Perceived_Support[AgentPop$AgentNo[i]] <- AgentPop$Perceived_Support[AgentPop$AgentNo[i]] + 0.7, AgentPop$Perceived_Support[AgentPop$AgentNo[i]] <- AgentPop$Perceived_Support[AgentPop$AgentNo[i]])
ifelse(Received_support_sample == 2, AgentPop$Perceived_Support[AgentPop$AgentNo[i]] <- AgentPop$Perceived_Support[AgentPop$AgentNo[i]] - 0.7, AgentPop$Perceived_Support[AgentPop$AgentNo[i]] <- AgentPop$Perceived_Support[AgentPop$AgentNo[i]])
#making sure that the value of perceived support stays in a range from 0 to 10
ifelse(AgentPop$Perceived_Support[AgentPop$AgentNo[i]] >= 10 ,AgentPop$Perceived_Support[AgentPop$AgentNo[i]] <- 10, ifelse(AgentPop$Perceived_Support[AgentPop$AgentNo[i]] <= 0, AgentPop$Perceived_Support[AgentPop$AgentNo[i]] <- 0, AgentPop$Perceived_Support[AgentPop$AgentNo[i]] <- AgentPop$Perceived_Support[AgentPop$AgentNo[i]]))
#finding out the changed perceived support level
Perceived_Support_update <- AgentPop$Perceived_Support[AgentPop$AgentNo[i]]
#PERCEIVED SUPPORT BUFFERS AGAINST DISTRESS
#calculating the individual buffer (%), which is determined by the perceived support level, and indicates by how much perceived support buffers against distress
individual_buffer <- Perceived_Support_update / 11
#individual buffer together with the set impact value of the event determines the amount of distress that is going to be subtracted from the distress level after the event has happened
AgentPop$Distress[AgentPop$AgentNo[i]] <- AgentPop$Distress[AgentPop$AgentNo[i]] - (Happened_Events$Impact[k] * individual_buffer)
#LIKELIHOOD OF MAJOR DEPRESSIVE DISORDER ONSET
#setting different distress thresholds for Major Depressive Disorder onset in low and high vulnerability groups
#I grab those in low vulnerability group with distress level bigger than 40
Low_Vulnerability <- (1:nrow(AgentPop))[AgentPop$Vulnerability_Group == "L" & AgentPop$Distress >= 40]
#I set their MDD onset to 1 (meaning it is very likely that Major Depressive Disorder might have started)
AgentPop$MDD_onset[Low_Vulnerability] <- 1
#I grab those in high vulnerability group with distress level bigger than 13.33 (there is a threefold increased risk of MDD onset in a high vulnerability group)
High_Vulnerability <- (1:nrow(AgentPop))[AgentPop$Vulnerability_Group == "H" & AgentPop$Distress >= 13.33]
#I set their MDD onset to 1 (meaning it is very likely that Major Depressive Disorder might have started)
AgentPop$MDD_onset[High_Vulnerability] <- 1
}
}
######## Simulation Ends ########
AgentPop$MDD_onset <- as.numeric(AgentPop$MDD_onset)
class(AgentPop$MDD_onset)
#Count of MDD onset in Low and High Vulnerability groups
c <- AgentPop %>% group_by(Vulnerability_Group) %>% count(MDD_onset) %>% na.omit()
#visualize MDD onset count for different intervals of perceived support in both vulnerability groups
plot(Distress ~ Perceived_Support, data = AgentPop)
ggplot(AgentPop, aes(x = Perceived_Support, y = Distress, colour = Vulnerability_Group)) +
geom_smooth(method = lm) + geom_hline(yintercept = 13.33, color='coral', linetype = "dashed")+ geom_hline(yintercept = 40, color='#00AFBB', linetype = "dashed") + geom_point() + ggtitle("Relationship between Perceived support and Distress level", "Received appropriate and inappropriate support") + theme_classic()
#visualize how many of MDD onset in total are in L and H groups
ggplot(AgentPop, aes(x=Vulnerability_Group, y=MDD_onset)) + geom_bar(stat="identity", fill = "#00AFBB") + ggtitle("Count of Major Depressive Disorder onset in Low and High Vulnerability groups") + ggtitle("Count of Major Depressive Disorder onset in Low and High Vulnerability groups", "Received appropriate and inappropriate support") + theme_classic()
AgentPop %>% group_by(Vulnerability_Group) %>% summarise(mean = mean(Perceived_Support), sd = sd(Perceived_Support))
AgentPop %>% group_by(Vulnerability_Group) %>% summarise(mean = mean(Distress), sd = sd(Distress), max = max(Distress), min = min(Distress))
AgentPop %>% group_by(Vulnerability_Group) %>% summarise(sum(Appropriate_Received_support_count))
AgentPop %>% group_by(Vulnerability_Group) %>% summarise(sum(Inappropriate_Received_support_count))
View(c)
#with the function created above I generate a dataset with desired number of events with desired valence
Happened_Events <- Events_f(0, 2, 8, 20)
Happened_Events$Impact <- as.numeric(Happened_Events$Impact)
class(Happened_Events$Impact)
AgentPop <- read.csv(file = "~/Rutos SmuTkes/Cognitive Science/4th Semester/Social and cultural dynamics/ABM models/Agent_Population.csv")
######## Simulation Starts ########
#creating a for loop to go through all the events
for (k in 1:nrow(Happened_Events)){
#creating a for loop to go through all the agents in a populations
for (i in 1:nrow(AgentPop)){
#STRESSFUL EVENT INCREASE DISTRESS LEVEL
#determine which event with what valence happens
Event <- Happened_Events$Valence[k]
#events increase the distress level of agents
ifelse(Event >= -13 & Event <= -12 ,AgentPop$Distress[AgentPop$AgentNo[i]] <- AgentPop$Distress[AgentPop$AgentNo[i]] + 14, ifelse(Event > -12 & Event <= -8 ,AgentPop$Distress[AgentPop$AgentNo[i]] <- AgentPop$Distress[AgentPop$AgentNo[i]] + 7, ifelse(Event > -8 & Event <= -4 ,AgentPop$Distress[AgentPop$AgentNo[i]] <- AgentPop$Distress[AgentPop$AgentNo[i]] + 3, ifelse(Event > -4 & Event <= 0 ,AgentPop$Distress[AgentPop$AgentNo[i]] <- AgentPop$Distress[AgentPop$AgentNo[i]] + 1))))
#RECEIVED and PERCEIVED SUPPORT INTERACTION
#received support happens randomly to each agent during each event. There is a 33.33% chance for each scenario to happen: a) agent receives appropriate support (1), b) agent receives inappropriate support (2), c) agent does not receive any support (3)
#how to keep track whether agent got or did not get received support (see how to save this kind of data)? Then I can run both ifelse at the same time and keep track of which scenario a-c happened
#determining which received support scenario (a-c) happens to the agent
Received_support <- data.frame(Received_Support = 1:3)
Received_support_sample <- sample_n(Received_support, 1, replace = TRUE)
#keeping track of how many times each agent received support
ifelse(Received_support_sample == 1, AgentPop$Appropriate_Received_support_count[AgentPop$AgentNo[i]] <- AgentPop$Appropriate_Received_support_count[AgentPop$AgentNo[i]] + 1, AgentPop$Appropriate_Received_support_count[AgentPop$AgentNo[i]] <- AgentPop$Appropriate_Received_support_count[AgentPop$AgentNo[i]])
ifelse(Received_support_sample == 2, AgentPop$Inappropriate_Received_support_count[AgentPop$AgentNo[i]] <- AgentPop$Inappropriate_Received_support_count[AgentPop$AgentNo[i]] + 1, AgentPop$Inappropriate_Received_support_count[AgentPop$AgentNo[i]] <- AgentPop$Inappropriate_Received_support_count[AgentPop$AgentNo[i]])
#received support increases/decreases perceived support level depending on its appropriateness
ifelse(Received_support_sample == 1, AgentPop$Perceived_Support[AgentPop$AgentNo[i]] <- AgentPop$Perceived_Support[AgentPop$AgentNo[i]] + 0.7, AgentPop$Perceived_Support[AgentPop$AgentNo[i]] <- AgentPop$Perceived_Support[AgentPop$AgentNo[i]])
ifelse(Received_support_sample == 2, AgentPop$Perceived_Support[AgentPop$AgentNo[i]] <- AgentPop$Perceived_Support[AgentPop$AgentNo[i]] - 0.7, AgentPop$Perceived_Support[AgentPop$AgentNo[i]] <- AgentPop$Perceived_Support[AgentPop$AgentNo[i]])
#making sure that the value of perceived support stays in a range from 0 to 10
ifelse(AgentPop$Perceived_Support[AgentPop$AgentNo[i]] >= 10 ,AgentPop$Perceived_Support[AgentPop$AgentNo[i]] <- 10, ifelse(AgentPop$Perceived_Support[AgentPop$AgentNo[i]] <= 0, AgentPop$Perceived_Support[AgentPop$AgentNo[i]] <- 0, AgentPop$Perceived_Support[AgentPop$AgentNo[i]] <- AgentPop$Perceived_Support[AgentPop$AgentNo[i]]))
#finding out the changed perceived support level
Perceived_Support_update <- AgentPop$Perceived_Support[AgentPop$AgentNo[i]]
#PERCEIVED SUPPORT BUFFERS AGAINST DISTRESS
#calculating the individual buffer (%), which is determined by the perceived support level, and indicates by how much perceived support buffers against distress
individual_buffer <- Perceived_Support_update / 11
#individual buffer together with the set impact value of the event determines the amount of distress that is going to be subtracted from the distress level after the event has happened
AgentPop$Distress[AgentPop$AgentNo[i]] <- AgentPop$Distress[AgentPop$AgentNo[i]] - (Happened_Events$Impact[k] * individual_buffer)
#LIKELIHOOD OF MAJOR DEPRESSIVE DISORDER ONSET
#setting different distress thresholds for Major Depressive Disorder onset in low and high vulnerability groups
#I grab those in low vulnerability group with distress level bigger than 40
Low_Vulnerability <- (1:nrow(AgentPop))[AgentPop$Vulnerability_Group == "L" & AgentPop$Distress >= 40]
#I set their MDD onset to 1 (meaning it is very likely that Major Depressive Disorder might have started)
AgentPop$MDD_onset[Low_Vulnerability] <- 1
#I grab those in high vulnerability group with distress level bigger than 13.33 (there is a threefold increased risk of MDD onset in a high vulnerability group)
High_Vulnerability <- (1:nrow(AgentPop))[AgentPop$Vulnerability_Group == "H" & AgentPop$Distress >= 13.33]
#I set their MDD onset to 1 (meaning it is very likely that Major Depressive Disorder might have started)
AgentPop$MDD_onset[High_Vulnerability] <- 1
}
}
######## Simulation Ends ########
AgentPop$MDD_onset <- as.numeric(AgentPop$MDD_onset)
class(AgentPop$MDD_onset)
#Count of MDD onset in Low and High Vulnerability groups
c <- AgentPop %>% group_by(Vulnerability_Group) %>% count(MDD_onset) %>% na.omit()
#visualize MDD onset count for different intervals of perceived support in both vulnerability groups
plot(Distress ~ Perceived_Support, data = AgentPop)
ggplot(AgentPop, aes(x = Perceived_Support, y = Distress, colour = Vulnerability_Group)) +
geom_smooth(method = lm) + geom_hline(yintercept = 13.33, color='coral', linetype = "dashed")+ geom_hline(yintercept = 40, color='#00AFBB', linetype = "dashed") + geom_point() + ggtitle("Relationship between Perceived support and Distress level", "Received appropriate and inappropriate support") + theme_classic()
#visualize how many of MDD onset in total are in L and H groups
ggplot(AgentPop, aes(x=Vulnerability_Group, y=MDD_onset)) + geom_bar(stat="identity", fill = "#00AFBB") + ggtitle("Count of Major Depressive Disorder onset in Low and High Vulnerability groups") + ggtitle("Count of Major Depressive Disorder onset in Low and High Vulnerability groups", "Received appropriate and inappropriate support") + theme_classic()
AgentPop %>% group_by(Vulnerability_Group) %>% summarise(mean = mean(Perceived_Support), sd = sd(Perceived_Support))
AgentPop %>% group_by(Vulnerability_Group) %>% summarise(mean = mean(Distress), sd = sd(Distress), max = max(Distress), min = min(Distress))
AgentPop %>% group_by(Vulnerability_Group) %>% summarise(sum(Appropriate_Received_support_count))
AgentPop %>% group_by(Vulnerability_Group) %>% summarise(sum(Inappropriate_Received_support_count))
View(c)
AgentPop <- read.csv(file = "~/Rutos SmuTkes/Cognitive Science/4th Semester/Social and cultural dynamics/ABM models/Agent_Population.csv")
######## Simulation Starts ########
#creating a for loop to go through all the events
for (k in 1:nrow(Happened_Events)){
#creating a for loop to go through all the agents in a populations
for (i in 1:nrow(AgentPop)){
#STRESSFUL EVENT INCREASE DISTRESS LEVEL
#determine which event with what valence happens
Event <- Happened_Events$Valence[k]
#events increase the distress level of agents
ifelse(Event >= -13 & Event <= -12 ,AgentPop$Distress[AgentPop$AgentNo[i]] <- AgentPop$Distress[AgentPop$AgentNo[i]] + 14, ifelse(Event > -12 & Event <= -8 ,AgentPop$Distress[AgentPop$AgentNo[i]] <- AgentPop$Distress[AgentPop$AgentNo[i]] + 7, ifelse(Event > -8 & Event <= -4 ,AgentPop$Distress[AgentPop$AgentNo[i]] <- AgentPop$Distress[AgentPop$AgentNo[i]] + 3, ifelse(Event > -4 & Event <= 0 ,AgentPop$Distress[AgentPop$AgentNo[i]] <- AgentPop$Distress[AgentPop$AgentNo[i]] + 1))))
#RECEIVED and PERCEIVED SUPPORT INTERACTION
#received support happens randomly to each agent during each event. There is a 33.33% chance for each scenario to happen: a) agent receives appropriate support (1), b) agent receives inappropriate support (2), c) agent does not receive any support (3)
#how to keep track whether agent got or did not get received support (see how to save this kind of data)? Then I can run both ifelse at the same time and keep track of which scenario a-c happened
#determining which received support scenario (a-c) happens to the agent
Received_support <- data.frame(Received_Support = 1:3)
Received_support_sample <- sample_n(Received_support, 1, replace = TRUE)
#keeping track of how many times each agent received support
ifelse(Received_support_sample == 1, AgentPop$Appropriate_Received_support_count[AgentPop$AgentNo[i]] <- AgentPop$Appropriate_Received_support_count[AgentPop$AgentNo[i]] + 1, AgentPop$Appropriate_Received_support_count[AgentPop$AgentNo[i]] <- AgentPop$Appropriate_Received_support_count[AgentPop$AgentNo[i]])
#ifelse(Received_support_sample == 2, AgentPop$Inappropriate_Received_support_count[AgentPop$AgentNo[i]] <- AgentPop$Inappropriate_Received_support_count[AgentPop$AgentNo[i]] + 1, AgentPop$Inappropriate_Received_support_count[AgentPop$AgentNo[i]] <- AgentPop$Inappropriate_Received_support_count[AgentPop$AgentNo[i]])
#received support increases/decreases perceived support level depending on its appropriateness
ifelse(Received_support_sample == 1, AgentPop$Perceived_Support[AgentPop$AgentNo[i]] <- AgentPop$Perceived_Support[AgentPop$AgentNo[i]] + 0.7, AgentPop$Perceived_Support[AgentPop$AgentNo[i]] <- AgentPop$Perceived_Support[AgentPop$AgentNo[i]])
#ifelse(Received_support_sample == 2, AgentPop$Perceived_Support[AgentPop$AgentNo[i]] <- AgentPop$Perceived_Support[AgentPop$AgentNo[i]] - 0.7, AgentPop$Perceived_Support[AgentPop$AgentNo[i]] <- AgentPop$Perceived_Support[AgentPop$AgentNo[i]])
#making sure that the value of perceived support stays in a range from 0 to 10
ifelse(AgentPop$Perceived_Support[AgentPop$AgentNo[i]] >= 10 ,AgentPop$Perceived_Support[AgentPop$AgentNo[i]] <- 10, ifelse(AgentPop$Perceived_Support[AgentPop$AgentNo[i]] <= 0, AgentPop$Perceived_Support[AgentPop$AgentNo[i]] <- 0, AgentPop$Perceived_Support[AgentPop$AgentNo[i]] <- AgentPop$Perceived_Support[AgentPop$AgentNo[i]]))
#finding out the changed perceived support level
Perceived_Support_update <- AgentPop$Perceived_Support[AgentPop$AgentNo[i]]
#PERCEIVED SUPPORT BUFFERS AGAINST DISTRESS
#calculating the individual buffer (%), which is determined by the perceived support level, and indicates by how much perceived support buffers against distress
individual_buffer <- Perceived_Support_update / 11
#individual buffer together with the set impact value of the event determines the amount of distress that is going to be subtracted from the distress level after the event has happened
AgentPop$Distress[AgentPop$AgentNo[i]] <- AgentPop$Distress[AgentPop$AgentNo[i]] - (Happened_Events$Impact[k] * individual_buffer)
#LIKELIHOOD OF MAJOR DEPRESSIVE DISORDER ONSET
#setting different distress thresholds for Major Depressive Disorder onset in low and high vulnerability groups
#I grab those in low vulnerability group with distress level bigger than 40
Low_Vulnerability <- (1:nrow(AgentPop))[AgentPop$Vulnerability_Group == "L" & AgentPop$Distress >= 40]
#I set their MDD onset to 1 (meaning it is very likely that Major Depressive Disorder might have started)
AgentPop$MDD_onset[Low_Vulnerability] <- 1
#I grab those in high vulnerability group with distress level bigger than 13.33 (there is a threefold increased risk of MDD onset in a high vulnerability group)
High_Vulnerability <- (1:nrow(AgentPop))[AgentPop$Vulnerability_Group == "H" & AgentPop$Distress >= 13.33]
#I set their MDD onset to 1 (meaning it is very likely that Major Depressive Disorder might have started)
AgentPop$MDD_onset[High_Vulnerability] <- 1
}
}
######## Simulation Ends ########
AgentPop$MDD_onset <- as.numeric(AgentPop$MDD_onset)
class(AgentPop$MDD_onset)
c <- AgentPop %>% group_by(Vulnerability_Group) %>% count(MDD_onset) %>% na.omit()
ggplot(AgentPop, aes(x = Perceived_Support, y = Distress, colour = Vulnerability_Group)) +
geom_smooth(method = lm) + geom_hline(yintercept = 13.33, color='coral', linetype = "dashed")+ geom_hline(yintercept = 40, color='#00AFBB', linetype = "dashed") + geom_point() + ggtitle("Relationship between Perceived support and Distress level", "Received appropriate support") + theme_classic()
#visualize how many of MDD onset in total are in L and H groups
ggplot(AgentPop, aes(x=Vulnerability_Group, y=MDD_onset)) + geom_bar(stat="identity", fill = "#00AFBB") + ggtitle("Count of Major Depressive Disorder onset in Low and High Vulnerability groups") + ggtitle("Count of Major Depressive Disorder onset in Low and High Vulnerability groups", "Received appropriate support") + theme_classic()
AgentPop %>% group_by(Vulnerability_Group) %>% summarise(mean = mean(Perceived_Support), sd = sd(Perceived_Support))
AgentPop %>% group_by(Vulnerability_Group) %>% summarise(mean = mean(Distress), sd = sd(Distress), max = max(Distress), min = min(Distress))
AgentPop %>% group_by(Vulnerability_Group) %>% summarise(sum(Appropriate_Received_support_count))
View(c)
View(c)
AgentPop <- read.csv(file = "~/Rutos SmuTkes/Cognitive Science/4th Semester/Social and cultural dynamics/ABM models/Agent_Population.csv")
######## Simulation Starts ########
#creating a for loop to go through all the events
for (k in 1:nrow(Happened_Events)){
#creating a for loop to go through all the agents in a populations
for (i in 1:nrow(AgentPop)){
#STRESSFUL EVENT INCREASE DISTRESS LEVEL
#determine which event with what valence happens
Event <- Happened_Events$Valence[k]
#events increase the distress level of agents
ifelse(Event >= -13 & Event <= -12 ,AgentPop$Distress[AgentPop$AgentNo[i]] <- AgentPop$Distress[AgentPop$AgentNo[i]] + 14, ifelse(Event > -12 & Event <= -8 ,AgentPop$Distress[AgentPop$AgentNo[i]] <- AgentPop$Distress[AgentPop$AgentNo[i]] + 7, ifelse(Event > -8 & Event <= -4 ,AgentPop$Distress[AgentPop$AgentNo[i]] <- AgentPop$Distress[AgentPop$AgentNo[i]] + 3, ifelse(Event > -4 & Event <= 0 ,AgentPop$Distress[AgentPop$AgentNo[i]] <- AgentPop$Distress[AgentPop$AgentNo[i]] + 1))))
#RECEIVED and PERCEIVED SUPPORT INTERACTION
#received support happens randomly to each agent during each event. There is a 33.33% chance for each scenario to happen: a) agent receives appropriate support (1), b) agent receives inappropriate support (2), c) agent does not receive any support (3)
#how to keep track whether agent got or did not get received support (see how to save this kind of data)? Then I can run both ifelse at the same time and keep track of which scenario a-c happened
#determining which received support scenario (a-c) happens to the agent
Received_support <- data.frame(Received_Support = 1:3)
Received_support_sample <- sample_n(Received_support, 1, replace = TRUE)
#keeping track of how many times each agent received support
#ifelse(Received_support_sample == 1, AgentPop$Appropriate_Received_support_count[AgentPop$AgentNo[i]] <- AgentPop$Appropriate_Received_support_count[AgentPop$AgentNo[i]] + 1, AgentPop$Appropriate_Received_support_count[AgentPop$AgentNo[i]] <- AgentPop$Appropriate_Received_support_count[AgentPop$AgentNo[i]])
ifelse(Received_support_sample == 2, AgentPop$Inappropriate_Received_support_count[AgentPop$AgentNo[i]] <- AgentPop$Inappropriate_Received_support_count[AgentPop$AgentNo[i]] + 1, AgentPop$Inappropriate_Received_support_count[AgentPop$AgentNo[i]] <- AgentPop$Inappropriate_Received_support_count[AgentPop$AgentNo[i]])
#received support increases/decreases perceived support level depending on its appropriateness
#ifelse(Received_support_sample == 1, AgentPop$Perceived_Support[AgentPop$AgentNo[i]] <- AgentPop$Perceived_Support[AgentPop$AgentNo[i]] + 0.7, AgentPop$Perceived_Support[AgentPop$AgentNo[i]] <- AgentPop$Perceived_Support[AgentPop$AgentNo[i]])
ifelse(Received_support_sample == 2, AgentPop$Perceived_Support[AgentPop$AgentNo[i]] <- AgentPop$Perceived_Support[AgentPop$AgentNo[i]] - 0.7, AgentPop$Perceived_Support[AgentPop$AgentNo[i]] <- AgentPop$Perceived_Support[AgentPop$AgentNo[i]])
#making sure that the value of perceived support stays in a range from 0 to 10
ifelse(AgentPop$Perceived_Support[AgentPop$AgentNo[i]] >= 10 ,AgentPop$Perceived_Support[AgentPop$AgentNo[i]] <- 10, ifelse(AgentPop$Perceived_Support[AgentPop$AgentNo[i]] <= 0, AgentPop$Perceived_Support[AgentPop$AgentNo[i]] <- 0, AgentPop$Perceived_Support[AgentPop$AgentNo[i]] <- AgentPop$Perceived_Support[AgentPop$AgentNo[i]]))
#finding out the changed perceived support level
Perceived_Support_update <- AgentPop$Perceived_Support[AgentPop$AgentNo[i]]
#PERCEIVED SUPPORT BUFFERS AGAINST DISTRESS
#calculating the individual buffer (%), which is determined by the perceived support level, and indicates by how much perceived support buffers against distress
individual_buffer <- Perceived_Support_update / 11
#individual buffer together with the set impact value of the event determines the amount of distress that is going to be subtracted from the distress level after the event has happened
AgentPop$Distress[AgentPop$AgentNo[i]] <- AgentPop$Distress[AgentPop$AgentNo[i]] - (Happened_Events$Impact[k] * individual_buffer)
#LIKELIHOOD OF MAJOR DEPRESSIVE DISORDER ONSET
#setting different distress thresholds for Major Depressive Disorder onset in low and high vulnerability groups
#I grab those in low vulnerability group with distress level bigger than 40
Low_Vulnerability <- (1:nrow(AgentPop))[AgentPop$Vulnerability_Group == "L" & AgentPop$Distress >= 40]
#I set their MDD onset to 1 (meaning it is very likely that Major Depressive Disorder might have started)
AgentPop$MDD_onset[Low_Vulnerability] <- 1
#I grab those in high vulnerability group with distress level bigger than 13.33 (there is a threefold increased risk of MDD onset in a high vulnerability group)
High_Vulnerability <- (1:nrow(AgentPop))[AgentPop$Vulnerability_Group == "H" & AgentPop$Distress >= 13.33]
#I set their MDD onset to 1 (meaning it is very likely that Major Depressive Disorder might have started)
AgentPop$MDD_onset[High_Vulnerability] <- 1
}
}
######## Simulation Ends ########
AgentPop$MDD_onset <- as.numeric(AgentPop$MDD_onset)
class(AgentPop$MDD_onset)
#Count of MDD onset in Low and High Vulnerability groups
c <- AgentPop %>% group_by(Vulnerability_Group) %>% count(MDD_onset) %>% na.omit()
#visualize MDD onset count for different intervals of perceived support in both vulnerability groups
plot(Distress ~ Perceived_Support, data = AgentPop)
ggplot(AgentPop, aes(x = Perceived_Support, y = Distress, colour = Vulnerability_Group)) +
geom_smooth(method = lm) + geom_hline(yintercept = 13.33, color='coral', linetype = "dashed")+ geom_hline(yintercept = 40, color='#00AFBB', linetype = "dashed") + geom_point() + ggtitle("Relationship between Perceived support and Distress level", "Received inappropriate support") + theme_classic()
#visualize how many of MDD onset in total are in L and H groups
ggplot(AgentPop, aes(x=Vulnerability_Group, y=MDD_onset)) + geom_bar(stat="identity", fill = "#00AFBB") + ggtitle("Count of Major Depressive Disorder onset in Low and High Vulnerability groups") + ggtitle("Count of Major Depressive Disorder onset in Low and High Vulnerability groups", "Received inappropriate support") + theme_classic()
AgentPop %>% group_by(Vulnerability_Group) %>% summarise(mean = mean(Perceived_Support), sd = sd(Perceived_Support))
AgentPop %>% group_by(Vulnerability_Group) %>% summarise(mean = mean(Distress), sd = sd(Distress), max = max(Distress), min = min(Distress))
AgentPop %>% group_by(Vulnerability_Group) %>% summarise(sum(Inappropriate_Received_support_count))
View(c)
View(AgentPop)
View(AgentPop)
write.csv(Happened_Events, "Happened_Events.csv")
AgentPop1 <- read.csv(file = "~/Rutos SmuTkes/Cognitive Science/4th Semester/Social and cultural dynamics/ABM models/1st event scenario appropriate received support/1.csv")
AgentPop1 <- read.csv(file = "~/Rutos SmuTkes/Cognitive Science/4th Semester/Social and cultural dynamics/ABM models/1.csv")
AgentPop1 <- read.csv(file = "~/Rutos SmuTkes/Cognitive Science/4th Semester/Social and cultural dynamics/ABM models/1.csv")
AgentPop1 <- read.csv(file = "~/Rutos SmuTkes/Cognitive Science/4th Semester/Social and cultural dynamics/ABM models/1.csv")
AgentPop1 <- read.csv(file = "~/Rutos SmuTkes/Cognitive Science/4th Semester/Social and cultural dynamics/ABM models/1.csv")
View(AgentPop1)
#count of MDD onset in Low and High Vulnerability groups
AgentPop1 %>% group_by(Vulnerability_Group) %>% count(MDD_onset) %>% na.omit()
which git
which git
knitr::opts_chunk$set(echo = TRUE)
setwd("~/Rutos SmuTkes/Cognitive Science/4th Semester/Social and cultural dynamics/ABM models")
library(tidyverse)
updateR()
library(installr)
updateR()
install.packages(c("dplyr", "ggplot2", "knitr", "tidyverse"))
install.packages("rlang")
install.packages("tidyverse")
library(tidyverse)
knitr::opts_chunk$set(echo = TRUE)
install.packages("tidyverse")
library(tidyverse)