
```{r}
table <- read.csv ("data_for_R.Table", sep='\t', stringsAsFactors=TRUE)
head (table, 20)
```

```{r}
nativity.contrast <- cbind (c(-0.5, +0.5))   # Foreign = -0.5; Native = +0.5
colnames (nativity.contrast) <- c("-F+N")
contrasts (table$Nativity) <- nativity.contrast
contrasts (table$Nativity)

language.contrast <- cbind (c(-0.5, +0.5))   # E = -0.5; G = +0.5
colnames (language.contrast) <- c("-E+G")
contrasts (table$PL) <- language.contrast
contrasts (table$PL)

gender.contrast <- cbind (c(+0.5, -0.5))   # F = +0.5; M = -0.5
colnames (gender.contrast) <- c("-M+F")
contrasts (table$SG) <- gender.contrast
contrasts (table$SG)

order.contrast <- cbind (c(+0.5, -0.5))   # FN = +0.5; NF = -0.5
colnames (order.contrast) <- c("-NF+FN")
contrasts (table$Order) <- order.contrast
contrasts (table$Order)

model <- lme4::glmer (Correct ~ Nativity * (SG * PL + Order) + (Nativity * SG | P) + (Nativity * (PL + Order) | S), data=table, family=binomial, control = lme4::glmerControl(optCtrl = list(maxfun = 1e5)))
summary (model)

```

```{r}
library(sjPlot)
library(sjmisc)
library(ggplot2)
theme_set(theme_sjplot())
library(emmeans)
```


```{r}
fit <- model
plot_model(fit, type = "pred", terms = c("Correct","Nativity", "PL", "SG"))
plot_model(fit, type = "int")

emmip(fit, Nativity ~ Nativity: PL, CIs = TRUE)


abline(lme4::glmer(Correct~(Nativity*SG)), data=table, family=binomial)
ggplot(table, aes(x = (Nativity:SG), y = )) + geom_point() 
```


```{r}
library("dplyr")
library(ggpubr)
ggdensity(table$Nativity)
```


```{r}
ggplot(data = table,aes(x=Nativity,y=Correct,col=PL))  + geom_jitter() + facet_wrap(~PL)
```


```{r}
xtabs(~ PL + PE, data = table)
plot(PE~PL, data = table)
```