Chapter 10 Assignments II

10.1 Andreas Flache

10.1.1 data

flache <- read.csv("https://stulp.gmw.rug.nl/Rworkshop/simulationResultsExpLevelAPolYouGov.csv")
library(skimr)
skim(flache)

10.1.2 visualisations

Andreas wrote: “association across country-elections of sdOpFin with r14Last at parameters sf=1, phi=1, eps=0.25, intergroupDistance=1”

library(tidyverse)
viz1_flache <- flache %>% 
  filter(sF == 0.1 & phi == 1 & eps0 == 0.2)

ggplot(viz1_flache, aes(x = as.numeric(sdOpFin), y = r14Last, fill = factor(intergroupDistance))) +
  geom_point() +
  geom_smooth(method = "lm", colour = "NA") +
  scale_fill_viridis_d() +
  facet_wrap(~intergroupDistance, nrow = 1) +
  theme_minimal() +
  theme(panel.grid.minor = element_blank()) +
  guides(fill = "none")

Andreas wrote: “correlation sdOpFin with r14last by eps0 and intergroupDistance, for SF = 0.5 and phi = 0.5”

viz2_flache <- flache %>% 
  filter(sF == 0.6 & phi == 0.5) %>% # sd = 0.5 does not exist
  select(eps0, intergroupDistance, sdOpFin, r14Last)

corr_flache <- viz2_flache %>% 
  group_by(eps0, intergroupDistance) %>% 
  summarise(cor = cor(as.numeric(sdOpFin), r14Last))

ggplot(corr_flache, aes(x = eps0, y = intergroupDistance, z = cor)) +
  geom_contour_filled() +
  theme_minimal()

10.1.3 running simulations in R

Andreas, people may tell you R is slow for simulations. Maybe, but code is relatively readable. A major benefit to me is that you can store output in the same ‘dataset’ as your parameters. Let’ see an example, a simple one. we’ll simulate from a normal distribution, but we vary the n, mean and sd.

library(tidyverse)
sims_params <- data.frame(n= c(10, 100, 1000), mean = c(0, 10, 100), sd = c(1, 10, 1000)) %>% 
  expand.grid() # this creates a dataframe with all combinations across parameters
head(sims_params)
##      n mean sd
## 1   10    0  1
## 2  100    0  1
## 3 1000    0  1
## 4   10   10  1
## 5  100   10  1
## 6 1000   10  1
sims_params <- sims_params %>% 
  # add new column with list of results in a cell, can also be a dataframe itself
  # mutate creates new column, results = name of column
  # pmap is a function that iterates over multiple lists/columns
  # and applies a function to it
  mutate(results = pmap(list(n, mean, sd), 
                        function(n, m, s) rnorm(n, m, s) ))
