#load data
wave2<-read.csv("~/Downloads/GG identification across groups Wave 2.csv") %>%
  dplyr::select(CONDITION:Trust_levelup_4) %>%
  mutate(CONDITION=dplyr::recode(CONDITION, '1'="1", '2'="2",'3'="3",'4'="4")) %>%
  dplyr::filter(Trust_levelup_4 > 1) %>%
  dplyr::select(CONDITION:workerId)

Heatmap

htmap4.p<-read.csv("~/Desktop/w4_anova_p.csv"); htmap4.f<-read.csv("~/Desktop/w4_anova_f.csv")
##wide to long, using Base R
htmap4.mp<-melt(htmap4.p) #wide to long base R, equivalently as the following 
htmap4.mf<-melt(htmap4.f)
#equivalently, using dplyr
#htmap.mp<-htmap.p %>% gather("comparison", "p_value", Ingroup.dual.p:control.outgroup.p) 
#htmap.mt<-htmap.t %>% gather("comparison", "t_value", Ingroup.dual.t:control.outgroup.t)
htmap4.mp$f_val<-htmap4.mf$value; names(htmap4.mp)<-c("var", "comparison", "p_val", "f_val")
combine4.pf<-htmap4.mp
#combine4.pf
#htmap.m<-ddply(htmap.m, .(variable), transform, rescale = rescale(value)) make the discrete p-val to continuous scale

combine4.pf$sig_level <- ifelse(combine4.pf$p_val <= 0.05, "sig",
                       ifelse(combine4.pf$p_val > 0.1, "non_sig", "marginally_sig")) #nested if_else statements to classify p-val into 3 groups: 0-0.05 very sig, 0.05-0.1 sig, >0.1 non_sig
combine4.pf$dir <- ifelse(combine4.pf$f_val > 0, "positive", "negative")

combine4.pf$sig_level<-factor(combine4.pf$sig_level, levels = c("sig", "marginally_sig", "non-sig")) #define the order of legend bar


ggplot(combine4.pf, aes(comparison, var, fill = sig_level, colour = dir)) + #x-axis comparison
  geom_tile() + 
  scale_fill_manual(values = c(sig = "black",marginally_sig = "gray",non_sig = "white")) +
  scale_colour_manual(values = c(positive = "green", negative = "red", size=50))+
  xlab("") +
  theme(axis.text.x = element_text( angle = 90, hjust = 1))

merge data and prepare for multilevel analysis

#merge data
wave_1<-read.csv("~/Downloads/GG identification across groups Wave 1.csv") %>%
  dplyr::select(CONDITION:Gender, LocationLatitude, LocationLongitude, workerId) %>%
  mutate(CONDITION=dplyr::recode(CONDITION, '0'="Experiment", '1'="Control"))
wave_merge<-merge(wave_1, wave2, by="workerId") %>% dplyr::select(-workerId)
wave_total <- wave_merge %>% dplyr::select(contains(".")) %>%
  dplyr::select(Essentialism_SCL.x, Essentialism_SCL.y, Identification_SCL.x, Identification_SCL.y) %>%
  tibble::rowid_to_column() %>%
  gather(key, value, Essentialism_SCL.x:Identification_SCL.y)
#########################
wave_select <-  wave_merge %>%  dplyr::select(CONDITION.y,Essentialism_SCL.x, Essentialism_SCL.y, Identification_SCL.x, Identification_SCL.y, Mexican_3, Mexican_4, GG_level_Mexican, GG_level_3, GG_level_4, GG_Level) %>% dplyr::rename(CONDITION = CONDITION.y)

wave_total$gp <- ifelse(grepl("^.+\\.(x)$", wave_total$key) == TRUE, "wave1", "wave2") #use regular expression to extract the last digit of group (eg:Muslims_4 -> 4)  
wave_total$key <-
  ifelse(wave_total$key == 'Essentialism_SCL.x' |wave_total$key == 'Essentialism_SCL.y',
  "Essentialism_SCL",
  ifelse(wave_total$key=='Identification_SCL.x'|wave_total$key=='Identification_SCL.y', "Identification_SCL",
  ifelse(wave_total$key=='Age.x'|wave_total$key=='Age.y', "Age", "Gender")))
wave <- wave_total %>% select(key,gp, value) %>% mutate()
###Compare the difference in "GG_Level"
wave.compare<-wave_merge 
wave.compare$diff <-
  ifelse(wave.compare$GG_Level > wave.compare$GG_level_Mexican ,"increase",
  ifelse(wave.compare$GG_Level < wave.compare$GG_level_Mexican,"decrease","same"))
wave.compare<- wave.compare %>%
  dplyr::select(diff, Simon_Dual_identity_SCL:Gender.y)%>%
  dplyr::rename(Identification_SCL = Identification_SCL.y, Essentialism_SCL = Essentialism_SCL.y, Age= Age.y, Gender= Gender.y)%>%
  drop_na(diff)

###Compare the difference in "Mexican_3"
wave.compare3<-wave_merge 
wave.compare3$diff <-
  ifelse(wave.compare3$GG_level_3 > wave.compare3$Mexican_3 ,"increase",
  ifelse(wave.compare3$GG_level_3 < wave.compare3$Mexican_3,"decrease","same"))
wave.compare3<- wave.compare3 %>%
  dplyr::select(diff, Simon_Dual_identity_SCL:Gender.y)%>%
  dplyr::rename(Identification_SCL = Identification_SCL.y, Essentialism_SCL = Essentialism_SCL.y, Age= Age.y, Gender= Gender.y)%>%
  drop_na(diff)

###Compare the difference in "Mexican_4"
wave.compare4<-wave_merge 
wave.compare4$diff <-
  ifelse(wave.compare4$GG_level_4 > wave.compare4$Mexican_4 ,"increase",
  ifelse(wave.compare4$GG_level_4 < wave.compare4$Mexican_4,"decrease","same"))
wave.compare4<- wave.compare4 %>%
  dplyr::select(diff, Simon_Dual_identity_SCL:Gender.y)%>%
  dplyr::rename(Identification_SCL = Identification_SCL.y, Essentialism_SCL = Essentialism_SCL.y, Age= Age.y, Gender= Gender.y)%>%
  drop_na(diff)

anova comparisons for each 33 dependent variables

library(multcomp)
#The function TukeyHD() takes the fitted ANOVA as an argument
#anova-p-results
p_rst<-function(X, data){
  resul<-TukeyHSD(aov(X ~ diff, data = data))
  return(resul$diff[,4])
}

anova_p<-function(X, data){
  
  res.aov<-aov(X~diff, data=data)
  res.anova<-anova(res.aov)
  return(res.anova$`Pr(>F)`[1])
}

#anova-f-results
f_rst<-function(X, data){
  result<-summary(glht(lm(X ~ diff, data = data), linfct = mcp(diff = "Tukey")))
  return(result$test$tstat)
}

anova_f<-function(X, data){
  
  res.aov<-aov(X~diff, data=data)
  res.anova<-anova(res.aov)
  return(res.anova$`F value`[1])
}

###GG_Level
wave.compare$diff<-as.factor(wave.compare$diff)
wave.compare3$diff<-as.factor(wave.compare3$diff)
wave.compare4$diff<-as.factor(wave.compare4$diff)
###test functions above, all worked !!!
#p_rst(wave.compare3$GG_Angst, wave.compare3)  
#anova_p(wave.compare$GG_Angst, wave.compare3)
#f_rst(wave.compare$GG_Angst, wave.compare3)
#anova_f(wave.compare$GG_Angst, wave.compare3)

result preparation

GG_Level

###p-value preparation
p_GG_Level<- data.frame(matrix(ncol = 4))

for (i in 2:34){
  p_GG_Level[(i-1),] <- c(p_rst(wave.compare[,i], wave.compare), anova_p(wave.compare[,i], wave.compare))
}

colnames(p_GG_Level) <- c("increase-decrease", "same-decrease", "same-increase", "ANOVA") 
p_GG_Level$var<-colnames(wave.compare[,-1])

###f-value preparation
f_GG_Level<-data.frame(matrix(ncol = 4))

for (i in 2:34){
  f_GG_Level[(i-1),] <- c(f_rst(wave.compare[,i], wave.compare), anova_f(wave.compare[,i], wave.compare))
}

colnames(f_GG_Level) <- c("increase-decrease", "same-decrease", "same-increase", "ANOVA") 
f_GG_Level$var <-colnames(wave.compare[,-1])

p_GG_Level;f_GG_Level
##    increase-decrease same-decrease same-increase        ANOVA
## 1         0.29984565  8.747923e-02  5.592627e-01 8.340027e-02
## 2         0.99733913  8.476278e-03  6.357651e-03 5.136427e-03
## 3         0.55227870  7.199666e-03  3.921190e-04 6.294379e-04
## 4         0.79126653  2.385674e-04  2.376385e-05 2.756228e-05
## 5         0.49121398  3.097606e-05  3.831217e-07 6.141102e-07
## 6         0.68332545  7.585790e-05  3.434732e-06 4.442838e-06
## 7         0.99910326  2.231525e-05  1.961500e-05 8.289604e-06
## 8         0.99832985  3.298897e-02  2.701060e-02 2.349450e-02
## 9         0.73563601  2.892140e-01  9.364428e-02 1.144493e-01
## 10        0.78755905  3.662794e-03  5.280300e-04 6.807195e-04
## 11        0.59778696  5.492170e-03  3.466864e-04 5.402859e-04
## 12        0.97400471  3.225853e-03  1.566902e-03 1.380080e-03
## 13        0.76645719  6.123785e-03  8.670617e-04 1.157315e-03
## 14        0.60731101  2.118117e-01  4.160060e-02 5.379728e-02
## 15        0.91546442  1.379197e-02  4.800450e-03 5.309156e-03
## 16        0.66493809  2.075563e-03  1.475121e-04 2.157432e-04
## 17        0.81387688  5.687123e-02  1.641946e-02 2.107987e-02
## 18        0.90550981  5.665290e-02  2.264084e-02 2.621219e-02
## 19        0.61372891  9.380703e-03  7.219606e-04 1.105387e-03
## 20        0.59575940  6.822451e-03  4.487338e-04 6.989819e-04
## 21        0.06889727  7.191020e-01  6.480707e-01 8.578662e-02
## 22        0.57334678  2.102608e-01  3.706550e-02 4.812318e-02
## 23        0.68890230  1.422431e-02  1.675136e-03 2.410769e-03
## 24        0.64159946  6.099618e-03  4.829156e-04 7.261736e-04
## 25        0.99416459  3.968352e-03  2.647838e-03 2.101176e-03
## 26        0.18215681  5.020139e-01  3.748337e-02 3.161163e-02
## 27        0.31633081  1.565994e-03  1.517995e-05 2.929835e-05
## 28        0.42728910  9.392130e-01  8.223998e-01 4.554729e-01
## 29        0.50502833  2.009688e-03  6.344576e-05 1.069571e-04
## 30        0.89689863  3.817979e-02  1.375559e-02 1.604826e-02
## 31        0.49318774  1.721208e-02  9.073028e-04 1.466702e-03
## 32        0.91081864  9.983936e-01  9.293169e-01 8.942851e-01
## 33        0.92614569  7.020339e-01  8.559464e-01 7.236039e-01
##                              var
## 1        Simon_Dual_identity_SCL
## 2                       GG_Angst
## 3                 Stereotype_SCL
## 4                Symb_Threat_SCL
## 5                Real_Threat_SCL
## 6                            ITT
## 7                   Humanization
## 8       Peception_of_GC_mediator
## 9         Peception_of_GC_bridge
## 10  Peception_of_GC_fifth_column
## 11      Peception_of_GC_traitors
## 12        Peception_of_GC_unique
## 13          Peception_of_GC_weak
## 14          Peception_of_GC_lmlm
## 15 Positive_perception_of_GC_SCL
## 16 Negative_perception_of_GC_SCL
## 17                      Surprise
## 18               Common_fate_SCL
## 19                      CIIM_SCL
## 20                       SDO_SCL
## 21            Identification_SCL
## 22              Essentialism_SCL
## 23                       Hatered
## 24                          Fear
## 25                         Anger
## 26                          Hope
## 27                       Empathy
## 28                       Despair
## 29                    Policy_SCL
## 30       Resource_allocation_SCL
## 31           Social_Distance_SCL
## 32                           Age
## 33                        Gender
##    increase-decrease same-decrease same-increase      ANOVA
## 1         1.48374338    2.11824448     1.0285892  2.5002009
## 2         0.06951833   -2.98439196    -3.0755293  5.3442366
## 3         1.03969245   -3.03642038    -3.8587300  7.5136107
## 4         0.65214070   -3.98413408    -4.5269543 10.7906517
## 5         1.13809580   -4.46743250    -5.3824366 14.8478728
## 6         0.83207984   -4.26114145    -4.9430483 12.7272826
## 7         0.04033907    4.54098230     4.5696358 12.0634130
## 8        -0.05506250    2.51390610     2.5880432  3.7877726
## 9        -0.74697550    1.50598476     2.0879984  2.1798733
## 10        0.65865921   -3.24380836    -3.7819135  7.4322471
## 11        0.96763705   -3.12097819    -3.8901326  7.6723637
## 12       -0.21859103    3.28147759     3.4887215  6.6995596
## 13        0.69516455   -3.08722665    -3.6507832  6.8818085
## 14        0.95260972   -1.68609544    -2.4252833  2.9448301
## 15       -0.40036586    2.82410154     3.1622681  5.3102460
## 16        0.86144234   -3.40936246    -4.1023058  8.6292419
## 17        0.61164469   -2.30373262    -2.7696614  3.9118052
## 18        0.42443816   -2.30237641    -2.6518865  3.6761924
## 19       -0.94248474    2.95166158     3.6996766  6.9293625
## 20        0.97083709   -3.05339768    -3.8240831  7.4047523
## 21       -2.22122284   -0.77419172     0.8882222  2.4716251
## 22        1.00625819   -1.69012117    -2.4697562  3.0580267
## 23        0.82312976   -2.81365929    -3.4700117  6.1231777
## 24        0.89846738   -3.08845848    -3.8051086  7.3651239
## 25        0.10303233   -3.21984628    -3.3392787  6.2650487
## 26       -1.76694930    1.12047423     2.4654696  3.4854077
## 27       -1.45016094    3.48888342     4.6261382 10.7261549
## 28       -1.24525533   -0.33733482     0.5958996  0.7880277
## 29        1.11557546   -3.41854951    -4.3029627  9.3636944
## 30       -0.44441233    2.45841545     2.8249935  4.1768237
## 31       -1.13486681    2.74835602     3.6385831  6.6365762
## 32       -0.41174226    0.05400026     0.3647250  0.1117631
## 33        0.37313574    0.80195650     0.5314271  0.3237832
##                              var
## 1        Simon_Dual_identity_SCL
## 2                       GG_Angst
## 3                 Stereotype_SCL
## 4                Symb_Threat_SCL
## 5                Real_Threat_SCL
## 6                            ITT
## 7                   Humanization
## 8       Peception_of_GC_mediator
## 9         Peception_of_GC_bridge
## 10  Peception_of_GC_fifth_column
## 11      Peception_of_GC_traitors
## 12        Peception_of_GC_unique
## 13          Peception_of_GC_weak
## 14          Peception_of_GC_lmlm
## 15 Positive_perception_of_GC_SCL
## 16 Negative_perception_of_GC_SCL
## 17                      Surprise
## 18               Common_fate_SCL
## 19                      CIIM_SCL
## 20                       SDO_SCL
## 21            Identification_SCL
## 22              Essentialism_SCL
## 23                       Hatered
## 24                          Fear
## 25                         Anger
## 26                          Hope
## 27                       Empathy
## 28                       Despair
## 29                    Policy_SCL
## 30       Resource_allocation_SCL
## 31           Social_Distance_SCL
## 32                           Age
## 33                        Gender

Draw heatmap

ggplot(combine.pf, aes(comparison, var, fill = sig_level, colour = dir)) + #x-axis comparison
  geom_tile() + 
  scale_fill_manual(values = c(sig = "black",marginally_sig = "gray",non_sig = "white")) +
  scale_colour_manual(values = c(positive = "green", negative = "red", size=50))+
  xlab("") + ylab("")+
  ggtitle("Heatmap focus on difference in \"GG level\"")+
  theme(axis.text.x = element_text( angle = 90, hjust = 1))

Mexican_3

###p-value preparation
p_Mexican_3<- data.frame(matrix(ncol = 4))

for (i in 2:34){
  p_Mexican_3[(i-1),] <- c(p_rst(wave.compare3[,i], wave.compare3), anova_p(wave.compare3[,i], wave.compare3))
}

colnames(p_Mexican_3) <- c("increase-decrease", "same-decrease", "same-increase", "ANOVA") 
p_Mexican_3$var<-colnames(wave.compare3[,-1])

###f-value preparation
f_Mexican_3<-data.frame(matrix(ncol = 4))

for (i in 2:34){
  f_Mexican_3[(i-1),] <- c(f_rst(wave.compare3[,i], wave.compare3), anova_f(wave.compare3[,i], wave.compare3))
}

colnames(f_Mexican_3) <- c("increase-decrease", "same-decrease", "same-increase", "ANOVA") 
f_Mexican_3$var <-colnames(wave.compare3[,-1])

p_Mexican_3; f_Mexican_3
##    increase-decrease same-decrease same-increase        ANOVA
## 1          0.9074727  1.108938e-01  3.862873e-02 3.511621e-02
## 2          0.9732761  4.225900e-05  8.224435e-05 6.786824e-06
## 3          0.8450830  3.366135e-07  3.468514e-06 4.382387e-08
## 4          0.4395175  0.000000e+00  9.889668e-09 1.382964e-12
## 5          0.9516825  0.000000e+00  0.000000e+00 7.369765e-16
## 6          0.9213060  1.107260e-10  6.793365e-10 1.646308e-12
## 7          0.9147573  2.932662e-07  2.269922e-08 1.745227e-09
## 8          0.9265700  2.099302e-04  7.011039e-04 6.721236e-05
## 9          0.9984866  1.454041e-03  1.458044e-03 3.603132e-04
## 10         0.1880325  3.717420e-09  1.768939e-05 3.076580e-09
## 11         0.4918158  4.215730e-08  8.169107e-06 1.511738e-08
## 12         0.9562909  5.107854e-06  9.170317e-07 1.076606e-07
## 13         0.9944997  1.390918e-05  1.603820e-05 1.244142e-06
## 14         0.9109896  1.125039e-01  2.350483e-01 1.076194e-01
## 15         0.9987569  6.117537e-06  5.484143e-06 3.831025e-07
## 16         0.2605471  8.585161e-10  2.340829e-06 4.311442e-10
## 17         0.8030955  5.044800e-07  1.156031e-08 2.086988e-09
## 18         0.8382205  8.024942e-05  5.929942e-04 3.167065e-05
## 19         0.9913629  1.080759e-04  1.404562e-04 1.704353e-05
## 20         0.6231028  1.322183e-06  6.633026e-09 1.591544e-09
## 21         0.9320125  1.877347e-01  3.294327e-01 1.817760e-01
## 22         0.4329889  8.810629e-04  3.893666e-06 3.802310e-06
## 23         0.9622286  2.441911e-05  5.437676e-06 8.678099e-07
## 24         0.8013678  1.815743e-06  4.819503e-08 7.924776e-09
## 25         0.9927810  1.243272e-06  1.503776e-06 6.345726e-08
## 26         0.9999720  1.066131e-03  8.565217e-04 2.142302e-04
## 27         0.1273559  1.138574e-03  1.623942e-07 2.776350e-07
## 28         0.8518135  8.742331e-01  5.565077e-01 5.861759e-01
## 29         0.9807892  5.186917e-09  1.030526e-09 2.179723e-11
## 30         0.6592391  9.347604e-05  2.035139e-03 6.479487e-05
## 31         0.9834609  8.443530e-07  1.362654e-06 4.676703e-08
## 32         0.9295302  9.730442e-01  8.283668e-01 8.390033e-01
## 33         0.7875216  9.899952e-01  8.654541e-01 7.850196e-01
##                              var
## 1        Simon_Dual_identity_SCL
## 2                       GG_Angst
## 3                 Stereotype_SCL
## 4                Symb_Threat_SCL
## 5                Real_Threat_SCL
## 6                            ITT
## 7                   Humanization
## 8       Peception_of_GC_mediator
## 9         Peception_of_GC_bridge
## 10  Peception_of_GC_fifth_column
## 11      Peception_of_GC_traitors
## 12        Peception_of_GC_unique
## 13          Peception_of_GC_weak
## 14          Peception_of_GC_lmlm
## 15 Positive_perception_of_GC_SCL
## 16 Negative_perception_of_GC_SCL
## 17                      Surprise
## 18               Common_fate_SCL
## 19                      CIIM_SCL
## 20                       SDO_SCL
## 21            Identification_SCL
## 22              Essentialism_SCL
## 23                       Hatered
## 24                          Fear
## 25                         Anger
## 26                          Hope
## 27                       Empathy
## 28                       Despair
## 29                    Policy_SCL
## 30       Resource_allocation_SCL
## 31           Social_Distance_SCL
## 32                           Age
## 33                        Gender
##    increase-decrease same-decrease same-increase      ANOVA
## 1       -0.419779878     2.0110527     2.4539262  3.3783957
## 2       -0.221675159    -4.3968198    -4.2421067 12.2760771
## 3       -0.552838607    -5.4074505    -4.9410163 17.7110846
## 4       -1.224310068    -7.1596487    -6.0568582 29.3384951
## 5       -0.299758158    -7.7131758    -7.5295985 38.1966905
## 6       -0.385672361    -6.7941347    -6.5125582 29.1377149
## 7       -0.402114323     5.4339927     5.9090915 21.2605740
## 8        0.372019670     4.0158589     3.7074505  9.8514509
## 9        0.052413461     3.5095566     3.5087924  8.0940790
## 10      -1.750229477    -6.2271132    -4.5924834 20.6319514
## 11      -1.137110883    -5.7970156    -4.7604044 18.8765622
## 12      -0.284759047     4.8600981     5.2112161 16.7318719
## 13      -0.100021657    -4.6452787    -4.6140634 14.0887818
## 14      -0.411328267    -2.0043631    -1.6280460  2.2421113
## 15       0.047498060     4.8220048     4.8451164 15.3567643
## 16      -1.568573651    -6.4741388    -5.0221209 22.8188468
## 17       0.631167616    -5.3630574    -6.0783836 21.4464797
## 18      -0.566085163    -4.2478967    -3.7515929 10.6439741
## 19       0.125438522     4.1772421     4.1141904 11.2989079
## 20       0.927692906    -5.1380611    -6.1268494 21.3629596
## 21      -0.357443376    -1.7510678    -1.4241836  1.7125527
## 22       1.235462186    -3.6464794    -4.9169586 12.8933276
## 23       0.264299956    -4.5208762    -4.8469119 14.4757368
## 24       0.634201669    -5.0739451    -5.7725551 19.5869362
## 25      -0.114638468    -5.1504239    -5.1121281 17.3072258
## 26      -0.007132052     3.5949081     3.6540662  8.6365951
## 27      -1.945915427     3.5769755     5.5466298 15.7047378
## 28       0.539640306    -0.4939644    -1.0329665  0.5348771
## 29       0.187584463    -6.1696066    -6.4439437 26.1833218
## 30       0.870504426     4.2118129     3.4149673  9.8899599
## 31       0.173933114     5.2276094     5.1319953 17.6401165
## 32       0.364153255    -0.2226483    -0.5847660  0.1756207
## 33       0.658724797     0.1350528    -0.5121911  0.2421989
##                              var
## 1        Simon_Dual_identity_SCL
## 2                       GG_Angst
## 3                 Stereotype_SCL
## 4                Symb_Threat_SCL
## 5                Real_Threat_SCL
## 6                            ITT
## 7                   Humanization
## 8       Peception_of_GC_mediator
## 9         Peception_of_GC_bridge
## 10  Peception_of_GC_fifth_column
## 11      Peception_of_GC_traitors
## 12        Peception_of_GC_unique
## 13          Peception_of_GC_weak
## 14          Peception_of_GC_lmlm
## 15 Positive_perception_of_GC_SCL
## 16 Negative_perception_of_GC_SCL
## 17                      Surprise
## 18               Common_fate_SCL
## 19                      CIIM_SCL
## 20                       SDO_SCL
## 21            Identification_SCL
## 22              Essentialism_SCL
## 23                       Hatered
## 24                          Fear
## 25                         Anger
## 26                          Hope
## 27                       Empathy
## 28                       Despair
## 29                    Policy_SCL
## 30       Resource_allocation_SCL
## 31           Social_Distance_SCL
## 32                           Age
## 33                        Gender

