Last active
April 16, 2020 08:49
-
-
Save JimGrange/f15a77967a8f6f7ae1fb2f0df74ffa83 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#------------------------------------------------------------------------------ | |
### Packages & functions | |
library(dplyr) | |
library(afex) | |
library(here) | |
# generate data from a multivariate normal distribution with known | |
# correlations between values | |
mvrnorm <- function(n, nValues, meanValues, sdValues, corMatrix) { | |
Z <- matrix(rnorm(n * nValues), nValues, n) | |
t(meanValues + sdValues * t(chol(corMatrix)) %*% Z) | |
} | |
#------------------------------------------------------------------------------ | |
sample_size <- 60 | |
set.seed(42) | |
# load data | |
data <- read.csv("Miles_data.csv") | |
#------- | |
# NV conditions | |
means <- data %>% | |
filter(Condition == "NV") %>% | |
select(Need_Sat_Pre, | |
Need_Sat_Post, | |
Mood_Pre, | |
Mood_Post, | |
State_Host_Pre, | |
State_Host_Post) %>% | |
sapply(., mean) %>% | |
as.numeric() | |
sds <- data %>% | |
filter(Condition == "NV") %>% | |
select(Need_Sat_Pre, | |
Need_Sat_Post, | |
Mood_Pre, | |
Mood_Post, | |
State_Host_Pre, | |
State_Host_Post) %>% | |
sapply(., sd) %>% | |
as.numeric() | |
cor_mat <- data %>% | |
filter(Condition == "NV") %>% | |
select(Need_Sat_Pre, | |
Need_Sat_Post, | |
Mood_Pre, | |
Mood_Post, | |
State_Host_Pre, | |
State_Host_Post) %>% | |
cor() %>% | |
as.numeric() %>% | |
round(., 3) | |
cor_mat <- matrix(cor_mat, nrow = length(means), | |
ncol = length(means), byrow = TRUE) | |
n <- 4 | |
a <- means[1:4] | |
b <- sds[1:4] | |
x <- cor_nv[1:4, 1:4] | |
first <- mvrnorm(sample_size, 4, a, b, x) | |
a <- means[5:6] | |
b <- sds[5:6] | |
x <- cor_nv[5:6, 5:6] | |
second <- mvrnorm(sample_size, 2, a, b, x) | |
cond_data <- data.frame(cbind(first, second)) | |
colnames(cond_data) <- c("Need_Sat_Pre", | |
"Need_Sat_Post", | |
"Mood_Pre", | |
"Mood_Post", | |
"State_Host_Pre", | |
"State_Host_Post") | |
nv_data <- cond_data %>% | |
mutate(Condition = "NV") | |
#------- | |
# NVNI conditions | |
means <- data %>% | |
filter(Condition == "NVNI") %>% | |
select(Need_Sat_Pre, | |
Need_Sat_Post, | |
Mood_Pre, | |
Mood_Post, | |
State_Host_Pre, | |
State_Host_Post) %>% | |
sapply(., mean) %>% | |
as.numeric() | |
sds <- data %>% | |
filter(Condition == "NVNI") %>% | |
select(Need_Sat_Pre, | |
Need_Sat_Post, | |
Mood_Pre, | |
Mood_Post, | |
State_Host_Pre, | |
State_Host_Post) %>% | |
sapply(., sd) %>% | |
as.numeric() | |
cor_mat <- data %>% | |
filter(Condition == "NVNI") %>% | |
select(Need_Sat_Pre, | |
Need_Sat_Post, | |
Mood_Pre, | |
Mood_Post, | |
State_Host_Pre, | |
State_Host_Post) %>% | |
cor() %>% | |
as.numeric() %>% | |
round(., 3) | |
cor_mat <- matrix(cor_mat, nrow = length(means), | |
ncol = length(means), byrow = TRUE) | |
n <- 4 | |
a <- means[1:4] | |
b <- sds[1:4] | |
x <- cor_nv[1:4, 1:4] | |
first <- mvrnorm(sample_size, 4, a, b, x) | |
a <- means[5:6] | |
b <- sds[5:6] | |
x <- cor_nv[5:6, 5:6] | |
second <- mvrnorm(sample_size, 2, a, b, x) | |
cond_data <- data.frame(cbind(first, second)) | |
colnames(cond_data) <- c("Need_Sat_Pre", | |
"Need_Sat_Post", | |
"Mood_Pre", | |
"Mood_Post", | |
"State_Host_Pre", | |
"State_Host_Post") | |
nvni_data <- cond_data %>% | |
mutate(Condition = "NVNI") | |
#------- | |
# V conditions | |
means <- data %>% | |
filter(Condition == "V") %>% | |
select(Need_Sat_Pre, | |
Need_Sat_Post, | |
Mood_Pre, | |
Mood_Post, | |
State_Host_Pre, | |
State_Host_Post) %>% | |
sapply(., mean) %>% | |
as.numeric() | |
sds <- data %>% | |
filter(Condition == "V") %>% | |
select(Need_Sat_Pre, | |
Need_Sat_Post, | |
Mood_Pre, | |
Mood_Post, | |
State_Host_Pre, | |
State_Host_Post) %>% | |
sapply(., sd) %>% | |
as.numeric() | |
cor_mat <- data %>% | |
filter(Condition == "V") %>% | |
select(Need_Sat_Pre, | |
Need_Sat_Post, | |
Mood_Pre, | |
Mood_Post, | |
State_Host_Pre, | |
State_Host_Post) %>% | |
cor() %>% | |
as.numeric() %>% | |
round(., 3) | |
cor_mat <- matrix(cor_mat, nrow = length(means), | |
ncol = length(means), byrow = TRUE) | |
n <- 4 | |
a <- means[1:4] | |
b <- sds[1:4] | |
x <- cor_nv[1:4, 1:4] | |
first <- mvrnorm(sample_size, 4, a, b, x) | |
a <- means[5:6] | |
b <- sds[5:6] | |
x <- cor_nv[5:6, 5:6] | |
second <- mvrnorm(sample_size, 2, a, b, x) | |
cond_data <- data.frame(cbind(first, second)) | |
colnames(cond_data) <- c("Need_Sat_Pre", | |
"Need_Sat_Post", | |
"Mood_Pre", | |
"Mood_Post", | |
"State_Host_Pre", | |
"State_Host_Post") | |
v_data <- cond_data %>% | |
mutate(Condition = "V") | |
#------- all data | |
all_data <- rbind(nv_data, nvni_data, v_data) | |
all_data <- all_data %>% mutate( | |
Age = round(rnorm(sample_size * 3, 22.5, 1.2), 0), | |
Sex = sample(c("Male", "Female", "Pref not to say"), sample_size * 3, replace = TRUE, | |
prob = c(.475, .475, .05)), | |
Videogame_experience = sample(data$Vidgame_experience, sample_size * 3, | |
replace = TRUE), | |
Weekly_gaming = sample(data$Weekly_gaming, sample_size * 3, replace = TRUE) | |
) | |
write.csv(all_data, "simulated_data.csv", row.names = FALSE) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment