Advertisement
Guest User

Untitled

a guest
Nov 22nd, 2019
144
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
R 2.26 KB | None | 0 0
  1. # Identify unique pairs within a given study (to keep things manageable) and create list of possible surrogate pairs (e.g. individual 1 from pair 1 and individual 2 from pair 2)
  2. Groups <- as.numeric(as.character(unique(s4$group[s4$StudyNum==4]))) # Lists all pairs
  3.  
  4. SurrogateList <- expand.grid(a = Groups, b = Groups) # Identify all possible combinations of 2 pairs
  5.  
  6. SurrogateList = subset(SurrogateList, a != b) # Exclude combinations with identical pairs
  7.  
  8. surrodf <- tibble()
  9. for (i in 1:nrow(SurrogateList)){  # loop through all combinations    
  10.   x <- subset(s4, group==SurrogateList$a[i]) # subset data from the first pair    
  11.   y <- subset(s4, group==SurrogateList$a[i]) # subset data from the second pair      
  12.   group <- c(800 + ((1:4)*i))                            # create new pair id  
  13.   for (co in c("Synchronous","TurnTaking", "Conversation", "MovementCoop", "MovementGuided")){ # loop through conditions      
  14.     if (co %in% unique(x$condition) & co %in% unique(y$condition)){      # check that both pairs have the data for that condition        
  15.       z1 <- subset(x, condition==co)            # subset only that condtion from first pair            
  16.       z2 <- subset(y, condition==co)            # subset only that condtion from second pair            
  17.       if (nrow(z1) > nrow(z2)) {    # make sure data have same length in both pairs                
  18.         z1<-z1[1:length(z2)]              
  19.         }          
  20.       if (nrow(z2) > nrow(z1)) {        
  21.         z2<-z2[1:length(z1)]            
  22.       }  
  23.       w1 <- z1 %>% mutate(  # assemble new pair combining the 2 pairs                
  24.         HR2 = z2$HR_other,                  
  25.         Resp2 = z2$Resp_other,              
  26.         HR2_lead = z2$HR_self,                
  27.         Resp2_lead = z2$Resp_self,                
  28.         HR2_change = z2$changeHR_other,              
  29.         Resp2_change = z2$changeResp_other)
  30.       }  ### DO SOMETHING TO SAVE THIS  # make sure that you do this!
  31.       surrodf <- rbind(surrodf, w1)
  32.      }}
  33.  
  34. # Starting from the wide format, create "surrogate" dataset with the data from surrogate pairs
  35.  
  36. # Make it into long format
  37.  
  38. # Create models as in chunks above, but adding an interaction with the Real vs. Surrogate variable (exclude shuffled ones for simplicity)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement