## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ dplyr 1.1.1 ✔ readr 2.1.4
## ✔ forcats 1.0.0 ✔ stringr 1.5.0
## ✔ ggplot2 3.4.2 ✔ tibble 3.2.1
## ✔ lubridate 1.9.2 ✔ tidyr 1.3.0
## ✔ purrr 1.0.1
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag() masks stats::lag()
## ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
## Loading required package: Matrix
##
##
## Attaching package: 'Matrix'
##
##
## The following objects are masked from 'package:tidyr':
##
## expand, pack, unpack
##
##
##
## Attaching package: 'psych'
##
##
## The following objects are masked from 'package:ggplot2':
##
## %+%, alpha
##
##
##
## Attaching package: 'MASS'
##
##
## The following object is masked from 'package:dplyr':
##
## select
##
##
##
## arm (Version 1.13-1, built: 2022-8-25)
##
##
## Working directory is /Users/user/Desktop/R - Spring
##
##
##
## Attaching package: 'arm'
##
##
## The following objects are masked from 'package:psych':
##
## logit, rescale, sim
# Lets start by reading in the Radon data
load("radon.Rdata")
# Now Read in the data and create factor variables for Haptic Search
hs_data <- read.csv("MLM_haptic_example_data.csv", header = TRUE)
hs_data <- as_tibble(hs_data) %>% mutate(Sub_Num = factor(Subject_Num))
hs_data <- hs_data %>% mutate(Ratio = factor(Relative_Size))
hs_data <- hs_data %>% mutate(Num_Dis = factor(Distractor_Number))
We need to make the appropriate changes to the radon data
# start by creating a county sample size column
county_n <- tibble(table(radon$county))
county_n <- county_n %>% mutate(county = 1:85)
county_n <- county_n %>% mutate(N = table(radon$county))
#put all data frames into list
df_list <- list(radon, radon_county, county_n)
#merge all data frames in list
radon_full <-df_list %>% reduce(full_join, by='county')
Just as a refresher lets go over the main model from chapter 12
vi_radon <- lmer(y ~ 1 + x + (1|county), data = radon_full)
display(vi_radon)
## lmer(formula = y ~ 1 + x + (1 | county), data = radon_full)
## coef.est coef.se
## (Intercept) 1.46 0.05
## x -0.69 0.07
##
## Error terms:
## Groups Name Std.Dev.
## county (Intercept) 0.33
## Residual 0.76
## ---
## number of obs: 919, groups: county, 85
## AIC = 2179.3, DIC = 2156
## deviance = 2163.7
vi_haptics <- lmer(Times ~ 1 + Distractor_Lengths + (1|Distractor_Number), data = hs_data)
display(vi_haptics)
## lmer(formula = Times ~ 1 + Distractor_Lengths + (1 | Distractor_Number),
## data = hs_data)
## coef.est coef.se
## (Intercept) 12.25 0.71
## Distractor_Lengths 2.46 0.32
##
## Error terms:
## Groups Name Std.Dev.
## Distractor_Number (Intercept) 1.05
## Residual 10.14
## ---
## number of obs: 3370, groups: Distractor_Number, 3
## AIC = 25191.7, DIC = 25184.2
## deviance = 25183.9
Answer: For the radon model, when floor level is held constant the radon level is 1.46. When going from the basement to the first floor there is a .69 decrease in radon. There is a .33 change in radon across counties? For the haptics model, when holding constant the distractor lengths reaction time is 12.25 seconds. For every one unit increase in distractor length there is a 2.46 second increase in reaction time. There is a 1.05 second difference in reaction time for every one unit increase in the number of distractors?
ranef() and the fixef() functions provide the
coefficient estimates for at least 3 of the groups.Answer:
ranef(vi_radon)
## $county
## (Intercept)
## 1 -0.27009750
## 2 -0.53395107
## 3 0.01761646
## 4 0.04290331
## 5 -0.01544759
## 6 0.01858386
## 7 0.39652761
## 8 0.22117571
## 9 -0.30152320
## 10 0.04701206
## 11 -0.02935302
## 12 0.11555412
## 13 -0.22454606
## 14 0.37642528
## 15 -0.05909970
## 16 -0.21829868
## 17 -0.08923455
## 18 -0.24065633
## 19 -0.11533676
## 20 0.12243536
## 21 0.16951571
## 22 -0.44040759
## 23 -0.02065353
## 24 0.39897419
## 25 0.35196058
## 26 -0.09891042
## 27 0.16066845
## 28 -0.11482866
## 29 -0.14661001
## 30 -0.36162037
## 31 0.27135836
## 32 -0.09691159
## 33 0.25819715
## 34 0.03993398
## 35 -0.37456628
## 36 0.40649202
## 37 -0.66877366
## 38 0.16875953
## 39 0.13639435
## 40 0.36445852
## 41 0.30203292
## 42 -0.01597290
## 43 0.07888623
## 44 -0.24162113
## 45 -0.12407819
## 46 -0.11990235
## 47 -0.16204982
## 48 -0.19922711
## 49 0.16784892
## 50 0.16376017
## 51 0.30257145
## 52 0.16847763
## 53 -0.07951423
## 54 -0.12876622
## 55 0.08786324
## 56 -0.13694655
## 57 -0.37382399
## 58 0.16880050
## 59 0.10598884
## 60 -0.04992385
## 61 -0.26205479
## 62 0.25045135
## 63 0.07226395
## 64 0.25896934
## 65 -0.04451816
## 66 0.13666918
## 67 0.23659674
## 68 -0.22351243
## 69 -0.09426076
## 70 -0.57164915
## 71 0.02131895
## 72 0.07732481
## 73 0.09046140
## 74 -0.20412162
## 75 0.09142952
## 76 0.23225113
## 77 0.20269435
## 78 -0.09074583
## 79 -0.36716012
## 80 -0.12111867
## 81 0.44445030
## 82 0.12198051
## 83 0.11008961
## 84 0.12903524
## 85 -0.07536845
##
## with conditional variances for "county"
fixef(vi_radon)
## (Intercept) x
## 1.4615979 -0.6929937
group 1 : 1.4615979 + -0.27009750 group 71 : 1.4615979 + 0.02131895 group 85 : 1.4615979 + -0.07536845
coef(vi_radon)
## $county
## (Intercept) x
## 1 1.1915004 -0.6929937
## 2 0.9276468 -0.6929937
## 3 1.4792143 -0.6929937
## 4 1.5045012 -0.6929937
## 5 1.4461503 -0.6929937
## 6 1.4801817 -0.6929937
## 7 1.8581255 -0.6929937
## 8 1.6827736 -0.6929937
## 9 1.1600747 -0.6929937
## 10 1.5086099 -0.6929937
## 11 1.4322449 -0.6929937
## 12 1.5771520 -0.6929937
## 13 1.2370518 -0.6929937
## 14 1.8380232 -0.6929937
## 15 1.4024982 -0.6929937
## 16 1.2432992 -0.6929937
## 17 1.3723633 -0.6929937
## 18 1.2209415 -0.6929937
## 19 1.3462611 -0.6929937
## 20 1.5840332 -0.6929937
## 21 1.6311136 -0.6929937
## 22 1.0211903 -0.6929937
## 23 1.4409443 -0.6929937
## 24 1.8605721 -0.6929937
## 25 1.8135585 -0.6929937
## 26 1.3626875 -0.6929937
## 27 1.6222663 -0.6929937
## 28 1.3467692 -0.6929937
## 29 1.3149879 -0.6929937
## 30 1.0999775 -0.6929937
## 31 1.7329562 -0.6929937
## 32 1.3646863 -0.6929937
## 33 1.7197950 -0.6929937
## 34 1.5015319 -0.6929937
## 35 1.0870316 -0.6929937
## 36 1.8680899 -0.6929937
## 37 0.7928242 -0.6929937
## 38 1.6303574 -0.6929937
## 39 1.5979922 -0.6929937
## 40 1.8260564 -0.6929937
## 41 1.7636308 -0.6929937
## 42 1.4456250 -0.6929937
## 43 1.5404841 -0.6929937
## 44 1.2199767 -0.6929937
## 45 1.3375197 -0.6929937
## 46 1.3416955 -0.6929937
## 47 1.2995481 -0.6929937
## 48 1.2623708 -0.6929937
## 49 1.6294468 -0.6929937
## 50 1.6253580 -0.6929937
## 51 1.7641693 -0.6929937
## 52 1.6300755 -0.6929937
## 53 1.3820836 -0.6929937
## 54 1.3328317 -0.6929937
## 55 1.5494611 -0.6929937
## 56 1.3246513 -0.6929937
## 57 1.0877739 -0.6929937
## 58 1.6303984 -0.6929937
## 59 1.5675867 -0.6929937
## 60 1.4116740 -0.6929937
## 61 1.1995431 -0.6929937
## 62 1.7120492 -0.6929937
## 63 1.5338618 -0.6929937
## 64 1.7205672 -0.6929937
## 65 1.4170797 -0.6929937
## 66 1.5982671 -0.6929937
## 67 1.6981946 -0.6929937
## 68 1.2380854 -0.6929937
## 69 1.3673371 -0.6929937
## 70 0.8899487 -0.6929937
## 71 1.4829168 -0.6929937
## 72 1.5389227 -0.6929937
## 73 1.5520593 -0.6929937
## 74 1.2574763 -0.6929937
## 75 1.5530274 -0.6929937
## 76 1.6938490 -0.6929937
## 77 1.6642922 -0.6929937
## 78 1.3708520 -0.6929937
## 79 1.0944378 -0.6929937
## 80 1.3404792 -0.6929937
## 81 1.9060482 -0.6929937
## 82 1.5835784 -0.6929937
## 83 1.5716875 -0.6929937
## 84 1.5906331 -0.6929937
## 85 1.3862294 -0.6929937
##
## attr(,"class")
## [1] "coef.mer"
radon_coef <- fixef(vi_radon) [[1]] + ranef(vi_radon) [[1]]
radon_coef
## (Intercept)
## 1 1.1915004
## 2 0.9276468
## 3 1.4792143
## 4 1.5045012
## 5 1.4461503
## 6 1.4801817
## 7 1.8581255
## 8 1.6827736
## 9 1.1600747
## 10 1.5086099
## 11 1.4322449
## 12 1.5771520
## 13 1.2370518
## 14 1.8380232
## 15 1.4024982
## 16 1.2432992
## 17 1.3723633
## 18 1.2209415
## 19 1.3462611
## 20 1.5840332
## 21 1.6311136
## 22 1.0211903
## 23 1.4409443
## 24 1.8605721
## 25 1.8135585
## 26 1.3626875
## 27 1.6222663
## 28 1.3467692
## 29 1.3149879
## 30 1.0999775
## 31 1.7329562
## 32 1.3646863
## 33 1.7197950
## 34 1.5015319
## 35 1.0870316
## 36 1.8680899
## 37 0.7928242
## 38 1.6303574
## 39 1.5979922
## 40 1.8260564
## 41 1.7636308
## 42 1.4456250
## 43 1.5404841
## 44 1.2199767
## 45 1.3375197
## 46 1.3416955
## 47 1.2995481
## 48 1.2623708
## 49 1.6294468
## 50 1.6253580
## 51 1.7641693
## 52 1.6300755
## 53 1.3820836
## 54 1.3328317
## 55 1.5494611
## 56 1.3246513
## 57 1.0877739
## 58 1.6303984
## 59 1.5675867
## 60 1.4116740
## 61 1.1995431
## 62 1.7120492
## 63 1.5338618
## 64 1.7205672
## 65 1.4170797
## 66 1.5982671
## 67 1.6981946
## 68 1.2380854
## 69 1.3673371
## 70 0.8899487
## 71 1.4829168
## 72 1.5389227
## 73 1.5520593
## 74 1.2574763
## 75 1.5530274
## 76 1.6938490
## 77 1.6642922
## 78 1.3708520
## 79 1.0944378
## 80 1.3404792
## 81 1.9060482
## 82 1.5835784
## 83 1.5716875
## 84 1.5906331
## 85 1.3862294
coefficient estimate instead of calling it slope
lm_radon_1 <- lmer(y ~ 1 + x + (1 + x | county), data = radon_full)
display(lm_radon_1)
## lmer(formula = y ~ 1 + x + (1 + x | county), data = radon_full)
## coef.est coef.se
## (Intercept) 1.46 0.05
## x -0.68 0.09
##
## Error terms:
## Groups Name Std.Dev. Corr
## county (Intercept) 0.35
## x 0.34 -0.34
## Residual 0.75
## ---
## number of obs: 919, groups: county, 85
## AIC = 2180.3, DIC = 2153.9
## deviance = 2161.1
Answer: When measured at basement level radon is 1.46 and when measuring on the first floor there is a .68 decrease in radon level. There is a .35 change in radon across counties. There is a .34 decrease in radon … I am getting confused.
lm_haptics_1 <- lmer(Times ~ 1 + Distractor_Lengths + (1 + Distractor_Lengths|Distractor_Number), data = hs_data)
## boundary (singular) fit: see help('isSingular')
display(lm_haptics_1)
## lmer(formula = Times ~ 1 + Distractor_Lengths + (1 + Distractor_Lengths |
## Distractor_Number), data = hs_data)
## coef.est coef.se
## (Intercept) 12.25 0.39
## Distractor_Lengths 2.45 0.58
##
## Error terms:
## Groups Name Std.Dev. Corr
## Distractor_Number (Intercept) 0.22
## Distractor_Lengths 0.84 1.00
## Residual 10.14
## ---
## number of obs: 3370, groups: Distractor_Number, 3
## AIC = 25191.2, DIC = 25180.1
## deviance = 25179.6
Answer: I am not sure if I did the above model correctly. When distactor length is held constant reaction time is average is 12.25 seconds. For every one unit increase in distractor length there is a 2.45 increase in reaction time.
lm_radon_2 <- lmer(y ~ x + u + x:u + (1 + x | county), data = radon_full, start = list(theta = c(1,2,3)))
display(lm_radon_2)
## lmer(formula = y ~ x + u + x:u + (1 + x | county), data = radon_full,
## start = list(theta = c(1, 2, 3)))
## coef.est coef.se
## (Intercept) 1.47 0.04
## x -0.67 0.08
## u 0.81 0.09
## x:u -0.42 0.23
##
## Error terms:
## Groups Name Std.Dev. Corr
## county (Intercept) 0.12
## x 0.31 0.41
## Residual 0.75
## ---
## number of obs: 919, groups: county, 85
## AIC = 2142.6, DIC = 2101.9
## deviance = 2114.2
the individual level has group level info now (u) so that may be why the correlations is positive and not negative anymore
ranef(lm_radon_2)
## $county
## (Intercept) x
## 1 -0.0099262107 0.024065420
## 2 0.0270474560 -0.218049019
## 3 0.0083394466 0.024347067
## 4 0.0589572534 0.071979801
## 5 0.0106302462 0.047659391
## 6 -0.0204904632 -0.020755635
## 7 0.1128314354 0.349713030
## 8 0.0359035472 0.074232215
## 9 -0.0276578946 0.107934642
## 10 -0.0033557994 -0.057670414
## 11 0.0513478982 0.052012404
## 12 0.0040184236 0.004070427
## 13 0.0233179570 0.023619720
## 14 0.0678184369 0.043988135
## 15 -0.0118190294 -0.016111738
## 16 -0.0206631195 -0.020930526
## 17 -0.0509104094 -0.195282818
## 18 0.0282085837 0.126322476
## 19 -0.0723718125 -0.153633672
## 20 0.0090466619 0.009163737
## 21 0.0337892319 0.081034669
## 22 -0.1526301833 -0.196581462
## 23 -0.0175281654 -0.018651614
## 24 0.0923044696 0.133879112
## 25 0.1099358693 0.332129462
## 26 -0.0245021854 -0.133596012
## 27 -0.0016548424 0.039469700
## 28 -0.0019245124 0.017778378
## 29 0.0148897040 0.015082395
## 30 -0.0015726630 -0.001593015
## 31 0.0365975053 0.037071123
## 32 -0.0187784624 -0.019021479
## 33 0.0500930327 0.050741299
## 34 0.0038833716 -0.018561721
## 35 -0.0238909657 0.007837547
## 36 0.0909039660 0.254164461
## 37 -0.1068452939 -0.114529268
## 38 0.0888506955 0.210051006
## 39 0.0244988568 0.087596051
## 40 0.0537707333 0.075776177
## 41 0.0491772868 0.157781735
## 42 -0.0061275363 -0.006206834
## 43 0.0048802901 -0.277654343
## 44 -0.1113107792 -0.275802654
## 45 -0.0762552578 -0.193453950
## 46 -0.0330973853 -0.033525707
## 47 -0.0501239459 -0.211016030
## 48 -0.0425612009 0.008682654
## 49 -0.0076852839 -0.161588149
## 50 0.0188315470 0.019075251
## 51 0.0492381441 0.049875347
## 52 0.0100351650 0.010165033
## 53 -0.0373918869 -0.081413084
## 54 -0.1411824539 -0.311122356
## 55 0.0525844550 0.141725090
## 56 -0.0437002473 -0.181480642
## 57 -0.0889151044 -0.135739218
## 58 0.0125420515 0.067788786
## 59 -0.0008631441 -0.060442185
## 60 -0.0217486460 -0.022030100
## 61 0.0137937760 0.193355842
## 62 0.0638062351 0.304909288
## 63 0.0099463029 0.107118312
## 64 0.0499157040 0.038592485
## 65 -0.0311383306 -0.031541299
## 66 0.0683792127 0.146769894
## 67 0.0818816631 0.320794936
## 68 0.0313794753 0.031785565
## 69 -0.0414800691 -0.042016873
## 70 -0.1739320519 -0.163149730
## 71 -0.0314643321 -0.118644796
## 72 -0.0230558207 -0.023354192
## 73 -0.0029920249 -0.003030745
## 74 -0.0731586843 -0.074105449
## 75 0.0378867152 0.153029037
## 76 0.0178887706 0.022735055
## 77 0.0229669722 -0.075568657
## 78 0.0215875982 -0.016696628
## 79 -0.1124254069 -0.206877638
## 80 -0.0208938136 -0.177603529
## 81 0.1069696350 0.315079584
## 82 0.0153023275 0.015500359
## 83 -0.0701376586 -0.363391197
## 84 0.0619131455 0.056053178
## 85 -0.0297281779 -0.030112897
##
## with conditional variances for "county"
fixef(lm_radon_2)
## (Intercept) x u x:u
## 1.4685942 -0.6709437 0.8081345 -0.4195150
coef(lm_radon_2)
## $county
## (Intercept) x u x:u
## 1 1.458668 -0.6468783 0.8081345 -0.419515
## 2 1.495642 -0.8889927 0.8081345 -0.419515
## 3 1.476934 -0.6465966 0.8081345 -0.419515
## 4 1.527551 -0.5989639 0.8081345 -0.419515
## 5 1.479224 -0.6232843 0.8081345 -0.419515
## 6 1.448104 -0.6916993 0.8081345 -0.419515
## 7 1.581426 -0.3212306 0.8081345 -0.419515
## 8 1.504498 -0.5967115 0.8081345 -0.419515
## 9 1.440936 -0.5630090 0.8081345 -0.419515
## 10 1.465238 -0.7286141 0.8081345 -0.419515
## 11 1.519942 -0.6189313 0.8081345 -0.419515
## 12 1.472613 -0.6668732 0.8081345 -0.419515
## 13 1.491912 -0.6473240 0.8081345 -0.419515
## 14 1.536413 -0.6269555 0.8081345 -0.419515
## 15 1.456775 -0.6870554 0.8081345 -0.419515
## 16 1.447931 -0.6918742 0.8081345 -0.419515
## 17 1.417684 -0.8662265 0.8081345 -0.419515
## 18 1.496803 -0.5446212 0.8081345 -0.419515
## 19 1.396222 -0.8245773 0.8081345 -0.419515
## 20 1.477641 -0.6617799 0.8081345 -0.419515
## 21 1.502383 -0.5899090 0.8081345 -0.419515
## 22 1.315964 -0.8675251 0.8081345 -0.419515
## 23 1.451066 -0.6895953 0.8081345 -0.419515
## 24 1.560899 -0.5370646 0.8081345 -0.419515
## 25 1.578530 -0.3388142 0.8081345 -0.419515
## 26 1.444092 -0.8045397 0.8081345 -0.419515
## 27 1.466939 -0.6314740 0.8081345 -0.419515
## 28 1.466670 -0.6531653 0.8081345 -0.419515
## 29 1.483484 -0.6558613 0.8081345 -0.419515
## 30 1.467022 -0.6725367 0.8081345 -0.419515
## 31 1.505192 -0.6338726 0.8081345 -0.419515
## 32 1.449816 -0.6899652 0.8081345 -0.419515
## 33 1.518687 -0.6202024 0.8081345 -0.419515
## 34 1.472478 -0.6895054 0.8081345 -0.419515
## 35 1.444703 -0.6631061 0.8081345 -0.419515
## 36 1.559498 -0.4167792 0.8081345 -0.419515
## 37 1.361749 -0.7854729 0.8081345 -0.419515
## 38 1.557445 -0.4608927 0.8081345 -0.419515
## 39 1.493093 -0.5833476 0.8081345 -0.419515
## 40 1.522365 -0.5951675 0.8081345 -0.419515
## 41 1.517771 -0.5131619 0.8081345 -0.419515
## 42 1.462467 -0.6771505 0.8081345 -0.419515
## 43 1.473474 -0.9485980 0.8081345 -0.419515
## 44 1.357283 -0.9467463 0.8081345 -0.419515
## 45 1.392339 -0.8643976 0.8081345 -0.419515
## 46 1.435497 -0.7044694 0.8081345 -0.419515
## 47 1.418470 -0.8819597 0.8081345 -0.419515
## 48 1.426033 -0.6622610 0.8081345 -0.419515
## 49 1.460909 -0.8325318 0.8081345 -0.419515
## 50 1.487426 -0.6518684 0.8081345 -0.419515
## 51 1.517832 -0.6210683 0.8081345 -0.419515
## 52 1.478629 -0.6607786 0.8081345 -0.419515
## 53 1.431202 -0.7523568 0.8081345 -0.419515
## 54 1.327412 -0.9820660 0.8081345 -0.419515
## 55 1.521179 -0.5292186 0.8081345 -0.419515
## 56 1.424894 -0.8524243 0.8081345 -0.419515
## 57 1.379679 -0.8066829 0.8081345 -0.419515
## 58 1.481136 -0.6031549 0.8081345 -0.419515
## 59 1.467731 -0.7313859 0.8081345 -0.419515
## 60 1.446846 -0.6929738 0.8081345 -0.419515
## 61 1.482388 -0.4775878 0.8081345 -0.419515
## 62 1.532400 -0.3660344 0.8081345 -0.419515
## 63 1.478540 -0.5638254 0.8081345 -0.419515
## 64 1.518510 -0.6323512 0.8081345 -0.419515
## 65 1.437456 -0.7024850 0.8081345 -0.419515
## 66 1.536973 -0.5241738 0.8081345 -0.419515
## 67 1.550476 -0.3501487 0.8081345 -0.419515
## 68 1.499974 -0.6391581 0.8081345 -0.419515
## 69 1.427114 -0.7129605 0.8081345 -0.419515
## 70 1.294662 -0.8340934 0.8081345 -0.419515
## 71 1.437130 -0.7895885 0.8081345 -0.419515
## 72 1.445538 -0.6942979 0.8081345 -0.419515
## 73 1.465602 -0.6739744 0.8081345 -0.419515
## 74 1.395436 -0.7450491 0.8081345 -0.419515
## 75 1.506481 -0.5179146 0.8081345 -0.419515
## 76 1.486483 -0.6482086 0.8081345 -0.419515
## 77 1.491561 -0.7465123 0.8081345 -0.419515
## 78 1.490182 -0.6876403 0.8081345 -0.419515
## 79 1.356169 -0.8778213 0.8081345 -0.419515
## 80 1.447700 -0.8485472 0.8081345 -0.419515
## 81 1.575564 -0.3558641 0.8081345 -0.419515
## 82 1.483897 -0.6554433 0.8081345 -0.419515
## 83 1.398457 -1.0343349 0.8081345 -0.419515
## 84 1.530507 -0.6148905 0.8081345 -0.419515
## 85 1.438866 -0.7010566 0.8081345 -0.419515
##
## attr(,"class")
## [1] "coef.mer"