Draw heatmap

ggplot(combine.pf, aes(comparison, var, fill = sig_level, colour = dir)) + #x-axis comparison
  geom_tile() + 
  scale_fill_manual(values = c(sig = "black",marginally_sig = "gray",non_sig = "white")) +
  scale_colour_manual(values = c(positive = "green", negative = "red", size=50))+
  xlab("") + ylab("")+
  ggtitle("Heatmap focus on difference in \"Mexican_3\"")+
  theme(axis.text.x = element_text( angle = 90, hjust = 1))

Mexican_4

###p-value preparation
p_Mexican_4<- data.frame(matrix(ncol = 4))

for (i in 2:34){
  p_Mexican_4[(i-1),] <- c(p_rst(wave.compare4[,i], wave.compare4), anova_p(wave.compare4[,i], wave.compare4))
}

colnames(p_Mexican_4) <- c("increase-decrease", "same-decrease", "same-increase", "ANOVA") 
p_Mexican_4$var<-colnames(wave.compare4[,-1])

###f-value preparation
f_Mexican_4<-data.frame(matrix(ncol = 4))

for (i in 2:34){
  f_Mexican_4[(i-1),] <- c(f_rst(wave.compare4[,i], wave.compare4), anova_f(wave.compare4[,i], wave.compare4))
}

colnames(f_Mexican_4) <- c("increase-decrease", "same-decrease", "same-increase", "ANOVA") 
f_Mexican_4$var <-colnames(wave.compare4[,-1])

