0

I fitted a linear mixed effect model using lme4, however, it is a hierarchical structure of data and I have two different data set from level 1 and level 2, but I was struggling to include a variable from both data sets in a linear mixed effect model.

Here is an example: dt1 is a data set for males and females students in schools a,b and c (they are in order in my data like a,a,a,b,b,b ...). The outcome y is the final test score which is a continuous variable.

> dt1
# A tibble: 9 x 3
  School gender     y
  <chr>  <chr>  <dbl>
1 a      m          1
2 b      F          3
3 c      m          5
4 a      F          4
5 b      m          2
6 c      F          1
7 a      m          4
8 b      F          3
9 c      m          1

In dt2, q and w are variables at the school level

> dt2
# A tibble: 3 x 3
  School     q     w
  <chr>  <dbl> <dbl>
1 a          2   8  
2 b          4   2.5
3 c          4   5 

I was able to run MLM from dt1 as follow:

lmer(y~gender +(1|School), data= dt1)

But how can I include a variable from dt2 in the previous model?

I've tried this but it didn't work:

lmer(y~ gender+dt2$q[dt2$q==q] +(1|School), data = dt1)

Any advice, please?

Hani
  • 35
  • 4
  • 1
    It's easier to help you if you include a simple [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) with sample input and desired output that can be used to test and verify possible solutions. – MrFlick Aug 07 '20 at 01:15
  • 1
    you probably want to use `merge` (base R) or `full_join` (dplyr) to combine the two data sets, then go from there ... – Ben Bolker Aug 07 '20 at 01:34
  • `dtcomb = merge(dt1, dt2, by ="School")` – Ben Bolker Aug 07 '20 at 16:08

0 Answers0