-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsimulation_4items_weak_correlation.Rmd
More file actions
202 lines (147 loc) · 7.13 KB
/
simulation_4items_weak_correlation.Rmd
File metadata and controls
202 lines (147 loc) · 7.13 KB
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
---
title: "Effect of missingness - four items w/ weak correlations"
output: html_document
author: "Pooya Razavi"
date: "last knitted: `r Sys.time()`"
editor_options:
chunk_output_type: console
---
```{r setup, include=FALSE}
library(MASS)
library(tidyverse)
library(GGally)
set.seed(110)
knitr::opts_chunk$set(echo = TRUE)
```
**Goal:** Determine the extent to which missing data can affect the accuracy of the results of a survey. <br>
**Survey parameters:** <br>
_Number of items:_ 4 <br>
_Average item interrelations:_ weak (mean(r) = .2) <br>
**Simulation parameters:** <br>
_Sample sizes:_ 5000, 3000, 1000, 500, and 200 <br>
_Number of iterations per condition:_ 100 <br>
_Missingness range:_ from 10 to 90 percent <br>
_Evaluated outcomes:_ (a) correlation between the items, (b) mean of the four items <br>
# Set up the parameters
```{r}
#correlation matrix
cor_matrix <- rbind(c(1, .25, .3, .1),
c(.25, 1, .2, .25),
c(.3, .2, 1, .1),
c(.1, .25, .1, 1))
cor_matrix
#vector of means
mean_vector <- c(5, 3, 4, 5)
#sample sizes for the simulation
sample_size_options <- c(5000, 3000, 1000, 500, 200)
```
# Simulation
```{r results=FALSE, fig.show='hide'}
for (sample_size in sample_size_options) {
#simulate the data
df <- as.data.frame(mvrnorm(n = sample_size,
mu = mean_vector,
Sigma = cor_matrix))
# Testing the effect of missingness
sim_summary <- data.frame(n = NA,
missing = NA,
iteration = NA,
sum_abs_dev = NA,
abs_mean_dif = NA)
missing_prob <- seq(0.1, .9, by = 0.025) #test NA percentage bet. .1 and .9
for (missing in missing_prob) {
for (iteration in 1:100) {
#creating a matrix of NAs
for (i in 5:8) {
df[, i] <- sample(c("response", NA),
replace = TRUE,
size = sample_size,
prob = c(1 - missing, missing))
}
#apply NAs to the complete dataset
df_w_na <- df %>%
dplyr::mutate(V1_na = if_else(V5 == "response", V1, -100),
V2_na = if_else(V6 == "response", V2, -100),
V3_na = if_else(V7 == "response", V3, -100),
V4_na = if_else(V8 == "response", V4, -100)
) %>%
dplyr::select(contains("_na"))
##outcome 1: correlation between items
#correlation matrix for the df with NAs
cor_matrix_w_na <- cor(df_w_na, use = "pairwise.complete.obs") %>% round(2)
#the original correlation matrix
orig_cor_matrix <- cor(df[, 1:4]) %>% round(2)
#compare the original correlation matrix and the one with NAs
dif_cor_matrix <- orig_cor_matrix - cor_matrix_w_na
#sum of absolute deviation
sum_abs_dev <- sum(abs(dif_cor_matrix)) / 2
##outcome 2: mean of four items
#the mean of four items (no NAs)
complete_mean <- (mean(df[,1]) + mean(df[,2]) + mean(df[,3]) + mean(df[,4]))/4
#the mean of four items (w/ NAs)
df_w_na_mean <- (mean(df_w_na[,1], na.rm = T) + mean(df_w_na[,2], na.rm = T) +
mean(df_w_na[,3], na.rm = T) + mean(df_w_na[,4], na.rm = T))/4
#absolute mean difference bet. complete and sparse datasets
abs_mean_dif <- abs(complete_mean - df_w_na_mean)
#save the output of this iteration
sim_summary <- rbind(sim_summary, c(sample_size, missing, iteration, sum_abs_dev, abs_mean_dif))
#print the iteration step (this is important for long simulations)
print(paste0("simulation step: n = ", sample_size, ", %NA = ", missing, ", iter. = ", iteration))
}
}
#save the plots for the 2 outcomes based on the full simulation for the assigned sample size
summary_plot_cor <- sim_summary[-1,] %>%
dplyr::group_by(missing) %>%
summarise(med_dev = median(sum_abs_dev),
sd = sd(sum_abs_dev)) %>%
ggplot(aes(x = missing, y = med_dev)) +
geom_line(color = "darkgray") +
geom_point() +
ylim(0, 2) +
labs(
title = paste0("N = ", sample_size),
x = "Proportion of missing data",
y = "Median sum of absolute deviation") +
geom_errorbar(aes(ymin = med_dev - sd/2, ymax = med_dev + sd/2), width=.01,
color = "gray") +
geom_hline(yintercept = .18, linetype = "dashed",
color = "red", size = .5) +
theme_minimal()
summary_plot_mean <- sim_summary[-1,] %>%
dplyr::group_by(missing) %>%
summarise(med_dev = median(abs_mean_dif),
sd = sd(abs_mean_dif)) %>%
ggplot(aes(x = missing, y = med_dev)) +
geom_line(color = "darkgray") +
geom_point() +
ylim(0, .5) +
labs(
title = paste0("N = ", sample_size),
x = "Proportion of missing data",
y = "Median absolute mean difference") +
geom_errorbar(aes(ymin = med_dev - sd/2, ymax = med_dev + sd/2), width=.01,
color = "gray") +
#geom_hline(yintercept = .18, linetype = "dashed",
# color = "red", size = .5) +
theme_minimal()
print(summary_plot_cor)
print(summary_plot_mean)
#save the plots as objects to be used outside the loop
cor_plot_name <- paste0("plot_cor_", sample_size)
assign(cor_plot_name, summary_plot_cor)
mean_plot_name <- paste0("plot_mean_", sample_size)
assign(mean_plot_name, summary_plot_mean)
}
```
# Outcome 1: Difference bet. Correlations
For this outcome, I compare the correlation matrix based on the complete data and correlation matrix based on the dataset that has NAs. The larger the differences, the stronger the deviation caused by missing data.
```{r fig.width=6, fig.height=17, warning=FALSE, fig.align='center'}
ggpubr::ggarrange(plot_cor_5000, plot_cor_3000, plot_cor_1000, plot_cor_500, plot_cor_200,
ncol = 1, nrow = 5)
```
# Outcome 2: Difference between Means
For this outcome, I compare the mean of 4 items based on the complete data and mean of four items based on the dataset that has NAs. The larger the differences, the stronger the deviation caused by missing data.
```{r fig.width=6, fig.height=17, warning=FALSE, fig.align='center'}
ggpubr::ggarrange(plot_mean_5000, plot_mean_3000, plot_mean_1000, plot_mean_500, plot_mean_200,
ncol = 1, nrow = 5)
```