p_Mexican_4;f_Mexican_4
##    increase-decrease same-decrease same-increase        ANOVA
## 1        0.154065147  0.0907075781  0.8546304224 5.630736e-02
## 2        0.814060371  0.0094759400  0.0525936731 1.212146e-02
## 3        0.999398088  0.0085913394  0.0138152849 6.750637e-03
## 4        0.819130256  0.0002180490  0.0025360192 2.601431e-04
## 5        0.988263105  0.0001462121  0.0004765781 9.897056e-05
## 6        0.911094479  0.0001874061  0.0013044745 1.821618e-04
## 7        0.525074514  0.0001006629  0.0050322635 1.663013e-04
## 8        0.116037038  0.0137494279  0.5047004024 1.132806e-02
## 9        0.022917859  0.0381782222  0.9683791539 8.689948e-03
## 10       0.972927281  0.0213894811  0.0179796636 1.316472e-02
## 11       0.998781882  0.0031785325  0.0057868287 2.366955e-03
## 12       0.114069597  0.0019400658  0.2115386390 2.213905e-03
## 13       0.801403249  0.0534901708  0.0181786292 2.002604e-02
## 14       0.291731276  0.4731806933  0.0552574478 6.179414e-02
## 15       0.028238971  0.0023197458  0.4653697481 1.304987e-03
## 16       0.994266896  0.0047256980  0.0056896989 2.980615e-03
## 17       0.230907928  0.0015221306  0.0872009090 2.282992e-03
## 18       0.936226278  0.0728536184  0.1624580296 8.104885e-02
## 19       0.674172085  0.0016588543  0.0229933625 2.407893e-03
## 20       0.194752158  0.0034845816  0.1929374679 4.468273e-03
## 21       0.041596144  0.2642502273  0.8986354058 4.138036e-02
## 22       0.337406023  0.1904451159  0.8491619741 1.555863e-01
## 23       0.973846900  0.0153328036  0.0131161303 9.179056e-03
## 24       0.765806969  0.0009944959  0.0109488819 1.325623e-03
## 25       0.800409478  0.0023605510  0.0187569232 3.051463e-03
## 26       0.603648420  0.1072457657  0.4692546630 1.259374e-01
## 27       0.600077029  0.0001163688  0.0041050962 1.804907e-04
## 28       0.386193330  0.9827912385  0.6531203904 3.997956e-01
## 29       0.598545352  0.0003918812  0.0103302951 6.082460e-04
## 30       0.463453161  0.0944363940  0.5459513509 1.037119e-01
## 31       0.942192812  0.0056332559  0.0189307484 5.865683e-03
## 32       0.008990561  0.2331657882  0.7022508433 1.082707e-02
## 33       0.971022633  0.9877118777  0.9990817475 9.712111e-01
##                              var
## 1        Simon_Dual_identity_SCL
## 2                       GG_Angst
## 3                 Stereotype_SCL
## 4                Symb_Threat_SCL
## 5                Real_Threat_SCL
## 6                            ITT
## 7                   Humanization
## 8       Peception_of_GC_mediator
## 9         Peception_of_GC_bridge
## 10  Peception_of_GC_fifth_column
## 11      Peception_of_GC_traitors
## 12        Peception_of_GC_unique
## 13          Peception_of_GC_weak
## 14          Peception_of_GC_lmlm
## 15 Positive_perception_of_GC_SCL
## 16 Negative_perception_of_GC_SCL
## 17                      Surprise
## 18               Common_fate_SCL
## 19                      CIIM_SCL
## 20                       SDO_SCL
## 21            Identification_SCL
## 22              Essentialism_SCL
## 23                       Hatered
## 24                          Fear
## 25                         Anger
## 26                          Hope
## 27                       Empathy
## 28                       Despair
## 29                    Policy_SCL
## 30       Resource_allocation_SCL
## 31           Social_Distance_SCL
## 32                           Age
## 33                        Gender
##    increase-decrease same-decrease same-increase      ANOVA
## 1         1.85276502     2.1022007    0.53405159 2.89853546
## 2        -0.61125319    -2.9483825   -2.33251720 4.46374452
## 3        -0.03304664    -2.9800578   -2.82353097 5.06356978
## 4        -0.60194682    -4.0064678   -3.35179153 8.43380455
## 5        -0.14634179    -4.1044544   -3.80852907 9.44509072
## 6        -0.41107397    -4.0438370   -3.53961802 8.80609738
## 7         1.08319248     4.1941986    3.14785798 8.90138130
## 8         1.98994047     2.8251447    1.11610799 4.53302103
## 9         2.64753255     2.4584312    0.24143802 4.80456704
## 10        0.22313725    -2.6721564   -2.73321193 4.37928501
## 11       -0.04701899    -3.2858348   -3.10481558 6.14210319
## 12        1.99792880     3.4285675    1.68680280 6.21109474
## 13        0.63413824    -2.3256971   -2.72937978 3.95071619
## 14        1.50067483    -1.1677907   -2.31252819 2.80417495
## 15        2.57172366     3.3775111    1.18077002 6.75746386
## 16        0.10212253    -3.1670507   -3.11005883 5.90436501
## 17       -1.63916573    -3.5068217   -2.12194537 6.21300813
## 18       -0.34579861    -2.1975503   -1.82600550 2.52917392
## 19        0.84672261     3.4727532    2.64635381 6.12440922
## 20       -1.73155621    -3.2586456   -1.73655361 5.48751294
## 21       -2.42532493    -1.5602538    0.44044133 3.21144257
## 22       -1.40865753    -1.7434715   -0.54486532 1.86957501
## 23        0.21926247    -2.7881230   -2.84102464 4.74845096
## 24       -0.69627465    -3.6137964   -2.90111380 6.74122428
## 25       -0.63591533    -3.3724934   -2.71844817 5.88015458
## 26        0.95838806     2.0264959    1.17430513 2.08316140
## 27        0.96402316     4.1595373    3.20966664 8.81573552
## 28       -1.31764324    -0.1774499    0.88021751 0.91898850
## 29       -0.96644019    -3.8588853   -2.92022376 7.54919853
## 30        1.18396646     2.0842287    1.04977193 2.27952970
## 31        0.32870164     3.1131433    2.71522045 5.20783671
## 32       -2.96541535    -1.6326013    0.80160546 4.57932500
## 33       -0.23096708    -0.1497600    0.04082035 0.02921363
##                              var
## 1        Simon_Dual_identity_SCL
## 2                       GG_Angst
## 3                 Stereotype_SCL
## 4                Symb_Threat_SCL
## 5                Real_Threat_SCL
## 6                            ITT
## 7                   Humanization
## 8       Peception_of_GC_mediator
## 9         Peception_of_GC_bridge
## 10  Peception_of_GC_fifth_column
## 11      Peception_of_GC_traitors
## 12        Peception_of_GC_unique
## 13          Peception_of_GC_weak
## 14          Peception_of_GC_lmlm
## 15 Positive_perception_of_GC_SCL
## 16 Negative_perception_of_GC_SCL
## 17                      Surprise
## 18               Common_fate_SCL
## 19                      CIIM_SCL
## 20                       SDO_SCL
## 21            Identification_SCL
## 22              Essentialism_SCL
## 23                       Hatered
## 24                          Fear
## 25                         Anger
## 26                          Hope
## 27                       Empathy
## 28                       Despair
## 29                    Policy_SCL
## 30       Resource_allocation_SCL
## 31           Social_Distance_SCL
## 32                           Age
## 33                        Gender

