Write R coding & answer questions in the assignment sheet (a total of 9 quesions)
Psychology 516 Name ________________________ Psychology 5068 Hierarchical Linear Models Homework 11 Due May 3, 2021 In class we examined the following model: For this assignment you will extend these analyses to test nonlinearity and interactions. The data for this assignment are contained in the file, Safety_Binary.csv. Some models will not converge and others may encounter singularity problems. Try alternative optimizer settings to eliminate these problems. The following increase the number of iterations that are used before the algorithm gives up and force the optimizer to check closer to parameter space boundaries. cl_a <- glmercontrol(optimizer="bobyqa" ,="" optctrl="list(maxfun=1e9)," check.conv.grad=".makeCC("warning"," tol="1e-3," reltol="NULL)," check.conv.singular=".makeCC(action" =="" "message",="" tol="1e-9)," check.conv.hess=".makeCC(action" =="" "warning",="" tol="1e-6))" cl_b="">-><- glmercontrol(optimizer="Nelder_Mead" ,="" optctrl="list(maxfun=1e9)," check.conv.grad=".makeCC("warning"," tol="1e-3," reltol="NULL)," check.conv.singular=".makeCC(action" =="" "message",="" tol="1e-9)," check.conv.hess=".makeCC(action" =="" "warning",="" tol="1e-6))" cl_c="">-><- glmercontrol(optimizer="optimx" ,="" optctrl="list(method="nlminb",maxiter=1e9)," check.conv.grad=".makeCC("warning"," tol="1e-3," reltol="NULL)," check.conv.singular=".makeCC(action" =="" "message",="" tol="1e-9)," check.conv.hess=".makeCC(action" =="" "warning",="" tol="1e-6))" cl_d="">-><- glmercontrol(optimizer="nloptwrap" ,="" optctrl="list(maxfun=1e9)," check.conv.grad=".makeCC("warning"," tol="1e-3," reltol="NULL)," check.conv.singular=".makeCC(action" =="" "message",="" tol="1e-9)," check.conv.hess=".makeCC(action" =="" "warning",="" tol="1e-6))" you="" will="" need="" the="" optimx="" package="" for="" the="" last="" one.="" switch="" to="" a="" different="" optimizer="" like="" this:="" safety_fit_1="">-><- glmer(unsafe ~ 1 + z_age + z_age_sq + sex + (1 + z_age + z_age_sq + sex|street), data=safety_data, binomial("logit"), control=cl_a) 1. in the data file, two of the variables are named age and crowded. standardize them and name them z_age and z_crowded. create a new variable that is the square of z_age; name it z_age_sq. use the describe function to verify the variables have been created correctly. 2. test a level 1 model that contains z_age, z_age_sq, and sex. level 2 should be unconditional (no predictors) with all residual variances estimated. (a) is there a nonlinear relationship between age and feeling unsafe? (b) is sex a significant predictor? (c) can this model be simplified by eliminating any level 2 variances? you will need to retain at least one variance at level 2, so keep the intercept variance in all models. then carry out tests that eliminate other variances systematically by first eliminating a single level 2 variance at a time. then eliminate pairs of variances. finally eliminate all of the variances (except the intercept). comparing each variation with the fully specified random model, identify the simplest model that is justified by likelihood ratio tests (not significantly different from the full model) and that minimizes aic and bic. 3. carry out a similar series of analyses, but substitute the sex:z_age interaction for the nonlinear age effect. (a) is there an interaction between age and sex? (b) what is the simplest level 2 variance model that can be used? you will need to retain at least one variance at level 2, so keep the intercept variance in all models. then carry out the tests that eliminate other variances systematically by first eliminating a single level 2 variance at a time. then eliminate pairs of variances. finally eliminate all of the variances (except the intercept). comparing each variation with the fully specified random model, identify the simplest model that is justified by likelihood ratio tests (not significantly different from the full model) and that minimizes aic and bic. 4. now add z_crowded to the simplest model from question 3 and include all two-way interactions and the three-way interaction. (a) what is the highest order effect that is significant? (b) plot the highest order significant effect with probability of feeling unsafe as the outcome. (c) provide an interpretation for the plotted effect. glmer(unsafe="" ~="" 1="" +="" z_age="" +="" z_age_sq="" +="" sex="" +="" (1="" +="" z_age="" +="" z_age_sq="" +="" sex|street),="" data="Safety_Data," binomial("logit"),="" control="cl_a)" 1.="" in="" the="" data="" file,="" two="" of="" the="" variables="" are="" named="" age="" and="" crowded.="" standardize="" them="" and="" name="" them="" z_age="" and="" z_crowded.="" create="" a="" new="" variable="" that="" is="" the="" square="" of="" z_age;="" name="" it="" z_age_sq.="" use="" the="" describe="" function="" to="" verify="" the="" variables="" have="" been="" created="" correctly.="" 2.="" test="" a="" level="" 1="" model="" that="" contains="" z_age,="" z_age_sq,="" and="" sex.="" level="" 2="" should="" be="" unconditional="" (no="" predictors)="" with="" all="" residual="" variances="" estimated.="" (a)="" is="" there="" a="" nonlinear="" relationship="" between="" age="" and="" feeling="" unsafe?="" (b)="" is="" sex="" a="" significant="" predictor?="" (c)="" can="" this="" model="" be="" simplified="" by="" eliminating="" any="" level="" 2="" variances?="" you="" will="" need="" to="" retain="" at="" least="" one="" variance="" at="" level="" 2,="" so="" keep="" the="" intercept="" variance="" in="" all="" models.="" then="" carry="" out="" tests="" that="" eliminate="" other="" variances="" systematically="" by="" first="" eliminating="" a="" single="" level="" 2="" variance="" at="" a="" time.="" then="" eliminate="" pairs="" of="" variances.="" finally="" eliminate="" all="" of="" the="" variances="" (except="" the="" intercept).="" comparing="" each="" variation="" with="" the="" fully="" specified="" random="" model,="" identify="" the="" simplest="" model="" that="" is="" justified="" by="" likelihood="" ratio="" tests="" (not="" significantly="" different="" from="" the="" full="" model)="" and="" that="" minimizes="" aic="" and="" bic.="" 3.="" carry="" out="" a="" similar="" series="" of="" analyses,="" but="" substitute="" the="" sex:z_age="" interaction="" for="" the="" nonlinear="" age="" effect.="" (a)="" is="" there="" an="" interaction="" between="" age="" and="" sex?="" (b)="" what="" is="" the="" simplest="" level="" 2="" variance="" model="" that="" can="" be="" used?="" you="" will="" need="" to="" retain="" at="" least="" one="" variance="" at="" level="" 2,="" so="" keep="" the="" intercept="" variance="" in="" all="" models.="" then="" carry="" out="" the="" tests="" that="" eliminate="" other="" variances="" systematically="" by="" first="" eliminating="" a="" single="" level="" 2="" variance="" at="" a="" time.="" then="" eliminate="" pairs="" of="" variances.="" finally="" eliminate="" all="" of="" the="" variances="" (except="" the="" intercept).="" comparing="" each="" variation="" with="" the="" fully="" specified="" random="" model,="" identify="" the="" simplest="" model="" that="" is="" justified="" by="" likelihood="" ratio="" tests="" (not="" significantly="" different="" from="" the="" full="" model)="" and="" that="" minimizes="" aic="" and="" bic.="" 4.="" now="" add="" z_crowded="" to="" the="" simplest="" model="" from="" question="" 3="" and="" include="" all="" two-way="" interactions="" and="" the="" three-way="" interaction.="" (a)="" what="" is="" the="" highest="" order="" effect="" that="" is="" significant?="" (b)="" plot="" the="" highest="" order="" significant="" effect="" with="" probability="" of="" feeling="" unsafe="" as="" the="" outcome.="" (c)="" provide="" an="" interpretation="" for="" the="" plotted="">- glmer(unsafe ~ 1 + z_age + z_age_sq + sex + (1 + z_age + z_age_sq + sex|street), data=safety_data, binomial("logit"), control=cl_a) 1. in the data file, two of the variables are named age and crowded. standardize them and name them z_age and z_crowded. create a new variable that is the square of z_age; name it z_age_sq. use the describe function to verify the variables have been created correctly. 2. test a level 1 model that contains z_age, z_age_sq, and sex. level 2 should be unconditional (no predictors) with all residual variances estimated. (a) is there a nonlinear relationship between age and feeling unsafe? (b) is sex a significant predictor? (c) can this model be simplified by eliminating any level 2 variances? you will need to retain at least one variance at level 2, so keep the intercept variance in all models. then carry out tests that eliminate other variances systematically by first eliminating a single level 2 variance at a time. then eliminate pairs of variances. finally eliminate all of the variances (except the intercept). comparing each variation with the fully specified random model, identify the simplest model that is justified by likelihood ratio tests (not significantly different from the full model) and that minimizes aic and bic. 3. carry out a similar series of analyses, but substitute the sex:z_age interaction for the nonlinear age effect. (a) is there an interaction between age and sex? (b) what is the simplest level 2 variance model that can be used? you will need to retain at least one variance at level 2, so keep the intercept variance in all models. then carry out the tests that eliminate other variances systematically by first eliminating a single level 2 variance at a time. then eliminate pairs of variances. finally eliminate all of the variances (except the intercept). comparing each variation with the fully specified random model, identify the simplest model that is justified by likelihood ratio tests (not significantly different from the full model) and that minimizes aic and bic. 4. now add z_crowded to the simplest model from question 3 and include all two-way interactions and the three-way interaction. (a) what is the highest order effect that is significant? (b) plot the highest order significant effect with probability of feeling unsafe as the outcome. (c) provide an interpretation for the plotted effect.>