---
title: "Reproducible code for Castanho Silva et al., 2017"
author: "Bruno Castanho Silva"
date: "11/17/2017"
output: pdf_document
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```

Required libraries

```{r message=F, warning=F}
library(lavaan)
library(mirt)
library(semTools)
library(lattice)
library(gridExtra)
library(psych)
library(ggplot2)
library(extrafont)
library(nFactors)
loadfonts()
```

Data for Figures 7.2, 7.3, 7.4, 7.5, and 7.6, and Table 7.2:

```{r}
data<-read.csv('data.merged_mar17.csv',header=F)

varnames<-unlist(strsplit(readLines('varnames_mar17.txt'), '\t'))
names(data)<-varnames


data[data==-999]<-NA

data<-subset(data, country != 10)

data$simple8.r<-8-data$simple8
data$rwpop8.r<-8-data$rwpop8
data$manich13.r<-8-data$manich13
```

## Figure 7.2

CFA model results

```{r results=F, warning=F, message=F}

model.cfa.ours<-'
antiel =~ antiel23 + rwpop8.r + antiel21
people =~ gewill17 + simple8.r + gewill3
manich =~ manich15 + manich13.r + manich14
method =~ gewill3 + b1*antiel23 +  b1*antiel21 + b1*gewill17 + b1*manich15 + b1*manich14
method ~~ 0*antiel + 0*people + 0*manich
'
ours.cfa.fit<-cfa(data=data,model=model.cfa.ours,estimator='mlr',missing='fiml',
                  group='country',group.equal='loadings')

summary(ours.cfa.fit,fit.measures=T)
```

## Table 2

```{r warning=F, message=F}
ours.mgcfa<-'
antiel =~ antiel23 + rwpop8.r + antiel21
people =~ gewill17 + simple8.r + gewill3
manich =~ manich15 + manich13.r + manich14
method =~ gewill3 + b1*antiel23 +  b1*antiel21 + b1*gewill17 + b1*manich15 + b1*manich14
method ~~ 0*antiel + 0*people + 0*manich
'
measurementInvariance(ours.mgcfa,data=data,estimator='mlr',missing='fiml',group='country')

akkerman.mgcfa<-'pop =~ akker1 + akker2 + akker3 + akker4 + akker5 + akker6'
measurementInvariance(model=akkerman.mgcfa,data=data,estimator='mlr',missing='fiml',
                      group = 'country')

```

## Figures 7.3-7.6

```{r message=F, warning=F}
data$Ant1<-data$antiel23
data$Ant2<-data$rwpop8.r
data$Ant3<-data$antiel21

data$Ppl1<-data$gewill17
data$Ppl2<-data$simple8.r
data$Ppl3<-data$gewill3

data$Man1<-data$manich15
data$Man2<-data$manich13.r
data$Man3<-data$manich1

bwtheme <- standard.theme(color=FALSE)

# 7.3:
model.people<-mirt(data['country' != 10,c('Ppl1','Ppl2','Ppl3')],1,itemtype='grsm',
                   technical = list(removeEmptyRows=T))

p.people<-plot(model.people, type = 'infoSE', sub='People-centrism',
     facet_items=F)
p.people<-update(p.people,sub='People-centrism',main='Information and SE',
                 par.settings=bwtheme)
          
p.people.trace<-plot(model.people, type = 'infotrace',
                     facet_items=F)
p.people.trace<-update(p.people.trace, sub ='People-centrism',main='Item Information',
                       par.settings=bwtheme,legend=NULL, 
                       auto.key = list(space = "inside", points = FALSE, lines = TRUE,
                                       cex=0.7))

grid.arrange(p.people,p.people.trace,ncol=2)

# 7.4:
model.antiel<-mirt(data['country' != 10,c('Ant1','Ant2','Ant3')],1,itemtype='grsm',
                   technical = list(removeEmptyRows=T))

p.antiel<-plot(model.antiel, type = 'infoSE', main='Information and SE',
     facet_items=F)
p.antiel<-update(p.antiel,sub='Antielitism',par.settings=bwtheme)

p.antiel.trace<-plot(model.antiel, type = 'infotrace', main='Item Information',
                     facet_items=F)
p.antiel.trace<-update(p.antiel.trace,sub='Antielitism',par.settings=bwtheme,
                       legend=NULL,auto.key = list(space = "inside", points = FALSE, 
                                                   lines = TRUE,cex=0.7))

grid.arrange(p.antiel, p.antiel.trace, ncol=2)

# 7.5:
model.manich<-mirt(data['country' != 10,c('Man1','Man2','Man3')],1,itemtype='grsm',
                   technical = list(removeEmptyRows=T))

p.manich<-plot(model.manich, type = 'infoSE',
     facet_items=F)
p.manich<-update(p.manich, main='Information and SE',sub='Manichaean',par.settings=bwtheme)

p.manich.trace<-plot(model.manich, type = 'infotrace',
               facet_items=F)
p.manich.trace<-update(p.manich.trace, main='Item Information',sub='Manichaean',
                       legend=NULL,par.settings=bwtheme,
                       auto.key = list(space = "inside", points = FALSE, lines = TRUE,
                                       cex=0.7))

grid.arrange(p.manich,p.manich.trace,ncol=2)


# 7.6:
model.populism.short<-mirt(data['country' != 10,c('antiel23','rwpop8.r','gewill17',
                                                  'simple8.r','manich15','manich13.r')],
                     1, itemtype='grsm', technical = list(removeEmptyRows=T))

p.short<-plot(model.populism.short,type = 'infoSE', 
              main='Information curve - Six item scale')
p.short<-update(p.short, main='Information and SE',
                sub='New scale',par.settings=bwtheme)