Draw heatmap

ggplot(combine.pf, aes(comparison, var, fill = sig_level, colour = dir)) + #x-axis comparison
  geom_tile() + 
  scale_fill_manual(values = c(sig = "black",marginally_sig = "gray",non_sig = "white")) +
  scale_colour_manual(values = c(positive = "green", negative = "red", size=50))+
  xlab("") + ylab("")+
  ggtitle("Heatmap focus on difference in \"Mexican_4\"")+
  theme(axis.text.x = element_text( angle = 90, hjust = 1))

within subject analysis comparing wave 1 and wave 2

#Multiple grouping variables
#Two independent sample comparisons after grouping the data by another variable:
#ggboxplot(wave, x = "key", y = "value", 
 #         color = "gp", palette = "jco", add = "jitter")+
  #stat_compare_means(aes(group = gp), method = "anova", label.y = 6.5)+
  #xlab("")

ggboxplot(wave, x = "gp", y = "value",
          color = "gp", palette = "jco",
          add = "jitter",
          facet.by = "key")+
  stat_compare_means(label = "p.format", method = "anova", label.x = 1.5)

wave$gp<-as.factor(wave$gp)
wave$key<-as.factor(wave$key)
#Paired sample comparisons after grouping the data by another variable
#ggpaired(wave, x = "key", y = "value",
#          color = "key", palette = "jco",
#          line.color = "gray", line.size = 0.4,
#          facet.by = "gp")+
#  stat_compare_means(label = "p.format", paired = TRUE)


##wave 1 vs wave 2 Overall Comparison
###Essentialism
wave.ess<-wave_select %>% dplyr::select(Essentialism_SCL.x, Essentialism_SCL.y) %>%
  dplyr::rename(wave1=Essentialism_SCL.x, wave2=Essentialism_SCL.y)

p1<-ggpaired(wave.ess, cond1 = "wave1", cond2 = "wave2",
         color = "condition", palette = "jco",
         line.color="gray", line.size = 0.4)+
  xlab("Essentialism")+
  stat_compare_means(label = "p.format", paired = TRUE)

###Identification
wave.id<-wave_select %>% dplyr::select(Identification_SCL.x, Identification_SCL.y) %>%
  dplyr::rename(wave1=Identification_SCL.x, wave2=Identification_SCL.y)

p2<-ggpaired(wave.id, cond1 = "wave1", cond2 = "wave2",
         color = "condition", palette = "jco",
         line.color="gray", line.size = 0.4)+
  xlab("Identification")+
  stat_compare_means(label = "p.format", paired = TRUE)


###Mexican_3
wave.mx3<-wave_select %>% dplyr::select(Mexican_3, GG_level_3) %>%
  dplyr::rename(wave1=Mexican_3, wave2=GG_level_3)

p3<-ggpaired(wave.mx3, cond1 = "wave1", cond2 = "wave2",
         color = "condition", palette = "jco",
         line.color="gray", line.size = 0.4)+
  xlab("Mexican_3")+
  stat_compare_means(label = "p.format", paired = TRUE)


###Mexican_4
wave.mx4<-wave_select %>% dplyr::select(Mexican_4, GG_level_4) %>%
  dplyr::rename(wave1=Mexican_4, wave2=GG_level_4)

p4<-ggpaired(wave.mx4, cond1 = "wave1", cond2 = "wave2",
         color = "condition", palette = "jco",
         line.color="gray", line.size = 0.4)+
  xlab("Mexican_4")+
  stat_compare_means(label = "p.format", paired = TRUE)

###GG_level_Mexican
wave.mx<-wave_select %>% dplyr::select(GG_level_Mexican, GG_Level) %>%
  dplyr::rename(wave1=GG_level_Mexican, wave2=GG_Level) 

p5 <- ggpaired(wave.mx, cond1 = "wave1", cond2 = "wave2",
         color = "condition", palette = "jco",
         line.color="gray", line.size = 0.4)+
  xlab("GG_level_Mexican")+
  stat_compare_means(label = "p.format", paired = TRUE)

grid.arrange(p1,p2,p3,p4,p5, nrow = 2, top = "Overall Comparison")

######### 4 Conditions

x<-c("1", "2","3", "4")
y<-c("Essentialism", "Identification", "Mexican_3", "Mexican_4", "GG_level_Mexican")

