simondp

conditional effect

Feb 25th, 2021
672
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. library(rethinking)
  2. library(brms)
  3. options(mc.cores = 4)
  4.  
  5. # Import data
  6. data(Trolley)
  7. data <- Trolley
  8.  
  9. # Subset some data to make process faster
  10. data <- subset(data, edu == "Bachelor's Degree" |  edu == "Some College"|  edu == "Master's Degree")
  11. data$edu <- factor(data$edu)
  12. levels(data$edu) <- c("bsc","msc","col")
  13.  
  14. # Make order and response into fewer categories
  15. data$order <- round(data$order/6)
  16. data$response <- round(data$response/2.4)+1
  17. data$response <- ordered(data$response)
  18.  
  19. # Make plot as wished for
  20. # Get usage for each dur per participant
  21. edus <- c("col","bsc","msc")
  22. cols <- brewer.pal(3, "Set2")
  23. par(mfrow=c(2,2))
  24. j <- 1
  25. for(j in 1:4){
  26.   resp.trolley <- data %>%
  27.     group_by(order, edu) %>%
  28.     summarise(
  29.       resp = mean(response==j))
  30.  
  31.   # plot raw
  32.   test <- subset(resp.trolley, edu==edus[1])
  33.   plot(jitter(test$order, 0.4),test$resp,lwd=3, pch=20, ylim=c(0,max(resp.trolley$resp)*1),
  34.        col=cols[1],xlab="Duration", ylab = paste("Proportion Rating",j))
  35.   lines(test$order,test$resp,
  36.         col=cols[1],lwd=0.5, lty=2)
  37.   if (j == 3) {
  38.     # Add legend
  39.     legend(x = "bottomright",          # Position
  40.            legend = c("Col", "Bsc","Msc"),  # Legend texts
  41.            lty = c(2),           # Line types
  42.            col = cols[1:3],           # Line colors
  43.            lwd = 1)                 # Line width
  44.   }
  45.   for(i in 2:3){
  46.     test <- subset(resp.trolley, edu==edus[i])
  47.     points(jitter(test$order, 0.4),test$resp,ylim=c(0,0.8),lwd=3, pch=20,
  48.            col=cols[i])
  49.     lines(jitter(test$order, 0.4),test$resp,ylim=c(0,0.8),
  50.           col=cols[i],lwd=0.5, lty=2)}
  51. }
  52.  
  53. # Make BRMS model
  54. data$response <- ordered(data$response)
  55. m1 <- brm(response  ~ edu*mo(order) + (1|id), data= data,
  56.           family = cumulative(threshold = "flexible"),
  57.           chains = 2)
  58.  
  59. # Plot
  60. conditional_effects(m1, categorical=T)
  61.  
  62. # Trying to make for conditions
  63. conditions <- make_conditions(m1, "response")
  64. conditional_effects(m1,categorical=TRUE, conditions = conditions)
RAW Paste Data

Adblocker detected! Please consider disabling it...

We've detected AdBlock Plus or some other adblocking software preventing Pastebin.com from fully loading.

We don't have any obnoxious sound, or popup ads, we actively block these annoying types of ads!

Please add Pastebin.com to your ad blocker whitelist or disable your adblocking software.

×