#May 2021 library("tidyverse") library("lme4") library("effects") library("arm") library("pbkrtest") setwd("") #your working directory here load("Belyk_EBR_S6h_preEMG.Rdata") ###################### ###custom functions### ###################### #preferred ggplot themes theme_mb<-function(){ theme_light(base_size=16) %+replace% theme(strip.background = element_rect(fill="grey95"), strip.text.x = element_text(colour="black"), strip.text.y = element_text(colour="black", angle=270), axis.text.x = element_text(angle = 35, vjust = 1, hjust=1)) } ################## ####some plots#### ################## #logs contains newly calculated observations based 50ms of EMG signal before stimulation logs %>% #filter(Participant >9) %>% mutate(Coil = as.factor(Coil)) %>% mutate(Coil = relevel(Coil, "Test")) %>% ggplot(aes(x=PreEMG_uVS, fill=Condition))+ geom_density()+ facet_grid(Condition~Coil)+ xlab("Pre-stimulus EMG (uV/S)")+ ggtitle("Distributions of prestimulation EMG")+ xlim(0,100)+ theme_mb() ggsave("Verify_precontraction_state.pdf") logs %>% #filter(Participant >9) %>% ggplot(aes(x=PreEMG_uVS, y=MEP, color=Condition))+ geom_point()+ geom_smooth(method="lm")+ facet_grid(Condition~Coil)+ xlab("Pre-stimulus EMG (uV/S)")+ xlim(0,100)+ theme_mb() PreEMGmod <- glmer(PreEMG_uVS ~ Coil*Condition + (1+Coil*Condition|Participant), family=Gamma(link="log"), data=logs) relgrad <- with(PreEMGmod@optinfo$derivs,solve(Hessian,gradient)) #is this less than about 0.0001? pushing it slightly max(abs(relgrad)) #good enough but not ideal summary(PreEMGmod) #################### ###Residual check### #################### plot(residuals(PreEMGmod)) #ok, a little funk at the lower tail but comprising few observations qqnorm(residuals(PreEMGmod)) #a few weirdos at the bottom tail #roughly these ones logs %>% filter(residuals(PreEMGmod)< -3) ###################################################################### ###contrasts for comparing rest in RR vs AR and active in AA vs AR### ###################################################################### f<-expand.grid(unique(logs$Coil), unique(logs$Condition)) names(f)<-c("Coil", "Condition") contrast_matrix<-model.matrix(~Coil*Condition,f) contrast_matrix<-as_tibble(contrast_matrix) contrast_matrix<-cbind(f, contrast_matrix) contrast_matrix<-logs contrast_matrix$ConRest=0 contrast_matrix$ConActive=0 contrast_matrix<-contrast_matrix %>% mutate(ConRest= replace(ConRest,Coil=="Conditioning" & Condition == "RR", 0.5)) %>% mutate(ConRest= replace(ConRest,Coil=="Conditioning" & Condition == "AR", -0.5)) %>% mutate(ConActive= replace(ConActive,Coil=="Test" & Condition == "AA", 0.5)) %>% mutate(ConActive= replace(ConActive, Coil=="Test" & Condition == "AR", -0.5)) PreEMGmodcons<-glmer(PreEMG_uVS ~1+ ConRest+ConActive + (ConActive|Participant), family=Gamma(link="log"), data=contrast_matrix) summary(PreEMGmodcons) ############################ ###estimates for plotting### ############################ PreEMGmod.effects<-Effect(c("Coil","Condition"),PreEMGmod, confint=T) preds<-PreEMGmod.effects$x preEMG.Gamma<-PreEMGmod.effects$fit PreEMGmod.plotalble<-cbind(PreEMGmod.effects$x,PreEMGmod.effects$fit,PreEMGmod.effects$lower,PreEMGmod.effects$upper) lower<-PreEMGmod.effects$lower upper<-PreEMGmod.effects$upper PreEMGmod.plotalble<-as_tibble(cbind(preds,preEMG.Gamma, lower,upper)) rm(preds, preEMG.Gamma, lower, upper) PreEMGmod.plotalble<-PreEMGmod.plotalble %>% mutate(Coil = fct_relevel(Coil, "Test")) #change order of levels for plotting ggplot(PreEMGmod.plotalble, aes(x=Coil, y=preEMG.Gamma, color=Condition))+ geom_point(size=2)+ geom_linerange(aes(ymin=lower,ymax=upper))+ facet_grid(.~Condition)+ ylab("Model coefficients")+ ggtitle("Pre-stimulus EMG")+ theme_mb() ggsave("Pre_EMG_gamma.pdf") ###################### ###DIFFERENCE MODEL### ###################### #attempt to run my original models again #dat2b_preEMG adds the newly computed prestim EMG to the original model data #data preEMG from 2 Subjects was missing. This dataframe has data for those participants removed #because bootsrapping with PBmodcomp cannot cope with missing data mod_inhibit_dif_preEMG<-lmer(Inhibition ~ #including all covariates here IHI+Condition*Cond_EMG #dropped IHI interaction. It caused fit problems and was not preffered by AIC anyway + Cond_Target_Norm +Cond_Twist_Norm+Cond_Angular_Norm + Test_Target_Norm +Test_Twist_Norm+Test_Angular_Norm + PreEMG_Test + PreEMG_Conditioning + (1|Subject)+(0+Cond_EMG|Subject) ,REML=F ,control =lmerControl(optimizer = "bobyqa", optCtrl=list(maxfun=1e5)) #fiddle with optimizers ,data=dat2b_preEMG) relgrad <- with(mod_inhibit_dif_preEMG@optinfo$derivs,solve(Hessian,gradient)) #is this less than about 0.0001? If so can probably ignore convergence warnings max(abs(relgrad)) #good enough mod_inhibit_dif_preEMG_stnd<-standardize(mod_inhibit_dif_preEMG) summary(mod_inhibit_dif_preEMG_stnd) mod_inhibit_dif_preEMG_stnd_CI<-confint(mod_inhibit_dif_preEMG_stnd) pdf("plots/Diff_QQ.pdf") #pretty good qqnorm(scale(residuals(mod_inhibit_dif_preEMG_stnd))) #something weird here, but should be fine with bootstrapping abline(a=0,b=1) dev.off() pdf("plots/Diff_dispersion.pdf") #pretty good plot(mod_inhibit_dif_preEMG_stnd, main="Dispersion of Differences Model") #pretty good dev.off() ###BOOTSTRAPPING### #reduced interactions #I = IHI, C = Condition, E = CS MEP mod_inhibit_dif_preEMG_mains<-update(mod_inhibit_dif_preEMG_stnd, .~. -Condition:z.Cond_EMG) mod_inhibit_dif_preEMG_I<-update(mod_inhibit_dif_preEMG_mains, .~. -c.IHI) mod_inhibit_dif_preEMG_C<-update(mod_inhibit_dif_preEMG_mains, .~. -Condition) mod_inhibit_dif_preEMG_E<-update(mod_inhibit_dif_preEMG_mains, .~. -z.Cond_EMG) set.seed(42) #for replicability (PB_CE_dif_preEMG<-PBmodcomp(mod_inhibit_dif_preEMG_stnd, mod_inhibit_dif_preEMG_mains, nsim=5000)) (PB_I_preEMG_dif<-PBmodcomp(mod_inhibit_dif_preEMG_mains, mod_inhibit_dif_preEMG_I, nsim=5000)) (PB_C_preEMG_dif<-PBmodcomp(mod_inhibit_dif_preEMG_mains, mod_inhibit_dif_preEMG_C, nsim=5000)) (PB_E_preEMG_dif<-PBmodcomp(mod_inhibit_dif_preEMG_mains, mod_inhibit_dif_preEMG_E, nsim=5000))