---
title: "CASE analysis, Naomi Nagy, 22 Jan 2023"
output:
  html_document:
    toc: yes
    number_section: yes
    code_folding: show
    df_print: paged
  word_document: default
---
```{r setup, include=FALSE, cache = FALSE}
knitr::opts_knit$set(root.dir = "/Users/naomi/Documents/RESEARCH/HLVC/CASE/Case for book/")
knitr::opts_chunk$set(echo = TRUE)

options(digits = 2)              # Modify number of digits after decimal in output
```
# Setting up R packages

Installing packages needed for analysis
lmerTest uses Satterthwaite approximations for pvalues in MEMs
https://www.r-bloggers.com/2014/02/three-ways-to-get-parameter-specific-p-values-from-lmer/)

```{r load_packages, include=FALSE}
options(repos = list(CRAN="http://cran.rstudio.com/"))
#update.packages(ask=FALSE)
install.packages("lme4")
#library(lmerTest)
install.packages("ordinal")
library(ordinal)
library(lme4)
library(tidyverse)
library(magrittr)
library(dplyr)
library(forcats)
library(tibble)
library(piecewiseSEM)

```
### Preliminary operations before the analysis

## Load data, make factors, see overview

```{r variables_to_factors, include=T}
#for Naomi's home (Blue) computer
Casedata <- read.delim(file="/Users/naomi/Documents/RESEARCH/HLVC/CASE/Case for book/Case_tokens_22Jan2023.csv",sep=",", header=T)

#for Naomi's MacBook Air
#Casedata <- read.delim(file="Case_tokens_3lgs_19Dec2022.csv",sep=",", header=T)

Casedata <- Casedata %>% mutate_at(vars(match, prescribed_by_selector, prescribed_case, observed_case, noun_or_pronoun, declension, case_selector_simplified, language, generation, sex, speaker), as.factor)

summary(Casedata)
addmargins(table(Casedata$language,Casedata$generation))
#prop.table(table(Casedata$language,Casedata$generation))

#exclude 17 speakers with NO mismatch: 

Casedata <- subset(Casedata, speaker !="UXF23A")
Casedata <- subset(Casedata, speaker !="UXM18A")

Casedata <- subset(Casedata, speaker !="RXFxxK")
Casedata <- subset(Casedata, speaker !="RXMxxE")
Casedata <- subset(Casedata, speaker !="R1F47A")
Casedata <- subset(Casedata, speaker !="R1F54A")
Casedata <- subset(Casedata, speaker !="R1F55B")
Casedata <- subset(Casedata, speaker !="R1F82A")
Casedata <- subset(Casedata, speaker !="R1M56A")

Casedata <- subset(Casedata, speaker !="PXF28A")
Casedata <- subset(Casedata, speaker !="PXF43A")
Casedata <- subset(Casedata, speaker !="PXF66A")
Casedata <- subset(Casedata, speaker !="PXM25A")
Casedata <- subset(Casedata, speaker !="PXM69A")
Casedata <- subset(Casedata, speaker !="P1M44A")
Casedata <- subset(Casedata, speaker !="P2F18A")
Casedata <- subset(Casedata, speaker !="P2F23A")
  
summary(Casedata$speaker)
addmargins(table(Casedata$language,Casedata$generation))
#prop.table(table(Casedata$language,Casedata$generation))
```

## Ordering factor vectors to determine the default level

```{r include=T}
 
levels(Casedata$match)
levels(Casedata$prescribed_case)
levels(Casedata$observed_case)
levels(Casedata$prescribed_by_selector)
levels(Casedata$noun_or_pronoun)
levels(Casedata$declension)
levels(Casedata$case_selector_simplified)
levels(Casedata$language)
levels(Casedata$generation)
levels(Casedata$sex)

Casedata <- Casedata %>% mutate(match = fct_relevel(match, 'mismatch', 'match'),
                              prescribed_by_selector =  fct_relevel(prescribed_by_selector,'GEN.merged', 'ACC.verb',
                              'ACC.preposition','ACC.number_or_quantifier', 
                              'DAT.verb', 'DAT.impersonal_dative',               
                            'GEN.verb', 'GEN.preposition', 'GEN.number_or_quantifier', 
                            'INS.verb', 'INS.preposition', 'INS.number_or_quantifier', 'INS.poss.neg', 'INS.other',                                   'LOC.preposition'),

                              observed_case = fct_relevel(observed_case, 'NOM','ACC','GEN', 'DAT', 'INS', 'LOC'),
                              noun_or_pronoun = fct_relevel(noun_or_pronoun, 'pronoun','noun'),
                              case_selector_simplified = fct_relevel(case_selector_simplified, 'verb','preposition', 'partitive','impersonal_dative','number_or_quantifier','poss.neg','other'),
                              generation = fct_relevel(generation, 'Homeland','Gen1','Gen2', 'Gen3'),
                              sex = fct_relevel(sex, 'M','F')
                              )
```
Subsetting data
```{r subsetting each language, include=T}
polishdata <- subset(Casedata, language =="Polish")
russiandata <- subset(Casedata, language =="Russian")
ukrainiandata <- subset(Casedata, language =="Ukrainian")

## exclude just for Polish
polishdata<-polishdata[polishdata$prescribed_by_selector!="DAT.verb",]
polishdata<-polishdata[polishdata$prescribed_by_selector!="DAT.impersonal_dative",]
polishdata<-polishdata[polishdata$prescribed_by_selector!="GEN.number_or_quantifier",]

## exclude just for Ukrainian
ukrainiandata<-ukrainiandata[ukrainiandata$prescribed_by_selector!="DAT.impersonal_dative",]

polish_HOM_data <- subset(polishdata, generation =="Homeland")
russian_HOM_data <- subset(russiandata, generation =="Homeland") 
ukrainian_HOM_data <- subset(ukrainiandata, generation =="Homeland")

## exclude just for Homeland Russian
russian_HOM_data<-russian_HOM_data[russian_HOM_data$prescribed_by_selector!="GEN.number_or_quantifier",]

polish_HOM_data$age <- as.numeric(polish_HOM_data$age)
#russian_HOM_data$age <- as.numeric(russian_HOM_data$age)
ukrainian_HOM_data$age <- as.numeric(ukrainian_HOM_data$age)

polish_HER_data <- subset(polishdata, generation !="Homeland")
russian_HER_data <- subset(russiandata, generation !="Homeland")
ukrainian_HER_data <- subset(ukrainiandata, generation !="Homeland")

polish_HER_data$EO_language <- as.numeric(polish_HER_data$EO_language)
polish_HER_data$EO_culture <- as.numeric(polish_HER_data$EO_culture)
russian_HER_data$EO_language <- as.numeric(russian_HER_data$EO_language)
russian_HER_data$EO_culture <- as.numeric(russian_HER_data$EO_culture)
ukrainian_HER_data$EO_language <- as.numeric(ukrainian_HER_data$EO_language)
ukrainian_HER_data$EO_culture <- as.numeric(ukrainian_HER_data$EO_culture)

```

### Excluding small cells & categorical levels from each language, & heritage data summary
```{r excluding small cells from heritage subsets}
addmargins(table(polish_HER_data$prescribed_by_selector, polish_HER_data$match)  )
addmargins(table(russian_HER_data$prescribed_by_selector, russian_HER_data$match)  )
addmargins(table(ukrainian_HER_data$prescribed_by_selector, ukrainian_HER_data$match))  

options(digits = 2)
prop.table(table(polish_HER_data$prescribed_by_selector, polish_HER_data$match), margin =1 )
prop.table(table(russian_HER_data$prescribed_by_selector, russian_HER_data$match) , margin =1)
prop.table(table(ukrainian_HER_data$prescribed_by_selector, ukrainian_HER_data$match), margin =1)  



```
```{r excluding small cells from homeland subsets, echo=T}
addmargins(table(polish_HOM_data$prescribed_by_selector, polish_HOM_data$match))
addmargins(table(russian_HOM_data$prescribed_by_selector, russian_HOM_data$match)) 
addmargins(table(ukrainian_HOM_data$prescribed_by_selector, ukrainian_HOM_data$match))

prop.table(table(polish_HOM_data$prescribed_by_selector, polish_HOM_data$match), margin =1)
prop.table(table(russian_HOM_data$prescribed_by_selector, russian_HOM_data$match), margin =1) 
prop.table(table(ukrainian_HOM_data$prescribed_by_selector, ukrainian_HOM_data$match), margin =1)
##very small n ("X" in exclusion contexts.xlsx, excluded from 22Jan token file)
## exclude from full data set (already minus categorical speakers); then repartition

Casedata<-Casedata[Casedata$prescribed_by_selector!="ACC.number_or_quantifier",]
Casedata<-Casedata[Casedata$prescribed_by_selector!="INS.number_or_quantifier",]
Casedata<-Casedata[Casedata$prescribed_by_selector!="LOC.number_or_quantifier",]
Casedata<-Casedata[Casedata$prescribed_by_selector!="LOC.verb",]
Casedata<-Casedata[Casedata$prescribed_by_selector!="ACC.partitive",]
Casedata<-Casedata[Casedata$prescribed_by_selector!="LOC.partitive",]
Casedata<-Casedata[Casedata$prescribed_by_selector!="ACC.poss.neg",]
Casedata<-Casedata[Casedata$prescribed_by_selector!="DAT.poss.neg",]
Casedata<-Casedata[Casedata$prescribed_by_selector!="INS.poss.neg",]
Casedata<-Casedata[Casedata$prescribed_by_selector!="LOC.poss.neg",]
Casedata<-Casedata[Casedata$prescribed_by_selector!="ACC.other",]
Casedata<-Casedata[Casedata$prescribed_by_selector!="DAT.other",]
Casedata<-Casedata[Casedata$prescribed_by_selector!="INS.other",]
Casedata<-Casedata[Casedata$prescribed_by_selector!="LOC.other",]
```

```{r merge GEN, include=FALSE}
##merge small GEN contexts (done in 22Jan token file)
GEN.merged<-Casedata[Casedata$prescribed_by_selector=="GEN.partitive",]
GEN.merged<-Casedata[Casedata$prescribed_by_selector=="GEN.poss.neg",]
GEN.merged<-Casedata[Casedata$prescribed_by_selector=="GEN.other",]
```

```{r reset data and subset, include=FALSE}
##reset the data structure, to have factors with only existing levels (not excluded ones), non-categorical dataset
Casedata <- Casedata %>% mutate_at(vars(match, prescribed_by_selector, prescribed_case, observed_case, noun_or_pronoun, declension, case_selector_simplified, language, generation, sex, speaker), as.factor)

Casedata <- Casedata %>% mutate(match = fct_relevel(match, 'mismatch', 'match'),

                                                               prescribed_by_selector =  fct_relevel(prescribed_by_selector,'GEN.merged', 'ACC.verb',
                              'ACC.preposition',
                              'DAT.verb', 'DAT.impersonal_dative',               
                            'GEN.verb', 'GEN.preposition', 'GEN.number_or_quantifier',   'INS.verb', 'INS.preposition', 'LOC.preposition'),

           observed_case = fct_relevel(observed_case, 'NOM','ACC','GEN', 'DAT', 'INS', 'LOC'),
           noun_or_pronoun = fct_relevel(noun_or_pronoun, 'pronoun','noun'),
           case_selector_simplified = fct_relevel(case_selector_simplified, 'verb','preposition', 'partitive','impersonal_dative','number_or_quantifier','poss.neg','other'),
            generation = fct_relevel(generation, 'Homeland','Gen1','Gen2', 'Gen3'),
            sex = fct_relevel(sex, 'M','F')
                              )

polishdata <- subset(Casedata, language =="Polish",)
russiandata <- subset(Casedata, language =="Russian",)
ukrainiandata <- subset(Casedata, language =="Ukrainian",)

##then partition for HOM & HER
polish_HOM_data <- subset(polishdata, generation =="Homeland",)
russian_HOM_data <- subset(russiandata, generation =="Homeland",) 
ukrainian_HOM_data <- subset(ukrainiandata, generation =="Homeland",)

polish_HOM_data$age <- as.numeric(polish_HOM_data$age)
#russian_HOM_data$age <- as.numeric(russian_HOM_data$age)
ukrainian_HOM_data$age <- as.numeric(ukrainian_HOM_data$age)

polish_HER_data$EO_language <- as.numeric(polish_HER_data$EO_language)
polish_HER_data$EO_culture <- as.numeric(polish_HER_data$EO_culture)
russian_HER_data$EO_language <- as.numeric(russian_HER_data$EO_language)
russian_HER_data$EO_culture <- as.numeric(russian_HER_data$EO_culture)
ukrainian_HER_data$EO_language <- as.numeric(ukrainian_HER_data$EO_language)
ukrainian_HER_data$EO_culture <- as.numeric(ukrainian_HER_data$EO_culture)


polish_HOM_data<-polish_HOM_data[polish_HOM_data$prescribed_by_selector!="ACC.verb",]
polish_HOM_data<-polish_HOM_data[polish_HOM_data$prescribed_by_selector!="ACC.preposition",]
polish_HOM_data<-polish_HOM_data[polish_HOM_data$prescribed_by_selector!="DAT.verb",]
polish_HOM_data<-polish_HOM_data[polish_HOM_data$prescribed_by_selector!="DAT.impersonal_dative",]
polish_HOM_data<-polish_HOM_data[polish_HOM_data$prescribed_by_selector!="GEN.number_or_quantifier",]
polish_HOM_data<-polish_HOM_data[polish_HOM_data$prescribed_by_selector!="INS.verb",]
polish_HOM_data<-polish_HOM_data[polish_HOM_data$prescribed_by_selector!="LOC.preposition",]

russian_HOM_data<-russian_HOM_data[russian_HOM_data$prescribed_by_selector!="ACC.verb",]
russian_HOM_data<-russian_HOM_data[russian_HOM_data$prescribed_by_selector!="DAT.verb",]
russian_HOM_data<-russian_HOM_data[russian_HOM_data$prescribed_by_selector!="DAT.impersonal_dative",]
russian_HOM_data<-russian_HOM_data[russian_HOM_data$prescribed_by_selector!="GEN.verb",]
russian_HOM_data<-russian_HOM_data[russian_HOM_data$prescribed_by_selector!="INS.verb",]
russian_HOM_data<-russian_HOM_data[russian_HOM_data$prescribed_by_selector!="INS.preposition",]
russian_HOM_data<-russian_HOM_data[russian_HOM_data$prescribed_by_selector!="LOC.preposition",]
russian_HOM_data<-russian_HOM_data[russian_HOM_data$prescribed_by_selector!="GEN.number_or_quantifier",]

ukrainian_HOM_data<-ukrainian_HOM_data[ukrainian_HOM_data$prescribed_by_selector!="DAT.impersonal_dative",]
ukrainian_HOM_data<-ukrainian_HOM_data[ukrainian_HOM_data$prescribed_by_selector!="GEN.verb",]
ukrainian_HOM_data<-ukrainian_HOM_data[ukrainian_HOM_data$prescribed_by_selector!="GEN.preposition",]

polish_HER_data <- subset(polishdata, generation !="Homeland",)
russian_HER_data <- subset(russiandata, generation !="Homeland",)
ukrainian_HER_data <- subset(ukrainiandata, generation !="Homeland",)

##resetting the data structure, to have factors with only existing levels (not excluded ones) in the 9 subsets
polishdata <- polishdata %>% mutate_at(vars(match, prescribed_by_selector, prescribed_case, observed_case, noun_or_pronoun, declension, case_selector_simplified, language, generation, sex, speaker), as.factor)
russiandata <- russiandata %>% mutate_at(vars(match, prescribed_by_selector, prescribed_case, observed_case, noun_or_pronoun, declension, case_selector_simplified, language, generation, sex, speaker), as.factor)
ukrainiandata <- ukrainiandata %>% mutate_at(vars(match, prescribed_by_selector, prescribed_case, observed_case, noun_or_pronoun, declension, case_selector_simplified, language, generation, sex, speaker), as.factor)

polish_HOM_data <- polish_HOM_data %>% mutate_at(vars(match, prescribed_by_selector, prescribed_case, observed_case, noun_or_pronoun, declension, case_selector_simplified, language, generation, sex, speaker), as.factor)
russian_HOM_data <- russian_HOM_data %>% mutate_at(vars(match, prescribed_by_selector, prescribed_case, observed_case, noun_or_pronoun, declension, case_selector_simplified, language, generation, sex, speaker), as.factor)
ukrainian_HOM_data <- ukrainian_HOM_data %>% mutate_at(vars(match, prescribed_by_selector, prescribed_case, observed_case, noun_or_pronoun, declension, case_selector_simplified, language, generation, sex, speaker), as.factor)

polish_HER_data <- polish_HER_data %>% mutate_at(vars(match, prescribed_by_selector, prescribed_case, observed_case, noun_or_pronoun, declension, case_selector_simplified, language, generation, sex, speaker), as.factor)
russian_HER_data <- russian_HER_data %>% mutate_at(vars(match, prescribed_by_selector, prescribed_case, observed_case, noun_or_pronoun, declension, case_selector_simplified, language, generation, sex, speaker), as.factor)
ukrainian_HER_data <- ukrainian_HER_data %>% mutate_at(vars(match, prescribed_by_selector, prescribed_case, observed_case, noun_or_pronoun, declension, case_selector_simplified, language, generation, sex, speaker), as.factor)

summary(polish_HER_data)
```

# sending the language subsets to the glmer function for MEMs
## full dataset for each language, & tables for n and rate

## Main effects Polish, all speakers
```{r main effects for Polish}
main_effect_polish <- glmer (match ~ generation +
                   sex +
                   noun_or_pronoun + 
                   prescribed_by_selector +
                   (1|speaker),
                    data = polishdata, family = "binomial", control = glmerControl(optCtrl = list(maxfun = 2e4), optimizer = "bobyqa"))
summary(main_effect_polish, correlation=F)

addmargins(table(polishdata$generation, polishdata$match))
addmargins(table(polishdata$sex, polishdata$match))
addmargins(table(polishdata$noun_or_pronoun, polishdata$match))
addmargins(table(polishdata$prescribed_by_selector, polishdata$match))

prop.table(table(polishdata$generation, polishdata$match), margin =1)
prop.table(table(polishdata$sex, polishdata$match), margin =1)
prop.table(table(polishdata$noun_or_pronoun, polishdata$match), margin =1)
prop.table(table(polishdata$prescribed_by_selector, polishdata$match), margin =1)
```

## Main effects Russian, all speakers
```{r main effects for Russian }
main_effect_russian <- glmer (match ~ generation +
                   sex +
                   noun_or_pronoun + 
                   prescribed_by_selector +
                   (1|speaker),
                    data = russiandata, family = "binomial", control = glmerControl(optCtrl = list(maxfun = 2e4), optimizer = "bobyqa"))
summary(main_effect_russian, correlation=F)

addmargins(table(russiandata$generation, russiandata$match))
addmargins(table(russiandata$sex, russiandata$match))
addmargins(table(russiandata$noun_or_pronoun, russiandata$match))
addmargins(table(russiandata$prescribed_by_selector, russiandata$match))

prop.table(table(russiandata$generation, russiandata$match), margin =1)
prop.table(table(russiandata$sex, russiandata$match), margin =1)
prop.table(table(russiandata$noun_or_pronoun, russiandata$match), margin =1)
prop.table(table(russiandata$prescribed_by_selector, russiandata$match), margin =1)
```
## Main effects Ukrainain, all speakers
```{r main effects for Ukrainain }
main_effect_ukrainian <- glmer (match ~ generation +
                   sex +
                   noun_or_pronoun + 
                   prescribed_by_selector +
                   (1|speaker),
                    data = ukrainiandata, family = "binomial", control = glmerControl(optCtrl = list(maxfun = 2e4), optimizer = "bobyqa"))
summary(main_effect_ukrainian, correlation=F)

addmargins(table(ukrainiandata$generation, ukrainiandata$match))
addmargins(table(ukrainiandata$sex, ukrainiandata$match))
addmargins(table(ukrainiandata$noun_or_pronoun, ukrainiandata$match))
addmargins(table(ukrainiandata$prescribed_by_selector, ukrainiandata$match))

prop.table(table(ukrainiandata$generation, ukrainiandata$match), margin =1)
prop.table(table(ukrainiandata$sex, ukrainiandata$match), margin =1)
prop.table(table(ukrainiandata$noun_or_pronoun, ukrainiandata$match), margin =1)
prop.table(table(ukrainiandata$prescribed_by_selector, ukrainiandata$match), margin =1)
```
## full dataset for each language, with interactions of Generation and Sex * each lx. factor