pairplot <- function(i,j){
  cond1 <- wave_select %>% dplyr::filter(CONDITION == x[i])
  plotdata <- cond1[,(2*j):(2*j+1)]
  colnames(plotdata)<-c("wave1","wave2")
  p<-ggpaired(plotdata, cond1 = "wave1", cond2 = "wave2",
         color = "condition", palette = "jco",
         line.color="gray", line.size = 0.4)+
  ggtitle(paste('Condition', i, y[j]))+
  xlab("")+
  stat_compare_means(label = "p.format", paired = TRUE)
  print(p)
}
#pairplot(2,1)

for (i in 1:4) {
  for (j in 1:5) {
  pairplot(i,j)
  }
}
## Warning: Removed 2 rows containing non-finite values (stat_boxplot).
## Warning: Removed 2 rows containing non-finite values (stat_compare_means).
## Warning: Computation failed in `stat_compare_means()`:
## 'x' and 'y' must have the same length
## Warning: Removed 2 rows containing missing values (geom_path).
## Warning: Removed 2 rows containing missing values (geom_point).

## Warning: Removed 2 rows containing non-finite values (stat_boxplot).
## Warning: Removed 2 rows containing non-finite values (stat_compare_means).
## Warning: Computation failed in `stat_compare_means()`:
## 'x' and 'y' must have the same length
## Warning: Removed 2 rows containing missing values (geom_path).
## Warning: Removed 2 rows containing missing values (geom_point).

## Warning: Removed 4 rows containing non-finite values (stat_boxplot).
## Warning: Removed 4 rows containing non-finite values (stat_compare_means).
## Warning: Removed 4 rows containing missing values (geom_path).
## Warning: Removed 4 rows containing missing values (geom_point).

## Warning: Removed 2 rows containing non-finite values (stat_boxplot).
## Warning: Removed 2 rows containing non-finite values (stat_compare_means).
## Warning: Computation failed in `stat_compare_means()`:
## 'x' and 'y' must have the same length
## Warning: Removed 2 rows containing missing values (geom_path).
## Warning: Removed 2 rows containing missing values (geom_point).

## Warning: Removed 2 rows containing non-finite values (stat_boxplot).
## Warning: Removed 2 rows containing non-finite values (stat_compare_means).
## Warning: Computation failed in `stat_compare_means()`:
## 'x' and 'y' must have the same length
## Warning: Removed 2 rows containing missing values (geom_path).
## Warning: Removed 2 rows containing missing values (geom_point).

## Warning: Removed 2 rows containing non-finite values (stat_boxplot).
## Warning: Removed 2 rows containing non-finite values (stat_compare_means).
## Warning: Computation failed in `stat_compare_means()`:
## 'x' and 'y' must have the same length
## Warning: Removed 2 rows containing missing values (geom_path).
## Warning: Removed 2 rows containing missing values (geom_point).

## Warning: Removed 6 rows containing non-finite values (stat_boxplot).
## Warning: Removed 6 rows containing non-finite values (stat_compare_means).
## Warning: Removed 6 rows containing missing values (geom_path).
## Warning: Removed 6 rows containing missing values (geom_point).

## Warning: Removed 3 rows containing non-finite values (stat_boxplot).
## Warning: Removed 3 rows containing non-finite values (stat_compare_means).
## Warning: Computation failed in `stat_compare_means()`:
## 'x' and 'y' must have the same length
## Warning: Removed 3 rows containing missing values (geom_path).
## Warning: Removed 3 rows containing missing values (geom_point).

## Warning: Removed 2 rows containing non-finite values (stat_boxplot).
## Warning: Removed 2 rows containing non-finite values (stat_compare_means).
## Warning: Removed 2 rows containing missing values (geom_path).
## Warning: Removed 2 rows containing missing values (geom_point).

## Warning: Removed 1 rows containing non-finite values (stat_boxplot).
## Warning: Removed 1 rows containing non-finite values (stat_compare_means).
## Warning: Computation failed in `stat_compare_means()`:
## 'x' and 'y' must have the same length
## Warning: Removed 1 rows containing missing values (geom_path).
## Warning: Removed 1 rows containing missing values (geom_point).

## Warning: Removed 4 rows containing non-finite values (stat_boxplot).
## Warning: Removed 4 rows containing non-finite values (stat_compare_means).
## Warning: Computation failed in `stat_compare_means()`:
## 'x' and 'y' must have the same length
## Warning: Removed 4 rows containing missing values (geom_path).
## Warning: Removed 4 rows containing missing values (geom_point).

## Warning: Removed 4 rows containing non-finite values (stat_boxplot).
## Warning: Removed 4 rows containing non-finite values (stat_compare_means).
## Warning: Computation failed in `stat_compare_means()`:
## 'x' and 'y' must have the same length
## Warning: Removed 4 rows containing missing values (geom_path).
## Warning: Removed 4 rows containing missing values (geom_point).

## Warning: Removed 10 rows containing non-finite values (stat_boxplot).
## Warning: Removed 10 rows containing non-finite values (stat_compare_means).
## Warning: Removed 10 rows containing missing values (geom_path).
## Warning: Removed 10 rows containing missing values (geom_point).

## Warning: Removed 5 rows containing non-finite values (stat_boxplot).
## Warning: Removed 5 rows containing non-finite values (stat_compare_means).
## Warning: Computation failed in `stat_compare_means()`:
## 'x' and 'y' must have the same length
## Warning: Removed 5 rows containing missing values (geom_path).
## Warning: Removed 5 rows containing missing values (geom_point).