Created
January 23, 2020 18:07
-
-
Save jackobailey/46d61c07e88f1c92e36d107023514a81 to your computer and use it in GitHub Desktop.
rlikert
This file contains 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
# Load packages using the 'librarian' package | |
librarian::shelf(tidyverse, | |
tidybayes, | |
brms, | |
here) | |
# Create function to simulate the outcome scale for arbitrary | |
# prior distributions | |
rlikert <- function(n = 1e3, | |
t1 = -0.84, | |
t2 = -0.25, | |
t3 = 0.25, | |
t4 = 0.84, | |
mean = 0, | |
disc = 0){ | |
require(tidyverse) | |
# Calculate standard deviation from discrimination | |
sd <- 1/exp(disc) | |
# Create empty dataframe | |
dta <- tibble(n = 1:n) | |
# Calculate probabilities of each response for each individual | |
dta <- | |
dta %>% | |
mutate( | |
p1 = pnorm(t1, mean, sd), | |
p2 = pnorm(t2, mean, sd) - pnorm(t1, mean, sd), | |
p3 = pnorm(t3, mean, sd) - pnorm(t2, mean, sd), | |
p4 = pnorm(t4, mean, sd) - pnorm(t3, mean, sd), | |
p5 = 1 - pnorm(t4, mean, sd) | |
) | |
# For each individual, sample a single response category | |
dta <- dta %>% mutate(resp = NA) | |
for (i in 1:nrow(dta)) { | |
dta$resp[i] <- | |
sample( | |
x = c(1:5), | |
size = 1, | |
prob = c(dta$p1[i], dta$p2[i], dta$p3[i], dta$p4[i], dta$p5[i]) | |
) | |
} | |
# Return | |
dta$resp | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment