# script for preparing the Kingston University studydata April 2020

setwd(dirname(rstudioapi::getActiveDocumentContext()$path))

library(readxl)
library(psych)
library(car)
library(ggplot2)
library(ggpubr)

studydata = read_excel("surveydata_cleaned_FULLAnon.xlsx")

# rename variables
colnames(studydata)[10] <- "age"
colnames(studydata)[11] <- "gender"
colnames(studydata)[12] <- "nationality"
colnames(studydata)[13] <- "residence"
colnames(studydata)[14] <- "postcode"
colnames(studydata)[15] <- "edu"
colnames(studydata)[16] <- "identity_religious"
colnames(studydata)[18] <- "covid_infection_status"

#national identity vars
colnames(studydata)[19] <- "sid_nation1"
colnames(studydata)[20] <- "sid_nation2"
colnames(studydata)[21] <- "sid_nation3"
colnames(studydata)[22] <- "sid_nation4"
# calculate national identity
studydata$sid_nation_full <- rowMeans(studydata[,19:22])
scale_sid_nation <- studydata[,19:22]

#national fusion vars
colnames(studydata)[23] <- "ift_nation1"
colnames(studydata)[24] <- "ift_nation2"
colnames(studydata)[25] <- "ift_nation3"
colnames(studydata)[26] <- "ift_nation4"
colnames(studydata)[27] <- "ift_nation5"
colnames(studydata)[28] <- "ift_nation6"
colnames(studydata)[29] <- "ift_nation7"
studydata$ift_nation_full <- rowMeans(studydata[,23:29])
scale_ift_nation <- studydata[,23:29]

# recode undesirables
colnames(studydata)[30] <- "wvs_undes_drugaddicts"
studydata$wvs_undes_drugaddicts[is.na(studydata$wvs_undes_drugaddicts)] <- 0
colnames(studydata)[31] <- "wvs_undes_diff_race"
studydata$wvs_undes_diff_race[is.na(studydata$wvs_undes_diff_race)] <- 0
colnames(studydata)[32] <- "wvs_undes_aids"
studydata$wvs_undes_aids[is.na(studydata$wvs_undes_aids)] <- 0
colnames(studydata)[33] <- "wvs_undes_immigrants"
studydata$wvs_undes_immigrants[is.na(studydata$wvs_undes_immigrants)] <- 0
colnames(studydata)[34] <- "wvs_undes_homosexuals"
studydata$wvs_undes_homosexuals[is.na(studydata$wvs_undes_homosexuals)] <- 0
colnames(studydata)[35] <- "wvs_undes_diff_religion"
studydata$wvs_undes_diff_religion[is.na(studydata$wvs_undes_diff_religion)] <- 0
colnames(studydata)[36] <- "wvs_undes_drinkers"
studydata$wvs_undes_drinkers[is.na(studydata$wvs_undes_drinkers)] <- 0
colnames(studydata)[37] <- "wvs_undes_unmarriedcouples"
studydata$wvs_undes_unmarriedcouples[is.na(studydata$wvs_undes_unmarriedcouples)] <- 0
colnames(studydata)[38] <- "wvs_undes_diff_language"
studydata$wvs_undes_diff_language[is.na(studydata$wvs_undes_diff_language)] <- 0

colnames(studydata)[43] <- "feelingstoward_muslims"
colnames(studydata)[44] <- "feelingstoward_christians"
colnames(studydata)[45] <- "feelingstoward_jews"
colnames(studydata)[46] <- "feelingstoward_atheists"
colnames(studydata)[47] <- "feelingstoward_immigrants"
colnames(studydata)[48] <- "feelingstoward_other_races"

#rename economic spectrums
colnames(studydata)[49] <- "politics_economic"
colnames(studydata)[50] <- "politics_social"
colnames(studydata)[51] <- "party_support"
colnames(studydata)[52] <- "EU_referendum"

#rename nationalism score
colnames(studydata)[53] <- "nationalism_1"
colnames(studydata)[54] <- "nationalism_2"
colnames(studydata)[55] <- "nationalism_3"
colnames(studydata)[56] <- "nationalism_4"
colnames(studydata)[57] <- "nationalism_5"
colnames(studydata)[58] <- "nationalism_6"
colnames(studydata)[59] <- "nationalism_7"
colnames(studydata)[60] <- "nationalism_8"
studydata$nationalism_orig <- rowMeans(studydata[,53:59])
studydata$nationalism_full <- rowMeans(studydata[,53:60])
scale_nationalism <- studydata[,53:59]
scale_nationalism_extended <- studydata[,53:60]

# rename supernatural belief scale
colnames(studydata)[61] <- "sbs1"
colnames(studydata)[62] <- "sbs2"
colnames(studydata)[63] <- "sbs3"
colnames(studydata)[64] <- "sbs4"
colnames(studydata)[65] <- "sbs5"
colnames(studydata)[66] <- "sbs6"
colnames(studydata)[67] <- "sbs7"
colnames(studydata)[68] <- "sbs8"
colnames(studydata)[69] <- "sbs9"
colnames(studydata)[70] <- "sbs10"
studydata$sbs_orig_full <- rowMeans(studydata[,61:70])
scale_sbs_origional <- studydata[,61:70]
# these two are not part of the origional scale, they're additions to address karma and a "universal force"
colnames(studydata)[71] <- "sbs11"
colnames(studydata)[72] <- "sbs12"
studydata$sbs_extended_full <- rowMeans(studydata[,61:70])
scale_sbs_extended <- studydata[,61:70]
# frequency of religious attendance
colnames(studydata)[73] <- "frequency"

# religious identity
colnames(studydata)[74] <- "sid_religious1"
colnames(studydata)[75] <- "sid_religious2"
colnames(studydata)[76] <- "sid_religious3"
colnames(studydata)[77] <- "sid_religious4"
studydata$sid_religious_full <- rowMeans(studydata[,74:77])
scale_sid_religion <- studydata[,74:77]

# fusion with religion
colnames(studydata)[78] <- "ift_religious1"
colnames(studydata)[79] <- "ift_religious2"
colnames(studydata)[80] <- "ift_religious3"
colnames(studydata)[81] <- "ift_religious4"
colnames(studydata)[82] <- "ift_religious5"
colnames(studydata)[83] <- "ift_religious6"
colnames(studydata)[84] <- "ift_religious7"
studydata$ift_religious_full <- rowMeans(studydata[,78:84])
scale_ift_religion <- studydata[,78:84]

# rename and reverse code appropriate personality variables
colnames(studydata)[85] <- "big_5_reserved"
studydata$big_5_reserved <- recode(studydata$big_5_reserved,  '1=5; 2=4; 3=3; 4=2; 5=1')
colnames(studydata)[86] <- "big_5_trusting"
colnames(studydata)[87] <- "big_5_lazy"
studydata$big_5_lazy <- recode(studydata$big_5_lazy,  '1=5; 2=4; 3=3; 4=2; 5=1')
colnames(studydata)[88] <- "big_5_relaxed"
colnames(studydata)[89] <- "big_5_unartistic"
studydata$big_5_unartistic <- recode(studydata$big_5_unartistic,  '1=5; 2=4; 3=3; 4=2; 5=1')
colnames(studydata)[90] <- "big_5_outgoing"
colnames(studydata)[91] <- "big_5_faultfinding"
studydata$big_5_faultfinding <- recode(studydata$big_5_faultfinding,  '1=5; 2=4; 3=3; 4=2; 5=1')
colnames(studydata)[92] <- "big_5_thorough"
colnames(studydata)[93] <- "big_5_nervous"
studydata$big_5_nervous <- recode(studydata$big_5_nervous,  '1=5; 2=4; 3=3; 4=2; 5=1')
colnames(studydata)[94] <- "big_5_imaginative"
scale_big_5 <- studydata[,85:94]
# now calculate the 5 dimensions
Openness <- data.frame(studydata$big_5_imaginative, studydata$big_5_unartistic)
Conscientiousness <- data.frame(studydata$big_5_thorough, studydata$big_5_trusting)
Extraversion <- data.frame(studydata$big_5_reserved, studydata$big_5_outgoing)
Agreeableness <- data.frame(studydata$big_5_faultfinding, studydata$big_5_relaxed)
Neuroticism <- data.frame(studydata$big_5_nervous, studydata$big_5_lazy)

studydata$Openness_full <- rowMeans(Openness)
studydata$Conscientiousness_full <- rowMeans(Conscientiousness)
studydata$Extraversion_full <- rowMeans(Extraversion)
studydata$Agreeableness_full <- rowMeans(Agreeableness)
studydata$Neuroticism_full <- rowMeans(Neuroticism)

#threat perception
colnames(studydata)[95] <- "cultureWar"
colnames(studydata)[96] <- "waylifethreatcovid"
colnames(studydata)[97] <- "financiallysecure"
studydata$financiallysecure <- recode(studydata$financiallysecure,  '1=5; 2=4; 3=3; 4=2; 5=1')
colnames(studydata)[98] <- "financiallyinsecure"
colnames(studydata)[99] <- "naturaldisasterconcern"
colnames(studydata)[100] <- "naturaldisasterunconcern"
studydata$naturaldisasterunconcern <- recode(studydata$naturaldisasterunconcern,  '1=5; 2=4; 3=3; 4=2; 5=1')
colnames(studydata)[101] <- "sociallysecure"
studydata$sociallysecure <- recode(studydata$sociallysecure,  '1=5; 2=4; 3=3; 4=2; 5=1')
colnames(studydata)[102] <- "sociallyinsecure"
colnames(studydata)[103] <- "contagionconcerned"
colnames(studydata)[104] <- "contagionunconcerned"
studydata$contagionunconcerned <- recode(studydata$contagionunconcerned,  '1=5; 2=4; 3=3; 4=2; 5=1')
colnames(studydata)[105] <- "predationconcern"
colnames(studydata)[106] <- "worldwillneverbethesame"
colnames(studydata)[107] <- "predationunconcerned"
studydata$predationunconcerned <- recode(studydata$predationunconcerned,  '1=5; 2=4; 3=3; 4=2; 5=1')

threat_extended <- data.frame(studydata$financiallyinsecure,
                     studydata$financiallysecure,
                     studydata$naturaldisasterunconcern,
                     studydata$naturaldisasterconcern,
                     studydata$sociallyinsecure,
                     studydata$sociallysecure,
                     studydata$contagionconcerned,
                     studydata$contagionunconcerned,
                     studydata$predationconcern,
                     studydata$predationunconcerned)
studydata$threat_extended_full <- rowMeans(threat_extended)
studydata$threat_social <- rowMeans(threat_extended[,5:6])
studydata$threat_natural <- rowMeans(threat_extended[,3:4])
studydata$threat_financial <- rowMeans(threat_extended[,1:2])
studydata$threat_contagion <- rowMeans(threat_extended[,7:8])
studydata$threat_predation <- rowMeans(threat_extended[,9:10])

scale_threat_extended <- threat_extended
rm(threat_extended)
threat_simple <- data.frame(studydata$naturaldisasterunconcern,
                            studydata$naturaldisasterconcern,
                            studydata$sociallyinsecure,
                            studydata$sociallysecure,
                            studydata$contagionconcerned,
                            studydata$contagionunconcerned,
                            studydata$predationconcern,
                            studydata$predationunconcerned)
studydata$threat_simple_full <- rowMeans(threat_simple)
scale_threat_simple <- threat_simple
rm(threat_simple)
colnames(studydata)[108] <- "wayoflifethreatened"
colnames(studydata)[109] <- "socialmedia_timespent"
colnames(studydata)[110] <- "media_frequency"
colnames(studydata)[111] <- "news_preference"

###################################################################################################################################
# Descriptives

#gender
table(studydata$gender)

#age
table(studydata$age)
mean(na.omit(as.numeric(studydata$age)))
sd(na.omit(as.numeric(studydata$age)))
hist(as.numeric(studydata$age),
     main="Participant Age Distribution",
     xlab="Age")

#nationality
table(studydata$nationality)
table(studydata$residence)

#education
table(studydata$edu)

#infection status
table(studydata$covid_infection_status)

# party
table(studydata$party_support)

#eu referendum
table(studydata$EU_referendum)

#religion
table(studydata$identity_religious)

# religious frequency
table(studydata$frequency)
#hist(as.numeric(studydata$frequency))
hist(as.numeric(studydata$frequency),
     main="Religious Attendance",
     xlab="Freqeuncy")

# economic politics
#hist(as.numeric(studydata$politics_economic))
hist(as.numeric(studydata$politics_economic),
     main="Economic Values (liberal-conservative)",
     xlab="Economic Values")

# social politics
#hist(as.numeric(studydata$politics_social))
hist(as.numeric(studydata$politics_social),
     main="Social Values (liberal-conservative)",
     xlab="Social Values")

# Default call (as object)
#p <- ggplot(studydata, aes(politics_economic,politics_social))
#h3 <- p + stat_bin2d()
#h3

qplot(politics_economic,politics_social,data=studydata, geom='bin2d')

qplot(ift_nation_full,sid_nation_full,data=studydata, geom='bin2d')
qplot(ift_religious_full,sid_religious_full,data=studydata, geom='bin2d')

qplot(ift_nation_full,politics_economic,data=studydata, geom='bin2d')
qplot(ift_religious_full,politics_economic,data=studydata, geom='bin2d')

qplot(ift_nation_full,politics_social,data=studydata, geom='bin2d')
qplot(ift_religious_full,politics_social,data=studydata, geom='bin2d')

qplot(ift_nation_full,feelingstoward_other_races,data=studydata, geom='bin2d')
qplot(ift_religious_full,feelingstoward_other_races,data=studydata, geom='bin2d')
qplot(ift_nation_full,feelingstoward_immigrants,data=studydata, geom='bin2d')
qplot(ift_religious_full,feelingstoward_immigrants,data=studydata, geom='bin2d')

qplot(ift_religious_full,sbs_extended_full,data=studydata, geom='bin2d')
qplot(sid_religious_full,sbs_extended_full,data=studydata, geom='bin2d')

qplot(ift_religious_full,sbs_orig_full,data=studydata, geom='bin2d')
qplot(sid_religious_full,sbs_orig_full,data=studydata, geom='bin2d')

qplot(ift_religious_full,sbs11,data=studydata, geom='bin2d')
qplot(ift_religious_full,sbs12,data=studydata, geom='bin2d')

# threat
#hist(as.numeric(studydata$threat_simple_full))
hist(as.numeric(studydata$threat_simple_full),
     main="Threat",
     xlab="Threat")
#hist(as.numeric(studydata$threat_extended_full))
hist(as.numeric(studydata$threat_extended_full),
     main="Threat",
     xlab="Threat (extended)")

# nationalism
#hist(as.numeric(studydata$nationalism_full))
hist(as.numeric(studydata$nationalism_full),
     main="Nationalism",
     xlab="Nationalism")
#sbs
hist(as.numeric(studydata$sbs_orig_full),
     main="Supernatural Belief Scale",
     xlab="Supernatural Belief Strength")
hist(as.numeric(studydata$sbs_extended_full),
     main="Supernatural Belief Scale (extended)",
     xlab="Supernatural Belief Strength")

###################################################################################################################################
# Now for scale validation of the different scales
library(ltm)
cronbach.alpha(scale_sid_nation, standardized = TRUE, CI = TRUE,na.rm=TRUE)
cronbach.alpha(scale_sid_religion, standardized = TRUE, CI = TRUE,na.rm=TRUE)
cronbach.alpha(scale_ift_nation, standardized = TRUE, CI = TRUE,na.rm=TRUE)
cronbach.alpha(scale_ift_religion, standardized = TRUE, CI = TRUE,na.rm=TRUE)
cronbach.alpha(scale_nationalism, standardized = TRUE, CI = TRUE,na.rm=TRUE)
cronbach.alpha(scale_nationalism_extended, standardized = TRUE, CI = TRUE,na.rm=TRUE)
cronbach.alpha(scale_sbs_origional, standardized = TRUE, CI = TRUE,na.rm=TRUE)
cronbach.alpha(scale_sbs_extended, standardized = TRUE, CI = TRUE,na.rm=TRUE)
cronbach.alpha(scale_threat_simple, standardized = TRUE, CI = TRUE,na.rm=TRUE)
cronbach.alpha(scale_threat_extended, standardized = TRUE, CI = TRUE,na.rm=TRUE)

fit_personality <- principal(scale_big_5, nfactors=5, rotate="varimax")
fit_personality
load <- fit_personality$loadings[,1:2]
plot(load,type="n") # set up plot
text(load,labels=names(scale_big_5),cex=.7) # add variable names

library(nFactors)
ev <- eigen(cor(na.omit(scale_big_5))) # get eigenvalues
ap <- parallel(subject=nrow(scale_big_5),var=ncol(scale_big_5),
               rep=100,cent=.05)
nS <- nScree(x=ev$values, aparallel=ap$eigen$qevpea)
plotnScree(nS)

# PCA Variable Factor Map
library(FactoMineR)
result <- PCA(scale_big_5) # graphs generated automatically

fit_threat_simple <- principal(scale_threat_simple, nfactors=4, rotate="varimax")
fit_threat_simple
load <- fit_threat_simple$loadings[,1:2]
plot(load,type="n") # set up plot
text(load,labels=names(scale_threat_simple),cex=.7) # add variable names

library(nFactors)
ev <- eigen(cor(na.omit(scale_threat_simple))) # get eigenvalues
ap <- parallel(subject=nrow(scale_threat_simple),var=ncol(scale_threat_simple),
               rep=100,cent=.05)
nS <- nScree(x=ev$values, aparallel=ap$eigen$qevpea)
plotnScree(nS)

# PCA Variable Factor Map
library(FactoMineR)
result <- PCA(scale_threat_simple) # graphs generated automatically

fit_threat_extended <- principal(scale_threat_extended, nfactors=4, rotate="varimax")
fit_threat_extended
load <- fit_threat_extended$loadings[,1:2]
plot(load,type="n") # set up plot
text(load,labels=names(scale_threat_extended),cex=.7) # add variable names

#library(nFactors)
ev <- eigen(cor(na.omit(scale_threat_extended))) # get eigenvalues
ap <- parallel(subject=nrow(scale_threat_extended),var=ncol(scale_threat_extended),
               rep=100,cent=.05)
nS <- nScree(x=ev$values, aparallel=ap$eigen$qevpea)
plotnScree(nS)

# PCA Variable Factor Map
#library(FactoMineR)
result <- PCA(scale_threat_extended) # graphs generated automatically

fit_nationalism <- principal(scale_nationalism, nfactors=1, rotate="varimax")
fit_nationalism
load <- fit_nationalism$loadings[,1:2]
plot(load,type="n") # set up plot
text(load,labels=names(scale_nationalism),cex=.7) # add variable names

library(nFactors)
ev <- eigen(cor(na.omit(scale_nationalism))) # get eigenvalues
ap <- parallel(subject=nrow(scale_nationalism),var=ncol(scale_nationalism),
               rep=100,cent=.05)
nS <- nScree(x=ev$values, aparallel=ap$eigen$qevpea)
plotnScree(nS)

# PCA Variable Factor Map
library(FactoMineR)
result <- PCA(scale_nationalism) # graphs generated automatically

fit_nationalism_extended <- principal(scale_nationalism_extended, nfactors=2, rotate="varimax")
fit_nationalism_extended
load <- fit_nationalism_extended$loadings[,1:2]
plot(load,type="n") # set up plot
text(load,labels=names(scale_nationalism_extended),cex=.7) # add variable names

library(nFactors)
ev <- eigen(cor(na.omit(scale_nationalism_extended))) # get eigenvalues
ap <- parallel(subject=nrow(scale_nationalism_extended),var=ncol(scale_nationalism_extended),
               rep=100,cent=.05)
nS <- nScree(x=ev$values, aparallel=ap$eigen$qevpea)
plotnScree(nS)

# PCA Variable Factor Map
library(FactoMineR)
result <- PCA(scale_nationalism_extended) # graphs generated automatically

###################################################################################################################################
# Now for between groups analysis

library(ggpubr)
bpg <- ggboxplot(studydata, x = "gender", y = "threat_simple_full",
          color = "gender", palette = c("#00AFBB", "#E7B800", "#FC4E07"),
          order = c("M", "F", "O"),
          ylab = "Threat", xlab = "Gender")
bpg + rotate_x_text(45)
gen_threat_aov <- aov(threat_simple_full ~ gender, data = studydata)
summary(gen_threat_aov)
TukeyHSD(gen_threat_aov)

library(ggpubr)
bpn <- ggboxplot(studydata, x = "nationality", y = "threat_simple_full",
          color = "nationality", #palette = c("#00AFBB", "#E7B800", "#FC4E07"),
          order = unique(studydata$nationality),
          ylab = "Threat", xlab = "Nationality")
bpn + rotate_x_text(45)
nation_threat_aov <- aov(threat_simple_full ~ nationality, data = studydata)
summary(nation_threat_aov)
TukeyHSD(nation_threat_aov)

library(ggpubr)
bp <- ggboxplot(studydata, x = "identity_religious", y = "threat_simple_full",
          color = "identity_religious", #palette = c("#00AFBB", "#E7B800", "#FC4E07"),
          order = unique(studydata$identity_religious),
          ylab = "Threat", xlab = "Religious Identity")
bp + rotate_x_text(45)
rel_threat_aov <- aov(threat_simple_full ~ identity_religious, data = studydata)
summary(rel_threat_aov)
TukeyHSD(rel_threat_aov)

library(ggpubr)
bpp <- ggboxplot(studydata, x = "party_support", y = "threat_simple_full",
                color = "party_support", #palette = c("#00AFBB", "#E7B800", "#FC4E07"),
                order = c("Republican", "Democrat", "Libertarian", "Green Party", "Labor", "Liberal Democrat", "Independent", "Liberal"),
                ylab = "Threat", xlab = "Political Party")
bpp + rotate_x_text(45)
party_threat_aov <- aov(threat_simple_full ~ party_support, data = studydata)
summary(party_threat_aov)
TukeyHSD(party_threat_aov)

# now for individual threats
#threat_predation
#threat_contagion
#threat_financial
#threat_natural
#threat_social

library(ggpubr)
bpp <- ggboxplot(studydata, x = "party_support", y = "threat_social",
                 color = "party_support", #palette = c("#00AFBB", "#E7B800", "#FC4E07"),
                 order = c("Republican", "Democrat", "Libertarian", "Green Party", "Labor", "Liberal Democrat", "Independent", "Liberal"),
                 ylab = "Threat", xlab = "Political Party")
bpp + rotate_x_text(45)
party_threat_aov <- aov(threat_predation ~ party_support, data = studydata)
summary(party_threat_aov)
TukeyHSD(party_threat_aov)

bp <- ggboxplot(studydata, x = "identity_religious", y = "threat_predation",
                color = "identity_religious", #palette = c("#00AFBB", "#E7B800", "#FC4E07"),
                order = unique(studydata$identity_religious),
                ylab = "Threat", xlab = "Religious Identity")
bp + rotate_x_text(45)
rel_threat_aov <- aov(threat_social ~ identity_religious, data = studydata)
summary(rel_threat_aov)
TukeyHSD(rel_threat_aov)

# politics by nationality and religion
bp <- ggboxplot(studydata, x = "identity_religious", y = "politics_economic",
                color = "identity_religious", #palette = c("#00AFBB", "#E7B800", "#FC4E07"),
                order = unique(studydata$identity_religious),
                ylab = "Politics Economic", xlab = "Religious Identity")
bp + rotate_x_text(45)
rel_threat_aov <- aov(politics_economic ~ identity_religious, data = studydata)
summary(rel_threat_aov)
TukeyHSD(rel_threat_aov)

bp <- ggboxplot(studydata, x = "identity_religious", y = "politics_social",
                color = "identity_religious", #palette = c("#00AFBB", "#E7B800", "#FC4E07"),
                order = unique(studydata$identity_religious),
                ylab = "Politics Social", xlab = "Religious Identity")
bp + rotate_x_text(45)
rel_threat_aov <- aov(politics_social ~ identity_religious, data = studydata)
summary(rel_threat_aov)
TukeyHSD(rel_threat_aov)

library(ggpubr)
bpn <- ggboxplot(studydata, x = "nationality", y = "politics_economic",
                 color = "nationality", #palette = c("#00AFBB", "#E7B800", "#FC4E07"),
                 order = unique(studydata$nationality),
                 ylab = "Politics Economic", xlab = "Nationality")
bpn + rotate_x_text(45)
nation_threat_aov <- aov(politics_economic ~ nationality, data = studydata)
summary(nation_threat_aov)
TukeyHSD(nation_threat_aov)

bpn <- ggboxplot(studydata, x = "nationality", y = "politics_social",
                 color = "nationality", #palette = c("#00AFBB", "#E7B800", "#FC4E07"),
                 order = unique(studydata$nationality),
                 ylab = "Politcs Social", xlab = "Nationality")
bpn + rotate_x_text(45)
nation_threat_aov <- aov(politics_social ~ nationality, data = studydata)
summary(nation_threat_aov)
TukeyHSD(nation_threat_aov)

bp <- ggboxplot(studydata, x = "identity_religious", y = "nationalism_full",
                color = "identity_religious", #palette = c("#00AFBB", "#E7B800", "#FC4E07"),
                order = unique(studydata$identity_religious),
                ylab = "Nationalism", xlab = "Religious Identity")
bp + rotate_x_text(45)
rel_nationalism_aov <- aov(nationalism_full ~ identity_religious, data = studydata)
summary(rel_nationalism_aov)
TukeyHSD(rel_nationalism_aov)

bp <- ggboxplot(studydata, x = "identity_religious", y = "sbs11",
                color = "identity_religious", #palette = c("#00AFBB", "#E7B800", "#FC4E07"),
                order = unique(studydata$identity_religious),
                ylab = "SBS Karma", xlab = "Religious Identity")
bp + rotate_x_text(45)
rel_karma_aov <- aov(sbs11 ~ identity_religious, data = studydata)
summary(rel_karma_aov)
TukeyHSD(rel_karma_aov)

bp <- ggboxplot(studydata, x = "identity_religious", y = "sbs12",
                color = "identity_religious", #palette = c("#00AFBB", "#E7B800", "#FC4E07"),
                order = unique(studydata$identity_religious),
                ylab = "SBS Universe", xlab = "Religious Identity")
bp + rotate_x_text(45)
rel_universe_aov <- aov(sbs12 ~ identity_religious, data = studydata)
summary(rel_universe_aov)
TukeyHSD(rel_universe_aov)

########################################################################
# some regressions for analyses
library(QuantPsyc)

# First break out all the threat correlates by measure

nat_threat <- lm(nationalism_full ~ threat_predation + threat_contagion + threat_financial + threat_natural + threat_social,
                 data = studydata)
summary(nat_threat)

immithreat_glm_fit <- glm(wvs_undes_immigrants ~  threat_predation + threat_contagion + threat_financial + threat_natural + threat_social,
                          data = studydata)
summary(immithreat_glm_fit)

sbs_threat <- lm(sbs_orig_full ~ threat_predation + threat_contagion + threat_financial + threat_natural + threat_social,
                 data = studydata)
summary(sbs_threat)

sbs_ex_threat <- lm(sbs_extended_full ~ threat_predation + threat_contagion + threat_financial + threat_natural + threat_social,
                 data = studydata)
summary(sbs_ex_threat)

rel_sid_threat <- lm(sid_religious_full ~ threat_predation + threat_contagion + threat_financial + threat_natural + threat_social,
                    data = studydata)
summary(rel_sid_threat)

rel_ift_threat <- lm(ift_religious_full ~ threat_predation + threat_contagion + threat_financial + threat_natural + threat_social,
                     data = studydata)
summary(rel_ift_threat)

nat_sid_threat <- lm(sid_nation_full ~ threat_predation + threat_contagion + threat_financial + threat_natural + threat_social,
                     data = studydata)
summary(nat_sid_threat)

nat_ift_threat <- lm(ift_nation_full ~ threat_predation + threat_contagion + threat_financial + threat_natural + threat_social,
                     data = studydata)
summary(nat_ift_threat)

open_threat <- lm(Openness_full ~ threat_predation + threat_contagion + threat_financial + threat_natural + threat_social,
                  data = studydata)
summary(open_threat)

consc_threat <- lm(Conscientiousness_full ~ threat_predation + threat_contagion + threat_financial + threat_natural + threat_social,
                  data = studydata)
summary(consc_threat)

extrav_threat <- lm(Extraversion_full ~ threat_predation + threat_contagion + threat_financial + threat_natural + threat_social,
                   data = studydata)
summary(extrav_threat)

agree_threat <- lm(Agreeableness_full ~ threat_predation + threat_contagion + threat_financial + threat_natural + threat_social,
                    data = studydata)
summary(agree_threat)

neurot_threat <- lm(Neuroticism_full ~ threat_predation + threat_contagion + threat_financial + threat_natural + threat_social,
                   data = studydata)
summary(neurot_threat)

# so religious people appear far more nationalistic than nonreligious across identities.
reg_nation_rel_threat <- lm(nationalism_full ~ identity_religious + sid_religious_full + ift_religious_full + threat_predation + threat_contagion + threat_financial + threat_natural + threat_social,
                            data = studydata)
summary(reg_nation_rel_threat)

glm_immi_all_fit <- glm(wvs_undes_immigrants ~ identity_religious + covid_infection_status +
                 politics_economic + politics_social + frequency + cultureWar + waylifethreatcovid + worldwillneverbethesame +
                 sid_religious_full + ift_religious_full +
                 sid_nation_full + ift_nation_full +
                 Openness_full + Conscientiousness_full + Extraversion_full + Agreeableness_full + Neuroticism_full +
                 threat_predation + threat_contagion + threat_financial + threat_natural + threat_social +
                 socialmedia_timespent + media_frequency
               , data = studydata)
summary(glm_immi_other_fit)

glm_immi_protestant_fit <- glm(wvs_undes_immigrants ~ covid_infection_status +
                            politics_economic + politics_social + frequency + cultureWar + waylifethreatcovid + worldwillneverbethesame +
                            sid_religious_full + ift_religious_full +
                            sid_nation_full + ift_nation_full +
                            Openness_full + Conscientiousness_full + Extraversion_full + Agreeableness_full + Neuroticism_full +
                            threat_predation + threat_contagion + threat_financial + threat_natural + threat_social +
                            socialmedia_timespent + media_frequency,
                            data = subset(studydata, identity_religious=="Protestant"))
summary(glm_immi_protestant_fit)

glm_immi_muslim_fit <- glm(wvs_undes_immigrants ~ covid_infection_status +
                                 politics_economic + politics_social + frequency + cultureWar + waylifethreatcovid + worldwillneverbethesame +
                                 sid_religious_full + ift_religious_full +
                                 sid_nation_full + ift_nation_full +
                                 Openness_full + Conscientiousness_full + Extraversion_full + Agreeableness_full + Neuroticism_full +
                                 threat_predation + threat_contagion + threat_financial + threat_natural + threat_social +
                                 socialmedia_timespent + media_frequency,
                               data = subset(studydata, identity_religious=="Muslim"))
summary(glm_immi_muslim_fit)

glm_rel_immi_fit_all <-glm(wvs_undes_immigrants ~ identity_religious,
                           data = studydata)
summary(glm_rel_immi_fit_all)

national1 <- lm(nationalism_full ~ age + gender + identity_religious + nationality + edu + covid_infection_status +
                  politics_economic + politics_social + frequency + cultureWar + waylifethreatcovid + worldwillneverbethesame +
                  sid_nation_full + sid_religious_full + ift_religious_full + ift_nation_full +
                  sbs_extended_full + threat_extended_full + Openness_full + Conscientiousness_full +
                  Extraversion_full + Agreeableness_full + Neuroticism_full + frequency +
                  socialmedia_timespent + media_frequency + wvs_undes_drugaddicts +
                  wvs_undes_diff_race + wvs_undes_aids + wvs_undes_immigrants + wvs_undes_homosexuals + wvs_undes_diff_religion +
                  wvs_undes_drinkers + wvs_undes_unmarriedcouples + wvs_undes_diff_language, data = studydata)
summary(national1)

cor.test(as.numeric(studydata$age), studydata$nationalism_full, method = "spearman", use = "na.or.complete")

qplot(as.numeric(age),nationalism_full,data=studydata, geom='bin2d')

qplot(as.numeric(age),nationalism_full,data=studydata[which(studydata$gender=='M'),], geom='bin2d')
qplot(as.numeric(age),nationalism_full,data=studydata[which(studydata$gender=='F'),], geom='bin2d')

immigrant1 <- lm(wvs_undes_immigrants ~ as.numeric(age) + gender + identity_religious + nationality + edu + covid_infection_status +
                  politics_economic + politics_social + frequency + cultureWar + waylifethreatcovid + worldwillneverbethesame +
                  sid_nation_full + sid_religious_full + ift_religious_full + ift_nation_full +
                  sbs_extended_full + threat_extended_full + Openness_full + Conscientiousness_full +
                  Extraversion_full + Agreeableness_full + Neuroticism_full + frequency +
                  socialmedia_timespent + media_frequency + wvs_undes_drugaddicts +
                  wvs_undes_diff_race + wvs_undes_aids + wvs_undes_homosexuals + wvs_undes_diff_religion +
                  wvs_undes_drinkers + wvs_undes_unmarriedcouples + wvs_undes_diff_language, data = studydata)
summary(immigrant1)

glm.fit <- glm(wvs_undes_immigrants ~ as.numeric(age) + gender + identity_religious + edu + covid_infection_status +
                 politics_economic + politics_social + frequency + cultureWar + waylifethreatcovid + worldwillneverbethesame +
                 sid_nation_full + sid_religious_full + ift_religious_full + ift_nation_full +
                 sbs_extended_full + threat_extended_full + Openness_full + Conscientiousness_full +
                 Extraversion_full + Agreeableness_full + Neuroticism_full + frequency +
                 socialmedia_timespent + media_frequency + wvs_undes_drugaddicts +
                 wvs_undes_diff_race + wvs_undes_aids + wvs_undes_homosexuals + wvs_undes_diff_religion +
                 wvs_undes_drinkers + wvs_undes_unmarriedcouples + wvs_undes_diff_language, data = studydata)
summary(glm.fit)

econpolitics <- lm(politics_economic ~ age + gender + edu + covid_infection_status + media_frequency + socialmedia_timespent +
                     threat_simple_full + threat_extended_full, data=studydata)
summary(econpolitics)
lm.beta(econpolitics)

econpolitics2 <- lm(politics_economic ~ as.numeric(age) + gender + edu + covid_infection_status + media_frequency + socialmedia_timespent +
                      threat_social + threat_natural + threat_financial + threat_contagion + threat_predation,
                    data=studydata)
summary(econpolitics2)
lm.beta(econpolitics2)

socialpolitics2 <- lm(politics_social ~ as.numeric(age) + gender + edu + covid_infection_status + media_frequency + socialmedia_timespent +
                      threat_social + threat_natural + threat_financial + threat_contagion + threat_predation,
                    data=studydata)
summary(socialpolitics2)
lm.beta(socialpolitics2)

reli1 <- lm(sbs_extended_full ~ as.numeric(age) + gender + edu + covid_infection_status + media_frequency + socialmedia_timespent +
                        ift_religious_full + sid_religious_full + frequency,
                      data=studydata)
summary(reli1)
lm.beta(reli1)

reli2 <- lm(sbs_orig_full ~ as.numeric(age) + gender + edu + covid_infection_status + media_frequency + socialmedia_timespent +
              ift_religious_full + sid_religious_full + frequency,
            data=studydata)
summary(reli2)
lm.beta(reli2)

cor.test(studydata$nationalism_full, studydata$sbs_orig_full, method = "spearman", use = "na.or.complete")
cor.test(studydata$nationalism_full, studydata$sid_religious_full, method = "spearman", use = "na.or.complete")
cor.test(studydata$nationalism_orig, studydata$nationalism_8, method = "spearman", use = "na.or.complete")

#To be truly part of my nation one must be born in the nation
#To be truly part of my nation one must live in the nation most of their life
#To be truly part of my nation one must be part of my religious group
#To be truly part of my nation one must speak my language
#To be truly part of my nation one must hold our citizenship
#To be truly part of my nation one must feel like they're part of my nationality
#To be truly part of my nation one must respect its laws
#To be truly part of my nation one must be of a specific race or ethnicity

# test for mediation X = religion, y = nationalism_orig, m1 = nationalism_8

###################################################################################################################################
# the code below will create a correlation plot of variables

important_data <- data.frame(studydata$sid_nation_full,
                             studydata$ift_nation_full,
                             studydata$feelingstoward_muslims,
                             studydata$feelingstoward_christians,
                             studydata$feelingstoward_jews,
                             studydata$feelingstoward_atheists,
                             studydata$feelingstoward_immigrants,
                             studydata$feelingstoward_other_races,
                             studydata$politics_economic,
                             studydata$politics_social,
                             studydata$nationalism_full,
                             studydata$sbs_extended_full,
                             studydata$frequency,
                             studydata$sid_religious_full,
                             studydata$ift_religious_full,
                             studydata$Openness_full,
                             studydata$Conscientiousness_full,
                             studydata$Extraversion_full,
                             studydata$Agreeableness_full,
                             studydata$Neuroticism_full,
                             studydata$cultureWar,
                             studydata$wayoflifethreatened,
                             studydata$waylifethreatcovid,
                             studydata$threat_social,
                             studydata$threat_contagion,
                             studydata$threat_financial,
                             studydata$threat_natural,
                             studydata$threat_predation,
                             studydata$socialmedia_timespent,
                             studydata$media_frequency)

important_data_num <- sapply( important_data, as.numeric )
important_data_num <- as.matrix(na.omit(important_data_num))
library(xtable)
library(Hmisc)
icor<-round(cor(important_data_num),2)
upper<-icor
upper[upper.tri(icor)]<-""
upper<-as.data.frame(upper)
print(xtable(upper), type="html")
# x is a matrix containing the data
# method : correlation method. "pearson"" or "spearman"" is supported
# removeTriangle : remove upper or lower triangle
# results :  if "html" or "latex"
# the results will be displayed in html or latex format
corstars <-function(x, method=c("pearson", "spearman"), removeTriangle=c("upper", "lower"),
                    result=c("none", "html", "latex")){
  #Compute correlation matrix
  require(Hmisc)
  x <- as.matrix(x)
  correlation_matrix<-rcorr(x, type=method[1])
  R <- correlation_matrix$r # Matrix of correlation coeficients
  p <- correlation_matrix$P # Matrix of p-value

  ## Define notions for significance levels; spacing is important.
  mystars <- ifelse(p < .0001, "****", ifelse(p < .001, "*** ", ifelse(p < .01, "**  ", ifelse(p < .05, "*   ", "    "))))

  ## trunctuate the correlation matrix to two decimal
  R <- format(round(cbind(rep(-1.11, ncol(x)), R), 2))[,-1]

  ## build a new matrix that includes the correlations with their apropriate stars
  Rnew <- matrix(paste(R, mystars, sep=""), ncol=ncol(x))
  diag(Rnew) <- paste(diag(R), " ", sep="")
  rownames(Rnew) <- colnames(x)
  colnames(Rnew) <- paste(colnames(x), "", sep="")

  ## remove upper triangle of correlation matrix
  if(removeTriangle[1]=="upper"){
    Rnew <- as.matrix(Rnew)
    Rnew[upper.tri(Rnew, diag = TRUE)] <- ""
    Rnew <- as.data.frame(Rnew)
  }

  ## remove lower triangle of correlation matrix
  else if(removeTriangle[1]=="lower"){
    Rnew <- as.matrix(Rnew)
    Rnew[lower.tri(Rnew, diag = TRUE)] <- ""
    Rnew <- as.data.frame(Rnew)
  }

  ## remove last column and return the correlation matrix
  Rnew <- cbind(Rnew[1:length(Rnew)-1])
  if (result[1]=="none") return(Rnew)
  else{
    if(result[1]=="html") print(xtable(Rnew), type="html")
    else print(xtable(Rnew), type="latex")
  }
}

corstars(icor, result="html")

icorBIG<-round(cor(important_data_num),2)
corstars(icorBIG, result="html")

res2 <- rcorr(as.matrix(na.omit(important_data_num)))
res2

library(corrplot)
#corrplot(res, type = "upper", order = "hclust", sig.level = 0.05, tl.col = "black", tl.srt = 45)

# Insignificant correlation are blank
corrplot(res2$r, type="upper", order="hclust",
         p.mat = res2$P, sig.level = 0.05, insig = "blank",tl.col = "black", tl.srt = 45)

###################################################################################################################################
# Now for the mediations and SEM part
library(lavaan)
library(semPlot)

threat_conservative_model <- '
  #measurement model
  threat =~ threat_financial + threat_contagion + threat_social + threat_predation + threat_natural
  #nationalism =~ nationalism_1 + nationalism_2 + nationalism_3 + nationalism_4 + nationalism_5 + nationalism_6 + nationalism_7 + nationalism_8
  # regressions
  nationalism_full ~ threat
  politics_social ~ threat + nationalism_full
  politics_economic ~ threat + nationalism_full
  # residuals
  #threat_financial ~~ threat_contagion
  #threat_financial ~~ threat_natural
  #threat_contagion ~~ threat_natural
  #threat_social ~~ threat_predation
'
semfit1 <- lavaan::sem(model = threat_conservative_model, data = studydata, se = "boot", bootstrap = 1000)
lavaan::parameterEstimates(semfit1, boot.ci.type = "bca.simple")
semPaths(semfit1,"std",edge.label.cex=1.5, curvePivot = TRUE)
summary(semfit1, fit.measures = TRUE)

immi_threat_conservative_model <- '
  #measurement model
  threat =~ threat_financial + threat_contagion + threat_social + threat_predation + threat_natural
  #nationalism =~ nationalism_1 + nationalism_2 + nationalism_3 + nationalism_4 + nationalism_5 + nationalism_6 + nationalism_7 + nationalism_8
  # regressions
  nationalism_full ~ threat
  politics_social ~ threat + nationalism_full
  politics_economic ~ threat + nationalism_full
  wvs_undes_immigrants ~ politics_social + politics_economic
  # residuals
  #threat_financial ~~ threat_contagion
  #threat_financial ~~ threat_natural
  #threat_contagion ~~ threat_natural
  #threat_social ~~ threat_predation
'
semfit2 <- lavaan::sem(model = immi_threat_conservative_model, data = studydata, se = "boot", bootstrap = 1000)
lavaan::parameterEstimates(semfit2, boot.ci.type = "bca.simple")
semPaths(semfit2,"std",edge.label.cex=1.5, curvePivot = TRUE)
summary(semfit2, fit.measures = TRUE)

immi_threat_conserv_b5_model <- '
  #measurement model
  threat =~ threat_financial + threat_contagion + threat_social + threat_predation + threat_natural
  #nationalism =~ nationalism_1 + nationalism_2 + nationalism_3 + nationalism_4 + nationalism_5 + nationalism_6 + nationalism_7 + nationalism_8
  # regressions
  nationalism_full ~ threat
  politics_social ~ threat + nationalism_full
  politics_economic ~ threat + nationalism_full
  wvs_undes_immigrants ~ politics_social + politics_economic
  politics_social ~ Openness_full
  politics_economic ~ Openness_full
  Openness_full ~ Neuroticism_full
  Openness_full ~ Agreeableness_full
  Neuroticism_full ~ socialmedia_timespent
  Agreeableness_full ~ socialmedia_timespent
  Neuroticism_full ~ media_frequency
  Agreeableness_full ~ media_frequency
  wayoflifethreatened ~ Openness_full
  wayoflifethreatened ~ sid_nation_full
  wayoflifethreatened ~ ift_nation_full
  wvs_undes_immigrants ~ wayoflifethreatened
  # residuals
  #threat_financial ~~ threat_contagion
  #threat_financial ~~ threat_natural
  #threat_contagion ~~ threat_natural
  #threat_social ~~ threat_predation
'
semfit3 <- lavaan::sem(model = immi_threat_conserv_b5_model, data = studydata, se = "boot", bootstrap = 1000)
lavaan::parameterEstimates(semfit3, boot.ci.type = "bca.simple")
semPaths(semfit3,"std",edge.label.cex=1.5, curvePivot = TRUE)
summary(semfit3, fit.measures = TRUE)

immi_threat_2_conserv_b5_model <- '
  #measurement model
  threat_soc_pred =~ threat_social + threat_predation
  threat_cont_fin_nat =~ threat_financial + threat_contagion + threat_natural
  #nationalism =~ nationalism_1 + nationalism_2 + nationalism_3 + nationalism_4 + nationalism_5 + nationalism_6 + nationalism_7 + nationalism_8
  # regressions
  nationalism_full ~ threat_soc_pred
  nationalism_full ~ threat_cont_fin_nat
  politics_social ~ threat_soc_pred + threat_cont_fin_nat + nationalism_full
  politics_economic ~ threat_soc_pred + threat_cont_fin_nat + nationalism_full
  #politics_social ~ threat_cont_fin_nat + nationalism_full
  #politics_economic ~ threat_cont_fin_nat + nationalism_full
  wvs_undes_immigrants ~ politics_social + politics_economic
  politics_social ~ Openness_full
  politics_economic ~ Openness_full
  Openness_full ~ Neuroticism_full
  Openness_full ~ Agreeableness_full
  Neuroticism_full ~ socialmedia_timespent
  Agreeableness_full ~ socialmedia_timespent
  Neuroticism_full ~ media_frequency
  Agreeableness_full ~ media_frequency
  wayoflifethreatened ~ Openness_full
  wayoflifethreatened ~ sid_nation_full
  wayoflifethreatened ~ ift_nation_full
  wvs_undes_immigrants ~ wayoflifethreatened
  # residuals
  #threat_financial ~~ threat_contagion
  #threat_financial ~~ threat_natural
  #threat_contagion ~~ threat_natural
  #threat_social ~~ threat_predation
'

semfit4 <- lavaan::sem(model = immi_threat_2_conserv_b5_model, data = studydata, se = "boot", bootstrap = 1000)
lavaan::parameterEstimates(semfit4, boot.ci.type = "bca.simple")
semPaths(semfit4,"std",edge.label.cex=1.5, curvePivot = TRUE)
summary(semfit4, fit.measures = TRUE)

threat_conservative_model_b <- '
  #measurement model
  threat_soc_pred =~ threat_social + threat_predation
  threat_cont_fin_nat =~ threat_financial + threat_contagion + threat_natural
  # regressions
  nationalism_full ~ threat_soc_pred + threat_cont_fin_nat
  politics_social ~ threat_soc_pred + threat_cont_fin_nat + nationalism_full
  politics_economic ~ threat_soc_pred + threat_cont_fin_nat + nationalism_full
  wvs_undes_immigrants ~ politics_social + politics_economic
  # residuals
  #threat_financial ~~ threat_contagion
  #threat_financial ~~ threat_natural
  #threat_contagion ~~ threat_natural
  #threat_social ~~ threat_predation
'
semfit1b <- lavaan::sem(model = threat_conservative_model_b, data = studydata, se = "boot", bootstrap = 1000)
lavaan::parameterEstimates(semfit1b, boot.ci.type = "bca.simple")
semPaths(semfit1b,"std",edge.label.cex=1.5, curvePivot = TRUE)
summary(semfit1b, fit.measures = TRUE)

threat_conservative_model_c <- '
  #measurement model
  threat_soc_pred =~ threat_social + threat_predation
  threat_cont_fin_nat =~ threat_financial + threat_contagion + threat_natural
  # regressions
  nationalism_full ~ threat_soc_pred + threat_cont_fin_nat
  politics_social ~ threat_soc_pred + threat_cont_fin_nat + nationalism_full + sid_religious_full
  politics_economic ~ threat_soc_pred + threat_cont_fin_nat + nationalism_full + sbs_extended_full
  wvs_undes_immigrants ~ politics_social + politics_economic
  # residuals
  #threat_financial ~~ threat_contagion
  #threat_financial ~~ threat_natural
  #threat_contagion ~~ threat_natural
  #threat_social ~~ threat_predation
'
semfit1c <- lavaan::sem(model = threat_conservative_model_c, data = studydata, se = "boot", bootstrap = 1000)
lavaan::parameterEstimates(semfit1c, boot.ci.type = "bca.simple")
semPaths(semfit1c,"std",edge.label.cex=1.5, curvePivot = TRUE)
summary(semfit1c, fit.measures = TRUE)

threat_conservative_model_d <- '
  #measurement model
threat_soc_pred =~ threat_social + threat_predation
threat_cont_fin_nat =~ threat_financial + threat_contagion + threat_natural
# regressions
nationalism_full ~ threat_soc_pred + threat_cont_fin_nat
politics_social ~ threat_soc_pred + threat_cont_fin_nat + nationalism_full + sid_religious_full + sbs_extended_full
politics_economic ~ threat_soc_pred + threat_cont_fin_nat + nationalism_full + sid_religious_full + sbs_extended_full
wvs_undes_immigrants ~ politics_social + politics_economic
# residuals
#threat_financial ~~ threat_contagion
#threat_financial ~~ threat_natural
#threat_contagion ~~ threat_natural
#threat_social ~~ threat_predation
'
semfit1d <- lavaan::sem(model = threat_conservative_model_d, data = studydata, se = "boot", bootstrap = 1000)
lavaan::parameterEstimates(semfit1d, boot.ci.type = "bca.simple")
semPaths(semfit1d,"std",edge.label.cex=1.5, curvePivot = TRUE)
summary(semfit1d, fit.measures = TRUE)

threat_conservative_model_e <- '
  #measurement model
  threat_soc_pred =~ threat_social + threat_predation
  threat_cont_fin_nat =~ threat_financial + threat_contagion + threat_natural
  rel =~ sid_religious_full + sbs_extended_full
  # regressions
  nationalism_full ~ threat_soc_pred + threat_cont_fin_nat
  politics_social ~ threat_soc_pred + threat_cont_fin_nat + nationalism_full + rel
  politics_economic ~ threat_soc_pred + threat_cont_fin_nat + nationalism_full + rel
  wvs_undes_immigrants ~ politics_social + politics_economic
  # residuals
  #threat_financial ~~ threat_contagion
  #threat_financial ~~ threat_natural
  #threat_contagion ~~ threat_natural
  #threat_social ~~ threat_predation
'
semfit1e <- lavaan::sem(model = threat_conservative_model_e, data = studydata, se = "boot", bootstrap = 1000)
lavaan::parameterEstimates(semfit1e, boot.ci.type = "bca.simple")
semPaths(semfit1e,"std",edge.label.cex=1.5, curvePivot = TRUE)
summary(semfit1e, fit.measures = TRUE)

threat_conservative_model_f <- '
  #measurement model
  threat_soc_pred =~ threat_social + threat_predation
  threat_cont_fin_nat =~ threat_financial + threat_contagion + threat_natural
  rel =~ sid_religious_full + sbs_extended_full
  # regressions
  nationalism_full ~ threat_soc_pred + threat_cont_fin_nat
  politics_social ~ threat_soc_pred + threat_cont_fin_nat + nationalism_full + rel
  politics_economic ~ threat_soc_pred + threat_cont_fin_nat + nationalism_full + rel
  wvs_undes_immigrants ~ politics_social + politics_economic + wayoflifethreatened
  wayoflifethreatened ~ sid_nation_full
  wayoflifethreatened ~ ift_nation_full
  #wvs_undes_immigrants ~ wayoflifethreatened
  # residuals
  #none
'
semfit1f <- lavaan::sem(model = threat_conservative_model_f, data = studydata, se = "boot", bootstrap = 1000)
lavaan::parameterEstimates(semfit1f, boot.ci.type = "bca.simple")
semPaths(semfit1f,"std",edge.label.cex=1.5, curvePivot = TRUE)
summary(semfit1f, fit.measures = TRUE)

threat_conservative_model_g <- '
  #measurement model
  threat_soc_pred =~ threat_social + threat_predation
  threat_cont_fin_nat =~ threat_financial + threat_contagion + threat_natural
  rel =~ sid_religious_full + sbs_extended_full
  # regressions
  nationalism_full ~ threat_soc_pred + threat_cont_fin_nat
  politics_social ~ threat_soc_pred + threat_cont_fin_nat + nationalism_full + rel
  politics_economic ~ threat_soc_pred + threat_cont_fin_nat + nationalism_full + rel
  wvs_undes_immigrants ~ politics_social + politics_economic + wayoflifethreatened
  wvs_undes_immigrants ~ sid_nation_full
  wvs_undes_immigrants ~ ift_nation_full
  #wvs_undes_immigrants ~ wayoflifethreatened
  # residuals
  #none
'
semfit1g <- lavaan::sem(model = threat_conservative_model_g, data = studydata, se = "boot", bootstrap = 1000)
lavaan::parameterEstimates(semfit1g, boot.ci.type = "bca.simple")
semPaths(semfit1g,"std",edge.label.cex=1.5, curvePivot = TRUE)
summary(semfit1g, fit.measures = TRUE)

threat_conservative_model_h <- '
  #measurement model
  threat_soc_pred =~ threat_social + threat_predation
  threat_cont_fin_nat =~ threat_financial + threat_contagion + threat_natural
  rel =~ sid_religious_full + sbs_extended_full
  # regressions
  #ift_nation_full ~ threat_soc_pred + threat_cont_fin_nat
  nationalism_full ~ ift_nation_full + threat_soc_pred + threat_cont_fin_nat
  #wayoflifethreatened ~ threat_soc_pred + threat_cont_fin_nat
  politics_social ~ threat_soc_pred + threat_cont_fin_nat + nationalism_full + rel
  politics_economic ~ threat_soc_pred + threat_cont_fin_nat + nationalism_full + rel
  wvs_undes_immigrants ~ politics_social + politics_economic
  wvs_undes_immigrants ~ sid_nation_full
  wvs_undes_immigrants ~ ift_nation_full
  #wvs_undes_immigrants ~ wayoflifethreatened
  # residuals
  #none
'
semfit1h <- lavaan::sem(model = threat_conservative_model_h, data = studydata, se = "boot", bootstrap = 1000)
lavaan::parameterEstimates(semfit1h, boot.ci.type = "bca.simple")
semPaths(semfit1h,"std",edge.label.cex=1.5, curvePivot = TRUE)
summary(semfit1h, fit.measures = TRUE)


library(ggplot2); library(scales); library(grid); library(RColorBrewer); library(Cairo)
# Für Cairo muss libcairo2-dev installiert sein, siehe http://www.cairographics.org/download/ und http://is-r.tumblr.com/post/33421588764/using-cairographics-with-ggsave

ackerstr <- read.csv("ackerstrasse_40_t30_05112014.txt",header=FALSE,sep=";")
names(ackerstr) <- c("date","place","tempo","length","direction")
ackerstr$tempoclass <- cut(ackerstr$tempo, breaks = c(0,30,36,Inf), labels=c("<30 km/h", "30–36 km/h", ">36 km/h"))
ackerstr$vehicleclass <-cut(ackerstr$length, breaks = c(0,8,12, Inf), labels=c("PKW", "LKW", "Lastzug"))
ackerstr$datetime <- as.POSIXct(ackerstr$date,format="%d.%m.%y %H:%M:%S")

aubruchsweg <- read.csv("aubruchsweg_22-24_t30_20102014.txt",header=FALSE,sep=";")
names(aubruchsweg) <- c("date","place","tempo","length","direction")
aubruchsweg$tempoclass <- cut(aubruchsweg$tempo, breaks = c(0,30,36,Inf), labels=c("<30 km/h", "30–36 km/h", ">36 km/h"))
aubruchsweg$vehicleclass <-cut(aubruchsweg$length, breaks = c(0,8,12, Inf), labels=c("PKW", "LKW", "Lastzug"))
aubruchsweg$datetime <- as.POSIXct(aubruchsweg$date,format="%d.%m.%y %H:%M:%S")

boschheideweg <- read.csv("boschheideweg_36_t30_24112014.txt",header=FALSE,sep=";")
names(boschheideweg) <- c("date","place","tempo","length","direction")
boschheideweg$tempoclass <- cut(boschheideweg$tempo, breaks = c(0,30,36,Inf), labels=c("<30 km/h", "30–36 km/h", ">36 km/h"))
boschheideweg$vehicleclass <-cut(boschheideweg$length, breaks = c(0,8,12, Inf), labels=c("PKW", "LKW", "Lastzug"))
boschheideweg$datetime <- as.POSIXct(boschheideweg$date,format="%d.%m.%y %H:%M:%S")

endstr <- read.csv("endstrasse_17_t50_03112014.txt",header=FALSE,sep=";")
names(endstr) <- c("date","place","tempo","length","direction")
endstr$tempoclass <- cut(endstr$tempo, breaks = c(0,50,56,Inf), labels=c("<50 km/h", "50–56 km/h", ">56 km/h"))
endstr$vehicleclass <-cut(endstr$length, breaks = c(0,8,12, Inf), labels=c("PKW", "LKW", "Lastzug"))
endstr$datetime <- as.POSIXct(endstr$date,format="%d.%m.%y %H:%M:%S")

eupenerstr <- read.csv("eupener_strasse_2a_t30_22102014.txt",header=FALSE,sep=";")
names(eupenerstr) <- c("date","place","tempo","length","direction")
eupenerstr$tempoclass <- cut(eupenerstr$tempo, breaks = c(0,30,36,Inf), labels=c("<30 km/h", "30–36 km/h", ">36 km/h"))
eupenerstr$vehicleclass <-cut(eupenerstr$length, breaks = c(0,8,12, Inf), labels=c("PKW", "LKW", "Lastzug"))
eupenerstr$datetime <- as.POSIXct(eupenerstr$date,format="%d.%m.%y %H:%M:%S")

friedenstr <- read.csv("friedenstrasse_5_t30_14052014.txt",header=FALSE,sep=";")
names(friedenstr) <- c("date","place","tempo","length","direction")
friedenstr$tempoclass <- cut(friedenstr$tempo, breaks = c(0,30,36,Inf), labels=c("<30 km/h", "30–36 km/h", ">36 km/h"))
friedenstr$vehicleclass <-cut(friedenstr$length, breaks = c(0,8,12, Inf), labels=c("PKW", "LKW", "Lastzug"))
friedenstr$datetime <- as.POSIXct(friedenstr$date,format="%d.%m.%y %H:%M:%S")

kirchweg <- read.csv("kirchweg_5_t30_08122014.txt",header=FALSE,sep=";")
names(kirchweg) <- c("date","place","tempo","length","direction")
kirchweg$tempoclass <- cut(kirchweg$tempo, breaks = c(0,30,36,Inf), labels=c("<30 km/h", "30–36 km/h", ">36 km/h"))
kirchweg$vehicleclass <-cut(kirchweg$length, breaks = c(0,8,12, Inf), labels=c("PKW", "LKW", "Lastzug"))
kirchweg$datetime <- as.POSIXct(kirchweg$date,format="%d.%m.%y %H:%M:%S")

kirschenallee <- read.csv("kirschenallee_33_t30_28082014.txt",header=FALSE,sep=";")
names(kirschenallee) <- c("date","place","tempo","length","direction")
kirschenallee$tempoclass <- cut(kirschenallee$tempo, breaks = c(0,30,36,Inf), labels=c("<30 km/h", "30–36 km/h", ">36 km/h"))
kirschenallee$vehicleclass <-cut(kirschenallee$length, breaks = c(0,8,12, Inf), labels=c("PKW", "LKW", "Lastzug"))
kirschenallee$datetime <- as.POSIXct(kirschenallee$date,format="%d.%m.%y %H:%M:%S")

landwehrstr <- read.csv("landwehrstrasse_57_t30_15122014.txt",header=FALSE,sep=";")
names(landwehrstr) <- c("date","place","tempo","length","direction")
landwehrstr$tempoclass <- cut(landwehrstr$tempo, breaks = c(0,30,36,Inf), labels=c("<30 km/h", "30–36 km/h", ">36 km/h"))
landwehrstr$vehicleclass <-cut(landwehrstr$length, breaks = c(0,8,12, Inf), labels=c("PKW", "LKW", "Lastzug"))
landwehrstr$datetime <- as.POSIXct(landwehrstr$date,format="%d.%m.%y %H:%M:%S")

pattbergstr <- read.csv("pattbergstrasse_12-16_t50_03122014.txt",header=FALSE,sep=";")
names(pattbergstr) <- c("date","place","tempo","length","direction")
pattbergstr$tempoclass <- cut(pattbergstr$tempo, breaks = c(0,50,56,Inf), labels=c("<50 km/h", "50–56 km/h", ">56 km/h"))
pattbergstr$vehicleclass <-cut(pattbergstr$length, breaks = c(0,8,12, Inf), labels=c("PKW", "LKW", "Lastzug"))
pattbergstr$datetime <- as.POSIXct(pattbergstr$date,format="%d.%m.%y %H:%M:%S")

roemerstr <- read.csv("roemerstrasse_501_t30_04062014.txt",header=FALSE,sep=";")
names(roemerstr) <- c("date","place","tempo","length","direction")
roemerstr$tempoclass <- cut(roemerstr$tempo, breaks = c(0,30,36,Inf), labels=c("<30 km/h", "30–36 km/h", ">36 km/h"))
roemerstr$vehicleclass <-cut(roemerstr$length, breaks = c(0,8,12, Inf), labels=c("PKW", "LKW", "Lastzug"))
roemerstr$datetime <- as.POSIXct(roemerstr$date,format="%d.%m.%y %H:%M:%S")

ruhrstr <- read.csv("ruhrstrasse_58_t30_14042014.txt",header=FALSE,sep=";")
names(ruhrstr) <- c("date","place","tempo","length","direction")
ruhrstr$tempoclass <- cut(ruhrstr$tempo, breaks = c(0,30,36,Inf), labels=c("<30 km/h", "30–36 km/h", ">36 km/h"))
ruhrstr$vehicleclass <-cut(ruhrstr$length, breaks = c(0,8,12, Inf), labels=c("PKW", "LKW", "Lastzug"))
ruhrstr$datetime <- as.POSIXct(ruhrstr$date,format="%d.%m.%y %H:%M:%S")

taubenstr <- read.csv("taubenstrasse_9_t30_01122014.txt",header=FALSE,sep=";")
names(taubenstr) <- c("date","place","tempo","length","direction")
taubenstr$tempoclass <- cut(taubenstr$tempo, breaks = c(0,30,36,Inf), labels=c("<30 km/h", "30–36 km/h", ">36 km/h"))
taubenstr$vehicleclass <-cut(taubenstr$length, breaks = c(0,8,12, Inf), labels=c("PKW", "LKW", "Lastzug"))
taubenstr$datetime <- as.POSIXct(taubenstr$date,format="%d.%m.%y %H:%M:%S")

vinnerstr <- read.csv("vinner_strasse_42_t30_27102014.txt",header=FALSE,sep=";")
names(vinnerstr) <- c("date","place","tempo","length","direction")
vinnerstr$tempoclass <- cut(vinnerstr$tempo, breaks = c(0,30,36,Inf), labels=c("<30 km/h", "30–36 km/h", ">36 km/h"))
vinnerstr$vehicleclass <-cut(vinnerstr$length, breaks = c(0,8,12, Inf), labels=c("PKW", "LKW", "Lastzug"))
vinnerstr$datetime <- as.POSIXct(vinnerstr$date,format="%d.%m.%y %H:%M:%S")

 # Plot der Tabelle; x-Achse ist das Datum in DateTime aufbereitet, y-Achse das Tempo, Einfärbung der Punkte nach Geschwindigkeitsklassifizierung
ggplot(eupenerstr, aes(x=datetime, y=tempo, colour = tempoclass)) +
# Punkte sind semitransparent, Größe abhängig von der Fahrzeugklasse
  geom_point(alpha=0.2, aes(size=vehicleclass)) +
# Farbenblindenfreundliche Palette, siehe http://www.cookbook-r.com/Graphs/Colors_(ggplot2)/#a-colorblind-friendly-palette
  scale_color_manual(values= c("#009E73", "#E69F00", "#D55E00"), name="Geschwindigkeit") +
  # Hilfslinie bei 30 km/h
  geom_hline(yintercept=30, size=0.4, color="#efefef") +
  # Hilfslinie auf dem Nullpunkt der Y-Achse
  geom_hline(yintercept=0, size=0.1, color="black") +
  # Trendlinie mit Generalized Additive Model, siehe http://minimaxir.com/2015/02/ggplot-tutorial/
  geom_smooth(alpha=0.25, color="black", fill="black") +
  # Achsenbeschriftungen
  labs(title="Geschwindigkeitsmessung in der Eupener Straße", x="Zeit", y="Geschwindigkeit", size="Fahrzeugart") +
  guides(colour = guide_legend(override.aes = list(size=5 ,alpha=1))) +
  scale_x_datetime(labels = date_format("%d.%m %H:%M"))

ggsave("eupenerstr.png", width=8, height=4, dpi=300, type="cairo-png")

ggplot(ackerstr, aes(x=datetime, y=tempo, colour = tempoclass)) +
  geom_point(alpha=0.2, aes(size=vehicleclass)) +
  scale_color_manual(values= c("#009E73", "#E69F00", "#D55E00"), name="Geschwindigkeit") +
  geom_hline(yintercept=30, size=0.4, color="#efefef") +
  geom_hline(yintercept=0, size=0.1, color="black") +
  geom_smooth(alpha=0.25, color="black", fill="black") +
  labs(title="Geschwindigkeitsmessung in der Ackerstraße", x="Zeit", y="Geschwindigkeit", size="Fahrzeugart") +
  guides(colour = guide_legend(override.aes = list(size=5 ,alpha=1))) +
  scale_x_datetime(labels = date_format("%d.%m %H:%M"))

ggsave("ackerstr.png", width=8, height=4, dpi=300, type="cairo-png")

ggplot(aubruchsweg, aes(x=datetime, y=tempo, colour = tempoclass)) +
  geom_point(alpha=0.2, aes(size=vehicleclass)) +
  scale_color_manual(values= c("#009E73", "#E69F00", "#D55E00"), name="Geschwindigkeit") +
  geom_hline(yintercept=30, size=0.4, color="#efefef") +
  geom_hline(yintercept=0, size=0.1, color="black") +
  geom_smooth(alpha=0.25, color="black", fill="black") +
  labs(title="Geschwindigkeitsmessung im Aubruchsweg", x="Zeit", y="Geschwindigkeit", size="Fahrzeugart") +
  guides(colour = guide_legend(override.aes = list(size=5 ,alpha=1))) +
  scale_x_datetime(labels = date_format("%d.%m %H:%M"))

ggsave("aubruchsweg.png", width=8, height=4, dpi=300, type="cairo-png")

ggplot(boschheideweg, aes(x=datetime, y=tempo, colour = tempoclass)) +
  geom_point(alpha=0.2, aes(size=vehicleclass)) +
  scale_color_manual(values= c("#009E73", "#E69F00", "#D55E00"), name="Geschwindigkeit") +
  geom_hline(yintercept=30, size=0.4, color="#efefef") +
  geom_hline(yintercept=0, size=0.1, color="black") +
  geom_smooth(alpha=0.25, color="black", fill="black") +
  labs(title="Geschwindigkeitsmessung im Boschheideweg", x="Zeit", y="Geschwindigkeit", size="Fahrzeugart") +
  guides(colour = guide_legend(override.aes = list(size=5 ,alpha=1))) +
  scale_x_datetime(labels = date_format("%d.%m %H:%M"))

ggsave("boschheideweg.png", width=8, height=4, dpi=300, type="cairo-png")

ggplot(endstr, aes(x=datetime, y=tempo, colour = tempoclass)) +
  geom_point(alpha=0.2, aes(size=vehicleclass)) +
  scale_color_manual(values= c("#009E73", "#E69F00", "#D55E00"), name="Geschwindigkeit") +
  geom_hline(yintercept=30, size=0.4, color="#efefef") +
  geom_hline(yintercept=0, size=0.1, color="black") +
  geom_smooth(alpha=0.25, color="black", fill="black") +
  labs(title="Geschwindigkeitsmessung in der Endstraße", x="Zeit", y="Geschwindigkeit", size="Fahrzeugart") +
  guides(colour = guide_legend(override.aes = list(size=5 ,alpha=1))) +
  scale_x_datetime(labels = date_format("%d.%m %H:%M"))

ggsave("endstr.png", width=8, height=4, dpi=300, type="cairo-png")

ggplot(friedenstr, aes(x=datetime, y=tempo, colour = tempoclass)) +
  geom_point(alpha=0.2, aes(size=vehicleclass)) +
  scale_color_manual(values= c("#009E73", "#E69F00", "#D55E00"), name="Geschwindigkeit") +
  geom_hline(yintercept=30, size=0.4, color="#efefef") +
  geom_hline(yintercept=0, size=0.1, color="black") +
  geom_smooth(alpha=0.25, color="black", fill="black") +
  labs(title="Geschwindigkeitsmessung in der Friedenstraße", x="Zeit", y="Geschwindigkeit", size="Fahrzeugart") +
  guides(colour = guide_legend(override.aes = list(size=5 ,alpha=1))) +
  scale_x_datetime(labels = date_format("%d.%m %H:%M"))

ggsave("friedenstr.png", width=8, height=4, dpi=300, type="cairo-png")