head(sims_params)
##      n mean sd
## 1   10    0  1
## 2  100    0  1
## 3 1000    0  1
## 4   10   10  1
## 5  100   10  1
## 6 1000   10  1
##                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                   results
## 1                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                 0.08253913, 1.14481207, 0.61831526, 1.18015927, 0.35906253, 0.29999311, 0.08924839, -1.30339310, 0.64521117, 0.00387676
## 2                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                           -0.12644266, -0.04013385, 0.13424358, 1.20695279, -0.15156935, 1.04678696, 0.50579736, -0.81840117, 0.96306738, -1.17807379, 1.74996793, 0.42864712, -0.36396594, 1.44339283, -1.11322643, 1.12180678, -1.04389519, 0.27865356, -0.85391621, 1.85298574, 1.16956680, 0.53401295, -0.46036706, -0.35324319, -0.32126563, 1.96983864, 2.18008183, 1.23845062, -0.31279422, 0.52552422, -0.64684565, 0.41770540, -0.39400747, 0.14923564, 1.10360119, -1.34003486, 0.19757137, -0.95493648, 0.21989905, -0.50736179, 0.46187524, 0.26324753, 1.10621382, 1.27377736, -0.21249891, -0.31845505, -1.22989788, -0.55419864, 1.05263581, -0.01807382, 0.01833068, 0.89320320, -1.66979955, 1.02548722, -0.74200304, -0.65300053, -2.17902232, -0.91480198, -0.14677017, -0.22364239, -0.02706994, 1.30482752, 1.40766161, 1.91699640, -0.09802513, -0.25228937, 0.42523511, -0.16277811, -0.50154037, 1.62074865, 0.79388037, -0.13541047, -0.32751044, 0.31246948, 0.80302704, 0.87625488, 3.06640101, 0.87609350, -0.41518104, 0.99094651, 0.59648482, 1.50981952, 0.38972467, -0.56961156, 0.28778694, -0.78895699, -1.57267127, 0.86330951, -1.14378044, -0.67695734, -0.53470414, 1.42206841, 0.30570597, 0.24837952, -0.38606898, -0.59450738, -0.73452015, 1.62861694, 0.29532419, 0.34363503
## 3 1.284910137, -0.965990664, -0.965915614, 0.422348457, 0.622262678, 0.397786224, 0.583377426, -1.178682323, 0.751745401, 2.868632849, 1.098618573, 0.007233447, 1.081316166, -0.005687142, 0.400639874, -0.458316027, -1.391275001, -1.770166615, -2.220107671, -0.965280351, -1.139785062, -2.131144914, -1.752217516, 0.666518134, -1.036250936, -0.480407953, 1.508379702, 0.858227267, 0.259811847, -0.051592931, -0.092162070, 0.944555400, 1.467954092, -0.384510291, 2.502911605, 1.369809014, -0.657358314, -1.141062069, 0.821721302, -1.165849727, 0.287745011, -0.327174616, -0.968445173, -0.214079444, -0.083389597, -0.359611343, -0.579272353, -0.762652361, -0.590932997, -0.680623358, -1.163207024, 0.288362267, 0.044986007, -0.887025958, -0.391935497, -1.112575585, -0.466624259, 1.406108891, -0.644197845, 0.281443816, 0.143096997, -0.280480017, -0.231191228, 4.023802008, 0.088471252, -0.270096062, -1.718992429, -0.394437777, 1.289285818, 0.462217735, -0.243117770, -0.604123575, 0.573491039, 0.373860068, 2.406912813, -2.014210583, 0.137235109, -0.609916238, 0.817517954, -0.928643260, 0.195067741, 1.925234718, -0.425181856, 0.231898995, -0.260745671, -0.140244615, 0.495736848, 0.424035666, -1.259547070, -1.257110103, -1.813214145, -1.375621432, -2.077739585, -0.911747820, -0.018392973, 0.548166176, -0.265064919, -0.207047866, -0.616731979, 0.899427340, 0.065382380, -2.496361728, 0.535452848, -1.083996618, -0.103946194, -1.790514981, 1.153685988, -0.008092557, -1.307306181, 1.629298292, 1.697988685, -0.997684247, -0.204194522, 0.327451424, -0.736569104, -0.329813958, -0.364367217, -0.357500042, 0.330886172, -0.797114579, 1.809015415, -0.048286644, -0.643295461, -0.013398365, -0.823628238, -0.872255966, 0.142446936, 0.568889481, -0.117573338, -1.367745417, -0.296742210, 1.026500521, 0.706021331, 0.469097233, 0.616919648, -0.239816098, 0.651851622, -0.470715645, -2.421586330, 2.225412313, -0.236927723, 1.794002844, -1.069051051, 0.677999356, 0.188962535, -0.043688351, -1.826860982, -0.995465454, -2.007177346, 1.085243835, -0.656638813, -0.093508214, 0.708562012, 1.140038026, -0.159952989, 0.761011349, -0.174644178, -0.391485794, 0.718828716, 0.134796154, -0.492921234, 0.615700635, 0.646740027, 0.111264988, -1.691042836, 0.835966756, -0.715483480, -1.184383201, 0.006341093, -0.240341830, 0.887651399, -0.191415889, -0.473415069, -0.247952267, 0.901019743, 0.174214286, 1.154388183, -0.294273568, -1.223429526, -0.863663224, -2.170931508, -1.037848643, -1.333564457, -1.840142378, 0.533950294, -0.564354200, 0.877294064, 0.282369650, 0.815387103, -0.265892777, -0.295998990, 0.663996339, 2.421322730, 0.117532141, 1.182262547, -0.128379453, 0.408473963, -0.365955256, -0.737520792, 0.468332594, 1.461841796, -0.578723418, 0.643368672, -0.688977086, 0.691928539, -1.537245113, -0.087456693, -1.379273375, -1.100383496, 1.282256235, 1.010256490, 0.732851982, -0.503684449, -0.910740833, 1.096321003, -0.673373406, -1.066783658, -0.266949204, -1.054930954, -1.352143189, -1.542869439, -0.584014680, 0.821877089, -0.647683795, 0.183432677, -0.330211664, 0.777821598, -1.124416527, -0.289175101, -0.478680912, -0.681098317, 1.043593174, -0.907601002, -2.290547085, 1.064291150, -0.100108634, -2.657047086, 1.563985758, 0.467763727, -0.543310408, 0.713284477, 1.053275907, -0.523842877, 2.104922217, -1.050623129, 0.978821516, 0.155361144, 0.970748072, 1.339405799, 0.437656161, 0.178761753, 1.243710735, -0.276553366, 0.088811367, 0.297731519, 1.786385334, 1.024422026, 1.124615592, -0.826663212, 0.955445536, -0.665907894, -0.188936142, -0.737232810, -0.080952354, -0.339051416, 1.461336496, 0.564973678, 0.159355303, -0.905498015, -0.163507742, 0.142015854, 1.110714442, 0.932243350, 0.887410102, 1.130577424, 0.927837933, -0.375140906, 0.613058230, 0.321941418, -0.017515322, 0.115124045, 1.434331752, 0.927391250, -0.624168898, 1.371801397, 1.210797317, 1.237276591, 1.637624706, 0.530374860, -1.740710058, -0.506287556, 0.479616757, -0.410875883, -0.752598347, -0.148386167, -0.715737522, 1.519075317, 0.685236039, 0.103915694, -0.575571570, 0.028797469, 1.286304801, 0.588582148, -1.223854324, -1.552050312, -0.167685509, -1.082537588, -0.421231777, 0.397487724, -0.568369725, 1.414220980, -0.204310808, -2.011552885, 0.978377520, -1.036723510, 0.168947129, -0.136521217, -0.171778112, 0.265031149, -1.124636309, 1.754577711, 0.360156728, -1.849150008, 0.747948497, 0.873734468, -1.145894264, 1.058577134, -0.355242645, 0.460316311, -0.941034280, 0.345510871, -1.410428704, 0.183174557, 1.294994847, -1.105135591, -1.431075310, 1.545088069, -0.892252102, 1.896480391, -0.993517707, 1.309863740, -0.844581626, -0.306796771, -0.419138212, 0.922596934, -0.977473754, 0.581385429, 0.338744913, 0.352303430, -0.811385799, -0.438068210, -1.657596084, -0.286538038, -1.506233194, -1.157669545, -1.981461270, -1.068532872, 0.514931556, 0.298451437, -1.198358276, 1.393545442, -0.403828907, 1.119375157, 0.819294550, -0.131316858, 0.044893432, 0.036871293, -0.512192685, -1.803827500, 2.416741999, -0.556968612, 0.023488620, -1.320678886, 2.300223678, 0.522589182, -0.941667349, -0.887805384, 0.769962636, -0.691727531, -1.743103196, 1.302161095, 0.825048906, -0.140511981, -1.237344624, -1.134202388, -2.109111139, 0.980027556, 1.086644585, -2.150483092, -0.632570975, 0.439631105, 0.570758493, 1.544626307, 0.160542551, 0.630703104, -0.485920466, 0.080173689, 0.644317400, 1.499185400, -1.538209470, 1.087918769, 0.324959213, -1.628941304, 0.703610323, 0.641217981, -0.961973327, 0.372594578, -0.324352064, -3.013590854, -0.945094190, -0.823180018, 0.741222406, 1.678506881, 3.113814262, -1.128127534, 0.003970783, -0.747489626, -0.424140820, -0.548797810, -0.547200981, -0.563407240, -0.156902439, 1.001038897, -0.778908450, 0.893427220, -2.091811048, 0.384655544, 0.837344662, -0.993828280, -0.269044044, 0.430820862, -1.199182146, 0.164437312, 0.592069796, -0.918778854, -0.930030175, 0.500287015, 0.601695332, -1.501256883, 1.136233004, 0.617624928, 0.345263892, 2.261945373, 1.099525756, -0.946933162, 0.331737238, -0.282451729, -0.656737441, -0.082963424, -0.817037151, -1.142696333, 0.093912890, -0.302153646, 0.068371947, -0.067719393, 0.415376251, 0.629600360, -0.551490901, -1.089949067, -1.108744314, -1.044492353, -1.060176699, 1.597336354, 0.702219812, 0.970670263, -0.921243186, 0.003365545, 0.048913641, 0.419280378, 0.475019122, -0.811357932, 1.580206265, -0.065760218, -0.209716097, 0.372251754, 1.721212357, 0.577082399, 0.810370107, -0.156311440, -0.364001292, -1.084251988, -0.764559176, 1.584259946, -1.384329025, 0.230837931, -0.266456490, 0.941113752, 0.407901238, -1.467635370, 1.903260079, -0.222045914, -1.319628601, -0.461682790, -1.072592686, -1.392482765, 0.201782703, -0.644573793, -0.749435448, 0.085932093, 0.546931225, 2.054450326, 1.753392414, 0.274992233, 2.222372066, -1.521748941, -0.472656123, -0.111678798, -0.343427061, -0.861596250, 1.557687545, -0.817281674, -1.520236438, -1.465500244, 0.480931859, 0.048089596, -1.812456718, 0.876479685, 0.222570773, -0.738599722, 0.914401792, -1.447311823, 0.118182882, 0.576848933, -0.248089234, 0.189891071, 0.172659233, -0.504658472, -0.887578892, -0.370284385, 0.364000534, -0.285151794, -1.532810855, -0.104207143, 0.447260116, -0.105222020, -1.744608008, -1.400934709, -0.946204858, -1.405182072, -1.987932193, 0.261904606, -0.513075521, 0.413402782, -0.573960993, -1.253908754, 0.451021376, -0.109919896, 1.627014004, 0.733701971, -0.460672713, 0.526889190, -1.234066604, 0.600937575, -1.161812197, -0.893904895, -0.236796734, 0.109621286, 1.118797777, -0.313439859, 0.026642827, -1.008844275, -0.631300362, 0.056375171, -0.792738130, 2.357854023, 0.040489660, 1.212150161, 1.201376631, -0.516854118, -0.829839477, 0.758709623, -1.122208507, 0.477512200, -0.955218599, -0.319302268, 0.336186120, 0.299165197, 1.383512952, -1.251747904, 0.429818418, 1.613004911, 1.264406937, -2.095221674, 0.594088951, -1.371806464, 1.407549449, -0.028551178, -0.745342386, -1.642867586, -0.435712730, 2.229504574, -0.201274158, -0.636289414, 0.892903760, 0.093015905, 1.835969009, -0.381647964, 0.323336722, 0.126224999, -0.684714033, -0.237413372, -0.891020403, 0.456642000, -0.638389684, 0.694335743, -0.529940014, -0.434434827, 0.814124186, 0.135369020, -0.155411633, -0.306377705, 2.437272541, -0.142056996, 1.952062273, 0.635027299, 0.587186467, -1.149101684, 1.168145948, 0.157739531, -0.924361607, -2.395288713, 0.678866892, 1.815095907, 0.095520001, -0.881520953, -0.517254365, -0.120676103, -0.848083025, -0.069931609, 0.686691543, -0.593919155, -0.405243405, 0.157671751, -2.063677508, 0.578398942, 0.745029956, 0.462993393, 0.469996907, -0.852575355, -0.664322322, -1.778805524, -0.438122916, 0.445296634, -0.403978592, -0.141456461, 0.437631658, 2.393534564, -0.099898904, 0.945739052, -1.657046847, -0.342376281, -2.094208231, 0.102052942, 1.181876671, 1.580551510, 1.828134773, -0.178305759, -1.059739923, -1.146229449, 0.688240525, 1.899771259, 0.849232860, -1.085840075, -0.479032732, -0.607482322, 0.198065005, 0.277077567, 0.445918343, 2.668623446, -0.465032660, -0.577124652, -2.315186602, 0.293444166, -0.239268738, 0.064705300, 0.652928268, -0.896930429, -0.666390621, 1.342724028, -0.914153587, 0.299061266, -0.250486214, 0.737590583, 0.756490732, 0.036946399, 0.140385036, -0.580046072, 0.555265172, 1.360027542, -0.187222927, 0.234652971, -0.219516338, -0.553960981, -1.647646316, -1.256315821, -0.402255588, 0.599705118, 0.456554631, 0.318656334, -0.129187139, -0.457280856, 0.804589041, -0.226353797, -0.607352388, -0.500003784, 0.355984098, -2.078812916, 0.742494279, -0.025085145, 1.098444399, 0.257873499, -1.616850593, -0.338835537, -1.992437295, -1.100449910, -2.340542208, -1.421434876, -1.769595208, -0.950332343, -0.552472269, 1.946223275, -0.151641249, 0.313764561, 1.446756468, 0.872802862, -2.571511435, 0.090367192, -0.263220582, 1.863611625, -0.289483915, -0.287714300, -2.187486878, -0.254317060, 1.585145418, 0.314982071, -0.560313572, 0.497099337, 0.222331451, 0.608450338, 0.338204649, 1.002988020, -0.036485909, 0.662555705, -0.362960351, -0.751492098, -0.515036268, -1.534777331, 1.414871191, -0.247055066, 0.666222753, 0.197870803, -0.299867153, 0.322871114, -1.163860003, 1.554955843, -0.921863710, 0.099947002, -0.651641803, 0.652059365, -0.594189229, 0.693459826, 0.394878722, -0.636618525, -1.317132596, -0.970587556, -0.828753324, -0.441575544, -0.847716395, 0.389558567, -1.258459226, 0.117777763, 1.279213436, -1.432999722, 0.445863536, 0.249532963, 1.127665564, 0.262876528, -0.204464659, -0.554586722, 0.337522350, 0.571370735, 0.685032360, -0.469612838, -0.962008715, 1.476499037, 1.755399126, -0.912351928, -0.157771168, -0.505098499, -0.959532805, 0.734831522, -0.745222443, 0.479253174, 0.564983206, -0.379909659, -0.469186217, 0.991709538, -0.507128202, 0.404648063, -0.206473819, -1.401855387, 0.278305862, 2.179118269, -0.030916654, -0.056820055, -0.081848828, 1.620440954, -0.998128009, -0.297234787, -0.276256970, -0.433611858, 0.822905144, -2.625692197, -1.859911233, -0.724606258, 0.732061910, 0.456557936, 0.361253935, -1.263465417, -0.402400574, -1.434166271, 2.825945657, -0.419643494, -0.290618353, 0.483620649, -0.075656513, -1.755978678, 1.314380488, 1.220747601, 0.371526021, -0.124408480, -0.988218148, 0.536842773, -0.632156842, 0.513217247, -0.801490645, 0.120907798, 1.492462059, -1.757192003, 0.746446587, -0.808819184, 0.494215646, 0.937675128, 0.913795055, -1.368555477, 1.391533574, -0.484386738, 0.690148047, 0.391624791, -0.460161879, -0.573166212, -0.159234679, -0.119194243, -0.352438146, -0.406355996, -1.031605579, -1.893957953, -0.056131215, 0.715196489, -0.099027767, 0.514298816, -0.346565219, -0.150498561, 0.255211147, -0.698901067, -0.052785371, -0.756986062, -0.393162772, -0.459390202, 0.579428861, 1.239617620, -1.287043212, 1.027964252, -0.754240761, -1.532410676, -0.433458844, 0.360369913, -1.539039854, 0.752706489, 1.813625756, -0.480155110, -2.085317960, 0.204431278, 2.444312808, -1.268728882, -0.935941462, 1.160068077, 0.406620281, -0.869926888, -0.901472651, -0.639139160, -1.068802996, -0.941339137, -1.856367294, -0.749149735, -0.938641328, 0.109811133, 0.216543457, 1.098584302, 0.046340908, -0.172913144, 1.243623750, 0.808379288, 0.041934289, 1.260819719, 0.544675343, 0.250088400, 0.921761106, 0.829128636, 0.065866161, -0.096423017, 1.297791577, 1.670212191, -0.850891331, -0.442759329, -0.014150350, 0.927137035, 0.219989310, -0.436489576, 1.366364580, 0.266942428, 1.679798894, 0.102225277, -0.090904514, 0.728086678, -0.438472844, 1.544956418, -0.574884056, 0.664183377, 0.287881602, -0.179136272, 1.379760009, -1.033023821, -0.145807429, -0.971836523, -1.103099048, 1.548697242, 0.951357580, 1.127513799, 0.956937042, 1.077986344, -0.230493566, 1.228312813, 0.530949502, 0.054913524, -0.918122765, -1.167990917, -0.039054439, -0.385677427, 0.082099318, -0.482486777, -0.743897409, -0.959592913, 0.547332017, 1.371863235, 0.987074353, 0.511032879, -0.991687209, -0.043033161, -1.898728874, 0.271699786, 0.541129186, 0.450067028, -0.564255928, 0.664138359, -0.593324022, 0.687474031, 0.204298531, -1.096752977, -1.417701113, -0.375530421, 1.205066484, -1.165862284, 1.059708771, -2.377487658, 0.637041611, 0.863331462, 0.068418540, 0.635301331, 1.553962931, 1.380384061, -0.039181127, 1.213221380, 1.010607940, -0.475636917, 0.590974227, -0.344408812, -0.333591577, 0.824837361, -0.430856654, -0.901159258, 0.045890839, -0.323768457, -1.367546528, 0.182311312, -1.289844646, -1.745502295, -1.096524320, -0.831204925, -0.346317154
## 4                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    8.766951, 9.916208, 10.681969, 10.219824, 9.645698, 8.456056, 9.358497, 9.758353, 8.364799, 9.109842
## 5                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            10.013276, 9.003234, 9.512473, 11.482904, 11.333754, 10.315594, 9.413221, 11.293851, 10.210573, 10.187081, 9.665132, 9.496611, 10.560666, 9.773229, 10.153726, 8.875782, 9.153158, 9.885102, 10.854091, 10.983281, 10.655535, 9.503668, 10.584081, 7.790547, 10.526621, 11.374476, 9.846156, 11.977505, 10.055137, 10.618027, 9.557403, 9.179893, 10.014511, 10.474342, 12.148060, 10.496651, 9.737687, 9.603052, 10.551177, 9.860411, 9.778223, 10.018564, 9.430931, 9.605745, 9.242329, 8.761382, 9.045070, 12.114589, 8.112512, 9.703065, 9.452200, 9.743365, 8.979015, 7.920891, 8.748625, 8.239042, 9.038123, 9.309724, 11.849483, 11.587624, 9.478112, 10.662735, 9.902571, 10.214461, 10.948827, 9.532308, 9.654804, 10.799529, 10.110343, 10.797373, 9.635561, 11.961176, 10.827048, 10.583447, 9.932555, 9.140787, 10.404370, 10.377485, 10.133418, 10.110438, 11.311388, 10.994017, 11.139376, 10.903522, 10.795338, 8.786192, 9.812807, 11.832230, 10.455167, 7.551468, 10.001574, 11.247531, 9.731868, 11.096129, 10.351853, 9.679640, 10.562622, 7.635083, 11.651947, 10.016915
## 6                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                           9.477562, 9.977058, 9.974844, 10.008179, 11.608159, 8.374145, 11.025970, 10.467922, 10.118501, 9.279936, 9.962589, 8.683943, 9.662898, 8.854335, 12.082842, 11.528198, 10.467551, 10.110976, 8.608957, 9.239243, 10.561875, 8.423583, 9.583281, 9.796159, 10.623297, 9.184807, 9.846544, 11.691508, 10.299703, 8.526404, 10.895011, 10.637559, 6.873112, 10.878282, 10.667601, 10.505379, 10.785710, 9.480026, 8.585067, 10.806939, 8.954733, 9.392333, 9.756607, 10.356257, 10.797369, 9.103217, 9.457823, 10.826402, 9.584278, 11.003623, 8.607599, 8.807024, 8.461442, 10.469930, 11.145573, 9.062749, 11.278614, 12.919709, 7.796748, 9.067483, 11.695434, 9.680086, 10.495174, 10.215256, 10.227871, 9.803502, 9.701477, 12.647164, 9.265913, 8.890708, 9.835611, 11.063336, 10.297175, 9.738232, 10.264158, 9.716277, 10.908181, 8.388173, 11.448555, 9.000950, 11.301788, 10.781935, 9.718257, 9.815015, 10.605263, 9.550405, 10.367422, 9.817029, 9.064936, 11.030010, 11.267156, 10.099500, 11.293414, 7.220752, 9.966569, 9.966144, 9.837681, 10.331614, 9.680197, 10.522291, 10.375178, 9.045589, 10.567563, 9.862556, 9.237693, 9.429200, 8.403389, 10.614783, 9.951034, 11.234820, 10.523631, 11.951781, 10.464530, 9.041958, 9.349689, 8.668057, 10.447433, 10.714184, 9.779572, 10.830988, 8.411627, 11.346225, 9.453912, 8.375343, 9.477883, 9.197599, 10.102880, 8.535413, 9.500280, 9.170030, 9.840671, 10.617267, 10.977821, 10.839845, 9.303911, 12.123752, 10.890116, 10.509766, 9.514757, 10.723855, 11.083165, 10.041750, 10.898951, 11.980302, 9.171282, 10.615049, 10.748416, 10.457392, 9.150742, 8.866135, 10.928472, 8.454130, 9.625054, 9.816677, 7.346588, 11.109343, 10.804379, 8.494355, 10.410231, 9.527726, 9.158129, 9.483268, 9.644262, 10.300256, 11.273300, 9.889437, 9.105915, 11.434448, 9.931584, 9.563840, 10.327901, 11.697838, 10.425846, 9.984231, 11.538808, 7.756544, 9.689935, 10.179971, 9.374801, 10.953818, 10.104893, 9.708048, 9.608734, 11.552856, 9.614431, 9.108512, 9.735640, 10.505052, 9.381372, 9.556536, 9.318158, 8.893354, 9.819397, 10.220769, 9.338889, 10.909214, 11.341672, 10.554844, 11.340981, 8.065340, 9.712870, 8.956329, 8.628486, 8.589137, 11.424984, 8.921048, 11.948042, 9.974631, 9.918264, 9.721483, 9.414146, 7.735448, 9.481529, 10.385333, 8.394366, 8.703584, 10.337384, 9.363762, 12.030218, 10.002434, 10.033665, 9.079645, 9.251287, 10.380921, 8.910145, 10.520859, 11.088967, 8.801548, 7.865491, 9.731099, 11.071408, 8.361920, 11.007890, 10.855314, 7.983821, 9.647599, 11.357988, 8.604580, 10.485563, 10.618192, 10.516689, 10.398124, 10.101865, 9.890380, 10.676917, 10.417483, 10.629173, 10.854915, 9.353603, 9.756364, 11.078696, 8.957220, 9.464640, 9.866547, 10.621835, 10.160295, 10.865933, 10.438742, 9.473533, 8.547007, 8.743030, 11.406013, 10.000415, 8.737515, 9.483523, 10.053220, 10.218004, 9.373738, 10.245062, 9.829824, 9.804809, 9.139024, 9.720202, 9.481660, 10.374143, 9.828244, 11.030852, 9.509621, 9.899924, 9.058790, 10.616638, 9.087714, 11.369178, 10.886867, 9.530914, 8.655659, 10.709440, 10.214579, 7.496464, 8.099766, 10.303902, 9.851182, 9.924839, 10.359090, 10.188894, 10.317724, 10.532389, 10.096718, 11.224760, 10.715237, 10.376329, 8.629746, 9.779219, 7.587239, 10.337707, 9.335903, 9.540416, 9.809032, 9.191135, 9.664903, 9.822669, 9.664891, 11.746400, 10.931075, 10.559912, 10.713799, 10.929169, 9.200900, 10.533869, 9.444188, 9.601617, 10.152938, 7.996364, 10.363335, 10.527915, 10.200442, 8.753844, 9.921353, 10.498439, 10.614009, 10.731923, 8.207377, 9.596588, 9.892893, 9.564749, 8.304073, 9.995899, 10.342967, 11.603626, 9.900089, 9.326121, 9.486064, 11.761325, 10.264157, 10.320654, 9.345746, 8.885696, 8.203427, 9.347462, 11.504827, 10.832491, 9.133868, 9.993324, 10.004427, 10.607414, 9.559897, 9.174931, 9.832111, 9.717593, 9.788197, 10.201523, 11.164702, 10.575093, 11.419238, 8.906236, 9.112049, 9.696263, 9.770257, 9.496419, 10.625385, 9.425075, 9.386025, 9.757357, 7.737303, 10.799894, 7.913607, 8.862294, 10.219134, 12.092253, 9.311706, 9.305460, 9.463501, 10.525033, 8.881583, 9.667052, 10.316318, 9.571801, 9.140278, 10.174949, 10.087803, 9.112264, 9.101806, 10.979977, 10.623635, 9.968905, 10.216783, 11.197280, 10.317132, 10.605772, 10.319681, 9.766572, 10.563078, 9.203115, 10.750117, 10.298165, 12.072843, 11.060393, 10.299479, 9.491145, 8.029435, 10.809034, 10.373499, 11.190284, 10.426747, 10.684293, 8.837594, 10.813157, 10.226372, 9.031395, 10.152485, 8.951677, 10.944782, 9.601706, 7.974729, 10.681787, 10.166822, 10.008238, 8.442486, 8.756618, 10.455850, 10.771917, 11.395517, 9.901488, 10.734908, 9.151000, 10.474453, 10.315320, 9.423482, 11.235143, 9.973593, 9.581985, 10.805422, 12.951618, 11.222901, 11.398180, 12.305573, 10.084300, 11.163184, 8.060712, 8.273327, 9.722170, 11.868268, 10.381129, 10.352963, 9.652463, 9.063947, 9.999939, 7.745706, 11.439191, 11.421585, 10.669235, 10.231542, 11.555473, 9.290163, 9.781588, 10.641631, 8.870737, 9.961228, 10.152471, 10.701095, 9.639386, 9.704847, 8.386438, 9.691039, 9.438302, 8.536975, 10.906786, 10.331116, 10.154511, 10.270707, 9.894782, 8.825470, 9.255693, 9.376458, 10.090437, 10.873842, 10.040061, 9.124264, 9.896380, 9.561775, 10.300410, 11.182623, 9.645314, 9.351918, 10.330386, 8.953582, 11.008343, 10.041948, 10.033368, 9.465004, 11.628076, 8.932290, 9.738400, 10.904389, 11.735299, 8.935832, 11.498027, 8.665653, 10.428195, 9.138071, 9.790693, 8.903969, 8.541032, 9.621205, 10.250937, 9.508771, 9.691424, 10.625531, 10.400719, 9.986159, 11.507922, 9.741586, 9.992386, 9.086492, 11.473888, 10.591698, 11.612968, 10.445655, 9.798401, 7.816810, 9.942985, 8.956580, 12.240080, 10.313457, 8.634876, 7.862968, 9.488418, 10.373834, 10.828103, 10.791451, 10.223457, 9.524728, 9.679661, 10.033102, 9.302010, 11.536446, 12.405076, 9.773102, 11.309863, 9.993004, 11.655022, 7.520774, 10.562319, 9.882691, 9.605436, 10.646069, 10.575667, 10.922361, 8.153279, 9.519408, 9.468581, 11.826312, 10.380124, 8.934935, 10.841604, 9.829076, 10.211765, 10.069486, 11.048851, 10.397219, 10.662034, 9.872268, 10.480888, 8.696845, 10.026067, 8.651362, 10.423995, 9.405661, 10.099827, 11.009172, 10.084169, 11.182502, 12.687428, 9.190498, 10.309547, 9.957170, 8.638909, 10.191562, 10.485705, 9.704820, 9.184108, 9.722958, 10.790946, 11.816174, 9.590653, 11.683918, 11.470011, 9.364330, 10.839935, 10.518452, 9.779177, 8.702325, 8.601475, 9.243668, 11.130121, 11.072347, 10.042778, 9.050042, 9.956540, 9.363137, 9.126041, 10.585496, 8.711752, 9.802268, 10.947973, 8.113576, 10.716731, 10.240010, 11.140043, 8.682857, 10.659638, 9.287213, 8.874848, 9.758189, 10.730460, 10.648399, 9.869584, 11.398486, 9.528981, 9.786899, 10.365807, 10.503342, 10.650460, 10.021140, 8.420704, 10.262457, 9.414031, 10.192640, 11.216565, 11.489888, 8.178023, 9.675902, 9.976851, 10.497954, 10.281422, 10.105191, 10.877848, 9.124498, 8.974505, 9.916633, 8.549409, 7.325130, 10.092245, 9.448228, 10.186502, 9.103377, 9.651142, 8.683039, 9.143402, 9.667670, 9.637887, 10.816128, 8.562564, 8.427163, 9.674526, 9.859896, 9.093664, 10.838368, 7.628519, 9.601675, 9.702483, 10.275522, 11.018597, 9.792508, 8.248438, 9.142949, 10.974740, 11.884209, 10.666975, 9.749937, 10.134521, 12.280345, 8.581153, 10.410645, 10.363688, 11.417167, 9.072678, 8.687405, 10.412317, 8.300738, 9.599063, 10.825961, 10.764990, 10.657208, 9.734481, 12.979993, 10.298546, 10.531114, 10.923880, 9.977144, 10.200539, 11.881036, 8.644229, 9.094089, 9.832888, 9.355230, 9.279063, 10.812236, 10.676737, 10.927068, 10.397510, 9.024753, 9.811172, 9.975316, 8.659678, 9.442479, 10.249591, 11.253545, 10.902825, 9.415690, 9.634740, 9.684527, 10.735558, 9.743835, 9.921465, 11.782390, 10.903383, 9.969399, 9.156814, 10.780347, 7.337662, 10.778125, 9.949904, 8.578268, 10.985283, 10.429938, 10.406474, 11.369870, 9.229029, 10.558955, 9.572663, 10.498102, 10.660994, 10.045021, 10.416994, 9.012258, 11.532279, 9.923012, 9.392907, 9.268365, 7.560612, 9.928200, 9.816891, 9.860343, 11.235790, 10.908594, 8.222334, 9.511190, 12.003088, 10.091970, 10.331374, 9.823783, 10.683826, 10.024939, 9.224565, 9.837166, 10.671292, 11.425289, 10.713830, 12.579985, 9.545282, 9.716812, 8.313902, 10.825890, 11.161697, 10.220967, 9.470631, 9.947429, 8.411280, 10.770040, 9.383231, 9.092753, 8.809386, 10.467792, 9.727430, 8.349651, 11.305646, 11.391385, 10.493144, 9.845840, 9.063731, 9.861723, 9.767982, 11.091891, 8.712233, 8.042582, 10.743650, 9.503386, 9.963313, 9.593963, 10.732379, 9.246339, 10.623622, 8.008479, 10.608146, 11.060495, 9.660293, 9.181529, 9.990229, 10.006011, 10.716400, 11.374642, 8.699556, 9.429442, 9.818635, 9.177998, 10.625990, 11.779092, 10.772575, 8.789054, 10.124886, 10.447428, 9.158372, 10.297997, 9.746652, 9.194531, 9.898574, 11.858540, 10.354109, 10.337246, 9.537548, 10.080415, 8.941232, 9.243643, 9.216450, 9.601460, 10.705705, 11.495498, 8.021209, 9.158023, 10.049786, 10.206002, 9.840382, 10.511339, 9.808722, 11.863674, 9.851566, 11.984424, 10.493033, 10.675728, 10.967017, 11.181461, 9.747133, 10.370004, 9.544052, 9.266302, 8.167258, 10.631599, 9.849817, 9.868850, 10.728361, 9.354710, 9.377939, 9.746100, 10.747862, 8.989380, 8.827305, 8.801978, 8.741015, 9.664542, 9.901717, 10.263631, 8.744049, 10.478964, 10.073194, 9.711056, 9.535023, 9.083965, 10.719378, 10.296844, 9.908362, 11.610282, 11.501248, 10.353716, 9.060216, 9.125652, 8.508152, 9.665984, 10.560817, 10.389446, 10.203933, 8.585846, 9.693076, 7.806456, 9.228678, 11.206045, 10.004210, 10.685133, 9.748971, 9.791968, 9.124395, 10.691426, 9.306298, 8.556914, 10.978633, 9.896028, 9.410492, 9.842168, 10.518184, 11.587089, 9.659977, 10.336470, 9.374351, 9.564199, 11.336254, 8.955792, 10.069912, 10.463644, 9.205037, 9.419180, 13.292354, 9.610713, 11.908050, 9.764769, 9.553787, 9.374184, 11.168603, 10.753956, 10.773290, 11.030094, 10.064411, 11.533500, 10.279346, 9.003859, 9.223373, 9.322732, 11.003284, 8.708577, 10.406839, 8.904925, 9.654805, 10.380845, 9.686316, 9.462291, 10.772549, 10.241606, 9.285133, 8.187873, 11.320723, 9.729517, 9.495353, 10.425702, 10.051868, 10.073336, 8.943924, 9.461441, 9.502669, 10.456348, 9.804450, 10.214987, 9.282813, 9.017329, 10.041324, 10.057059, 9.969523, 8.797695, 10.320788, 9.092741, 11.278159, 8.739611, 9.243788, 10.262926, 8.625632, 11.958114, 10.113955, 9.505054, 9.376113, 12.324662, 9.860883, 11.368752, 8.683030, 10.769603, 9.829992, 9.413043, 9.627115, 11.885099, 10.511566, 9.654177, 9.146754, 10.104255, 10.237020, 9.180521, 10.166853, 10.734459, 8.077281, 10.786672, 10.074116, 9.709398, 11.252524, 8.617186

10.2 Marieke de Haan

10.2.1 data

library(haven)
haan <- read_sav("https://stulp.gmw.rug.nl/Rworkshop/haan.sav")
library(skimr)
skim(haan)

10.2.2 accessing the labels from SPSS

This turns most variables into factors, also the ones you may not want to (likert scales)

haan <- as_factor(haan)
skim(haan)
library(tidyverse)
haan_summ <- haan %>% 
  group_by(Q8) %>% 
  # calculate sample size of each group
  summarise(n = n()) %>% 
  # calculate total n + percentages per group
  mutate(tot_n = sum(n),
         perc = round(100 * n / tot_n)) %>% 
  # remove na
  filter(!is.na(Q8))

ggplot(haan_summ, aes(x = Q8, y = n, fill = Q8)) +
  geom_bar(stat = "identity") +
  geom_text(aes(label = paste0(perc, "%")), hjust = 1, colour = "white", size = 8) +
  theme_linedraw() +
  scale_fill_viridis_d() +
  coord_flip() +
  guides(fill = "none") +
  labs(x = NULL, title = "Vul jij meestal cursusevaluaties in?") +
  theme(panel.grid.minor = element_blank(),
        panel.grid.major.y = element_blank())

10.2.3 reshaping data

# select three variables with same measurement
haan_sel <- haan %>% 
  select(Q13, Q14, Q15)

