################################################################# ## Code for Book: Applied Statistical Analysis in R ## A Graduate course for Psychology, Human factors, and Data Science ## (c) 2018 Shane T. Mueller, Ph.D. ## Michigan Technological University ## shanem@mtu.edu ## ## Textbook and videos available at http://pages.mtu.edu/~shanem/psy5210 ## ## ## This file contains example software code relevant for Chapter 20: ## Within-subject repeated measures ANOVA and randomized factors ## ## ## ## ##The experiment works like this: people visit a web site we have created, and it contains different types of advertisements we have identified. ##For each person, we get end up getting a server log indicating whether they clicked on or hovered their mouse over each type of advertisement. We want to know whether ##the social ads (ones sensitive to their on-line identity) were better than the non-social advertisements. ##Let's suppose there is actually no difference. Suppose that different advertisements we sample from actually differ in their ''true'' click-through ##rate, but not between social versus normal ads. Simulate the population of click-through rates like this: ads.social <- 0:100/100 ads.normal <- 0:100/100 ads.animated <- 150:250/250 #Now, suppose that in an experiment, we have selected four advertisements of each type, and created a web page including all eight #advertisements. In the study, each of 50 people either does or does not follow each link. Ignoring for the moment #within-subject effects, we have a data set like this: set.seed(100) social.eff <- sample(ads.social,4) normal.eff <- sample(ads.normal,4) animated.eff <- sample(ads.animated,4) ##100 of each ad type clicks <- c(runif(50*4)0,] ##First, aggregate by subject, stem, stemcond, dat2 <- aggregate(data$uniquecount,data[,c(2,6,7,1)],max) dat2$lengthcond <- as.factor(dat2$lengthcond) dat2$stemcond <- as.factor(dat2$stemcond) library(lme4) stemmodel<-lmer(x~lengthcond + stemcond + (1|subnum),data=dat2) stemmodel2<-lmer(x~lengthcond + stemcond + (1|subnum)+(1|stem),data=dat2) stemmodel3<-lmer(x~lengthcond * stemcond + (1|subnum)+(1|stem),data=dat2) stemmodel4<-lmer(x~ stemcond + (1|subnum)+(1|stem),data=dat2) anova(stemmodel4,stemmodel2) summary(stemmodel) summary(stemmodel2) summary(stemmodel3) summary(stemmodel4) anova(stemmodel) anova(stemmodel2) anova(stemmodel,stemmodel2) anova(stemmodel2,stemmodel3) plot(ranef(stemmodel)) plot(ranef(stemmodel2)) par(mfrow=c(1,2)) qqnorm(ranef(stemmodel2)$subnum) dotplot(ranef(stemmodel2)) qqmath(ranef(stemmodel2)) plot(ranef(stemmodel2,whichel="subnum")) plot(ranef(stemmodel2,whichel="stem"))