ggplot(kirchweg, aes(x=datetime, y=tempo, colour = tempoclass)) +
  geom_point(alpha=0.2, aes(size=vehicleclass)) +
  scale_color_manual(values= c("#009E73", "#E69F00", "#D55E00"), name="Geschwindigkeit") +
  geom_hline(yintercept=30, size=0.4, color="#efefef") +
  geom_hline(yintercept=0, size=0.1, color="black") +
  geom_smooth(alpha=0.25, color="black", fill="black") +
  labs(title="Geschwindigkeitsmessung im Kirchweg", x="Zeit", y="Geschwindigkeit", size="Fahrzeugart") +
  guides(colour = guide_legend(override.aes = list(size=5 ,alpha=1))) +
  scale_x_datetime(labels = date_format("%d.%m %H:%M"))

ggsave("kirchweg.png", width=8, height=4, dpi=300, type="cairo-png")

ggplot(kirschenallee, aes(x=datetime, y=tempo, colour = tempoclass)) +
  geom_point(alpha=0.2, aes(size=vehicleclass)) +
  scale_color_manual(values= c("#009E73", "#E69F00", "#D55E00"), name="Geschwindigkeit") +
  geom_hline(yintercept=30, size=0.4, color="#efefef") +
  geom_hline(yintercept=0, size=0.1, color="black") +
  geom_smooth(alpha=0.25, color="black", fill="black") +
  labs(title="Geschwindigkeitsmessung in der Kirschenallee", x="Zeit", y="Geschwindigkeit", size="Fahrzeugart") +
  guides(colour = guide_legend(override.aes = list(size=5 ,alpha=1))) +
  scale_x_datetime(labels = date_format("%d.%m %H:%M"))

ggsave("kirschenallee.png", width=8, height=4, dpi=300, type="cairo-png")

ggplot(landwehrstr, aes(x=datetime, y=tempo, colour = tempoclass)) +
  geom_point(alpha=0.2, aes(size=vehicleclass)) +
  scale_color_manual(values= c("#009E73", "#E69F00", "#D55E00"), name="Geschwindigkeit") +
  geom_hline(yintercept=30, size=0.4, color="#efefef") +
  geom_hline(yintercept=0, size=0.1, color="black") +
  geom_smooth(alpha=0.25, color="black", fill="black") +
  labs(title="Geschwindigkeitsmessung in der Landwehrstraße", x="Zeit", y="Geschwindigkeit", size="Fahrzeugart") +
  guides(colour = guide_legend(override.aes = list(size=5 ,alpha=1))) +
  scale_x_datetime(labels = date_format("%d.%m %H:%M"))

ggsave("landwehrstr.png", width=8, height=4, dpi=300, type="cairo-png")

ggplot(pattbergstr, aes(x=datetime, y=tempo, colour = tempoclass)) +
  geom_point(alpha=0.2, aes(size=vehicleclass)) +
  scale_color_manual(values= c("#009E73", "#E69F00", "#D55E00"), name="Geschwindigkeit") +
  geom_hline(yintercept=30, size=0.4, color="#efefef") +
  geom_hline(yintercept=0, size=0.1, color="black") +
  geom_smooth(alpha=0.25, color="black", fill="black") +
  labs(title="Geschwindigkeitsmessung in der Pattbergstraße", x="Zeit", y="Geschwindigkeit", size="Fahrzeugart") +
  guides(colour = guide_legend(override.aes = list(size=5 ,alpha=1))) +
  scale_x_datetime(labels = date_format("%d.%m %H:%M"))

ggsave("pattbergstr.png", width=8, height=4, dpi=300, type="cairo-png")

ggplot(roemerstr, aes(x=datetime, y=tempo, colour = tempoclass)) +
  geom_point(alpha=0.2, aes(size=vehicleclass)) +
  scale_color_manual(values= c("#009E73", "#E69F00", "#D55E00"), name="Geschwindigkeit") +
  geom_hline(yintercept=30, size=0.4, color="#efefef") +
  geom_hline(yintercept=0, size=0.1, color="black") +
  geom_smooth(alpha=0.25, color="black", fill="black") +
  labs(title="Geschwindigkeitsmessung in der Römerstraße", x="Zeit", y="Geschwindigkeit", size="Fahrzeugart") +
  guides(colour = guide_legend(override.aes = list(size=5 ,alpha=1))) +
  scale_x_datetime(labels = date_format("%d.%m %H:%M"))

ggsave("roemerstr.png", width=8, height=4, dpi=300, type="cairo-png")

ggplot(ruhrstr, aes(x=datetime, y=tempo, colour = tempoclass)) +
  geom_point(alpha=0.2, aes(size=vehicleclass)) +
  scale_color_manual(values= c("#009E73", "#E69F00", "#D55E00"), name="Geschwindigkeit") +
  geom_hline(yintercept=30, size=0.4, color="#efefef") +
  geom_hline(yintercept=0, size=0.1, color="black") +
  geom_smooth(alpha=0.25, color="black", fill="black") +
  labs(title="Geschwindigkeitsmessung in der Ruhrstraße", x="Zeit", y="Geschwindigkeit", size="Fahrzeugart") +
  guides(colour = guide_legend(override.aes = list(size=5 ,alpha=1))) +
  scale_x_datetime(labels = date_format("%d.%m %H:%M"))

ggsave("ruhrstr.png", width=8, height=4, dpi=300, type="cairo-png")

ggplot(taubenstr, aes(x=datetime, y=tempo, colour = tempoclass)) +
  geom_point(alpha=0.2, aes(size=vehicleclass)) +
  scale_color_manual(values= c("#009E73", "#E69F00", "#D55E00"), name="Geschwindigkeit") +
  geom_hline(yintercept=30, size=0.4, color="#efefef") +
  geom_hline(yintercept=0, size=0.1, color="black") +
  geom_smooth(alpha=0.25, color="black", fill="black") +
  labs(title="Geschwindigkeitsmessung in der Taubenstraße", x="Zeit", y="Geschwindigkeit", size="Fahrzeugart") +
  guides(colour = guide_legend(override.aes = list(size=5 ,alpha=1))) +
  scale_x_datetime(labels = date_format("%d.%m %H:%M"))

ggsave("taubenstr.png", width=8, height=4, dpi=300, type="cairo-png")

ggplot(vinnerstr, aes(x=datetime, y=tempo, colour = tempoclass)) +
  geom_point(alpha=0.2, aes(size=vehicleclass)) +
  scale_color_manual(values= c("#009E73", "#E69F00", "#D55E00"), name="Geschwindigkeit") +
  geom_hline(yintercept=30, size=0.4, color="#efefef") +
  geom_hline(yintercept=0, size=0.1, color="black") +
  geom_smooth(alpha=0.25, color="black", fill="black") +
  labs(title="Geschwindigkeitsmessung in der Vinner Straße", x="Zeit", y="Geschwindigkeit", size="Fahrzeugart") +
  guides(colour = guide_legend(override.aes = list(size=5 ,alpha=1))) +
  scale_x_datetime(labels = date_format("%d.%m %H:%M"))

ggsave("vinnerstr.png", width=8, height=4, dpi=300, type="cairo-png")


TESTING= T
groupingspec = 3 ##1:KI only, 2: KO only somehting else: all

group1 =ifelse(groupingspec ==1, "KI", "")
group1 =ifelse(groupingspec ==2, "KO", group1)
group1

if(TESTING){
  library(dplyr)
  library(ggplot2)
  datapath="Y:/AOCF-User Projects/1412_Rosenmund_VGlut1-KI"
  tmazerawdatalink= "datafortest/20150701_TMaze-rm/20150701_Rosenmund_VGlut1-KI_TMaze.csv"
  tmazerawdatalink= paste(datapath,"/6. Data from Behavioral Tests/Tmaze-rm/,datafortest/20150701_TMaze-rm/20151028_Rosenmund_VGlut1-KI_TMaze-rm.csv")

  #IF = read.csv2("datafortest/initial_form.csv")
  #Initiation_form = as.data.frame(t(IF[-1]))
  #names(Initiation_form)= IF[,1]

}
tmazerawdir= paste0(datapath,"/6. Data from Behavioral Tests/")
INT=dir(tmazerawdir)
INT = INT[grepl("TMaze",INT)]

Tmazedata=c()
tmazemeta=c()
for (i in c(1: length(INT))){
  INT2=dir(paste0(tmazerawdir,"/",INT[i]))
  INT2 = INT2[grepl(".csv",INT2)]
  tmazerawdatalink=paste0(tmazerawdir,"/",INT[i],"/",INT2)
  tmazeraw= read.csv(tmazerawdatalink, sep= ";",skip = 6, header = T)
  tmazerawmeta= read.csv(tmazerawdatalink, sep= ";",skip = 2 ,nrows = 1, header = T)
  #tmazerawmeta
  tmazeraw = data.frame(unclass (tmazeraw))
  tmazerawmeta = data.frame(unclass (tmazerawmeta))

  tmazeraw$Date= as.Date(tmazeraw$Date,"%d.%m.%Y" )
  tmazeraw$animal.ID = as.factor(tmazeraw$animal.ID)

  tmazeraw$habituationtime=difftime(strptime(tmazeraw$Start.Time ,"%H:%M"),strptime(tmazeraw$habituation,"%H:%M"), units = "mins")
  tmazeraw$agegroup=i
  Tmazedata= rbind(Tmazedata,tmazeraw)
  #tmazemeta=rbind(tmazemeta,tmazerawmeta)
}

tmazeraw=Tmazedata

projectlagesoID= tmazerawmeta$ProjectID..lageso.

## get group name (work only if grouped by strain)
grouping =as.character(tmazeraw$animal.ID)
for (i in c(1: length(grouping))){
  grouping [i]= as.character(animalsproj$strain [animalsproj$animal.ID== grouping [i]])
}
tmazeraw$group= factor(grouping)

##special
# separating the data in the groups set earlier

tmazeraw =
  tmazeraw %>%
    filter (grepl(group1, group))%>%
    droplevels()

## calculate scores

Ntrials= (tmazerawmeta$number.of.trials-1)

DATA = select(tmazeraw,
              num_range("Trial.", 1:Ntrials) )
for (i in c(1:Ntrials)){
    name=c(names(DATA),paste0("success.",i))
    DATA[,Ntrials+i]= (DATA[,i]+DATA[,i+1])-2*DATA[,i]*DATA[,i+1]
    names(DATA)=name

}

DATA=DATA %>%
  mutate(Successrate = (rowSums(.[Ntrials:(2*Ntrials)]))/Ntrials*100)

SCORE= DATA[-(1:Ntrials)]

#adding scores data to tmazeraw
tmazeraw= cbind(tmazeraw,SCORE)

#plotting data
plotdata= tmazeraw
ylab = "success rate [%]"
title= paste0("Succes rate in the Tmaze test with ",Ntrials+1," trials")
analysis= aes(x=agegroup,y= Successrate, fill= group)

plotdatag =group_by(plotdata, group,agegroup)
n = summarise(plotdatag,
              count = n())

p=ggplot(plotdata, analysis)
plot=p+ geom_boxplot()+ labs(x="genotype",y=ylab, title=title)+
  ylim(-10, 100)+
  #scale_fill_grey(start = 0.9, end = 0.9)+ ##allow good bw prints

  geom_text(aes(y= -5,label=paste("n =", count)), data=n, size=5)

boxpl_OF(plotdata,aes(x=factor(agegroup),y= Successrate, fill= group),"success rate [%]",paste0("Succes rate in the Tmaze test with ",Ntrials+1," trials") )

p=ggplot(plotdata, aes(x=factor(agegroup),y= Successrate, fill= group))

p+

#boxpl_OF(OFdata,aes(x=exptype ,y= distancetraveled, fill=group),"distance traveled",paste0("locomotor activity"))