# go from wide to long
haan_long <- haan_sel %>% 
  pivot_longer(cols = c(Q13, Q14, Q15), names_to = "question", values_to = "answer") %>% 
  filter(!is.na(answer)) # exclude NA

ggplot(haan_long, aes(y = answer)) +
  geom_bar(fill = "grey") +
  theme_classic() +
  facet_wrap(~question)

ggplot(haan_long, aes(x = question, fill = answer)) +
  geom_bar(position = "fill") +
  theme_classic() +
  scale_fill_viridis_d() +
  coord_flip()

10.3 Wietske de Vries

10.3.1 data

vries <- read_sav("https://stulp.gmw.rug.nl/Rworkshop/vries.sav") %>% 
  as_factor()
library(skimr)
skim(vries)
library(lme4)
# empty model, random intercept
null_mod <- lmer(Self_efficacy ~ 1 + (1 | sub_ID), data = vries)
summary(null_mod)
## Linear mixed model fit by REML ['lmerMod']
## Formula: Self_efficacy ~ 1 + (1 | sub_ID)
##    Data: vries
## 
## REML criterion at convergence: 1762.5
## 
## Scaled residuals: 
##     Min      1Q  Median      3Q     Max 
## -5.5184 -0.3095  0.0804  0.4563  2.8046 
## 
## Random effects:
##  Groups   Name        Variance Std.Dev.
##  sub_ID   (Intercept) 8.170    2.858   
##  Residual             4.561    2.136   
## Number of obs: 352, groups:  sub_ID, 133
## 
## Fixed effects:
##             Estimate Std. Error t value
## (Intercept)  35.2133     0.2742   128.4
# List of fitted intercepts (estimate average self_efficacy for each respondent)
head(coef(null_mod)$sub_ID) # alternatively:  ranef(null_mod)[["sub_ID"]]
##   (Intercept)
## 1    38.40594
## 2    37.84386
## 3    32.50941
## 4    37.56282
## 5    31.66100
## 6    39.24905
# all fixed effectsl, random intercept
mod_fe <- lmer(Self_efficacy ~ 1 + time + Group + Biomed_beliefs + (1 | sub_ID), data = vries)
summary(mod_fe)
## Linear mixed model fit by REML ['lmerMod']
## Formula: Self_efficacy ~ 1 + time + Group + Biomed_beliefs + (1 | sub_ID)
##    Data: vries
## 
## REML criterion at convergence: 1749.7
## 
## Scaled residuals: 
##     Min      1Q  Median      3Q     Max 
## -5.1893 -0.3698  0.0683  0.4544  2.5142 
## 
## Random effects:
##  Groups   Name        Variance Std.Dev.
##  sub_ID   (Intercept) 9.022    3.004   
##  Residual             4.095    2.024   
## Number of obs: 352, groups:  sub_ID, 133
## 
## Fixed effects:
##                         Estimate Std. Error t value
## (Intercept)             33.71864    0.64287  52.450
## timepost-measure         0.34428    0.26948   1.278
## timefollow-up           -0.49680    0.28350  -1.752
## Groupexperimental group -0.34269    0.58499  -0.586
## Biomed_beliefs           0.08559    0.03067   2.791
## 
## Correlation of Fixed Effects:
##             (Intr) tmpst- tmfll- Grpxpg
## timepst-msr  0.062                     
## timefollw-p -0.093  0.452              
## Grpxprmntlg -0.235  0.082  0.040       
## Biomed_blfs -0.755 -0.319 -0.108 -0.249
# all fixed effectsl, random intercept, random slope of Biomed_beliefs
mod_rs <- lmer(Self_efficacy ~ 1 + time + Group + Biomed_beliefs + (1 + Biomed_beliefs | sub_ID), data = vries)
summary(mod_rs)
## Linear mixed model fit by REML ['lmerMod']
## Formula: 
## Self_efficacy ~ 1 + time + Group + Biomed_beliefs + (1 + Biomed_beliefs |  
##     sub_ID)
##    Data: vries
## 
## REML criterion at convergence: 1711.3
## 
## Scaled residuals: 
##     Min      1Q  Median      3Q     Max 
## -3.8355 -0.3406  0.0413  0.4184  2.7887 
## 
## Random effects:
##  Groups   Name           Variance Std.Dev. Corr 
##  sub_ID   (Intercept)    35.90404 5.9920        
##           Biomed_beliefs  0.05607 0.2368   -0.88
##  Residual                 2.78444 1.6687        
## Number of obs: 352, groups:  sub_ID, 133
## 
## Fixed effects:
##                         Estimate Std. Error t value
## (Intercept)             34.51437    0.78502  43.966
## timepost-measure         0.25599    0.23798   1.076
## timefollow-up           -0.41104    0.25026  -1.642
## Groupexperimental group -0.05147    0.57267  -0.090
## Biomed_beliefs           0.03278    0.03723   0.880
## 
## Correlation of Fixed Effects:
##             (Intr) tmpst- tmfll- Grpxpg
## timepst-msr  0.032                     
## timefollw-p -0.085  0.477              
## Grpxprmntlg -0.163  0.024 -0.001       
## Biomed_blfs -0.848 -0.177 -0.034 -0.230
# save estimates per respondent of intercept and slope of Biomed_beliefs into dataframe
df <- ranef(mod_rs)[["sub_ID"]]