p.short.trace<-plot(model.populism.short,type = 'infotrace', 
                    main='Item Information',facet_items=F)
p.short.trace<-update(p.short.trace, main='Item Information',
                      sub='New scale',legend=NULL,par.settings=bwtheme,
                       auto.key = list(space = "inside", points = FALSE, 
                                       lines = TRUE,cex=0.7))

model.akkerman<-mirt(data['country' != 10,c('akker1','akker2','akker3','akker4',
                                            'akker5','akker6')],
                     1, itemtype='grsm',technical = list(removeEmptyRows=T))

p.akker<-plot(model.akkerman,type='infoSE',facet_items=F, main=' ')
p.akker<-update(p.akker, sub='Akkerman et al. (2014) items',par.settings=bwtheme)

grid.arrange(p.short,p.akker,ncol=1)
```

## Figure 7A.1

```{r message=F, warning=F}
pop1<-read.csv('populism_oct24_Mplus.csv',header=F)
vars<-c('Resp','Codect','praise1','praise2','praise3','praise4','praise5','praise6',
        'praise7','praise8','praise9','praise10','praise11','praise12','gewill1',
        'gewill2','gewill3','gewill4','gewill5','gewill6','gewill7','gewill8','gewill9',
        'gewill10','gewill11','gewill12','gewill13','gewill14','gewill15','gewill16',
        'gewill17','akker1','akker2','crisis1','crisis2','crisis3','crisis4','crisis5',
        'crisis6','crisis7','strong1','strong2','strong3','strong4','strong5','strong6',
        'strong7','strong8','strong9','strong10','strong11','strong12','strong13','strong14',
        'simple1','simple2','simple3','simple4','simple5','simple6','simple7','simple8',
        'simple9','simple10','simple11','simple12','simple13','simple14','manich1','manich2',
        'manich3','manich4','manich5','manich6','manich7','manich8','manich9','manich10',
        'manich11','manich12','manich13','manich14','manich15','manich16','manich17','manich18',
        'manich19','manich20','manich21','antiel1','antiel2','antiel3','antiel4','antiel5',
        'antiel6','antiel7','antiel8','antiel9','antiel10','antiel11','antiel12','antiel13',
        'antiel14','antiel15','antiel16','antiel17','antiel18','antiel19','antiel20',
        'antiel21','antiel22','antiel23','antiel24','rwpop1','rwpop2','rwpop3','rwpop4',
        'rwpop5','rwpop6','rwpop7','rwpop8','rwpop9','rwpop10','rwpop11','rwpop12','lwpop1',
        'lwpop2','lwpop3','lwpop4','lwpop5','lwpop6','lwpop7','lwpop8','lwpop9','lwpop10',
        'lwpop11','lwpop12','any1','any2','any3','any4','any5','any6','any7','any8','any9',
        'any10','conspop1','conspop2','conspop3','conspop4','conspop5','conspop6','conspop7',
        'conspop8','consel1','consel2','consel3','consel4','consel5','consel6','consel7',
        'consel8','consplu1','consplu2','consplu3','consplu4','consplu5','consplu6','consplu7',
        'consplu8','wpc1','wpc2','wpc3','wpc4','wpc5','wpc6','wpc7','wpc8','wpc9','wpc10',
        'wpc11','wpc12','wpc13','wpc14','wpc15','wpc16','wpc17','wpc18','wpc19','wpc20','wpl1',
        'wpl2','wpl3','wpl4','wpl5','wpl6','wpl7','wpl8','wpl9','wpl10','wpl11','wpl12','wpl13',
        'wpl14','wpl15','wpl16','wpl17','wpl18','wpl19','wpl20','lr','aneslr','year','marital',
        'female','class','class1','class2','class3','class4')

names(pop1)<-vars

pop1[pop1 == -999]<-NA

pop<-subset(pop1, Resp < 235 | Resp > 718)

eig<-c(15.84,9.645,6.941,5.514,4.283,3.245,2.696,2.317,2.215,2.104,1.868,1.695,1.662,1.625,1.555
       ,1.425,1.367,1.356,1.324,1.287,1.283,1.208,1.204,1.19,1.168,1.126,1.095,1.082,1.059,1.038
       ,1.024,1.005,0.998,0.975,0.971,0.954,0.944,0.934,0.916,0.914,0.89,0.889,0.882,0.87,0.858
) # Taken from efa_pooled4.out


a<-fa.parallel(pop[,3:147],fa='pc',fm='ml',n.iter=200,cor='cor',sim=T, use='pairwise')

eigen.sim<-a$pc.sim[1:length(eig)]
nfactors<-c(1:45)
data.gr<-data.frame(matrix(c(eig,eigen.sim,nfactors,nfactors),ncol=2,byrow=F))
data.gr$X3<-c(rep(1,45),rep(2,45))
p<-ggplot(data.gr,aes(X2,X1,linetype=factor(X3)))+geom_line()+
  scale_linetype_manual(values=c('solid','dashed'),labels=c('Sample','PA'), 
                        guide = guide_legend(title = NULL))+
  geom_vline(xintercept=8,linetype='dotted')+ylab('Eigenvalues')+xlab('Nr of factors')+
  scale_x_continuous(breaks = c(0,8,10,20,30,40,50))+
  theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(), 
        panel.background = element_blank(),#axis.line.x = element_line(colour = 'black'),
        axis.line = element_line(colour = 'black'),
        axis.text.x = element_text(colour = 'black'),axis.text.y = element_text(colour = 'black'),
        legend.key = element_blank(),legend.title = element_blank(),legend.justification=c(1,1),
        legend.position=c(.8,.8))

p
```