#wilcox.test(y~x)
#t.test(ytest~x)


SMS2FLSMSs<-function(sumfile=file.path(data.path,'summary.out'),residualsFile="Catch_survey_residuals.out",bio.interact=FALSE,read.input=FALSE,read.output=TRUE,
                      control=NULL,FLStocksMulti=NULL) {

    if (is.null(control) | (!inherits(control, "FLSMS.control")))
        stop("A valid 'FLSMS.control'  must be given!")

    nsp<-slot(control,"no.species")
    if (slot(control,"last.season")==1) annual<-TRUE else annual<-FALSE
    info<-slot(control,"species.info")[,"predator"]
    first.VPA<-max(length(info[info==2])+1,1) # other predator have predator code =2
    pl.grp<-slot(control,"species.info")[,"+group"]
    max.age<-slot(control,"species.info")[,"last-age"]
    pl.grp[pl.grp==1]<-max.age[pl.grp==1]
    pl.grp[pl.grp==0]<-NA

    #read summary.out file from SMS run
    sms<-read.table(sumfile,header=TRUE)
    var.names<-names(sms)
    index<-list(sms$Species.n,sms$Age,sms$Year,sms$Quarter)

    condense<-function(x,index) {
      y<-tapply(x,index,sum)
      y[is.na(y)]<-0
      y
    }
    if (read.input) {
      if ("C.obs" %in% var.names) c.n<-condense(sms$C.obs,index)  # catch numbers observed
      if ("west" %in% var.names) s.wt<-condense(sms$west,index)  # catch mean weight
      if ("weca" %in% var.names) c.wt<-condense(sms$weca,index)
      if ("M" %in% var.names) m<-condense(sms$M,index)
      if ("propmat" %in% var.names) mat<-condense(sms$propmat,index)
    }
    if (read.output) {
       if ("C.hat" %in% var.names) c.n.hat<-condense(sms$C.hat,index) # catch numbers, prediction
       if ("N" %in% var.names) s.n<-condense(sms$N,index)          # stock numbers
       if ("F" %in% var.names) harvest<-condense(sms$F,index)      # harvest rate=F
       if (bio.interact) {
         if ("M2" %in% var.names) m2<-condense(sms$M2,index)     # Predation mortality, multispecies species
       }
    }

      condense2<-function(x,index) {
      y<-tapply(x,index,sum)
      y[y==-99.9]<-NA
      y
    }
    if (read.output) {
      #read Catch_survey_residuals.out file from SMS run
      resids<-read.table(residualsFile,header=TRUE)

      # catch data
      resid.c<-subset(resids,data=="catch")
      index<-list(resid.c$Species.n,resid.c$Age,resid.c$Year,resid.c$Quarter)

      c.n.resid<-condense2(resid.c$residual,index)  # catch residuals
    }

    # species names
    s<-slot(control,"species.names")

    if (nsp>1) new.fls <- new("FLSMSs") else new.fls<-FLSMS

    # template for quant
    c1<-c.n[1,,,]
    dc<-dim(c1)
    nc<-dimnames(c1)
    if (annual) {nc[[3]]="all"; dc[3]<-1}
    dimnames<-list(age=nc[[1]],year=nc[[2]],unit="all",season=nc[[3]],area="all",iter="none")
    dim<-c(dc[1],dc[2],1,dc[3],1,1)
    dimnames2<-list(age='all',year=nc[[2]],unit="all",season=nc[[3]],area="all",iter="none")
    dim2<-c(1,dc[2],1,dc[3],1,1)

    for (si in (first.VPA:nsp)) {
        sp.no<-si-first.VPA+1
        if (is.null(FLStocksMulti)) {  # create a new Stock object
            if (nsp==1) q<-c.n else q<-c.n[sp.no,,,]   # Why ?

            s.<-FLStockMulti(name =s[si], desc =s[si],plusgroup=pl.grp[si],
                     iniFLQuant=FLQuant(q,dim=dim,dimnames=dimnames,quant="age",units="1000"))
        } else s.<-new.fls[[si]]        # take the old one
        if (read.input) {

            s.@catch.n <-FLQuant(    q,dim=dim,dimnames=dimnames,quant="age",units="1000")

            if (nsp==1) q<-c.wt else q<-c.wt[sp.no,,,]
            if ("weca" %in% var.names) s.@catch.wt<-FLQuant(q,dim=dim,dimnames=dimnames,quant="age",units="kg")
            if ("weca" %in% var.names) s.@catch   <-FLQuant(apply(s.@catch.n*s.@catch.wt,c(2,3,4),sum),dimnames=dimnames2,quant="all",units="ton")

            if (nsp==1) q<-s.wt else q<-s.wt[sp.no,,,]
            if ("west" %in% var.names) s.@stock.wt<-FLQuant(q,dim=dim,dimnames=dimnames,quant="age",units="kg")

            if (nsp==1) q<-m else q<-m[sp.no,,,]
            if ("M" %in% var.names) s.@m          <-FLQuant(q,dim=dim,dimnames=dimnames,quant="age",units=" ")

            if (nsp==1) q<-mat else q<-mat[sp.no,,,]
            if ("propmat" %in% var.names) s.@mat  <-FLQuant(q,dim=dim,dimnames=dimnames,quant="age",units="proportion")

            if (bio.interact) {
               if (nsp==1) q<-m1 else q<-m1[sp.no,,,]
               if ("M1" %in% var.names) s.@m1    <-FLQuant(q,dim=dim,dimnames=dimnames,quant="age",units=" ")
            }
        }
        if (read.output) {
            if (nsp==1) q<-s.n else q<-s.n[sp.no,,,]
            if ("N" %in% var.names) s.@stock.n  <-FLQuant(q,dim=dim,dimnames=dimnames,quant="age",units="1000")

            if (nsp==1) q<-harvest else q<-harvest[sp.no,,,]
            if ("F" %in% var.names) s.@harvest  <-FLQuant(q,dim=dim,dimnames=dimnames,quant="age",units="f")

            if (("west" %in% var.names) & ("N" %in% var.names)) s.@stock    <-FLQuant(apply(s.@stock.n*s.@stock.wt,c(2,3,4),sum),dimnames=dimnames2,quant="all",units="ton")

            if (bio.interact) {
              if (nsp==1) q<-m2 else q<-m2[sp.no,,,]   # Why ?
               if ("M2" %in% var.names) s.@m2    <-FLQuant(q,dim=dim,dimnames=dimnames,quant="age",units=" ")
            }

        }

        if (nsp>1) new.fls[[sp.no]]<-s.
    }
    ifelse(nsp>1, return(new.fls),return(s.))
}


#' @title summary_deviance
#' @description unknown
#' @family abysmally documented
#' @author  unknown, \email{<unknown>@@dfo-mpo.gc.ca}
#' @export
	summary_deviance = function( M ) {
	  deviance = M$null.deviance-M$deviance
		df = M$df.null-M$df.residual
		p = dchisq( deviance, df )
		list(
			deviance = deviance,
			df = df,
			p = p,
			logLik = logLik( M )
		)
	}


pdf_file<-"pdf/maps_germany_zipcodes_sample.pdf"
cairo_pdf(bg="grey98", pdf_file,width=10,height=10)

par(bg="darkolivegreen1",mai=c(0,0,0,0),oma=c(1,1.5,1,1),family="Lato Light",las=1)
library(maptools)
library(rgdal)

# Import data and prepare chart

myY<-readShapeSpatial("myData/PLZ/post_pl.shp",proj4string=CRS("+proj=longlat"))
myX<-spTransform(myY,CRS=CRS("+proj=merc"))
myY<-subset(myX,substr(myX$PLZ99,1,3)=="539")

# Create chart

plot(myY,col="darkblue",border="white",lwd=3)
text(getSpPPolygonsLabptSlots(myY),labels=myY$PLZ99,cex=1.5,col="white",family="Lato Bold")

# Titling

mtext("Zip Code border, districts 539..",side=3,line=-4,adj=0,cex=2.7,family="Lato Black")
mtext("Source: arnulf.us/PLZ",side=1,line=-1,adj=1,cex=1.3)
dev.off()


#' Cell count estimate quality plot
#'
#' @param count.objects A list of objects each obtained from
#' \code{\link{meffil.estimate.cell.counts}()}.
#' @return Two \link{ggplot2} boxplot objects:
#' - \code{betas} Contains one box per sample or reference cell type
#' representing the distribution of methylation levels for the CpG sites used to
#' estimate cell counts.
#' - \code{counts} Contains one box per reference cell type
#' representing the distribution of cell count estimates across the samples.
#'
#' @export
meffil.cell.count.qc.plots <- function(count.objects) {
    stopifnot(is.list(count.objects) && length(count.objects) > 0)
    stopifnot(sapply(count.objects, is.cell.count.object))
    stopifnot(length(unique(sapply(count.objects, function(object) object$reference))) == 1)

    if (is.null(names(count.objects)))
        names(count.objects) <- paste0("s", 1:length(count.objects))

    reference <- count.objects[[1]]$reference
    reference.object <- get.cell.type.reference(reference)

    beta <- sapply(count.objects, function(object) object$beta)
    common.sites <- intersect(rownames(reference.object$beta), rownames(beta)) ## for backwards compatibility
    beta <- cbind(reference.object$beta[common.sites,], beta[common.sites,])

    if (ncol(beta) <= 200) {
        dat <- sapply(colnames(beta), function(sample)
                      data.frame(sample=sample, beta=beta[,sample]), simplify=F)
        dat <- dat[order(sapply(dat, function(x) mean(x$beta)))]
        dat <- do.call(rbind, dat)
        dat$is.reference <- dat$sample %in% colnames(reference.object$beta)
        beta.plot <- (ggplot(dat, aes(x=sample, y=beta, fill=is.reference)) +
                      geom_boxplot() +
                      guides(fill=FALSE) +
                      stat_summary(fun.y=mean, geom="point", shape=5, size=3) +
                      theme(axis.text.x = element_text(angle = 90, vjust = 0.5, hjust=1,
                                                       face="bold", size=rel(2))) +
                      ggtitle("Distribution of beta values per sample"))
    }
    else {
        dat <- data.frame(sample=colnames(beta), beta=colMeans(beta), na.rm=T)
        dat <- dat[order(dat$beta, decreasing=F),]
        dat$x <- 1:nrow(dat)
        dat$is.reference <- dat$sample %in% colnames(reference.object$beta)
        beta.plot <- (ggplot(dat, aes(x=x, y=beta)) +
                      geom_point(size=3, colour="skyblue3") +
                      geom_text(data=dat[dat$is.reference,],
                                aes(x=x, y=beta, label=sample),
                                vjust=0.5, hjust=0.5,
                                angle=0, fontface="bold") +
                      labs(y="Mean methylation level",
                           x="Samples (sorted by mean methylation level)") +
                      ggtitle("Mean beta values per sample") +
                      theme(axis.text.x = element_blank()))
    }

    counts <- t(sapply(count.objects, function(object) object$counts))
    dat <- sapply(colnames(counts), function(cell.type)
                  data.frame(cell.type=cell.type, count=counts[,cell.type]), simplify=F)
    dat <- dat[order(sapply(dat, function(x) mean(x$count)))]
    dat <- do.call(rbind, dat)

    count.plot <- (ggplot(dat, aes(x=cell.type, y=count)) +
                   geom_boxplot() +
                   guides(fill=FALSE) +
                   stat_summary(fun.y=mean, geom="point", shape=5, size=3) +
                   theme(axis.text.x = element_text(angle=90, vjust=0.5, hjust=1,
                                                    face="bold", size=rel(2))) +
                   ggtitle("Distribution of cell count estimates per cell type"))

    list(betas=beta.plot, counts=count.plot, reference=reference)
}