keep <- keep & tmp.data$Rater != "MCC" & tmp.data$rated != "MCC" #initial <- as.formula(paste("rating~ 0+",paste(colnames[c(1:5,7:11)],collapse="+"))) vars4 <- vars3[keep,] initial <- as.formula(paste("rating~ 0+",paste(varnames,collapse="+"))) start <- lm(formula=initial,data=vars4) full <- lm(formula=full.model,data=vars4) base <- lm(formula=base.model,data=vars4) x3 <- stepAIC(start,scope=c(lower=lower.model,upper=upper.model),trace=F,direction="both") cont <- 1 m1 <- x3 lastlogLike <- sum(log(dnorm(m1$residuals,0,summary(m1)$sigma))) cluster <- rep(1,length(m1$residuals)) ## A base rate distribution over the ratings #m2 <- table(vars4$rating)/nrow(vars4) m2 <- rep(1,11)/11 #m2 <- round(m2+1)/11##make it uniform actually. step <- 1 while(cont) { modelLike <- data.frame(m1Like = dnorm(m1$residuals,0,summary(m1)$sigma), m2Like = as.vector(m2)[vars4$rating+1]) cluster <- apply(modelLike,1, which.max) modelLike$maxlike <- modelLike[cbind(1:nrow(modelLike),cluster)] logLike <- sum(log(modelLike$maxlike)) if(logLike > lastlogLike) { ##row, re-run the step model using m1 as the starting point, but ## with mstep==1 as the filter. start <- lm(formula=formula(m1),data=vars4,weights= ((cluster==1)+0)) m1 <- stepAIC(start,scope=c(lower=lower.model,upper=upper.model),trace=F,direction="both")#,k=log(nrow(r.ls.long))) lastlogLike <- logLike cat("Step: [",step,"] log likelihood:",lastlogLike, "->",logLike,"\n") } else{ ## we have not improved, so end the model cycles cont <- 0 } step <- step + 1 } fit <- coef(m1) fit[is.na(fit)] <- 0 rated <- fit[1:5] rater <- fit[6:10] daycoef <- fit[23:147] daynames <- names(daycoef) byday <- data.frame(Day=sapply(daynames, function(x){as.numeric(substr(strsplit(x,'[.]')[[1]][[2]],4,6))}), Rated = sapply(daynames, function(x){strsplit(x,'[.]')[[1]][[1]]}), value = daycoef) byday$ratedval <- rep(rated,each=25) ##hand-code 25 times here. byday$adjusted <- byday$ratedval + byday$value meanrated <- mean(rated) meanrater <- mean(rater) meanday <- mean(byday$value) grandmean <- mean(vars3$rating[keep]) int2 <- grandmean tmp <- data.frame(Member=substr(names(rated),7,9),rating=rated) p.adapt.rated <-ggplot(tmp,aes(Member,y=rating)) + geom_bar(stat="identity")+theme_bw() + ggtitle(paste("Mean rating of each member by others:",dimension)) barplot(rated,names=roles[1:5],main=paste("Mean rating of each member by others:",dimension),col="gold") abline(int2,0) barplot(rater, names=roles[1:5],main=paste("Bias of each rater:",dimension), col="cadetblue4") abline(int2,0) ggplot(byday,aes(x=Day,y=value,group=Rated,color=Rated)) + geom_point()+geom_line() + ggtitle(label=paste(dimension,"per team member over time")) + theme_bw() ggplot(byday,aes(x=Day,y=adjusted,group=Rated,color=Rated)) + geom_point()+geom_line() + ggtitle(label=paste(dimension,"per team member over time (re-meaned)")) + theme_bw() extra <- fit[c(11:21)] pairs.adapt <- data.frame(pair=names(extra), adjustment= extra) ggplot(pairs.adapt,aes(pair,adjustment)) + geom_bar(stat="identity") + theme_bw() + ggtitle(label=paste("Pairwise deviations from model:",dimension)) allout3 <- r.adapt.long[keep,] allout3$value[allout3$Rater==allout3$rated] <- NA allout3$fittedbase <- base$fitted.values allout3$fitted <- m1$fitted.values allout3$fittedfull <- full$fitted.values allout3$cluster <- as.factor(cluster) ggplot(allout3,aes(x=Day,y=value,group=cond,col=Rater)) + geom_line() +geom_point() + theme_bw() + ylim(0,11) + xlim(0,240)+ ggtitle(label=paste(dimension,": As rated by others (data)",sep="")) + facet_wrap(~rated,ncol=6) ggplot(allout3,aes(x=Day,y=fittedbase,group=cond,col=Rater)) + geom_line() + theme_bw() + ylim(0,11) + xlim(0,240)+ ggtitle(label=paste(dimension,": As rated by others (Baseline model)",sep="")) + facet_wrap(~rated,ncol=6) ggplot(allout3,aes(x=Day,y=fittedfull,group=cond,col=Rater)) + geom_line() + theme_bw() + ylim(0,11) + xlim(0,240)+ ggtitle(label=paste(dimension,": As rated by others (Full model)",sep="")) + facet_wrap(~rated,ncol=6) ggplot(allout3,aes(x=Day,y=fitted,group=cond,col=Rater)) + geom_line() + theme_bw() + ylim(0,11) + xlim(0,240)+ ggtitle(label=paste(dimension,": As rated by others (Simplified model)",sep="")) + geom_point()+ geom_point(data=allout3[allout3$cluster==2,],aes(x=Day,y=value),color="red",shape=11) + facet_wrap(~rated,ncol=6) ggplot(allout3,aes(x=value,y=fitted,col=Rater)) + geom_point(aes(shape=cluster)) + theme_minimal() + ggtitle(label=paste("Data vs. fitted model:",dimension)) table(cluster) tmp <- r.adapt.long tmp <- tmp[tmp$Rater != tmp$rated,] tmp <- tmp[tmp$rated != "MCC",] ggplot(tmp,aes(x=Day,y=value,group=cond,col=rated)) + geom_line() + theme_bw() + ylim(0,10) + xlim(0,240)+ ggtitle(label=paste("Outlier detection | ",dimension,": Self-ratings vs. how member rated others",sep="") )+ geom_point(data=allout3[cluster==2,],aes(x=Day,y=value),size=1.5,colour="red",shape=11) + facet_wrap(~Rater,ncol=5) # geom_line(data=self.ls,aes(x=Day,y=value),col="grey20",alpha=.4,size=2) aggregate(allout3$rated,list(Dimension=allout3$Dimension, rater=allout3$Rater,keep=allout3$cluster),length) dimension <- "Shared Mental Model" tmp.data <- r.smm.long vars3 <- vars vars3$rating <- tmp.data$value vars3$CDR.day <- as.character(vars3$Day) vars3$CS.day <- as.character(vars3$Day) vars3$FE.day <- as.character(vars3$Day) vars3$MS1.day <- as.character(vars3$Day) vars3$MS3.day <- as.character(vars3$Day) vars3$CDR.day[vars3$rated.CDR==0] <- 0 vars3$CS.day[vars3$rated.CS==0] <- 0 vars3$FE.day[vars3$rated.FE==0] <- 0 vars3$MS1.day[vars3$rated.MS1==0] <- 0 vars3$MS3.day[vars3$rated.MS3==0] <- 0 varnames <- c(colnames,"CDR.day","CS.day","FE.day","MS1.day","MS3.day") upper.model <- as.formula(paste("~ 0+",paste(varnames,collapse="+"))) lower.model <- as.formula(paste("~ 0+",paste(varnames[c(1:5,7:11, (length(varnames) - (4:0))) ],collapse="+"))) full.model <- as.formula(paste("rating ~ 0+",paste(varnames,collapse="+"))) base.model <- as.formula(paste("rating ~ 0+",paste(varnames[c(1:5,7:11, (length(varnames) - (4:0))) ],collapse="+"))) keep <- !is.na(vars3$rating) keep <- keep & !(tmp.data$Rater == tmp.data$rated) keep <- keep & tmp.data$Rater != "MCC" & tmp.data$rated != "MCC" #initial <- as.formula(paste("rating~ 0+",paste(colnames[c(1:5,7:11)],collapse="+"))) vars4 <- vars3[keep,] initial <- as.formula(paste("rating~ 0+",paste(varnames,collapse="+"))) start <- lm(formula=initial,data=vars4) full <- lm(formula=full.model,data=vars4) base <- lm(formula=base.model,data=vars4) x3 <- stepAIC(start,scope=c(lower=lower.model,upper=upper.model),trace=F,direction="both") cont <- 1 m1 <- x3 lastlogLike <- sum(log(dnorm(m1$residuals,0,summary(m1)$sigma))) cluster <- rep(1,length(m1$residuals)) ## A base rate distribution over the ratings #m2 <- table(vars4$rating)/nrow(vars4) m2 <- rep(1,11)/11 #m2 <- round(m2+1)/11##make it uniform actually. step <- 1 while(cont) { modelLike <- data.frame(m1Like = dnorm(m1$residuals,0,summary(m1)$sigma), m2Like = as.vector(m2)[vars4$rating+1]) cluster <- apply(modelLike,1, which.max) modelLike$maxlike <- modelLike[cbind(1:nrow(modelLike),cluster)] logLike <- sum(log(modelLike$maxlike)) if(logLike > lastlogLike) { ##row, re-run the step model using m1 as the starting point, but ## with mstep==1 as the filter. start <- lm(formula=formula(m1),data=vars4,weights= ((cluster==1)+0)) m1 <- stepAIC(start,scope=c(lower=lower.model,upper=upper.model),trace=F,direction="both")#,k=log(nrow(r.ls.long))) lastlogLike <- logLike cat("Step: [",step,"] log likelihood:",lastlogLike, "->",logLike,"\n") } else{ ## we have not improved, so end the model cycles cont <- 0 } step <- step + 1 } cluster.smm <- apply(modelLike,1, which.max) fit <- coef(m1) fit[is.na(fit)] <- 0 rated <- fit[1:5] rater <- fit[6:10] daycoef <- fit[22:146] daynames <- names(daycoef) byday <- data.frame(Day=sapply(daynames, function(x){as.numeric(substr(strsplit(x,'[.]')[[1]][[2]],4,6))}), Rated = sapply(daynames, function(x){strsplit(x,'[.]')[[1]][[1]]}), value = daycoef) byday$ratedval <- rep(rated,each=25) ##hand-code 25 times here. byday$adjusted <- byday$ratedval + byday$value meanrated <- mean(rated) meanrater <- mean(rater) meanday <- mean(byday$value) grandmean <- mean(vars3$rating[keep]) int2 <- grandmean tmp <- data.frame(Member=substr(names(rated),7,9),rating=rated) p.smm.rated <-ggplot(tmp,aes(Member,y=rating)) + geom_bar(stat="identity")+theme_bw() + ggtitle(paste("Mean rating of each member by others:",dimension)) barplot(rated,names=roles[1:5],main=paste("Mean rating of each member by others:",dimension),col="gold") abline(int2,0) barplot(rater, names=roles[1:5],main=paste("Bias of each rater:",dimension), col="cadetblue4") abline(int2,0) ggplot(byday,aes(x=Day,y=value,group=Rated,color=Rated)) + geom_point()+geom_line() + ggtitle(label=paste(dimension,"per team member over time")) + theme_bw() ggplot(byday,aes(x=Day,y=adjusted,group=Rated,color=Rated)) + geom_point()+geom_line() + ggtitle(label=paste(dimension,"per team member over time (re-meaned)")) + theme_bw() extra <- fit[c(11:20)] pairs.smm <- data.frame(pair=names(extra), adjustment= extra) ggplot(pairs.smm,aes(pair,adjustment)) + geom_bar(stat="identity") + theme_bw() + ggtitle(label=paste("Pairwise deviations from model:",dimension)) allout3 <- r.smm.long[keep,] allout3$value[allout3$Rater==allout3$rated] <- NA allout3$fittedbase <- base$fitted.values allout3$fitted <- m1$fitted.values allout3$fittedfull <- full$fitted.values allout3$cluster <- as.factor(cluster) ggplot(allout3,aes(x=Day,y=value,group=cond,col=Rater)) + geom_line() +geom_point() + theme_bw() + ylim(0,11) + xlim(0,240)+ ggtitle(label=paste(dimension,": As rated by others (data)",sep="")) + facet_wrap(~rated,ncol=6) ggplot(allout3,aes(x=Day,y=fittedbase,group=cond,col=Rater)) + geom_line() + theme_bw() + ylim(0,11) + xlim(0,240)+ ggtitle(label=paste(dimension,": As rated by others (Baseline model)",sep="")) + facet_wrap(~rated,ncol=6) ggplot(allout3,aes(x=Day,y=fittedfull,group=cond,col=Rater)) + geom_line() + theme_bw() + ylim(0,11) + xlim(0,240)+ ggtitle(label=paste(dimension,": As rated by others (Full model)",sep="")) + facet_wrap(~rated,ncol=6) ggplot(allout3,aes(x=Day,y=fitted,group=cond,col=Rater)) + geom_line() + theme_bw() + ylim(0,11) + xlim(0,240)+ ggtitle(label=paste(dimension,": As rated by others (Simplified model)",sep="")) + geom_point()+ geom_point(data=allout3[allout3$cluster==2,],aes(x=Day,y=value),color="red",shape=11) + facet_wrap(~rated,ncol=6) ggplot(allout3,aes(x=value,y=fitted,col=Rater)) + geom_point(aes(shape=cluster)) + theme_minimal() + ggtitle(label=paste("Data vs. fitted model:",dimension)) table(cluster) tmp <- r.smm.long tmp <- tmp[tmp$Rater != tmp$rated,] tmp <- tmp[tmp$rated != "MCC",] ggplot(tmp,aes(x=Day,y=value,group=cond,col=rated)) + geom_line() + theme_bw() + ylim(0,10) + xlim(0,240)+ ggtitle(label=paste("Outlier detection | ",dimension,": Self-ratings vs. how member rated others",sep="") )+ geom_point(data=allout3[cluster==2,],aes(x=Day,y=value),size=1.5,colour="red",shape=11) + facet_wrap(~Rater,ncol=5) # geom_line(data=self.ls,aes(x=Day,y=value),col="grey20",alpha=.4,size=2) aggregate(allout3$rated,list(Dimension=allout3$Dimension, rater=allout3$Rater,keep=allout3$cluster),length) library(gridExtra) grid.arrange(p.trust.rated, p.torient.rated, p.comm.rated, p.backup.rated, p.monitor.rated, p.adapt.rated, p.ls.rated, p.smm.rated, nrow=4) p.trust.rated p.torient.rated p.comm.rated p.backup.rated p.monitor.rated p.adapt.rated p.ls.rated library(ggnetwork) library(network) library(sna) pairs.smm$dimension <- "SMM" pairs.comm$dimension <- "COMM" pairs.ls$dimension <- "Leadership" pairs.trust$dimension <- "Trust" pairs.backup$dimension <- "Backup" pairs.adapt$dimension <- "Adaptability" pairs.torient$dimension <- "Team orientation" pairs.monitor$dimension <- "Performance monitoring" pairs.all <- rbind(pairs.smm,pairs.comm,pairs.ls,pairs.trust, pairs.backup,pairs.adapt,pairs.torient,pairs.monitor) members <-sapply(pairs.all$pair, function(x){strsplit(x,'[.]')[[1]]}) pairs.all$rated <- factor( members[1,],levels=c("CDR","CS","FE","MS1","MS3")) pairs.all$rater <- factor(members[2,],levels=c("CDR","CS","FE","MS1","MS3")) pairs.tab <- tapply(pairs.all$adjustment,list(pairs.all$rater,pairs.all$rated,pairs.all$dimension), function(x){tmp <- mean(x)}) pairs.tab[is.na(pairs.tab)] <- 0 library(tidyr) ##convert back to a data frame pairs.all2 <-as.data.frame.table(pairs.tab) colnames(pairs.all2) <- c("rater","rated","dimension","adjustment") pairs.aug <- rbind(pairs.all, data.frame(pair=rep("",40), adjustment=rep(NA,40), dimension=rep(unique(pairs.all$dimension),5), rated=rep(levels(pairs.all$rated),each=8), rater=rep(levels(pairs.all$rated),each=8))) ggplot(pairs.all2, aes(x=rated,y=rater,fill=adjustment))+ geom_tile(lwd=.5,color="grey80",linetype=1)+theme_minimal() + coord_fixed()+facet_wrap(~dimension,ncol=4) + scale_fill_gradient2(low="red",high=("white"),midpoint=-1) for(i in unique(pairs.all$dimension)) { tmp0 <- pairs.all[pairs.all$dimension==i,] tmp <- as.network(data.frame(from=tmp0$rater, to= tmp0$rated, weight=abs(tmp0$adjustment)), vertices = data.frame(member=c("CDR","CS","FE","MS1","MS3"),xpos=1:5,ypos=c(1,1,2,2,1))) p <- ggplot(ggnetwork(tmp),aes(x=x,y=y,xend=xend,yend=yend), layout="circle") + geom_edges(size=.8,aes(color=weight) ,arrow = arrow(length = unit(.5, "cm"), angle=20,type = "closed"))+ geom_nodes(size=13,color="grey")+geom_nodetext(aes(label=vertex.names))+ theme_blank() + ggtitle(i) print(p) } ratings$month <- as.factor(floor(ratings$Week /8.999)+1) r.trust <-dplyr::select(ratings,Day,Week,month,TaskType,Rater, starts_with("Trust")) r.torient <- dplyr::select(ratings,Day,Week,month,TaskType,Rater, starts_with("Torient")) r.comm <- dplyr::select(ratings,Day,Week,month,TaskType,Rater, starts_with("Comm")) r.ls <- dplyr::select(ratings,Day,Week,month,TaskType,Rater, starts_with("LS")) r.backup <- dplyr::select(ratings,Day,Week,month,TaskType,Rater, starts_with("Backup")) r.monitor <- dplyr::select(ratings,Day,Week,month,TaskType,Rater, starts_with("Monitor")) r.adapt <- dplyr::select(ratings,Day,Week,month,TaskType,Rater, starts_with("Adapt")) r.smm <- dplyr::select(ratings,Day,Week,month,TaskType,Rater, starts_with("SMM")) r.trust.long <- melt(r.trust,measure.vars=c("Trust.CDR","Trust.FE","Trust.MS1","Trust.MS3","Trust.CS","Trust.MCC")) r.trust.long$cond <- paste(r.trust.long$variable,r.trust.long$Rater) r.trust.long$rated <- substr(r.trust.long$variable,7,12) r.trust.long$rated <- factor(r.trust.long$rated,levels=roles) r.trust.long$Dimension <- "Mutual Trust" self.trust <- subset(r.trust.long,Rater==rated) self.trust$Rater <- "SELF" r.torient.long <- melt(r.torient,measure.vars=c("Torient.CDR","Torient.FE","Torient.MS1","Torient.MS3","Torient.CS","Torient.MCC")) r.torient.long$cond <- paste(r.torient.long$variable,r.torient.long$Rater) r.torient.long$rated <- substr(r.torient.long$variable,9,14) r.torient.long$rated <- factor(r.torient.long$rated,levels=roles) r.torient.long$Dimension <- "Team orientations" self.torient <- subset(r.torient.long,Rater==rated) self.torient$Rater <- "SELF" r.comm.long <- melt(r.comm,measure.vars=c("Comm.CDR","Comm.FE","Comm.MS1","Comm.MS3","Comm.CS","Comm.MCC")) r.comm.long$cond <- paste(r.comm.long$variable,r.comm.long$Rater) r.comm.long$rated <- substr(r.comm.long$variable,6,12) r.comm.long$rated <- factor(r.comm.long$rated,levels=roles) r.comm.long$Dimension <- "Communications" self.comm <- subset(r.comm.long,Rater==rated) self.comm$Rater <- "SELF" r.ls.long <- melt(r.ls,measure.vars=c("LS.CDR","LS.FE","LS.MS1","LS.MS3","LS.CS","LS.MCC")) r.ls.long$cond <- paste(r.ls.long$variable,r.ls.long$Rater) r.ls.long$rated <- substr(r.ls.long$variable,4,12) r.ls.long$rated <- factor(r.ls.long$rated,levels=roles) r.ls.long$Dimension <- "Leadership" self.ls <- subset(r.ls.long,Rater==rated) self.ls$Rater <- "SELF" ls.fe <- subset(r.ls.long,rated=="FE") r.backup.long <- melt(r.backup,measure.vars=c("Backup.CDR","Backup.FE","Backup.MS1","Backup.MS3","Backup.CS","Backup.MCC")) r.backup.long$cond <- paste(r.backup.long$variable,r.backup.long$Rater) r.backup.long$rated <- substr(r.backup.long$variable,8,14) r.backup.long$rated <- factor(r.backup.long$rated,levels=roles) r.backup.long$Dimension <- "Backup behavior" self.backup <- subset(r.backup.long,Rater==rated) self.backup$Rater <- "SELF" r.monitor.long <- melt(r.monitor,measure.vars=c("Monitor.CDR","Monitor.FE","Monitor.MS1","Monitor.MS3","Monitor.CS","Monitor.MCC")) r.monitor.long$cond <- paste(r.monitor.long$variable,r.monitor.long$Rater) r.monitor.long$rated <- substr(r.monitor.long$variable,9,15) r.monitor.long$rated <- factor(r.monitor.long$rated,levels=roles) r.monitor.long$Dimension <- "Performance monitoring" self.monitor <- subset(r.monitor.long,Rater==rated) self.monitor$Rater <- "SELF" r.adapt.long <- melt(r.adapt,measure.vars=c("Adapt.CDR","Adapt.FE","Adapt.MS1","Adapt.MS3","Adapt.CS","Adapt.MCC")) r.adapt.long$cond <- paste(r.adapt.long$variable,r.adapt.long$Rater) r.adapt.long$rated <- substr(r.adapt.long$variable,7,15) r.adapt.long$rated <- factor(r.adapt.long$rated,levels=roles) r.adapt.long$Dimension <- "Adaptability" self.adapt <- subset(r.adapt.long,Rater==rated) self.adapt$Rater <- "SELF" r.smm.long <- melt(r.smm,measure.vars=c("SMM.CDR","SMM.FE","SMM.MS1","SMM.MS3","SMM.CS","SMM.MCC")) r.smm.long$cond <- paste(r.smm.long$variable,r.smm.long$Rater) r.smm.long$rated <- substr(r.smm.long$variable,5,15) r.smm.long$rated <- factor(r.smm.long$rated,levels=roles) r.smm.long$Dimension <- "Shared mental model" self.smm <- subset(r.smm.long,Rater==rated) self.smm$Rater <- "SELF" ggplot(r.trust.long,aes(x=Day,y=value,group=cond,col=Rater)) + geom_line() +geom_point() + theme_bw() + ylim(0,10) + xlim(0,240)+ ggtitle(label="Trust: As rated by others vs. self")+ geom_jitter(width=.5) + facet_wrap(~rated,ncol=6) + geom_line(data=self.trust,aes(x=Day,y=value),col="grey20",alpha=.4,size=2) ggplot(r.torient.long,aes(x=Day,y=value,group=cond,col=Rater)) + geom_line() +geom_point() + theme_bw() + ylim(0,10)+ xlim(0,240)+ ggtitle(label="Team Orientation: As rated by others vs. self")+ geom_jitter(width=.5) + facet_wrap(~rated,ncol=6) + geom_line(data=self.torient,aes(x=Day,y=value),col="grey20",alpha=.4,size=2) ggplot(r.comm.long,aes(x=Day,y=value,group=cond,col=Rater)) + geom_line() +geom_point() + theme_bw() + ylim(0,10)+ xlim(0,240)+ ggtitle(label="Communications: As rated by others vs. self")+ geom_jitter(width=.5) + facet_wrap(~rated,ncol=6) + geom_line(data=self.comm,aes(x=Day,y=value),col="grey20",alpha=.4,size=2) ggplot(r.ls.long,aes(x=Day,y=value,group=cond,col=Rater)) + geom_line() +geom_point() + theme_bw() + ylim(0,10) + xlim(0,240)+ ggtitle(label="Leadership: As rated by others vs. self")+ geom_jitter(width=.5) + facet_wrap(~rated,ncol=6) + geom_line(data=self.ls,aes(x=Day,y=value),col="grey20",alpha=.4,size=2) ggplot(r.backup.long,aes(x=Day,y=value,group=cond,col=Rater)) + geom_line() +geom_point() + theme_bw() + ylim(0,10) + xlim(0,240)+ ggtitle(label="Back-up behavior: As rated by others vs. self")+ geom_jitter(width=.5) + facet_wrap(~rated,ncol=6) + geom_line(data=self.backup,aes(x=Day,y=value),col="grey20",alpha=.4,size=2) ggplot(r.monitor.long,aes(x=Day,y=value,group=cond,col=Rater)) + geom_line() +geom_point() + theme_bw() + ylim(0,10) + xlim(0,240)+ ggtitle(label="Performance monitoring: As rated by others vs. self")+ geom_jitter(width=.5) + facet_wrap(~rated,ncol=6) + geom_line(data=self.monitor,aes(x=Day,y=value),col="grey20",alpha=.4,size=2) ggplot(r.adapt.long,aes(x=Day,y=value,group=cond,col=Rater)) + geom_line() +geom_point() + theme_bw() + ylim(0,10) + xlim(0,240)+ ggtitle(label="Adaptability: As rated by others vs. self")+ geom_jitter(width=.5) + facet_wrap(~rated,ncol=6) + geom_line(data=self.adapt,aes(x=Day,y=value),col="grey20",alpha=.4,size=2) ggplot(r.smm.long,aes(x=Day,y=value,group=cond,col=Rater)) + geom_line() +geom_point() + theme_bw() + ylim(0,10) + xlim(0,240)+ ggtitle(label="Shared mental model: As rated by others vs. self") + geom_jitter(width=.5) + facet_wrap(~rated,ncol=6) + geom_line(data=self.smm,aes(x=Day,y=value),col="grey20",alpha=.3,size=2) #title self-image versus others' ratings # self-rating compared to how others rate person #vs aggregate(allout3$rated,list(Dimension=allout3$Dimension, rater=allout3$Rater,rated=allout3$rated,keep=allout3$cluster),length) setwd("~/Dropbox/courses/5210-2022/web-5210/psy5210/Projects/Chapter4") dat2 <- read.csv("pooled-pursuitrotor.csv") dim(dat2) head(dat2) table(dat2$subnum) table(dat2$subnum,dat2$trial) ##select on a specific trial/participant tmp <- dat2[dat2$trial==1&dat2$subnum==12818,] dim(dat2) dim(tmp) table(tmp$tdiff) par(mfrow=c(1,3)) plot(tmp$timeElapsed, main= paste("Mean:" , round(mean(tmp$tdiff),3))) plot(tmp$tdiff) hist(tmp$tdiff) abline(v=mean(tmp$tdiff)) matplot(cbind(tmp$targX,tmp$targY), type="l",bty="L",las=1,lty=2, xlab="Time step",ylab="Pixel location", ylim=c(0,1500)) legend(550,1500,c("X position","Y position"),lty=1:2,col=1:2) matplot(cbind(tmp$mouseX,tmp$mouseY),add=T, type="l",las=1,lty=1) xmid <- mean(tmp$targX) ## find the x center ymid <- mean(tmp$targY) ## find they center plot(tmp$targX-xmid,ymid-tmp$targY,pch=1,cex=.2, xlim=c(-300,300),ylim=c(-300,300), xlab="Horizontal pixel",ylab="Vertical pixel") points(tmp$mouseX-xmid,ymid-tmp$mouseY,type="l",col="grey") segments(tmp$targX-xmid,ymid-tmp$targY,tmp$mouseX-xmid, ymid-tmp$mouseY,col="grey") for(i in 1:nrow(tmp)) { if(tmp[i,]$ontarget) { points(tmp$mouseX[i]-xmid,ymid-tmp$mouseY[i],col="red",cex=.3) }else{ points(tmp$mouseX[i]-xmid,ymid-tmp$mouseY[i],col="grey",cex=.3) } } plottrial <- function(tmp,header="") { xmid <- mean(tmp$targX) ymid <- mean(tmp$targY) meanoffset <- round(mean(tmp$diff),2) plot(tmp$targX-xmid,ymid-tmp$targY,pch=1,cex=.2, xlim=c(-300,300),ylim=c(-300,300), # main=paste(header,"\n","mean offset =", meanoffset), xlab="",ylab="",axes=F) #mousex, mousey specify the mouse coordinates: Plot them to: points(tmp$mouseX-xmid,ymid-tmp$mouseY,type="l",col="grey") segments(tmp$targX-xmid,ymid-tmp$targY,tmp$mouseX-xmid,ymid-tmp$mouseY,col="grey") ##I'll use a faster points method than we did above: points(tmp$mouseX-xmid,ymid-tmp$mouseY, col=c("grey","red")[tmp$ontarget+1],cex=.3) } plottrial(tmp,"test") sub1 <- dat2[dat2$subnum==12887,] for(i in levels(sub1$trial)) { print(i) } levels(sub1$trial) names(table(sub1$trial)) levels(as.factor(sub1$trial)) for(i in levels(as.factor(sub1$trial))) { print(i) } sub1 <- dat2[dat2$subnum==12887,] par(mfrow=c(2,2)) for(i in levels(as.factor(sub1$trial))) { tmp <- sub1[sub1$trial==i,] plottrial(tmp) } tapply(dat2$ontarget,list(dat2$sub,dat2$trial),mean) for(sub in levels(as.factor(dat2$subnum))) { par(mfrow=c(2,2)) tmp <- dat2[dat2$subnum==sub,] for(trial in levels(as.factor(tmp$trial))) { print(paste("plotting subject:", sub, " trial: ", trial)) tmp2 <- tmp[tmp$trial==trial,] plottrial(tmp2,header=paste("plotting subject:", sub, " trial: ", trial)) } } pr.agg <- aggregate(dat2$diff,list(dat2$subnum,dat2$trial),mean) par(mfrow=c(2,2)) hist(pr.agg$x,xlab="Mean deviation") hist(pr.agg$x,xlab= "Mean deviation", col="navy",breaks=20) hist(pr.agg$x,xlab= "Mean deviation", col="red",breaks=c(0:40)) ##plots density instead of counts: hist(pr.agg$x,xlab= "Mean deviation", col="darkgreen",breaks=c(0,10:20,25,30,40),freq=F ) View(InsectSprays) ## Notice this is in long data format. ## Not included in book boxplot(InsectSprays$count~InsectSprays$spray) ##figures here will be 3x4 inches set.seed(100) data2 <- data.frame(q1=sample(letters[1:10],100,replace=T), q2=sample(letters[1:10],100,replace=T), q3=sample(letters[1:10],100,replace=T), q4=sample(letters[1:10],100,replace=T), q5=sample(letters[1:10],100,replace=T)) datatable2<-apply(data2,2,table) lf <- c(8.167,1.492,2.782,4.253,12.702,2.228,2.015,6.094, 6.966,0.153,0.772,4.025,2.406,6.749,7.507,1.929, 0.095,5.987,6.327,9.056,2.758,0.978,2.36,0.15,1.974,0.074)/100 pts <- c(1,3,3,2,1,4,2,4,1,8,5,1,3,1,1,3,10,1,1,1,1,4,4,8,4,10) tiles <- c(9,2,2,4,12,2,3,2,9,1,1,4,2,6,8,2,1,6,4,6,4,2,2,1,2,1) lf.table <- data.frame(LETTERS, freq=lf, points=pts, ntiles=tiles) scoreme <- function(word) { lets <- strsplit(splus2R::upperCase(word),"")[[1]] data <- matrix(0,ncol=4,nrow=length(lets)) for(i in 1:length(lets)) { index <- which(lets[i]==LETTERS) data[i,1] <- lf.table$freq[index] data[i,2] <- lf.table$points[index] data[i,3] <- lf.table$ntiles[index] } list(suminvfreq= sum(1/data[,1]), points=sum(data[,2]), meantiles=mean(data[,3]), length=length(lets)) } horses <- scoreme("HORSES") horses print(horses$points) test <- read.table(text='rank word frequency 1081 CUP 1441306 2310 FOUND 573305 5285 BUTTERFLY 171410 7371 brew 94904 11821 CUMBERSOME 39698 17331 useable 17790 18526 WHITTLE 15315 25416 SPINY 7207 27381 uppercase 5959 37281 halfnaked 2459 47381 bellhop 1106 57351 tetherball 425 7309 attic 2711 17311 tearful 542 27303 tailgate 198 37310 hydraulically 78 47309 unsparing 35 57309 embryogenesis 22 ',header=T)[,c(2,1,3)] ##reorder colums test$meantiles <- NA test$suminvfreq <- NA test$points <- NA test$length <- NA