ggplot(df, aes(x = `(Intercept)`, y = Biomed_beliefs)) +
  geom_point() +
  geom_smooth(method = "lm") +
  theme_minimal() +
  labs(
    x = "per respondent fitted intercept of self-efficacy",
    y = "per respondent fitted slope of\nbiomedical beliefs on self-efficacy",
    title = "respondents that had higher levels of self-efficacy,\nhad lower effects of biomedical beliefs on self-efficacy"
  )

10.4 Goda Perlaviciute

library(haven)
goda <- read_sav("https://stulp.gmw.rug.nl/Rworkshop/goda.sav") %>% 
  as_factor()
library(skimr)
skim(goda)

10.4.1 visualisation

library(tidyverse)
goda_sel <- goda %>% 
  select(Q1, Q9) %>% 
  rename(pre = "Q1",
         post = "Q9") %>% 
  filter(!is.na(pre) & !is.na(post))

10.4.1.1 option 1

goda_sel_summ <- goda_sel %>% 
  group_by(pre, post) %>% 
  summarise(n = n()) %>%
  group_by(pre) %>%
  mutate(n_tot = sum(n),
         prop = n / n_tot)

ggplot(goda_sel_summ, aes(x = pre, y = post, fill = prop)) +
  geom_tile() +
  geom_text(aes(label = paste0(round(100 * prop), "%")), colour = "white", size = 5 ) +
  scale_fill_viridis_c() +
  coord_fixed() +
  scale_x_discrete(expand = c(0, 0)) +
  labs(
    x = "opinion prior to intervention",
    y = "opinion post intervention"
  ) + 
  theme_minimal() +
  theme(axis.text.x = element_text(angle = 25, hjust = 1),
        panel.grid.major = element_blank())

