Toxicology data A toxicologist wonders if exposure to a chemical increases mortality as its dose increases. She has exposed test organisms to different doses of a chemical and recorded the number that died in each test tank. Each dose was repeated on three different days. First, build a GLM. Building a simple model makes debugging easier. Also, you will later compare the results to GLMER. This model includes dose and replicate as a fixed-effects. Second, build a GLMER. This time, dose is a fixed-effect and replicate is a random-effects intercept. Last, examine the coefficient estimates with coef() from each model. Notice how the intercept estimates are displayed differently from each model. #Create glm_out, a glm() model that predicts mortality as a function of dose and replicate. #Estimate an intercept for the replicate group using replicate - 1. glm_out <- glm(mortality ~ dose + replicate - 1, family = "binomial", data = df) coef(glm_out) #Create glmer_out, a glmer() model that predicts mortality as a function of dose while accounting #for replicate as a random effect. # Load lmerTest library(lmerTest) glmer_out <- glmer(mortality ~ dose + (1 | replicate), family = "binomial", data = df) coef(glmer_out) Notice how the intercept coefficient estimates for the glmer() outputs are closer together. However, in this case both model estimates are similar and either model would be a tenable choice. ############################################################################################################### Marketing example As described in the video, our client is interested in knowing if a friend's recommendation increases the number of people who buy, rather than pass, on his online product. He has given us a summary of his data as a data.frame called all_data. This data includes the number of Purchases and Passes for 4 test cities (city) as well as the customer ranking. This data structure lends itself to using cbind() on the two columns of interest to create a matrix (You could use other methods of making a matrix in R, but this is one of the easiest methods). You are interested to see if the recommendation from a friend increases people buying the product. To answer this question, you will build a glmer() model and then examine the model's output. If the parameter estimate for friend is significantly greater than zero, then a friend's recommendation increases the chance somebody makes a purchase. If the parameter estimate for friend is significantly less than zero, then a friend's recommendation decreases the chance somebody makes a purchase. If the parameter estimate for friend is not significantly different than zero, then a friend's recommendation has no effect on somebody making a purchase. # Load lmerTest library(lmerTest) #Fit a glmer() with all_data data.frame. Use cbind(Purchases, Pass) being predicted by friend and #ranking (friend goes first). Use city as your your random-effect and family = "binomial". model_out <- glmer(cbind(Purchases, Pass) ~ friend + ranking + (1 | city), family = "binomial", data = all_data) summary(model_out) How did a friend's recommendation (the friendyes coefficient) change the chance of a purchase? The coefficient estimate for slope is positive and significantly different than zero. This indicates a friend’s recommendation increases the chance of purchasing an item. However, describing the coefficient to your friend would be hard. In the next exercise, you will see how odds-ratios can be used to describe logistic regression outputs. ############################################################################################################### Calculating odds-ratios In the previous exercise, we saw how to compare the effects of a friend's recommendation on sales. However, regression outputs can be hard to describe and sometimes odds-ratios can be easier to use. Using the outputs from the previous exercise, we're going to calculate odds-ratios. Refresher on odds-ratios: * If an odds-ratio is 1.0, then both events have an equal chance of occurring. For example, if the odds-ratio for a friend's recommendation was 1.0, then a friend would have no influence on a purchase decision. * If an odds-ratio is less than 1, then a friend's recommendation would decrease the chance of a purchase occurring. For example, an odds-ratio of 0.5 would mean a friend's recommendation has odds of 1:2 or 1 purchase occurring for every 2 passes. * If an odds-ratio is greater than 1, then a friend's recommendation would increase the chance of a purchase occurring. For example, an odds-ratio of 3.0 would mean a friend's recommendation has odds of 3:1 or 3 purchases occurring for every 1 passes. Note on course code: Since this course launched, the broom package has dropped support for lme4::lmer() models. If you try to repeat this on your own, you will need the broom.mixed package, which is on cran. #Extract the coefficients from model_out with fixef() and then convert to an odds-ratio by taking exponential. #Repeat with confint() to get the confidence intervals. summary(model_out) exp(fixef(model_out)) exp(confint(model_out)) #Calculate the confidence intervals and then exponentiate the effect of friends on a purchase using tidy(). #Make sure to set the conf.int and exponentiate parameters. tidy(model_out, conf.int = TRUE, exponentiate = TRUE)