## Interaction effects Polish, all speakers
```{r Interaction effects for Polish }
interaction_effect_polish <- glmer (match ~ generation +
                   sex +
                   noun_or_pronoun + 
                   prescribed_by_selector +
                   generation*noun_or_pronoun +
                   generation*prescribed_by_selector +
                   sex*noun_or_pronoun + 
                   sex*prescribed_by_selector + 
                     (1|speaker),
                    data = polishdata, family = "binomial", control = glmerControl(optCtrl = list(maxfun = 2e4), optimizer = "bobyqa"))
summary(interaction_effect_polish, correlation=F)
```

## Interaction effects Russian, all speakers
```{r Interaction effects for Russian}
interaction_effect_russian <- glmer (match ~ generation +
                   sex +
                   noun_or_pronoun + 
                   prescribed_by_selector +
                   generation*noun_or_pronoun +
                   generation*prescribed_by_selector +
                   sex*noun_or_pronoun + 
                   sex*prescribed_by_selector + 
                   (1|speaker),
                    data = russiandata, family = "binomial", control = glmerControl(optCtrl = list(maxfun = 2e4), optimizer = "bobyqa"))
summary(interaction_effect_russian, correlation=F)
```

## Interaction effects Ukrainain, all speakers
```{r Interaction effects for Ukrainain}
interaction_effect_ukrainian <- glmer (match ~ generation +
                   sex +
                   noun_or_pronoun + 
                   prescribed_by_selector +
                   generation*noun_or_pronoun +
                   generation*prescribed_by_selector +
                   sex*noun_or_pronoun + 
                   sex*prescribed_by_selector + 
                   (1|speaker),
                    data = ukrainiandata, family = "binomial", control = glmerControl(optCtrl = list(maxfun = 2e4), optimizer = "bobyqa"))
summary(interaction_effect_ukrainian, correlation=F)
```

# Main effects for each heritage language 

## Main effects Polish, heritage speakers
```{r main effects for HER Polish}
main_effect_HER_polish <- glmer (match ~ generation +
                   sex +
                   noun_or_pronoun + 
                   prescribed_by_selector +
                   (1|speaker),
                    data = polish_HER_data, family = "binomial", control = glmerControl(optCtrl = list(maxfun = 2e4), optimizer = "bobyqa"))
summary(main_effect_HER_polish, correlation=F)

addmargins(table(polish_HER_data$generation,polish_HER_data$match))
addmargins(table(polish_HER_data$sex,polish_HER_data$match))
addmargins(table(polish_HER_data$noun_or_pronoun,polish_HER_data$match))
addmargins(table(polish_HER_data$prescribed_by_selector,polish_HER_data$match))

prop.table(table(polish_HER_data$generation,polish_HER_data$match), margin =1)
prop.table(table(polish_HER_data$sex,polish_HER_data$match), margin =1)
prop.table(table(polish_HER_data$noun_or_pronoun,polish_HER_data$match), margin =1)
prop.table(table(polish_HER_data$prescribed_by_selector,polish_HER_data$match), margin =1)
```

## Main effects Russian, heritage speakers 
```{r main effects for HER Russian}
main_effect_HER_russian <- glmer (match ~ generation +
                   sex +
                   noun_or_pronoun + 
                   prescribed_by_selector +
                   (1|speaker),
                    data = russian_HER_data, family = "binomial", control = glmerControl(optCtrl = list(maxfun = 2e4), optimizer = "bobyqa"))
summary(main_effect_HER_russian, correlation=F)

addmargins(table(russian_HER_data$generation, russian_HER_data$match))
addmargins(table(russian_HER_data$sex, russian_HER_data$match))
addmargins(table(russian_HER_data$noun_or_pronoun, russian_HER_data$match))
addmargins(table(russian_HER_data$prescribed_by_selector, russian_HER_data$match))

prop.table(table(russian_HER_data$generation, russian_HER_data$match), margin =1)
prop.table(table(russian_HER_data$sex, russian_HER_data$match), margin =1)
prop.table(table(russian_HER_data$noun_or_pronoun, russian_HER_data$match), margin =1)
prop.table(table(russian_HER_data$prescribed_by_selector, russian_HER_data$match), margin =1)
```
## Main effects Ukrainain, heritage speakers
```{r main effects for HER Ukrainain}
main_effect_HER_ukrainian <- glmer (match ~ generation +
                   sex +
                   noun_or_pronoun + 
                   prescribed_by_selector +
                   (1|speaker),
                    data = ukrainian_HER_data, family = "binomial", control = glmerControl(optCtrl = list(maxfun = 2e4), optimizer = "bobyqa"))
summary(main_effect_HER_ukrainian, correlation=F)

addmargins(table(ukrainian_HER_data$generation,ukrainian_HER_data$match))
addmargins(table(ukrainian_HER_data$sex,ukrainian_HER_data$match))
addmargins(table(ukrainian_HER_data$noun_or_pronoun,ukrainian_HER_data$match))
addmargins(table(ukrainian_HER_data$prescribed_by_selector,ukrainian_HER_data$match))

prop.table(table(ukrainian_HER_data$generation,ukrainian_HER_data$match), margin =1)
prop.table(table(ukrainian_HER_data$sex,ukrainian_HER_data$match), margin =1)
prop.table(table(ukrainian_HER_data$noun_or_pronoun,ukrainian_HER_data$match), margin =1)
prop.table(table(ukrainian_HER_data$prescribed_by_selector,ukrainian_HER_data$match), margin =1)

```

## Main effects Polish, heritage speakers with EO instead of generation
```{r main effects for HER Polish EO}
main_effect_HER_polish_EO <- glmer (match ~ 
     #sex +   only 6 spkrs with EO scores and non-categ. matching; all Male. So can't run Male here.
                EO_language +
                  EO_culture +
                   noun_or_pronoun + 
                   prescribed_by_selector +
                   (1|speaker),
                    data = polish_HER_data, family = "binomial", control = glmerControl(optCtrl = list(maxfun = 2e4), optimizer = "bobyqa"))
summary(main_effect_HER_polish_EO, correlation=F)
```

## Main effects Russian, heritage speakers with EO instead of generation
```{r main effects for HER Russian EO}
main_effect_HER_russian_EO <- glmer (match ~ 
                   sex +
                  EO_language +
                  EO_culture +
                   noun_or_pronoun + 
                   prescribed_by_selector +
                   (1|speaker),
                    data = russiandata, family = "binomial", control = glmerControl(optCtrl = list(maxfun = 2e4), optimizer = "bobyqa"))
summary(main_effect_HER_russian_EO, correlation=F)
```

## Main effects Ukrainain, heritage speakers with EO instead of generation
```{r main effects for HER Ukrainain EO}
main_effect_HER_ukrainian_EO <- glmer (match ~ 
                   sex +
                  EO_language +
                  EO_culture +
                   noun_or_pronoun + 
                   prescribed_by_selector +
                   (1|speaker),
                    data = ukrainian_HER_data, family = "binomial", control = glmerControl(optCtrl = list(maxfun = 2e4), optimizer = "bobyqa"))
summary(main_effect_HER_ukrainian_EO, correlation=F)
```
# Interactions for each heritage language (generation, sex, *and* each linguistic factor)

## Interaction effects Polish, heritage speakers
```{r Interaction effects for HER Polish by generation}
interaction_effect_HER_polish <- glmer (match ~ generation +
                   sex +
                  noun_or_pronoun + 
                   prescribed_by_selector +
                     generation*noun_or_pronoun +
                     generation*prescribed_by_selector +
                     sex*noun_or_pronoun +
                     sex*prescribed_by_selector + 
                   (1|speaker),
                    data = polish_HER_data, family = "binomial", control = glmerControl(optCtrl = list(maxfun = 2e4), optimizer = "bobyqa"))
summary(interaction_effect_HER_polish, correlation=F)
```

## Interaction effects Russian, heritage speakers 
```{r Interaction effects for HER Russian by generation}
main_effect_HER_russian <- glmer (match ~ generation +
                   sex +
                   noun_or_pronoun + 
                   prescribed_by_selector +
                     generation*noun_or_pronoun +
                     generation*prescribed_by_selector +
                     sex*noun_or_pronoun +
                     sex*prescribed_by_selector + 
                   (1|speaker),
                    data = russian_HER_data, family = "binomial", control = glmerControl(optCtrl = list(maxfun = 2e4), optimizer = "bobyqa"))
summary(main_effect_HER_russian, correlation=F)
```

## Interaction effects Ukrainain, heritage speakers
```{r Interaction effects for HER Ukrainain by generation}
interaction_effect_HER_ukrainian <- glmer (match ~ generation +
                   sex +
                   noun_or_pronoun + 
                   prescribed_by_selector +
                     generation*noun_or_pronoun +
                     generation*prescribed_by_selector +
                     sex*noun_or_pronoun +
                     sex*prescribed_by_selector + 
                   (1|speaker),
                    data = ukrainian_HER_data, family = "binomial", control = glmerControl(optCtrl = list(maxfun = 2e4), optimizer = "bobyqa"))
summary(interaction_effect_HER_ukrainian, correlation=F)
```