#### option 2 See https://ggforce.data-imaginist.com/reference/geom_parallel_sets.html

library(ggforce)
temp <- reshape2::melt(Titanic)
temp <- gather_set_data(temp, 1:4)

# select only relevant variables
goda_sankey <- goda_sel_summ %>% 
  select(pre, post, n) 

# code from ggforce package
goda_sankey <- gather_set_data(goda_sankey, 1:2)


ggplot(goda_sankey, aes(x, id = id, split = y, value = n)) +
  geom_parallel_sets(aes(fill = pre), alpha = 0.3, axis.width = 0.1) +
  geom_parallel_sets_axes(axis.width = 0.1) +
  geom_parallel_sets_labels(colour = 'white', angle = 0, nudge_x = 0, hjust = 0.5) +
  scale_fill_brewer(palette = "Set1") +
  scale_x_continuous(limits = c(0.8, 2.2), expand = c(0, 0)) +
  coord_cartesian(clip = "off") +
  theme_void() +
  guides(fill = "none") +
  theme(panel.background = element_rect(colour = "grey77", fill = "grey77"))

10.5 Thomas de Lang

[I am absolutely not knowledgeable about structure equation modelling or confirmatory factor analyses. This was helpful to me: https://lavaan.ugent.be/tutorial/tutorial.pdf]

library(lavaan)

There is this dataset mpg on cars in the ggplot package, let’s use it for a confirmatory factor analysis. There are three variables that measure efficiency of the car, hwy, cty, and displ

library(ggplot2)
data <- mpg

# model specifications
cars_model_spec <- ' efficiency =~ hwy + cty + displ'

# fit model
fit <- cfa(cars_model_spec, data = data)
summary(fit, fit.measures = TRUE)
## lavaan 0.6-12 ended normally after 82 iterations
## 
##   Estimator                                         ML
##   Optimization method                           NLMINB
##   Number of model parameters                         6
## 
##   Number of observations                           234
## 
## Model Test User Model:
##                                                       
##   Test statistic                                 0.000
##   Degrees of freedom                                 0
## 
## Model Test Baseline Model:
## 
##   Test statistic                               811.079
##   Degrees of freedom                                 3
##   P-value                                        0.000
## 
## User Model versus Baseline Model:
## 
##   Comparative Fit Index (CFI)                    1.000
##   Tucker-Lewis Index (TLI)                       1.000
## 
## Loglikelihood and Information Criteria:
## 
##   Loglikelihood user model (H0)              -1405.396
##   Loglikelihood unrestricted model (H1)      -1405.396
##                                                       
##   Akaike (AIC)                                2822.792
##   Bayesian (BIC)                              2843.524
##   Sample-size adjusted Bayesian (BIC)         2824.506
## 
## Root Mean Square Error of Approximation:
## 
##   RMSEA                                          0.000
##   90 Percent confidence interval - lower         0.000
##   90 Percent confidence interval - upper         0.000
##   P-value RMSEA <= 0.05                             NA
## 
## Standardized Root Mean Square Residual:
## 
##   SRMR                                           0.000
## 
## Parameter Estimates:
## 
##   Standard errors                             Standard
##   Information                                 Expected
##   Information saturated (h1) model          Structured
## 
## Latent Variables:
##                    Estimate  Std.Err  z-value  P(>|z|)
##   efficiency =~                                       
##     hwy               1.000                           
##     cty               0.745    0.019   39.903    0.000
##     displ            -0.181    0.010  -18.893    0.000
## 
## Variances:
##                    Estimate  Std.Err  z-value  P(>|z|)
##    .hwy               2.930    0.558    5.247    0.000
##    .cty               0.064    0.271    0.234    0.815
##    .displ             0.599    0.058   10.389    0.000
##     efficiency       32.376    3.289    9.843    0.000