Presentation is loading. Please wait.

Presentation is loading. Please wait.

Multilevel Analysis With R

Similar presentations


Presentation on theme: "Multilevel Analysis With R"— Presentation transcript:

1 Multilevel Analysis With R 26.04.2016
Nayla Sokhn

2 Session 3: Multilevel Analysis
Together! Concepts of Multilevel Data Distinguish the fixed and random predictors Random Intercept Model Random Slope Model Random intercept and Slope Model Group Mean and Grand Mean « Once you know that hierarchies exist, you see them everywhere» [Kreft and Lueew 1998]

3 Multilevel or Hierarchical Data
Data are often hierarchical. This means that some variables are clustered or nested within other groups. Level 2 : Class Multilevel session This is me! This is you! Students are nested/clustered within classes Level 1 students We may want to analyze the Performance (Response variable/Dependent variable) of each student with respect to the gender (Explanatory variable of independent variable)!

4 Multilevel or Hierarchical Data
session This is me! This is you! This is not me! Multilevel session This is not you! This is not me! This is not me! Multilevel session Multilevel session This is not you! This is not you! - The Performance of each student can be influenced by the class (different teachers!) - Students from the same class are more likely to behave the same, they are taught by the same teacher! - Residuals for outcomes from the same unit (class) are likely to be related, which violates the general linear model “independence” assumption

5 Multilevel or Hierarchical Data (Repeated Measures!)
Subject 1 Subject 2 Subject 3 Subject 4 Trial 1 Trial 2 Trial 3 Trial 10 Trial 1 Trial 2 Trial 3 Trial 1 Trial 2 Trial 3 Trial 4 Trial 5 Trial 1 Trial 2 Trial 3 Trial 4

6 2. Benefits of multilevel Data
1. We Can cope with «Missing» data  Class 1 Class 2 6 students 9 students We can cope with the dependency of data  (OLS ordinary least square cannot! ) We can include categorical or continuous predictors at any level  (Student level (Gender) and Class Level (Teaching Method))

7 3. Fixed and Random Predictors
3.1 Fixed Predictors A predictor is fixed when the levels under study are the only levels of interest. Example: gender, treatment conditions (Placebo, low dose,high dose) 3.2 Random Predictors A predictor is random when the levels under study are a random sample from a larger population. Example: subjects, schools

8 4.3 The random intercept and slope model
4. Models 4.1 The random intercept model 4.2 The random slope model 4.3 The random intercept and slope model