## Interaction effects Polish, heritage speakers EO instead of generation
```{r Interaction effects for HER Polish by EO}
interaction_effect_HER_polish_EO <- glmer (match ~ 
#                   sex +
                   EO_language +
                  EO_culture +
                  noun_or_pronoun + 
                   prescribed_by_selector +
 #                    sex*noun_or_pronoun +
 #                    sex*prescribed_by_selector + 
                     EO_language*noun_or_pronoun +
                     EO_language*prescribed_by_selector +
                     EO_culture*noun_or_pronoun + 
                     EO_culture*prescribed_by_selector + 
                   (1|speaker),
                    data = polish_HER_data, family = "binomial", control = glmerControl(optCtrl = list(maxfun = 2e4), optimizer = "bobyqa"))
summary(interaction_effect_HER_polish_EO, correlation=F)
```

## Interaction effects Russian, heritage speakers EO instead of generation
```{r Interaction effects for HER Russian by EO, eval=FALSE, include=FALSE}
main_effect_HER_russian_EO <- glmer (match ~ 
                   sex +
                  EO_language +
                  EO_culture +
                   noun_or_pronoun + 
                   prescribed_by_selector +
                     sex*noun_or_pronoun +
                     sex*prescribed_by_selector + 
                     EO_language*noun_or_pronoun +
                     EO_language*prescribed_by_selector +
                     EO_culture*noun_or_pronoun + 
                     EO_culture*prescribed_by_selector + 
                   (1|speaker),
                    data = russian_HER_data, family = "binomial", control = glmerControl(optCtrl = list(maxfun = 2e4), optimizer = "bobyqa"))
summary(main_effect_HER_russian_EO, correlation=F)
```

## Interaction effects Ukrainain, heritage speakers EO instead of generation
```{r Interaction effects for HER Ukrainain by EO}
interaction_effect_HER_ukrainian_EO <- glmer (match ~ 
                   sex +
                  EO_language +
                  EO_culture +
                   noun_or_pronoun + 
                   prescribed_by_selector +
                     sex*noun_or_pronoun +
                     sex*prescribed_by_selector + 
                     EO_language*noun_or_pronoun +
                     EO_language*prescribed_by_selector +
                     EO_culture*noun_or_pronoun + 
                     EO_culture*prescribed_by_selector + 
                   (1|speaker),
                    data = ukrainian_HER_data, family = "binomial", control = glmerControl(optCtrl = list(maxfun = 2e4), optimizer = "bobyqa"))
summary(interaction_effect_HER_ukrainian_EO, correlation=F)
```

# Models for homeland speakers 

## Main effects for homeland speakers

## Main effects Polish, homeland speakers
```{r main effects for homeland Polish}
main_effect_HOM_polish <- glmer (match ~ age +
                   sex +
                   noun_or_pronoun + 
                   prescribed_by_selector +
                   (1|speaker),
                    data = polish_HOM_data, family = "binomial", control = glmerControl(optCtrl = list(maxfun = 2e4), optimizer = "bobyqa"))
summary(main_effect_HOM_polish, correlation=F)

addmargins(table(polish_HOM_data$sex, polish_HOM_data$match))
addmargins(table(polish_HOM_data$noun_or_pronoun, polish_HOM_data$match))
addmargins(table(polish_HOM_data$prescribed_by_selector, polish_HOM_data$match))

prop.table(table(polish_HOM_data$sex, polish_HOM_data$match), margin =1)
prop.table(table(polish_HOM_data$noun_or_pronoun, polish_HOM_data$match), margin =1)
prop.table(table(polish_HOM_data$prescribed_by_selector, polish_HOM_data$match), margin =1)
```

## Main effects Russian, homeland speakers
```{r main effects for homeland Russian}
main_effect_HOM_russian <- glmer (match ~ #age +
                   #sex +
                   noun_or_pronoun + 
                   prescribed_by_selector +
                   (1|speaker),
                    data = russian_HOM_data, family = "binomial", control = glmerControl(optCtrl = list(maxfun = 2e4), optimizer = "bobyqa"))
summary(main_effect_HOM_russian, correlation=F)

addmargins(table(russian_HOM_data$sex, russian_HOM_data$match))
addmargins(table(russian_HOM_data$noun_or_pronoun, russian_HOM_data$match))
addmargins(table(russian_HOM_data$prescribed_by_selector, russian_HOM_data$match))

prop.table(table(russian_HOM_data$sex, russian_HOM_data$match), margin =1)
prop.table(table(russian_HOM_data$noun_or_pronoun, russian_HOM_data$match), margin =1)
prop.table(table(russian_HOM_data$prescribed_by_selector, russian_HOM_data$match), margin =1)
```

