-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCOD_20231109_MGK_BTE3207_week11_2.Rmd
331 lines (224 loc) · 11 KB
/
COD_20231109_MGK_BTE3207_week11_2.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
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
---
title: "COD_week11_2_MGK_BTE3207"
author: "Minsik Kim"
date: "2023-11-08"
output:
rmdformats::downcute:
downcute_theme: "chaos"
code_folding: show
fig_width: 6
fig_height: 6
df_print: paged
editor_options:
chunk_output_type: inline
markdown:
wrap: 72
---
```{r warning=FALSE, message=FALSE, echo=FALSE, results='hide', setup, cache=TRUE}
#===============================================================================
#BTC.LineZero.Header.1.1.0
#===============================================================================
#R Markdown environment setup and reporting utility.
#===============================================================================
#RLB.Dependencies:
# knitr, magrittr, pacman, rio, rmarkdown, rmdformats, tibble, yaml
#===============================================================================
#Input for document parameters, libraries, file paths, and options.
#=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=
knitr::opts_chunk$set(message=FALSE, warning = FALSE)
path_working <- "/Users/minsikkim/Dropbox (Personal)/Inha/5_Lectures/Advanced biostatistics/scripts/BTE3207_Advanced_Biostatistics/"
path_library <- "/Library/Frameworks/R.framework/Resources/library"
str_libraries <- c("tidyverse", "pacman", "yaml")
YAML_header <-
'---
title: "BTE3207 week 11-2"
author: "Minsik Kim"
date: "2032.11.5"
output:
rmdformats::downcute:
downcute_theme: "chaos"
code_folding: hide
fig_width: 6
fig_height: 6
---'
seed <- "20230920"
#=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
#Loads libraries, file paths, and other document options.
#=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
FUN.LineZero.Boot <- function() {
.libPaths(path_library)
require(pacman)
pacman::p_load(c("knitr", "rmarkdown", "rmdformats", "yaml"))
knitr::opts_knit$set(root.dir = path_working)
str_libraries |> unique() |> sort() -> str_libraries
pacman::p_load(char = str_libraries)
set.seed(seed)
}
FUN.LineZero.Boot()
#=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
#Outputs R environment report.
#=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
FUN.LineZero.Report <- function() {
cat("Line Zero Environment:\n\n")
paste("R:", pacman::p_version(), "\n") |> cat()
cat("Libraries:\n")
for (str_libraries in str_libraries) {
paste(
" ", str_libraries, ": ", pacman::p_version(package = str_libraries),
"\n", sep = ""
) |> cat()
}
paste("\nOperating System:", pacman::p_detectOS(), "\n") |> cat()
paste(" Library Path:", path_library, "\n") |> cat()
paste(" Working Path:", path_working, "\n") |> cat()
paste("Seed:", seed, "\n\n") |> cat()
cat("YAML Header:\n")
cat(YAML_header)
}
FUN.LineZero.Report()
```
# mediation pacakge
`The framing data` contains 265 rows and 15 columns of data from a framing experiment conducted by Brader, Valentino and Suhay (2008).
Brader et al. (2008) conducted a randomized experiment where subjects are exposed to different media stories about immigration and the authors investigated how their framing influences attitudes and political behavior regarding immigration policy. They posit anxiety as the mediating variable for the causal effect of framing on public opinion. We first fit the mediator model where the measure of anxiety (emo) is modeled as a function of the framing treatment (treat) and pre-treatment covariates (age, educ, gender, and income). Next, we model the outcome variable, which is a binary variable indicating whether or not the participant agreed to send a letter about immigration policy to his or her member of Congress (cong_mesg). The explanatory variables of the outcome model include the mediator, treatment status, and the same set of pre-treatment variables as those used in the mediator model.1 In this example, the treatment is expected to increase the level of respondents’ emotional response, which in turn is hypothesized to make subjects more likely to send a letter to his or her member of Congress. We use the linear regression fit with least squares and the probit regression for the mediator and outcome models, respectively.
https://www.jstor.org/stable/25193860
Abstract: We examine whether and how elite discourse shapes mass opinion and action on immigration policy. One popular but untested suspicion is that reactions to news about the costs of immigration depend upon who the immigrants are. We confirm this suspicion in a nationally representative experiment: news about the costs of immigration boosts white opposition far more when Latino immigrants, rather than European immigrants, are featured. We find these group cues influence opinion and political action by triggering emotions-in particular, anxiety-not simply by changing beliefs about the severity of the immigration problem. A second experiment replicates these findings but also confirms their sensitivity to the stereotypic consistency of group cues and their context. While these results echo recent insights about the power of anxiety, they also suggest the public is susceptible to error and manipulation when group cues trigger anxiety independently of the actual threat posed by the group.
A data frame containing the following variables:
immigr:
A four-point scale measuring subjects' attitudes toward increased immigration. Larger values indicate more negative attitudes.
english:
A four-point scale indicating whether subjects favor or oppose a law making English the official language of the U.S.
cong_mesg:
Whether subjects requested sending an anti-immigration message to Congress on their behalf.
anti_info:
Whether subjects wanted to receive information from anti-immigration organizations.
tone:
1st treatment; whether the news story is framed positively or negatively.
eth:
2nd treatment; whether the news story features a Latino or European immigrant.
cond:
Four level measure recording joint treatment status of tone and eth.
treat:
Product of the two treatment variables. In the original study the authors only find this cell to be significant.
emo:
Measure of subjects' negative feeling during the experiment. A numeric scale ranging between 3 and 12 where 3 indicates the most negative feeling.
anx:
A four-point scale measuring subjects' anxiety about increased immigration.
p_harm:
Subjects' perceived harm caused by increased immigration. A numeric scale between 2 and 8.
age:
Subjects' age.
educ:
Subjects' highest educational attainments.
gender:
Subjects' gender.
income:
Subjects' income, measured as a 19-point scale.
```{r}
install.packages("mediation")
library(mediation)
mediation_data <- mediation::framing
?mediation::framing
```
```{r}
head(mediation_data)
```
Is there a mediation effect by `emo` (Negative feelings) on `cong_mesg` (Whether subjects requested sending an anti-immigration message to Congress on their behalf).
```{r}
med.fit <- lm(emo ~ treat * age + educ + gender + income, data=mediation_data)
med.fit %>% summary
```
```{r}
out.fit <- glm(cong_mesg ~ emo + treat * age + emo * age + educ + gender + income, data = mediation_data, family = binomial("probit"))
out.fit %>% summary
```
```{r}
med.out <- mediate(med.fit, out.fit, treat = "treat", mediator = "emo",
robustSE = TRUE, sims = 100)
```
```{r}
summary(med.out)
```
Where
ACME: The average causal mediation effects
ADE: The average direct effects
The results suggest that the treatment in the framing experiment may have increased emotional response, which in turn made subjects more likely to send a message to his or her member of Congress. Here, since the outcome is binary all estimated effects are expressed as the increase in probability that the subject sent a message to his or her Congress person.
# Mediation analysis with SBP dataset
# Before begin..
Let's load the SBP dataset.
```{r}
dataset_sbp <- vroom::vroom(file = "/Users/minsikkim/Dropbox (Personal)/Inha/5_Lectures/Advanced biostatistics/scripts/BTE3207_Advanced_Biostatistics/dataset/sbp_dataset_korea_2013-2014.csv")
#vroom does the same thing as read.csv but much faster
head(dataset_sbp)
```
Making a new variable `hypertension`
```{r}
dataset_sbp$hypertension <- ifelse(dataset_sbp$SBP > 130 |
dataset_sbp$DBP > 80,
T,
F)
dataset_sbp$history_diabete <- ifelse(dataset_sbp$DIS == 1 |
dataset_sbp$DIS == 3,
T,
F)
```
Step 1.
```{r}
lm(SBP ~ BMI + hypertension + DBP + SEX, data = dataset_sbp) %>%
summary
```
Step 2
```{r}
lm(history_diabete ~ BMI + hypertension + DBP + SEX, data = dataset_sbp) %>%
summary
```
Step 3
```{r}
lm(SBP ~ BMI + history_diabete + hypertension + DBP + SEX, data = dataset_sbp) %>%
summary
```
Mediation fit, adjusted by status of hypertension
```{r}
med.fit <- lm(history_diabete ~ BMI + hypertension + DBP + SEX, data = dataset_sbp)
```
Outcome fit, adjusted by status of hypertension
```{r}
out.fit <- lm(SBP ~ BMI + history_diabete + hypertension + DBP + SEX, data = dataset_sbp)
```
Mediation analysis, adjusted for hypertension status (for each group with hypertension and without hypertension)
```{r}
med.out <- mediate(med.fit, out.fit, treat = "hypertension", mediator = "history_diabete",
robustSE = TRUE, sims = 100)
med.out
```
```{r}
summary(med.out)
```
# Bibliography
```{r warning=FALSE, message=FALSE, echo=FALSE}
#===============================================================================
#BTC.LineZero.Footer.1.1.0
#===============================================================================
#R markdown citation generator.
#===============================================================================
#RLB.Dependencies:
# magrittr, pacman, stringr
#=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
#BTC.Dependencies:
# LineZero.Header
#===============================================================================
#Generates citations for each explicitly loaded library.
#=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
str_libraries <- c("r", str_libraries)
for (str_libraries in str_libraries) {
str_libraries |>
pacman::p_citation() |>
print(bibtex = FALSE) |>
capture.output() %>%
.[-1:-3] %>% .[. != ""] |>
stringr::str_squish() |>
stringr::str_replace("_", "") |>
cat()
cat("\n")
}
#===============================================================================
```