9 4.1 The random intercept model
Random intercept means that the value of intercept will vary accross groups But slope would remain the same accross groups 1. Each line represents a group. In this following figure we have 4 lines (four groups) 2. Lines are parallel because the slope is fixed! [

10 4.1 The random slope model (Rarely used)
Random slope means that the value of slope will vary accross groups But interecept would remain the same accross groups 1. Each line represents a group. In this following figure we have 4 lines (four groups) 2. Lines are crossing at the interecept because the interecept is fixed! [

11 4.1 The random slope and intercept model
Random intercept and slope means that both slope and interecept will vary accross groups 1. Each line represents a group. In this following figure we have 4 lines (four groups) 2. Lines have different slope and interecept [

12 A Quick Parenthese (Linear regression)
1. Unconditional Model (one level, no predictors) 𝐲 :𝐭𝐡𝐞 𝐫𝐞𝐬𝐩𝐨𝐧𝐬𝐞 𝐯𝐚𝐫𝐢𝐚𝐛𝐥𝐞 x : the predictor variable 𝜷 𝟎 : the intercept 𝜷 𝟏 : the slope 𝜺 𝒊 : the residuals 𝒊 : the individual/case of the data that we are studying (level 1) 𝒚 𝒊 = 𝜷 𝟎 + 𝜺 𝒊 2. Model with one predictor (one level) 𝒚 𝒊 = 𝜷 𝟎 + 𝜷 𝟏 𝒙 𝒊 + 𝜺 𝒊 Linear regression Level 1 students We don’t have a second level! Only working for one class! i: refers to level 1, if we have 8 students i goes from 1 to 8

13 Multilevel scenario (Two levels)
j Level 2 : Class Multilevel session Level 1 students i We have a second level! We are dealing with several classes! i: refers to level 1, if we have 8 students i goes from 1 to 8 j: refers to level 2, if we have 5 class, j goes from 1 to 5 Two levels  two equations and two indexes (i) for Level 1 and (j) for Level 2

14 1. Random Intercept Model (two levels : two equations!)
Multilevel Analaysis with R Equation for Level 1 𝒚 𝒊𝒋 = 𝜷 𝟎𝒋 + 𝜺 𝒊𝒋 𝐲 :𝐭𝐡𝐞 𝐫𝐞𝐬𝐩𝐨𝐧𝐬𝐞 𝐯𝐚𝐫𝐢𝐚𝐛𝐥𝐞 x : the predictor variable 𝜷 𝟎 : the intercept 𝜺 𝒊 : the residuals 𝒊 : the individual/case of the data that we are studying (level 1) 𝒋 : the level 2 group. 𝝁 𝟎𝒋 : the residuals 𝜸 𝟎𝟎 : Mean intercept Equation for Level 2 𝜷 𝟎𝒋 = 𝜸 𝟎𝟎 + 𝝁 𝟎𝒋 Estimated intercept for group 4 and 5 (school for example) 𝜷 𝟎𝟒 = 𝜸 𝟎𝟎 + 𝝁 𝟎𝟒 𝜷 𝟎𝟓 = 𝜸 𝟎𝟎 + 𝝁 𝟎𝟓 Grouping level 1 and 2 together gives the following equation 𝒚 𝒊𝒋 = 𝜸 𝟎𝟎 + 𝝁 𝟎𝒋 + 𝜺 𝒊𝒋 𝒚 𝒊𝒋 = 𝜸 𝟎𝟎 + 𝝁 𝟎𝒋 + 𝜺 𝒊𝒋 β0j Fixed part Random part β0j is the intercept of yij for each of the j level 2 𝜺 ij residuals, deviation of each observation of the group from the mean of that group γ00 is the mean of the means u0j deviation of the intercept of each group from this mean

15 2. Random intercept model with one predictor for Level 1
Multilevel Analaysis with R Equation for Level 1 𝒚 𝒊𝒋 = 𝜷 𝟎𝒋 + 𝜷 𝟏𝒋 𝒙 𝒊 +𝜺 𝒊𝒋 𝐲 :𝐭𝐡𝐞 𝐫𝐞𝐬𝐩𝐨𝐧𝐬𝐞 𝐯𝐚𝐫𝐢𝐚𝐛𝐥𝐞 x : the predictor variable 𝜷 𝟎 : the intercept 𝜷 𝟏 : the slope 𝜺 𝒊 : the residuals 𝒊 : the individual/case of the data that we are studying (level 1) 𝒋 : the level 2 group. 𝝁 𝟎𝒋 : the residuals 𝜸 𝟎𝟎 : Mean intercept Equation for Level 2 𝜷 𝟎𝒋 = 𝜸 𝟎𝟎 + 𝝁 𝟎𝒋 𝜷 𝟏𝒋 = 𝜸 𝟏𝟎 Grouping level 1 and 2 together gives the following equation 𝒚 𝒊𝒋 = 𝜸 𝟎𝟎 + 𝝁 𝟎𝒋 + 𝜸 𝟏𝟎 + 𝜺 𝒊𝒋 β0j β1j 𝒚 𝒊𝒋 = 𝜸 𝟎𝟎 + 𝜸 𝟏𝟎 + 𝝁 𝟎𝒋 +𝜺 𝒊𝒋 Fixed Part Random Part

16 3. Random intercept and slope model with one predictor for Level 1
Multilevel Analaysis with R Equation for Level 1 𝒚 𝒊𝒋 = 𝜷 𝟎𝒋 + 𝜷 𝟏𝒋 𝒙 𝒊 +𝜺 𝒊𝒋 𝐲 :𝐭𝐡𝐞 𝐫𝐞𝐬𝐩𝐨𝐧𝐬𝐞 𝐯𝐚𝐫𝐢𝐚𝐛𝐥𝐞 x : the predictor variable 𝜷 𝟎 : the intercept 𝜷 𝟏 : the slope 𝜺 𝒊 : the residuals 𝒊 : the individual/case of the data that we are studying (level 1) 𝒋 : the level 2 group. 𝝁 𝟎𝒋 : the residuals 𝜸 𝟎𝟎 : Mean intercept Equation for Level 2 𝜷 𝟎𝒋 = 𝜸 𝟎𝟎 + 𝝁 𝟎𝒋 𝜷 𝟏𝒋 = 𝜸 𝟏𝟎 + 𝝁 𝟏𝒋 Grouping level 1 and 2 together gives the following equation 𝒚 𝒊𝒋 = 𝜸 𝟎𝟎 + 𝝁 𝟎𝒋 + 𝜸 𝟏𝟎 𝜺 𝒊𝒋 + 𝝁 𝟏𝒋 + β0j β1j 𝒚 𝒊𝒋 = 𝜸 𝟎𝟎 + 𝜸 𝟏𝟎 + 𝝁 𝟎𝒋 + 𝝁 𝟏𝒋 +𝜺 𝒊𝒋 Fixed Part Random Part

17 Multilevel Analaysis with R
FUN GAME!!! Multilevel Analaysis with R We will split the class in two groups! One group (A) will have to take one blue and orange paper The other group (B) will have to take one red and green and paper 2. Each person of Group A has to match the papers with a person of Group B

18 Multilevel Analaysis with R
A less FUN GAME! Multilevel Analaysis with R 20 students 60 classes Gender «Female» and «Male» Grade of students in multilevel course Are we in a Multilevel data scenario? What is your level 1 and level 2? What is your predictor? What is your dependent variable/response variable? 5. Write the Random Intercept Model with 0 predictor 6. Write the Random Intercept Model with one predictor 7. Write the Random Intercept and slope Model with one predictor

19 Multilevel Analaysis with R
Recap of session 3 Multilevel Analaysis with R Level 1 equations are represented with Betas (𝜷) Coefficients of intercept are subscripted 0 𝜷 𝟎𝒋 Coefficients for the first predictor subscripted 1 𝜷 𝟏𝒋 Coefficients for the second predictor subscripted 2 𝜷 𝟐𝒋 etc… The level 1 observations are modeled as a function of the mean for each group (Intercept 𝜷 𝟎𝒋 )…. …. and the deviation of each observation in a group from the mean of that group (residuals 𝜺 𝒊𝒋 ) 𝜷 𝟏𝒋 represents the within-group relationship between the predictor «1» and the dependent variable (y) 𝜷 𝟐𝒋 represents the within-group relationship between the predictor «2» and the dependent variable (y) Level 2 equations are represented with Gammas (𝜸)!

20 An Example with 0 predictor!
Multilevel Analaysis with R We have 3 Schools and students are nested within these schools We have the gender as an explanatory variable for students for level 1 Unconditional Model Random intercept Model with one predictor Unconditional Model Equation for Level 1 𝒚 𝒊𝒋 = 𝜷 𝟎𝒋 +𝜺 𝒊𝒋 Let’s dot it in R! Equation for Level 2 𝜷 𝟎𝒋 = 𝜸 𝟎𝟎 + 𝝁 𝟎𝒋

21 An Example with 0 predictor using R! ( Unconditional Model)
Multilevel Analaysis with R We have 3 Schools and students are nested within these schools We have the gender as an explanatory variable for students for level 1 The data has is not a realistic one and is found in the file data.csv on moodle. Equation for Level 1 𝒚 𝒊𝒋 = 𝜷 𝟎𝒋 +𝜺 𝒊𝒋 Equation for Level 2 𝜷 𝟎𝒋 = 𝜸 𝟎𝟎 + 𝝁 𝟎𝒋 𝝁 𝟎𝒋 𝜺 𝒊𝒋 Grouping equation 1 and equation 2 𝜸 𝟎𝟎 𝒚 𝒊𝒋 = 𝜸 𝟎𝟎 + 𝝁 𝟎𝒋 + 𝜺 𝒊𝒋 Fixed part Random part 𝜸 𝟎𝟎 is the mean of the means of all groups 𝝁 𝟎𝒋 is the deviation of the intercept (mean) from each group from the mean ( 𝜸 𝟎𝟎 )

22 An Example with 1 predictor! (Random Intercept Model)
Multilevel Analaysis with R We have 3 Schools and students are nested within these schools We have the gender as an explanatory variable for students for level 1 Equation for Level 1 𝒚 𝒊𝒋 = 𝜷 𝟎𝒋 + 𝜷 𝟏𝒋 ∗𝑮𝒆𝒏𝒅𝒆𝒓+𝜺 𝒊𝒋 Equation for Level 2 𝜷 𝟎𝒋 = 𝜸 𝟎𝟎 + 𝝁 𝟎𝒋 𝜷 𝟏𝒋 = 𝜸 𝟏𝟎 𝝁 𝟎𝒋 𝜺 𝒊𝒋 Grouping equation 1 and equation 2 𝒚 𝒊𝒋 = 𝜸 𝟎𝟎 + 𝜸 𝟏𝟎 + 𝝁 𝟎𝒋 + 𝜺 𝒊𝒋 𝜸 𝟎𝟎 𝜸 𝟏𝟎 Fixed part Random part 𝜸 𝟎𝟎 is the average of the means of all schools 𝝁 𝟎𝒋 is the deviation of the intercept (mean) from each school from the mean ( 𝜸 𝟎𝟎 ) 𝜸 𝟏𝟎 is the average slope, i.e. the relationship between the dependent variable (y) and gender across all school)

23 An Example with 1 predictor! (Random Intercept and Slope)
Multilevel Analaysis with R We have 3 Schools and students are nested within these schools We have the gender as an explanatory variable for students for level 1 Equation for Level 1 𝒚 𝒊𝒋 = 𝜷 𝟎𝒋 + 𝜷 𝟏𝒋 ∗𝑮𝒆𝒏𝒅𝒆𝒓+𝜺 𝒊𝒋 Equation for Level 2 𝜷 𝟎𝒋 = 𝜸 𝟎𝟎 + 𝝁 𝟎𝒋 𝝁 𝟎𝒋 𝝁 𝟏𝒋 𝜷 𝟏𝒋 = 𝜸 𝟏𝟎 + 𝝁 𝟏𝒋 𝜺 𝒊𝒋 Grouping equation 1 and equation 2 𝜸 𝟎𝟎 𝜸 𝟏𝟎 𝒚 𝒊𝒋 = 𝜸 𝟎𝟎 + 𝜸 𝟏𝟎 + 𝝁 𝟎𝒋 + 𝝁 𝟏𝒋 + 𝜺 𝒊𝒋 𝜸 𝟎𝟎 is the average of the means of all schools 𝝁 𝟎𝒋 is the deviation of the intercept (mean) from each school from the mean ( 𝜸 𝟎𝟎 ) Fixed part Random part 𝜸 𝟏𝟎 is the average slope, i.e. the relationship between the dependent variable (y) and gender across all school)

24 Now it is your turn to try it with R «by-yourself»!
Multilevel Analaysis with R Read the file «data_session4.txt» in R Use str to see the structure of your file (name of column and type: factor,numeric) Is gender is a categorical variable? Does R know about that? Change the name of the categories of Gender» 0 becomes Male and 1 becomes Female Run the unconditional model (random interecept model with 0 predictor) Run the random intercept model with one predictor Retrieve the fixed and random coefficients What do you notice regarding the slope of the random intercept model Run the random and slope intercept model with one predictor What do you notice regarding the slope of the random intercept and slope model

25 Continuous Predictors (Centering)
Intercept is the outcome value (value of response variable) when all predictors are equal to 0. In some cases the value 0 for x does not make any sense Example: You are studying the weight of people with respect to the age. You obtain the following equation WEIGHT = *AGE 30.6 represents the value (intercept) when age has a value of 0! «0» is not a meaningful value in that case. Therefore it is wise to center the variable «Age» In general we center the variable by subtracting around the mean’s predictor or around a meaningful constant C Age Centered Age Person Person Take home message: If all continuous predictors have a meaningful value of 0. DO NOT CENTER 25

26 Group MEAN vs Grand MEAN centering
Multilevel Analaysis with R Grand Mean centering We take each value of Variable A and substract from it the mean of all values of variable A Mean of Variable A : ( )/6 = 3.5 Example Subjects Variable A Conditions Subject Condition X Subject Condition X Subject Condition X Subject Condition Y Subject Condition Y Subject Condition Y Grand Mean Centering of Variable A 5-3.5 3-3.5 2-3.5 4-3.5

27 Group MEAN vs Grand MEAN centering
Multilevel Analaysis with R Group Mean centering We take each value of Variable A and substract from it the mean of values within the groups of variable A Mean of Variable for group X: (5+4+3)/3 = 4 Mean of variable for group Y : (2+3+4)/3 = 3 Example Subjects Variable A Conditions Subject Condition X Subject Condition X Subject Condition X Subject Condition Y Subject Condition Y Subject Condition Y Group Mean Centering of Variable A 5-4 3-4 2-3 3-3 4-3

28 Group MEAN vs Grand MEAN centering
Multilevel Analaysis with R 1. Centering using grand or group mean? Level 1 variables are usually centered using group mean Level 2 variables are usually centered using grand mean 2. To center or not to center this is the question  - There is not realy a straightforwatd answer! - It depends on the context of the data We can center around a meaningful value C (c.f the slide page 19)

29 Let’s test the group and grand mean on the LungCap data!
Multilevel Analaysis with R Load again the lungCap Data Try to do the grand mean for lungCap! Try to do the group mean of LungCap: we consider two groups: Smoker and Not Smoker


Download ppt "Multilevel Analysis With R"

Similar presentations


Ads by Google