## Main effects Ukrainain, homeland speakers
```{r main effects for homeland Ukrainain}
main_effect_HOM_ukrainian <- glmer (match ~ age +
                   sex +
                   noun_or_pronoun + 
                   prescribed_by_selector +
                   (1|speaker),
                    data = ukrainian_HOM_data, family = "binomial", control = glmerControl(optCtrl = list(maxfun = 2e4), optimizer = "bobyqa"))
summary(main_effect_HOM_ukrainian, correlation=F)

addmargins(table(ukrainian_HOM_data$sex, ukrainian_HOM_data$match))
addmargins(table(ukrainian_HOM_data$noun_or_pronoun, ukrainian_HOM_data$match))
addmargins(table(ukrainian_HOM_data$prescribed_by_selector, ukrainian_HOM_data$match))

prop.table(table(ukrainian_HOM_data$sex, ukrainian_HOM_data$match), margin =1)
prop.table(table(ukrainian_HOM_data$noun_or_pronoun, ukrainian_HOM_data$match), margin =1)
prop.table(table(ukrainian_HOM_data$prescribed_by_selector, ukrainian_HOM_data$match), margin =1)
```

## Interaction effects for homeland speakers

## Interaction effects Polish, homeland speakers
```{r Interaction effects for homeland Polish}
main_effect_HOM_polish <- glmer (match ~ age +
                   sex +
                   noun_or_pronoun + 
                   prescribed_by_selector +
                     age*noun_or_pronoun + 
                     age*prescribed_by_selector +
                     sex*noun_or_pronoun + 
                     sex*prescribed_by_selector +
                     (1|speaker),
                    data = polish_HOM_data, family = "binomial", control = glmerControl(optCtrl = list(maxfun = 2e4), optimizer = "bobyqa"))
summary(main_effect_HOM_polish, correlation=F)

addmargins(table(polish_HOM_data$sex, polish_HOM_data$match))
addmargins(table(polish_HOM_data$noun_or_pronoun, polish_HOM_data$match))
addmargins(table(polish_HOM_data$prescribed_by_selector, polish_HOM_data$match))

prop.table(table(polish_HOM_data$sex, polish_HOM_data$match), margin =1)
prop.table(table(polish_HOM_data$noun_or_pronoun, polish_HOM_data$match), margin =1)
prop.table(table(polish_HOM_data$prescribed_by_selector, polish_HOM_data$match), margin =1)
```

## Interaction effects Russian, homeland speakers
```{r Interaction effects for homeland Russian, eval=FALSE, include=FALSE}
main_effect_HOM_russian <- glmer (match ~ #age +
                 #  sex +
                   noun_or_pronoun + 
                   prescribed_by_selector +
                    # age*noun_or_pronoun + 
                    # age*prescribed_by_selector +
                    #sex*noun_or_pronoun + 
                    # sex*prescribed_by_selector +
                     (1|speaker),
                    data = russian_HOM_data, family = "binomial", control = glmerControl(optCtrl = list(maxfun = 2e4), optimizer = "bobyqa"))
summary(main_effect_HOM_russian, correlation=F)

addmargins(table(russian_HOM_data$sex, russian_HOM_data$match))
addmargins(table(russian_HOM_data$noun_or_pronoun, russian_HOM_data$match))
addmargins(table(russian_HOM_data$prescribed_by_selector, russian_HOM_data$match))

prop.table(table(russian_HOM_data$sex, russian_HOM_data$match), margin =1)
prop.table(table(russian_HOM_data$noun_or_pronoun, russian_HOM_data$match), margin =1)
prop.table(table(russian_HOM_data$prescribed_by_selector, russian_HOM_data$match), margin =1)
```

## Interaction effects Ukrainain, homeland speakers
```{r Interaction effects for homeland Ukrainain}
main_effect_HOM_ukrainian <- glmer (match ~ age +
                   sex +
                   noun_or_pronoun + 
                   prescribed_by_selector +
                     age*noun_or_pronoun + 
                     age*prescribed_by_selector +
                     sex*noun_or_pronoun + 
                     sex*prescribed_by_selector +
                   (1|speaker),
                    data = ukrainian_HOM_data, family = "binomial", control = glmerControl(optCtrl = list(maxfun = 2e4), optimizer = "bobyqa"))
summary(main_effect_HOM_ukrainian, correlation=F)

addmargins(table(ukrainian_HOM_data$sex, ukrainian_HOM_data$match))
addmargins(table(ukrainian_HOM_data$noun_or_pronoun, ukrainian_HOM_data$match))
addmargins(table(ukrainian_HOM_data$prescribed_by_selector, ukrainian_HOM_data$match))

prop.table(table(ukrainian_HOM_data$sex, ukrainian_HOM_data$match), margin =1)
prop.table(table(ukrainian_HOM_data$noun_or_pronoun, ukrainian_HOM_data$match), margin =1)
prop.table(table(ukrainian_HOM_data$prescribed_by_selector, ukrainian_HOM_data$match), margin =1)

```
