---
title: "ISI impacts"
output: html_document
---

``` {r loadpackages}
library(tidyverse)
library(haven)
set.seed(02092023)
#save.image(file = "EC_IDA_QR.Rdata", compress = T) #saves the workspace image
load("EC_IDA_QR.Rdata")
```
```{r loaddata}
fulldata<- read_csv("https://ldbase.org/system/files/datasets/2021-08/PK_FullData.csv")  
itemlevel <- read_csv("https://ldbase.org/system/files/datasets/2021-05/PK_ItemLevelData.csv")
```
```{r combineDataFile}
#Make the data set for the paper. Only including the variables necessary for demographics and analyses.
##Fitler out projects 7 & 8, because those are second and third grade students.
WJ_PV_full <- fulldata %>% select("PK_ID", "Gender", "PK_WJPV_AAA_w1", "PK_WJPV_AAA_w2", "PK_WJPV_AAA_w3", "project", "LEP", "ESE", "FARL", "Ethnicity", "Race", "treatment") %>% filter(project != 7) %>% filter(project != 8) %>%
    mutate(treatment = case_when(treatment == 0 ~ 0, TRUE ~ 1)) %>% 
    mutate(treatment = case_when(project == 3 ~ 1, TRUE ~ treatment))
WJ_PV_full %>% summarise(count=sum(is.na(ESE)))
WJ_PV_full %>% summarise(count=sum(is.na(LEP)))
WJ_PV_full %>% summarise(count=sum(is.na(FARL)))
WJ_PV_full %>% summarise(count=sum(is.na(Race)))
WJ_PV_full %>% summarise(count=sum(is.na(Ethnicity)))
  WJ_PV_item <- itemlevel %>% select("PK_ID", starts_with("WJ_PV")) %>% select("PK_ID", ends_with(c("w1", "w2", "w3")))
WJ_PV <- WJ_PV_full %>% full_join(WJ_PV_item) %>% filter(project != 7) %>% filter(project != 8)

#descriptives of demographics
psych::describe(WJ_PV)
table(WJ_PV$Gender)
table(WJ_PV$Gender, WJ_PV$treatment)
table(WJ_PV$Ethnicity)
table(WJ_PV$Ethnicity, WJ_PV$treatment)
table(WJ_PV$Race)
table(WJ_PV$Race, WJ_PV$treatment)
table(WJ_PV$LEP)
table(WJ_PV$LEP, WJ_PV$treatment)

WJ_LW_full <- fulldata %>% select("PK_ID", "Gender", "PK_WJLW_AAA_w1", "PK_WJLW_AAA_w2", "PK_WJLW_AAA_w3", "project", "LEP", "ESE", "FARL", "Ethnicity", "Race")
WJ_LW_item <- itemlevel %>% select("PK_ID", starts_with("WJ_LW")) %>% select("PK_ID", ends_with(c("w1", "w2", "w3")))
WJ_LW <- WJ_LW_full %>% full_join(WJ_LW_item) %>% filter(project != 7) %>% filter(project != 8)
```
```{r Reliability}
#calculate alpha per project
## Check the Coefficient Alpha code.
```
```{r descriptives}
total_descr <- fulldata %>% select(c(treatment, project, WJ_LW_Total_w1, WJ_LW_Total_w3, WJ_PV_Total_w1, WJ_PV_Total_w3)) %>%
    mutate(treatment = case_when(treatment == 0 ~ 0, TRUE ~ 1)) %>% 
    mutate(treatment = case_when(project == 3 ~ 1, TRUE ~ treatment)) %>%
  filter(project != 7) %>% filter(project != 8)


psych::describe(total_descr)
psych::describeBy(total_descr ~ treatment)
psych::describeBy(total_descr ~ project)
psych::describeBy(total_descr ~ project + treatment)

t.test(WJ_LW_Total_w1 ~ treatment, data = total_descr)
#Cohen’s d = (x1 – x2) / √(s12 + s22) / 2
(20.30-21.22)/ (sqrt(9.94 + 9.99)/2)
t.test(WJ_LW_Total_w3 ~ treatment, data = total_descr)
(31.77-33.16)/ (sqrt(10.51 + 10.64)/2)
t.test(WJ_PV_Total_w1 ~ treatment, data = total_descr)
t.test(WJ_PV_Total_w3 ~ treatment, data = total_descr)


total_descr_t1 <- total_descr %>% filter(project == 1) 
t.test(WJ_LW_Total_w1 ~ treatment, data = total_descr_t1)
total_descr_t2 <- total_descr %>% filter(project == 2) 
t.test(WJ_LW_Total_w1 ~ treatment, data = total_descr_t2)
total_descr_t5 <- total_descr %>% filter(project == 5) 
t.test(WJ_LW_Total_w1 ~ treatment, data = total_descr_t5)
(25.63-23.22)/ (sqrt(8.41 + 7.38)/2)
total_descr_t6 <- total_descr %>% filter(project == 6) 
t.test(WJ_LW_Total_w1 ~ treatment, data = total_descr_t6)
total_descr_t9 <- total_descr %>% filter(project == 9) 
t.test(WJ_LW_Total_w1 ~ treatment, data = total_descr_t9)


t.test(WJ_LW_Total_w3 ~ treatment, data = total_descr_t1)
t.test(WJ_LW_Total_w3 ~ treatment, data = total_descr_t2)
t.test(WJ_LW_Total_w3 ~ treatment, data = total_descr_t5)
(37.32 - 35.95)/ (sqrt(7.72 + 6.98)/2)
t.test(WJ_LW_Total_w3 ~ treatment, data = total_descr_t6)
t.test(WJ_LW_Total_w3 ~ treatment, data = total_descr_t9)
(37.79 - 39.83)/ (sqrt(6.69 + 6.33)/2)

t.test(WJ_PV_Total_w1 ~ treatment, data = total_descr_t1)
t.test(WJ_PV_Total_w1 ~ treatment, data = total_descr_t2)
t.test(WJ_PV_Total_w1 ~ treatment, data = total_descr_t5)
(19.06 - 17.85)/ (sqrt(3.14 + 3.18)/2)
t.test(WJ_PV_Total_w1 ~ treatment, data = total_descr_t6)
t.test(WJ_PV_Total_w1 ~ treatment, data = total_descr_t9)

t.test(WJ_PV_Total_w3 ~ treatment, data = total_descr_t1)
t.test(WJ_PV_Total_w3 ~ treatment, data = total_descr_t2)
t.test(WJ_PV_Total_w3 ~ treatment, data = total_descr_t5)
(20.38-19.37)/ (sqrt(3.05 +2.95)/2)
t.test(WJ_PV_Total_w3 ~ treatment, data = total_descr_t6)
t.test(WJ_PV_Total_w3 ~ treatment, data = total_descr_t9)


```
```{r ChangeToAmnlfaPreferred}
#change from wide to long
WJ_PV_long <- WJ_PV %>% pivot_longer(cols = contains(c("WJ_PV", "AAA")), names_to = c(".value", "wave"), names_sep = "_w")

WJ_LW_long <- WJ_LW %>% pivot_longer(cols = contains(c("WJ_LW", "AAA")), names_to = c(".value", "wave"), names_sep = "_w")

#add age squared  + add contrast codes or effect codes for gender, project, race, LEP, ESE, and FARL #as per Gottfredson et al.
WJ_PV_long <- WJ_PV_long %>% 
  mutate(AGE = PK_WJPV_AAA-mean(PK_WJPV_AAA, na.rm = T)) %>%
  mutate(AGE_2 = AGE^2) %>% 
  mutate(P201 = if_else(project == 1, -1, if_else(project == 2, 1, 0))) %>%
  mutate(P301 = if_else(project == 1, -1, if_else(project == 3, 1, 0))) %>%
  mutate(P501 = if_else(project == 1, -1, if_else(project == 5, 1, 0))) %>%
  mutate(P601 = if_else(project == 1, -1, if_else(project == 6, 1, 0))) %>%
  mutate(P901 = if_else(project == 1, -1, if_else(project == 9, 1, 0))) %>%
  mutate(GEND = if_else(Gender == 0, 1, -1)) %>% #contrast coding for gender female =-1, male - 1
  mutate(LEP01 = LEP) %>%
  mutate(LEP = if_else(LEP == 0, "Proficient", "notProficient")) %>%
  mutate(ESE01 = ESE) %>%
  mutate(ESE = if_else(ESE == 0, "noServices", "Services")) %>%
  mutate(FARL01 = FARL) %>%
  mutate(FARL = if_else(FARL == 0, "noFARL", "FARL")) %>%
  mutate(ETHN = if_else(Ethnicity == 0, "nonHispanic", "Hispanic")) %>%
  mutate(BLA01 = if_else(Race == 3, 1, 0)) %>%
  mutate(WHI01 = if_else(Race == 5, 1, 0)) %>% as.data.frame() %>%
  mutate(project = as.factor(project)) %>%
  mutate_if(is.numeric, round, 3) %>% select(-"PK_WJPV_AAA") %>%
  mutate(ID2 = PK_ID) %>% #adding these final two for auxiliary and then clustering
  mutate(WAVE2 = wave)

WJ_LW_long  <- WJ_LW_long %>% 
  mutate(AGE = PK_WJLW_AAA-mean(PK_WJLW_AAA, na.rm = T)) %>%
  mutate(AGE_2 = AGE^2) %>% 
  mutate(P201 = if_else(project == 1, -1, if_else(project == 2, 1, 0))) %>%
  mutate(P301 = if_else(project == 1, -1, if_else(project == 3, 1, 0))) %>%
  mutate(P501 = if_else(project == 1, -1, if_else(project == 5, 1, 0))) %>%
  mutate(P601 = if_else(project == 1, -1, if_else(project == 6, 1, 0))) %>%
  mutate(P901 = if_else(project == 1, -1, if_else(project == 9, 1, 0))) %>%
  mutate(GEND = if_else(Gender == 0, 1, -1)) %>%
  mutate(LEP01 = LEP) %>%
  mutate(LEP = if_else(LEP == 0, "Proficient", "notProficient")) %>%
  mutate(ESE01 = ESE) %>%
  mutate(ESE = if_else(ESE == 0, "noServices", "Services")) %>%
  mutate(FARL01 = FARL) %>%
  mutate(FARL = if_else(FARL == 0, "noFARL", "FARL")) %>%
  mutate(ETHN = if_else(Ethnicity == 0, "nonHispanic", "Hispanic")) %>%
  mutate(BLA01 = if_else(Race == 3, 1, 0)) %>%
  mutate(WHI01 = if_else(Race == 5, 1, 0))  %>% as.data.frame() %>%
  mutate(project = as.factor(project)) %>%
  mutate_if(is.numeric, round, 3) %>% select(-"PK_WJLW_AAA")  %>%
  mutate(ID2 = PK_ID) %>% #adding these final two for auxiliary and then clustering
  mutate(WAVE2 = wave)

#Check the column names against the long form dataframes when you change any of the columns or mutate more variables. These names are to help with MPLUS.
colnames(WJ_PV_long) <- c("ID", "MALE", "PROJECT", "LEP","ESE","FARL", 
"ETHN01", "RACE", "WAVE", "WJPV1", "WJPV10", "WJPV11", "WJPV12", "WJPV13", "WJPV14", "WJPV15", "WJPV16", "WJPV17", "WJPV18", "WJPV19", "WJPV2", "WJPV20", "WJPV21", "WJPV22", "WJPV23", "WJPV24", "WJPV25", "WJPV26", "WJPV27", "WJPV28", "WJPV29", "WJPV3", "WJPV30","WJPV31", "WJPV32", "WJPV33", "WJPV34", "WJPV35", "WJPV36", "WJPV37", "WJPV38", "WJPV39", "WJPV4", "WJPV40", "WJPV41", "WJPV42", "WJPV43", "WJPV44", "WJPV5", "WJPV6", "WJPV7", "WJPV8", "WJPV9", "AGE", "AGE_2", "P201", "P301", "P501", "P601", "P901", "GEND", "LEP01", "ESE01", "FARL01", "ETHN", "BLA01", "WHI01", "ID2", "WAVE2")

colnames(WJ_LW_long) <- c("ID", "MALE", "PROJECT", "LEP","ESE","FARL", 
"ETHN01", "RACE", "WAVE", "WJLW1", "WJLW10", "WJLW11", "WJLW12", "WJLW13", "WJLW14", "WJLW15", "WJLW16", "WJLW17", "WJLW18", "WJLW19", "WJLW2", "WJLW20", "WJLW21", "WJLW22", "WJLW23", "WJLW24", "WJLW25", "WJLW26", "WJLW27", "WJLW28", "WJLW29", "WJLW3", "WJLW30","WJLW31", "WJLW32", "WJLW33", "WJLW34", "WJLW35", "WJLW36", "WJLW37", "WJLW38", "WJLW39", "WJLW4", "WJLW40", "WJLW41", "WJLW42", "WJLW43", "WJLW44", "WJLW45", "WJLW46", "WJLW47", "WJLW48", "WJLW49", "WJLW5", "WJLW50", "WJLW51", "WJLW52", "WJLW53", "WJLW54", "WJLW55", "WJLW56", "WJLW57", "WJLW58", "WJLW59", "WJLW6", "WJLW60", "WJLW61", "WJLW62", "WJLW63", "WJLW64", "WJLW65", "WJLW66", "WJLW67", "WJLW68", "WJLW69", "WJLW7", "WJLW76", "WJLW70", "WJLW71", "WJLW72", "WJLW73", "WJLW74", "WJLW75", "WJLW8", "WJLW9", "AGE", "AGE_2", "P201", "P301", "P501", "P601", "P901", "GEND", "LEP01", "ESE01", "FARL01", "ETHN", "BLA01", "WHI01", "ID2", "WAVE2")
```
```{r FixItems}
#remove rows with all NA on a measurement wave
#change remaining NA on items to 0
WJ_PV_naremove <- WJ_PV_long %>% 
                    filter_at(vars(starts_with("WJPV")), any_vars(!is.na(.))) %>% #this code says that at least one of the variables in the selected needs to be not NA. This will exclude the variables that are all NA on the measure
                   mutate(across(starts_with("WJPV"), ~replace_na(.x, 0)))  #this code replace remaining NA's with 0

(WJ_PV_long %>% filter(WAVE != 2) %>% summarise(count = n_distinct(ID)))

#Check how many SIDs were removed for PV
(WJ_PV_removed <- WJ_PV_naremove %>% filter(WAVE != 2) %>% summarise(count = n_distinct(ID)))
                  

WJ_LW_naremove <- WJ_LW_long %>% 
                    filter_at(vars(starts_with("WJLW")), any_vars(!is.na(.))) %>% #this code says that at least one of the variables in the selected needs to be not NA. This will exclude the variables that are all NA on the measure
                   mutate(across(starts_with("WJLW"), ~replace_na(.x, 0)))  #this code replace remaining NA's with 0

(WJ_LW_long %>% filter(WAVE != 2) %>% summarise(count = n_distinct(ID)))

(WJ_LW_removed <- WJ_LW_naremove %>% filter(WAVE != 2) %>% summarise(count = n_distinct(ID)))

```
```{r CheckCoverage}
#Using the long file. Coverage is across all waves.
#Count number of 1's across all items
WJ_PV_1_coverage <- WJ_PV_naremove %>% summarise(across(starts_with("WJPV"), sum))
# divide by number of rows in PV_remove dataframe
WJ_PV_2_coverage <- WJ_PV_1_coverage/nrow(WJ_PV_naremove) 
#pivot longer and filter items with % between 0.05 and 0.95
WJ_PV_3_coverage <- WJ_PV_2_coverage %>% pivot_longer(cols = starts_with("WJPV"), names_to = "item", values_to = "coverage") %>% filter(coverage >0.05) %>% filter(coverage <0.95)



#Same for LW
WJ_LW_1_coverage <- WJ_LW_naremove %>% summarise(across(starts_with("WJLW"), sum))
WJ_LW_2_coverage <- WJ_LW_1_coverage/nrow(WJ_LW_naremove) 
WJ_LW_3_coverage <- WJ_LW_2_coverage %>% pivot_longer(cols = starts_with("WJLW"), names_to = "item", values_to = "coverage") %>% filter(coverage >0.05) %>% filter(coverage <0.95)
```
```{r savefile}
#save as dat file (to be used in MPLUS)
write.table(WJ_PV_naremove, "WJPVlong.dat", sep = "\t", row.names = F)
WJ_PV_naremove_w1 <- WJ_PV_naremove %>% filter(WAVE == 1) %>% select (ID, ID2, WAVE2, WJPV10, WJPV12, WJPV13, WJPV14, WJPV15, WJPV16, WJPV17, WJPV18, WJPV19,
     WJPV20, WJPV21, WJPV22, WJPV23, WJPV24, WJPV25, WJPV26, WJPV27, GEND, P201, P301, P501,
     P601, P901, AGE, AGE_2)
WJ_PV_naremove_w3 <- WJ_PV_naremove %>% filter(WAVE == 3) %>% select (ID, ID2, WAVE2, WJPV10, WJPV12, WJPV13, WJPV14, WJPV15, WJPV16, WJPV17, WJPV18, WJPV19,
     WJPV20, WJPV21, WJPV22, WJPV23, WJPV24, WJPV25, WJPV26, WJPV27, GEND, P201, P301, P501,
     P601, P901, AGE, AGE_2)
write.table(WJ_PV_naremove_w1, "WJPVWave1.dat", sep = "\t", row.names = F, col.names = F)
write.table(WJ_PV_naremove_w3, "WJPVWave3.dat", sep = "\t", row.names = F, col.names = F)

write.table(WJ_LW_naremove, "WJLWlong.dat", sep = "\t")
WJ_LW_naremove_w1 <- WJ_LW_naremove %>% filter(WAVE == 1) %>% select (ID, ID2, WAVE2, WJLW8, WJLW9, WJLW10, WJLW11, WJLW12, WJLW13, WJLW14, WJLW15, WJLW16,
     WJLW17, WJLW18, WJLW19, WJLW20, WJLW21, WJLW22, WJLW23, WJLW24, WJLW25, WJLW26, WJLW27,
     WJLW28, WJLW29, WJLW30, WJLW31, WJLW32, WJLW33, WJLW34, WJLW35, WJLW36, WJLW37, WJLW38,
     WJLW39, WJLW40, WJLW41, WJLW42, WJLW43, WJLW44, WJLW45, WJLW46, WJLW47, GEND, P201, P301,
     P501, P601, P901, AGE, AGE_2)
WJ_LW_naremove_w1[is.na(WJ_LW_naremove_w1)] <- "."
WJ_LW_naremove_w3 <- WJ_LW_naremove %>% filter(WAVE == 3) %>% select (ID, ID2, WAVE2, WJLW8, WJLW9, WJLW10, WJLW11, WJLW12, WJLW13, WJLW14, WJLW15, WJLW16,
     WJLW17, WJLW18, WJLW19, WJLW20, WJLW21, WJLW22, WJLW23, WJLW24, WJLW25, WJLW26, WJLW27,
     WJLW28, WJLW29, WJLW30, WJLW31, WJLW32, WJLW33, WJLW34, WJLW35, WJLW36, WJLW37, WJLW38,
     WJLW39, WJLW40, WJLW41, WJLW42, WJLW43, WJLW44, WJLW45, WJLW46, WJLW47, GEND, P201, P301,
     P501, P601, P901, AGE, AGE_2) 
WJ_LW_naremove_w3[is.na(WJ_LW_naremove_w3)] <- "."
write.table(WJ_LW_naremove_w1, "WJLWWave1.dat", sep = "\t", row.names = F, col.names = F)
write.table(WJ_LW_naremove_w3, "WJLWWave3.dat", sep = "\t", row.names = F, col.names = F)
```
```{r AMNLFAPV}
#Continuing with the aMNLFA procedure
library(aMNLFA)
library(MplusAutomation)
```
```{r defineObjectPV}
# PV_obj <- aMNLFA.object(
#               dir = "/Users/willavandijk/Desktop/MNLFA_V1",
#               mrdata = WJ_PV_naremove,
#               indicators = c ("WJPV10" ,"WJPV12", "WJPV13", "WJPV14", "WJPV15", #only the factor indicators
#                               "WJPV16", "WJPV17", "WJPV18", "WJPV19", 
#                               "WJPV20", "WJPV21", "WJPV22", "WJPV23", 
#                               "WJPV24", "WJPV25", "WJPV26", "WJPV27"),
#               catindicators = c ( "WJPV10" ,"WJPV12", "WJPV13", 
#                                  "WJPV14", "WJPV15", "WJPV16", "WJPV17", 
#                                  "WJPV18", "WJPV19", "WJPV20", "WJPV21", 
#                                  "WJPV22", "WJPV23", "WJPV24", "WJPV25",  
#                                  "WJPV26", "WJPV27"),
#               countindicators = NULL,
#               ID = "ID",
#               auxiliary = c ("ID2", "WAVE2"), #used the two newly created ones to. the combo will identify a case
#               time = "WAVE", #this is used for the plots
#               factors =  c ("GEND", "P201", "P301", "P501", "P601", "P901" ), #this is used for the plots
#               meanimpact = c ("GEND", "P201", "P301", "P501", "P601", "P901", "AGE", "AGE_2"), #all binary
#               varimpact = c ("GEND", "P201", "P301", "P501", "P601", "P901", "AGE", "AGE_2"), #all binary
#               measinvar = c ("GEND", "P201", "P301", "P501", "P601", "P901", "AGE", "AGE_2" ), #effect coded
#               thresholds = FALSE)
# PV_obj #check if everything is coming out right
# #this gives plots that are saved to the working directory directly. The age one is not working, maybe bc it's continuous.
# aMNLFA.itemplots(PV_obj)
# ```
# ```{r GetSamplesInputFilesPV}
# #get random samples for calibration purposes. These are stored in the WD
# aMNLFA.sample(PV_obj)
# 
# #make Mplus Input files
# aMNLFA.initial(PV_obj)
# #run models manually in MPLUS.
# #1. Open dat file in textedit, change to rich text then back to plain text and save as unicode 8 
# #2 in mplus file change extension of file to txt and make sure lines are not over 90 characters
```
```{r Calibration}
#Run this next line to create an MPLUS input file with all significant moderations. (This actually works, whoohooo)
#Run the model with the calibration sample
# aMNLFA.simultaneous(PV_obj)
# 
# prune.PV <- aMNLFA.prune(PV_obj)
# 
# aMNLFA.DIFplot(prune.PV, "loading", log = T)
# aMNLFA.DIFplot(prune.PV, "intercept", log = T)
# aMNLFA.final(PV_obj, method = "bh", mchoice = "actual")
# aMNLFA.scores(PV_obj)
```
```{r defineObjectLW}
# LW_obj <- aMNLFA.object(
#               dir = "/Users/willavandijk/Desktop/MNLFA_V1",
#               mrdata = WJ_LW_naremove,
#               indicators = c ("WJLW8" ,"WJLW9", "WJLW10" , "WJLW11",
#                               "WJLW12", "WJLW13", "WJLW14", "WJLW15", #only the factor indicators
#                               "WJLW16", "WJLW17", "WJLW18", "WJLW19",
#                               "WJLW20", "WJLW21", "WJLW22", "WJLW23",
#                               "WJLW24", "WJLW25", "WJLW26", "WJLW27",
#                               "WJLW28", "WJLW29",
#                               "WJLW30", "WJLW31", "WJLW32", "WJLW33",
#                               "WJLW34", "WJLW35", "WJLW36", "WJLW37",
#                               "WJLW38", "WJLW39",
#                               "WJLW40", "WJLW41", "WJLW42", "WJLW43",
#                               "WJLW44", "WJLW45", "WJLW46", "WJLW47"),
#               catindicators = c ("WJLW8" ,"WJLW9", "WJLW10" , "WJLW11",
#                               "WJLW12", "WJLW13", "WJLW14", "WJLW15", #only the factor indicators
#                               "WJLW16", "WJLW17", "WJLW18", "WJLW19",
#                               "WJLW20", "WJLW21", "WJLW22", "WJLW23",
#                               "WJLW24", "WJLW25", "WJLW26", "WJLW27",
#                               "WJLW28", "WJLW29",
#                               "WJLW30", "WJLW31", "WJLW32", "WJLW33",
#                               "WJLW34", "WJLW35", "WJLW36", "WJLW37",
#                               "WJLW38", "WJLW39",
#                               "WJLW40", "WJLW41", "WJLW42", "WJLW43",
#                               "WJLW44", "WJLW45", "WJLW46", "WJLW47"),
#               countindicators = NULL,
#               ID = "ID",
#               auxiliary = c ("ID2", "WAVE2"), #used the two newly created ones to. the combo will identify a case
#               time = "WAVE", #this is used for the plots
#               factors =  c ("GEND", "P201", "P301", "P501", "P601", "P901" ), #this is used for the plots
#               meanimpact = c ("GEND", "P201", "P301", "P501", "P601", "P901", "AGE", "AGE_2"), #all binary
#               varimpact = c ("GEND", "P201", "P301", "P501", "P601", "P901", "AGE", "AGE_2"), #all binary
#               measinvar = c ("GEND", "P201", "P301", "P501", "P601", "P901", "AGE", "AGE_2" ), #effect coded
#                thresholds = FALSE)
# LW_obj #check if everything is coming out right
# #this gives plots that are saved to the working directory directly. The age one is not working, maybe bc it's continuous.
# aMNLFA.itemplots(LW_obj)
# ```
# ```{r GetSamplesInputFilesLW}
# #get random samples for calibration purposes. These are stored in the WD
# aMNLFA.sample(LW_obj)
# 
# #make Mplus Input files
# aMNLFA.initial(LW_obj)
# #run models manually in MPLUS.
# #1. Open dat file in textedit, change to rich text then back to plain text and save as unicode 8 
# #2 in mplus file change extension of file to txt and make sure lines are not over 90 characters
# ```
# ```{r CalibrationLW}
# #Run this next line to create an MPLUS input file with all significant moderations. (This actually works, whoohooo)
# #Run the model with the calibration sample
# aMNLFA.simultaneous(LW_obj)
# 
# prune.LW <- aMNLFA.prune(LW_obj)
# 
# aMNLFA.DIFplot(prune.LW, "loading", log = T)
# aMNLFA.DIFplot(prune.LW, "intercept", log = T)
# aMNLFA.final(LW_obj, method = "bh", mchoice = "actual")
 #aMNLFA.scores(LW_obj)
```
```{r MergeFS}
PV_factorscores <- read_table("/Users/willavandijk/Desktop/MNLFA_V1/PV_Finals/PVfactorscores.txt", col_names = F) %>% select(X26:X28) %>% rename(
  ID = X26,
  WAVE = X27,
  PV_Factor = X28
)
WJ_PV_FS <-WJ_PV_naremove %>% mutate(WAVE = as.numeric(WAVE)) %>% left_join(PV_factorscores, by = c("ID", "WAVE"))

LW_factorscores <- read_table("/Users/willavandijk/Desktop/MNLFA_V1/LWscores.dat", col_names = F) %>% select(X49:X51) %>% rename(
  ID = X49,
  WAVE = X50,
  LW_Factor = X51
)

WJ_LW_FS <-WJ_LW_naremove %>% mutate(WAVE = as.numeric(WAVE)) %>% left_join(LW_factorscores, by = c("ID", "WAVE"))

FULL_DATA <- WJ_PV_FS %>% full_join(WJ_LW_FS, by = join_by(ID, MALE, PROJECT, LEP, ESE, FARL, ETHN01, RACE, WAVE, P201, P301, P501, P601, P901, GEND, LEP01, ESE01, FARL01, ETHN, BLA01, WHI01, ID2, WAVE2)) %>% select(-(starts_with(c("WJ"))))
write.csv(FULL_DATA, "MNLFA_scores.csv")
```
```{r GettingDataForMI}

Other_vars <- fulldata %>% select("PK_ID", "treatment", "TID", "sID", "PK_WJPV_AAA_w1", "project") %>%
    mutate(treatment = case_when(treatment == 0 ~ 0, TRUE ~ 1)) %>% 
    mutate(treatment = case_when(project == 3 ~ 1, TRUE ~ treatment)) 
QR_data <- read_csv("MNLFA_scores.csv")
QR_data_wide <- QR_data %>% select("ID", "WAVE", "P201", "P301", "P501", "P601", "P901", "GEND", "LEP01", "ESE01", "FARL01", "ETHN", "BLA01", "WHI01", "PV_Factor", "LW_Factor") %>% pivot_wider(names_from = WAVE, values_from = c(PV_Factor, LW_Factor)) %>% left_join(Other_vars, join_by(ID == PK_ID))

QR_data_wide_1 <- QR_data_wide 
require(mice)
require(lattice)
require(broom.mixed)
md.pattern(QR_data_wide_1)

#change the predictors for imputation. First run "empty" imputation algorithm. Get predictor matrix
QR_data_empty <- mice(QR_data_wide_1, maxit = 0, print = F) #this will give you an empty box
QR_data_pred <- QR_data_empty$predictorMatrix
#remove demographics from variables to be imputed
QR_data_pred["ID" ,] <- 0
QR_data_pred["GEND" ,] <- 0
QR_data_pred["FARL01" ,] <- 0
QR_data_pred["LEP01" ,] <- 0
QR_data_pred["PK_WJPV_AAA_w1" ,] <- 0
QR_data_pred["sID" ,] <- 0
QR_data_pred["TID" ,] <- 0
QR_data_pred["treatment" ,] <- 0
QR_data_pred["ETHN" ,] <- 0
QR_data_pred["ESE01" ,] <- 0
QR_data_pred["BLA01" ,] <- 0
QR_data_pred["WHI01" ,] <- 0

#select demographics, and pretest scores, to impute factor scores (here unselecting the other variables)

QR_data_pred["PV_Factor_1", "treatment"] <- 0
QR_data_pred["PV_Factor_1", "LW_Factor_2"] <- 0
QR_data_pred["PV_Factor_1", "LW_Factor_3"] <- 0


QR_data_pred["LW_Factor_1", "treatment"] <- 0
QR_data_pred["LW_Factor_1", "PV_Factor_2"] <- 0
QR_data_pred["LW_Factor_1", "PV_Factor_3"] <- 0


#never use student ID or school ID for imputations
QR_data_pred[ ,"ID"] <- 0
QR_data_pred[ ,"sID"] <- 0

#set TID as class
QR_data_pred[ , "TID"] <- -2
#change all 1 to 2 (1 signifies fixed effect but does not work in algorithm yet. 2 signifies random effect)
QR_data_pred[QR_data_pred == 1] <- 2

(QR_data_meth <- QR_data_empty$method) 
QR_data_meth[[14]] <- "2l.pan"
QR_data_meth[[15]] <- "2l.pan"
QR_data_meth[[16]] <- "2l.pan"
QR_data_meth[[17]] <- "2l.pan"
QR_data_meth[[18]] <- "2l.pan"
QR_data_meth[[19]] <- "2l.pan"

QR_data_imp <- mice(QR_data_wide_1, pred = QR_data_pred, meth = QR_data_meth, m = 25, maxit = 25)

QR_data_imp$loggedEvents
#plot to check convergence
plot(QR_data_imp)
```
```{r LQMM}
library(lqmm)
library(mitml)

 QR_data_imp_1 <- mice::complete(QR_data_imp, "long") # %>% select(PV_Factor_3, treatment, P201, P301, P501, P601, P901, LW_Factor_1, PV_Factor_1, TID, .imp)
# 
write_csv(QR_data_imp_1, "imputed.csv") #Saving this so we don't have to rerun all the things all the time :D.

QR_data_imp_1 <- read_csv("imputed.csv") 

#running each of the imputed data sets separately, because lqmm cannot handle the full data set. Ugliest code ever, but it functions, ok!
#first filter out each data set to a separate file
QR_data_imp_I1 <- QR_data_imp_1 %>% filter(.imp == 1) 
QR_data_imp_I2 <- QR_data_imp_1 %>% filter(.imp == 2) 
QR_data_imp_I3 <- QR_data_imp_1 %>% filter(.imp == 3) 
QR_data_imp_I4 <- QR_data_imp_1 %>% filter(.imp == 4) 
QR_data_imp_I5 <- QR_data_imp_1 %>% filter(.imp == 5)
QR_data_imp_I6 <- QR_data_imp_1 %>% filter(.imp == 6) 
QR_data_imp_I7 <- QR_data_imp_1 %>% filter(.imp == 7) 
QR_data_imp_I8 <- QR_data_imp_1 %>% filter(.imp == 8) 
QR_data_imp_I9 <- QR_data_imp_1 %>% filter(.imp == 9) 
QR_data_imp_I10 <- QR_data_imp_1 %>% filter(.imp == 10) 
QR_data_imp_I11 <- QR_data_imp_1 %>% filter(.imp == 11) 
QR_data_imp_I12 <- QR_data_imp_1 %>% filter(.imp == 12) 
QR_data_imp_I13 <- QR_data_imp_1 %>% filter(.imp == 13) 
QR_data_imp_I14 <- QR_data_imp_1 %>% filter(.imp == 14) 
QR_data_imp_I15 <- QR_data_imp_1 %>% filter(.imp == 15) 
QR_data_imp_I16 <- QR_data_imp_1 %>% filter(.imp == 16) 
QR_data_imp_I17 <- QR_data_imp_1 %>% filter(.imp == 17) 
QR_data_imp_I18 <- QR_data_imp_1 %>% filter(.imp == 18) 
QR_data_imp_I19 <- QR_data_imp_1 %>% filter(.imp == 19) 
QR_data_imp_I20 <- QR_data_imp_1 %>% filter(.imp == 20) 
QR_data_imp_I21 <- QR_data_imp_1 %>% filter(.imp == 21) 
QR_data_imp_I22 <- QR_data_imp_1 %>% filter(.imp == 22) 
QR_data_imp_I23 <- QR_data_imp_1 %>% filter(.imp == 23) 
QR_data_imp_I24 <- QR_data_imp_1 %>% filter(.imp == 24) 
QR_data_imp_I25 <- QR_data_imp_1 %>% filter(.imp == 25) 

 psych::describe(QR_data_wide)

  mean_MI_PV1 <- with(QR_data_imp, mean(PV_Factor_1))
  do.call(sum, mean_MI_PV1$analyses)/25
   sd_MI_PV1 <- with(QR_data_imp, stats::sd(PV_Factor_1))
  do.call(sum, sd_MI_PV1$analyses)/25
  
 
  
   mean_MI_PV3 <- with(QR_data_imp, mean(PV_Factor_3))
  do.call(sum, mean_MI_PV3$analyses)/25
   sd_MI_PV3 <- with(QR_data_imp, stats::sd(PV_Factor_3))
  do.call(sum, sd_MI_PV3$analyses)/25
  
    mean_MI_LW1 <- with(QR_data_imp, mean(LW_Factor_1))
  do.call(sum, mean_MI_LW1$analyses)/25
       sd_MI_LW1 <- with(QR_data_imp, stats::sd(LW_Factor_1))
  do.call(sum, sd_MI_LW1$analyses)/25
  
   mean_MI_LW3 <- with(QR_data_imp, mean(LW_Factor_3))
  do.call(sum, mean_MI_LW3$analyses)/25
   sd_MI_LW3 <- with(QR_data_imp, stats::sd(LW_Factor_3))
  do.call(sum, sd_MI_LW3$analyses)/25
```
```{r PV_Rq1}
#Then run all the analyses
#control functions to reach convergence
RQ1vocab_I1 <- lqmm(PV_Factor_3 ~ treatment + P201 + P301 + P501 + P601 + P901 + LW_Factor_1 + PV_Factor_1, random = ~1, group = TID, tau = c(1:9/10), data = QR_data_imp_I1, control = list(UP_max_iter = 1000, UP_tol = 1e-6))
RQ1vocab_I2 <- lqmm(PV_Factor_3 ~ treatment + P201 + P301 + P501 + P601 + P901 + LW_Factor_1 + PV_Factor_1, random = ~1, group = TID, tau = c(1:9/10), data = QR_data_imp_I2, control = list(UP_max_iter = 1000, UP_tol = 1e-6))
RQ1vocab_I3 <- lqmm(PV_Factor_3 ~ treatment + P201 + P301 + P501 + P601 + P901 + LW_Factor_1 + PV_Factor_1, random = ~1, group = TID, tau = c(1:9/10), data = QR_data_imp_I3, control = list(UP_max_iter = 1000, UP_tol = 1e-6))
RQ1vocab_I4 <- lqmm(PV_Factor_3 ~ treatment + P201 + P301 + P501 + P601 + P901 + LW_Factor_1 + PV_Factor_1, random = ~1, group = TID, tau = c(1:9/10), data = QR_data_imp_I4, control = list(LP_max_iter = 1000, lP_tol = 1e-6))
RQ1vocab_I5 <- lqmm(PV_Factor_3 ~ treatment + P201 + P301 + P501 + P601 + P901 + LW_Factor_1 + PV_Factor_1, random = ~1, group = TID, tau = c(1:9/10), data = QR_data_imp_I5, control = list(LP_max_iter = 1000, LP_tol = 1e-6))
RQ1vocab_I6 <- lqmm(PV_Factor_3 ~ treatment + P201 + P301 + P501 + P601 + P901 + LW_Factor_1 + PV_Factor_1, random = ~1, group = TID, tau = c(1:9/10), data = QR_data_imp_I6, control = list(UP_max_iter = 1000, UP_tol = 1e-7))
RQ1vocab_I7 <- lqmm(PV_Factor_3 ~ treatment + P201 + P301 + P501 + P601 + P901 + LW_Factor_1 + PV_Factor_1, random = ~1, group = TID, tau = c(1:9/10), data = QR_data_imp_I7, control = list(UP_max_iter = 1000, UP_tol = 1e-7))
RQ1vocab_I8 <- lqmm(PV_Factor_3 ~ treatment + P201 + P301 + P501 + P601 + P901 + LW_Factor_1 + PV_Factor_1, random = ~1, group = TID, tau = c(1:9/10), data = QR_data_imp_I8, control = list(UP_max_iter = 1000, UP_tol = 1e-6))
RQ1vocab_I9 <- lqmm(PV_Factor_3 ~ treatment + P201 + P301 + P501 + P601 + P901 + LW_Factor_1 + PV_Factor_1, random = ~1, group = TID, tau = c(1:9/10), data = QR_data_imp_I9, control = list(UP_max_iter = 1000, UP_tol = 1e-6))
RQ1vocab_I10 <- lqmm(PV_Factor_3 ~ treatment + P201 + P301 + P501 + P601 + P901 + LW_Factor_1 + PV_Factor_1, random = ~1, group = TID, tau = c(1:9/10), data = QR_data_imp_I10, control = list(UP_max_iter = 1000, UP_tol = 1e-6))
RQ1vocab_I11 <- lqmm(PV_Factor_3 ~ treatment + P201 + P301 + P501 + P601 + P901 + LW_Factor_1 + PV_Factor_1, random = ~1, group = TID, tau = c(1:9/10), data = QR_data_imp_I11, control = list(UP_max_iter = 1000, UP_tol = 1e-7))
RQ1vocab_I12 <- lqmm(PV_Factor_3 ~ treatment + P201 + P301 + P501 + P601 + P901 + LW_Factor_1 + PV_Factor_1, random = ~1, group = TID, tau = c(1:9/10), data = QR_data_imp_I12, control = list(UP_max_iter = 1000, UP_tol = 1e-6))
RQ1vocab_I13 <- lqmm(PV_Factor_3 ~ treatment + P201 + P301 + P501 + P601 + P901 + LW_Factor_1 + PV_Factor_1, random = ~1, group = TID, tau = c(1:9/10), data = QR_data_imp_I13, control = list(UP_max_iter = 1000, UP_tol = 1e-7))
RQ1vocab_I14 <- lqmm(PV_Factor_3 ~ treatment + P201 + P301 + P501 + P601 + P901 + LW_Factor_1 + PV_Factor_1, random = ~1, group = TID, tau = c(1:9/10), data = QR_data_imp_I14, control = list(UP_max_iter = 1000, UP_tol = 1e-6))
RQ1vocab_I15 <- lqmm(PV_Factor_3 ~ treatment + P201 + P301 + P501 + P601 + P901 + LW_Factor_1 + PV_Factor_1, random = ~1, group = TID, tau = c(1:9/10), data = QR_data_imp_I15, control = list(UP_max_iter = 1000, UP_tol = 1e-6))
RQ1vocab_I16 <- lqmm(PV_Factor_3 ~ treatment + P201 + P301 + P501 + P601 + P901 + LW_Factor_1 + PV_Factor_1, random = ~1, group = TID, tau = c(1:9/10), data = QR_data_imp_I16, control = list(UP_max_iter = 1000, UP_tol = 1e-6))
RQ1vocab_I17 <- lqmm(PV_Factor_3 ~ treatment + P201 + P301 + P501 + P601 + P901 + LW_Factor_1 + PV_Factor_1, random = ~1, group = TID, tau = c(1:9/10), data = QR_data_imp_I17, control = list(UP_max_iter = 1000, UP_tol = 1e-6))
RQ1vocab_I18 <- lqmm(PV_Factor_3 ~ treatment + P201 + P301 + P501 + P601 + P901 + LW_Factor_1 + PV_Factor_1, random = ~1, group = TID, tau = c(1:9/10), data = QR_data_imp_I18, control = list(UP_max_iter = 1000, UP_tol = 1e-6))
RQ1vocab_I19 <- lqmm(PV_Factor_3 ~ treatment + P201 + P301 + P501 + P601 + P901 + LW_Factor_1 + PV_Factor_1, random = ~1, group = TID, tau = c(1:9/10), data = QR_data_imp_I19, control = list(UP_max_iter = 1000, UP_tol = 1e-6))
RQ1vocab_I20 <- lqmm(PV_Factor_3 ~ treatment + P201 + P301 + P501 + P601 + P901 + LW_Factor_1 + PV_Factor_1, random = ~1, group = TID, tau = c(1:9/10), data = QR_data_imp_I20, control = list(UP_max_iter = 1000, UP_tol = 1e-6))
RQ1vocab_I21 <- lqmm(PV_Factor_3 ~ treatment + P201 + P301 + P501 + P601 + P901 + LW_Factor_1 + PV_Factor_1, random = ~1, group = TID, tau = c(1:9/10), data = QR_data_imp_I21, control = list(UP_max_iter = 1000, UP_tol = 1e-6))
RQ1vocab_I22 <- lqmm(PV_Factor_3 ~ treatment + P201 + P301 + P501 + P601 + P901 + LW_Factor_1 + PV_Factor_1, random = ~1, group = TID, tau = c(1:9/10), data = QR_data_imp_I22, control = list(UP_max_iter = 1000, UP_tol = 1e-6))
RQ1vocab_I23 <- lqmm(PV_Factor_3 ~ treatment + P201 + P301 + P501 + P601 + P901 + LW_Factor_1 + PV_Factor_1, random = ~1, group = TID, tau = c(1:9/10), data = QR_data_imp_I23, control = list(UP_max_iter = 1000, UP_tol = 1e-6))
RQ1vocab_I24 <- lqmm(PV_Factor_3 ~ treatment + P201 + P301 + P501 + P601 + P901 + LW_Factor_1 + PV_Factor_1, random = ~1, group = TID, tau = c(1:9/10), data = QR_data_imp_I24, control = list(UP_max_iter = 1000, UP_tol = 1e-6))
RQ1vocab_I25 <- lqmm(PV_Factor_3 ~ treatment + P201 + P301 + P501 + P601 + P901 + LW_Factor_1 + PV_Factor_1, random = ~1, group = TID, tau = c(1:9/10), data = QR_data_imp_I25, control = list(UP_max_iter = 1000, UP_tol = 1e-6))


#Extract the regression coefficients for each model
I1.coef <- coef.lqmm(RQ1vocab_I1) %>% t() %>% as.data.frame() %>% rownames_to_column() #transpose and make a datafrome
I2.coef <- coef.lqmm(RQ1vocab_I2) %>% t() %>% as.data.frame() %>% rownames_to_column()
I3.coef <- coef.lqmm(RQ1vocab_I3) %>% t() %>% as.data.frame() %>% rownames_to_column()
I4.coef <- coef.lqmm(RQ1vocab_I4) %>% t() %>% as.data.frame() %>% rownames_to_column()
I5.coef <- coef.lqmm(RQ1vocab_I5) %>% t() %>% as.data.frame() %>% rownames_to_column()
I6.coef <- coef.lqmm(RQ1vocab_I6) %>% t() %>% as.data.frame() %>% rownames_to_column()
I7.coef <- coef.lqmm(RQ1vocab_I7) %>% t() %>% as.data.frame() %>% rownames_to_column()
I8.coef <- coef.lqmm(RQ1vocab_I8) %>% t() %>% as.data.frame() %>% rownames_to_column()
I9.coef <- coef.lqmm(RQ1vocab_I9) %>% t() %>% as.data.frame() %>% rownames_to_column()
I10.coef <- coef.lqmm(RQ1vocab_I10) %>% t() %>% as.data.frame() %>% rownames_to_column()
I11.coef <- coef.lqmm(RQ1vocab_I11) %>% t() %>% as.data.frame() %>% rownames_to_column()
I12.coef <- coef.lqmm(RQ1vocab_I12) %>% t() %>% as.data.frame() %>% rownames_to_column()
I13.coef <- coef.lqmm(RQ1vocab_I13) %>% t() %>% as.data.frame() %>% rownames_to_column()
I14.coef <- coef.lqmm(RQ1vocab_I14) %>% t() %>% as.data.frame() %>% rownames_to_column()
I15.coef <- coef.lqmm(RQ1vocab_I15) %>% t() %>% as.data.frame() %>% rownames_to_column()
I16.coef <- coef.lqmm(RQ1vocab_I16) %>% t() %>% as.data.frame() %>% rownames_to_column()
I17.coef <- coef.lqmm(RQ1vocab_I17) %>% t() %>% as.data.frame() %>% rownames_to_column()
I18.coef <- coef.lqmm(RQ1vocab_I18) %>% t() %>% as.data.frame() %>% rownames_to_column()
I19.coef <- coef.lqmm(RQ1vocab_I19) %>% t() %>% as.data.frame() %>% rownames_to_column()
I20.coef <- coef.lqmm(RQ1vocab_I20) %>% t() %>% as.data.frame() %>% rownames_to_column()
I21.coef <- coef.lqmm(RQ1vocab_I21) %>% t() %>% as.data.frame() %>% rownames_to_column()
I22.coef <- coef.lqmm(RQ1vocab_I22) %>% t() %>% as.data.frame() %>% rownames_to_column()
I23.coef <- coef.lqmm(RQ1vocab_I23) %>% t() %>% as.data.frame() %>% rownames_to_column()
I24.coef <- coef.lqmm(RQ1vocab_I24) %>% t() %>% as.data.frame() %>% rownames_to_column()
I25.coef <- coef.lqmm(RQ1vocab_I25) %>% t() %>% as.data.frame() %>% rownames_to_column()
#combine all to one dataframe
All.coef <- I1.coef %>% full_join(I2.coef) %>% full_join(I3.coef) %>% full_join(I4.coef) %>% full_join(I5.coef) %>% full_join(I6.coef) %>% full_join(I7.coef) %>% full_join(I8.coef) %>% full_join(I9.coef) %>% full_join(I10.coef) %>% full_join(I11.coef) %>% full_join(I12.coef) %>% full_join(I13.coef) %>% full_join(I14.coef) %>% full_join(I15.coef) %>% full_join(I16.coef) %>% full_join(I17.coef) %>% full_join(I18.coef) %>% full_join(I19.coef) %>% full_join(I20.coef) %>% full_join(I21.coef) %>% full_join(I22.coef) %>% full_join(I23.coef) %>% full_join(I24.coef) %>% full_join(I25.coef)

#average the coefficients
coef_pool <- All.coef  %>% group_by(rowname) %>% summarise(across(everything(), mean)) %>% ungroup()
print(coef_pool)


#Extract covariance matrix for each model
sum.I1 <-summary(RQ1vocab_I1, covariance = T, R = 50)
Cov.I1 <-as.data.frame(sum.I1[["Cov"]]) %>% as.data.frame() %>% rownames_to_column() %>% pivot_longer(cols= !rowname, names_to = c(".value", "quantile"), names_sep = -3)

sum.I2 <-summary(RQ1vocab_I2, covariance = T, R = 50) 
Cov.I2 <-as.data.frame(sum.I2[["Cov"]]) %>% as.data.frame() %>% rownames_to_column() %>% pivot_longer(cols= !rowname, names_to = c(".value", "quantile"), names_sep = -3)
 
sum.I3 <-summary(RQ1vocab_I3, covariance = T, R = 50)
Cov.I3 <-as.data.frame(sum.I3[["Cov"]]) %>% as.data.frame() %>% rownames_to_column() %>% pivot_longer(cols= !rowname, names_to = c(".value", "quantile"), names_sep = -3)

sum.I4 <-summary(RQ1vocab_I4, covariance = T, R = 50)
Cov.I4 <-as.data.frame(sum.I4[["Cov"]]) %>% as.data.frame() %>% rownames_to_column() %>% pivot_longer(cols= !rowname, names_to = c(".value", "quantile"), names_sep = -3)

sum.I5 <-summary(RQ1vocab_I5, covariance = T, R = 50)
Cov.I5 <-as.data.frame(sum.I5[["Cov"]]) %>% as.data.frame() %>% rownames_to_column() %>% pivot_longer(cols= !rowname, names_to = c(".value", "quantile"), names_sep = -3)

sum.I6 <-summary(RQ1vocab_I6, covariance = T, R = 50)
Cov.I6 <-as.data.frame(sum.I6[["Cov"]]) %>% as.data.frame() %>% rownames_to_column() %>% pivot_longer(cols= !rowname, names_to = c(".value", "quantile"), names_sep = -3)

sum.I7 <-summary(RQ1vocab_I7, covariance = T, R = 50)
Cov.I7 <-as.data.frame(sum.I7[["Cov"]]) %>% as.data.frame() %>% rownames_to_column() %>% pivot_longer(cols= !rowname, names_to = c(".value", "quantile"), names_sep = -3)

sum.I8 <-summary(RQ1vocab_I8, covariance = T, R = 50)
Cov.I8 <-as.data.frame(sum.I8[["Cov"]]) %>% as.data.frame() %>% rownames_to_column() %>% pivot_longer(cols= !rowname, names_to = c(".value", "quantile"), names_sep = -3)

sum.I9 <-summary(RQ1vocab_I9, covariance = T, R = 50)
Cov.I9 <-as.data.frame(sum.I9[["Cov"]]) %>% as.data.frame() %>% rownames_to_column() %>% pivot_longer(cols= !rowname, names_to = c(".value", "quantile"), names_sep = -3)

sum.I10 <-summary(RQ1vocab_I10, covariance = T, R = 50)
Cov.I10 <-as.data.frame(sum.I10[["Cov"]]) %>% as.data.frame() %>% rownames_to_column() %>% pivot_longer(cols= !rowname, names_to = c(".value", "quantile"), names_sep = -3)

sum.I11 <-summary(RQ1vocab_I11, covariance = T, R = 50)
Cov.I11 <-as.data.frame(sum.I11[["Cov"]]) %>% as.data.frame() %>% rownames_to_column() %>% pivot_longer(cols= !rowname, names_to = c(".value", "quantile"), names_sep = -3)

sum.I12 <-summary(RQ1vocab_I12, covariance = T, R = 50)
Cov.I12 <-as.data.frame(sum.I12[["Cov"]]) %>% as.data.frame() %>% rownames_to_column() %>% pivot_longer(cols= !rowname, names_to = c(".value", "quantile"), names_sep = -3)

sum.I13 <-summary(RQ1vocab_I13, covariance = T, R = 50)
Cov.I13 <-as.data.frame(sum.I13[["Cov"]]) %>% as.data.frame() %>% rownames_to_column() %>% pivot_longer(cols= !rowname, names_to = c(".value", "quantile"), names_sep = -3)

sum.I14 <-summary(RQ1vocab_I14, covariance = T, R = 50)
Cov.I14 <-as.data.frame(sum.I14[["Cov"]]) %>% as.data.frame() %>% rownames_to_column() %>% pivot_longer(cols= !rowname, names_to = c(".value", "quantile"), names_sep = -3)

sum.I15 <-summary(RQ1vocab_I15, covariance = T, R = 50)
Cov.I15 <-as.data.frame(sum.I15[["Cov"]]) %>% as.data.frame() %>% rownames_to_column() %>% pivot_longer(cols= !rowname, names_to = c(".value", "quantile"), names_sep = -3)

sum.I16 <-summary(RQ1vocab_I16, covariance = T, R = 50)
Cov.I16 <-as.data.frame(sum.I16[["Cov"]]) %>% as.data.frame() %>% rownames_to_column() %>% pivot_longer(cols= !rowname, names_to = c(".value", "quantile"), names_sep = -3)

sum.I17 <-summary(RQ1vocab_I17, covariance = T, R = 50)
Cov.I17 <-as.data.frame(sum.I17[["Cov"]]) %>% as.data.frame() %>% rownames_to_column() %>% pivot_longer(cols= !rowname, names_to = c(".value", "quantile"), names_sep = -3)

sum.I18 <-summary(RQ1vocab_I18, covariance = T, R = 50)
Cov.I18 <-as.data.frame(sum.I18[["Cov"]]) %>% as.data.frame() %>% rownames_to_column() %>% pivot_longer(cols= !rowname, names_to = c(".value", "quantile"), names_sep = -3)

sum.I19 <-summary(RQ1vocab_I19, covariance = T, R = 50)
Cov.I19 <-as.data.frame(sum.I19[["Cov"]]) %>% as.data.frame() %>% rownames_to_column() %>% pivot_longer(cols= !rowname, names_to = c(".value", "quantile"), names_sep = -3)

sum.I20 <-summary(RQ1vocab_I20, covariance = T, R = 50)
Cov.I20 <-as.data.frame(sum.I20[["Cov"]]) %>% as.data.frame() %>% rownames_to_column() %>% pivot_longer(cols= !rowname, names_to = c(".value", "quantile"), names_sep = -3)

sum.I21 <-summary(RQ1vocab_I21, covariance = T, R = 50)
Cov.I21 <-as.data.frame(sum.I21[["Cov"]]) %>% as.data.frame() %>% rownames_to_column() %>% pivot_longer(cols= !rowname, names_to = c(".value", "quantile"), names_sep = -3)

sum.I22 <-summary(RQ1vocab_I22, covariance = T, R = 50)
Cov.I22 <-as.data.frame(sum.I22[["Cov"]]) %>% as.data.frame() %>% rownames_to_column() %>% pivot_longer(cols= !rowname, names_to = c(".value", "quantile"), names_sep = -3)

sum.I23 <-summary(RQ1vocab_I23, covariance = T, R = 50)
Cov.I23 <-as.data.frame(sum.I23[["Cov"]]) %>% as.data.frame() %>% rownames_to_column() %>% pivot_longer(cols= !rowname, names_to = c(".value", "quantile"), names_sep = -3)

sum.I24 <-summary(RQ1vocab_I24, covariance = T, R = 50)
Cov.I24 <-as.data.frame(sum.I24[["Cov"]]) %>% as.data.frame() %>% rownames_to_column() %>% pivot_longer(cols= !rowname, names_to = c(".value", "quantile"), names_sep = -3)

sum.I25 <-summary(RQ1vocab_I25, covariance = T, R = 50)
Cov.I25 <-as.data.frame(sum.I25[["Cov"]]) %>% as.data.frame() %>% rownames_to_column() %>% pivot_longer(cols= !rowname, names_to = c(".value", "quantile"), names_sep = -3)

#combine all COV matrices into one giant thing
all.cov <- Cov.I1 %>% full_join(Cov.I2) %>% full_join(Cov.I3) %>% full_join(Cov.I4) %>% full_join(Cov.I5) %>% full_join(Cov.I6) %>% full_join(Cov.I7) %>% full_join(Cov.I8) %>% full_join(Cov.I9) %>% full_join(Cov.I10) %>% full_join(Cov.I11) %>% full_join(Cov.I12) %>% full_join(Cov.I13) %>% full_join(Cov.I14) %>% full_join(Cov.I15) %>% full_join(Cov.I16) %>% full_join(Cov.I17) %>% full_join(Cov.I18) %>% full_join(Cov.I19) %>% full_join(Cov.I20) %>% full_join(Cov.I21) %>% full_join(Cov.I22) %>% full_join(Cov.I23) %>% full_join(Cov.I24) %>% full_join(Cov.I25) %>% filter(rowname != "reStruct1") %>% filter(rowname != "scale") %>% select(-c("reStruct1.", "scale."))



#get avergaes per variable and per quantile # need to resort the rowname variable to preserve original order
cov_pool <- all.cov  %>% group_by(rowname, quantile) %>% summarise(across(everything(), mean)) %>% ungroup() %>% arrange(factor(rowname, levels = (c("(Intercept)", "treatment", "P201", "P301", "P501", "P601", "P901", "LW_Factor_1", "PV_Factor_1")))) %>% ungroup()
print(cov_pool)

#quantify uncertainty due to missing data
  #VCOVmissing.data=∑mi=1(bi−bpool)T(bi−bpool)m−1
#calculate the difference for each coefficient from the pooled coefficient
coef.dev.I1 <- I1.coef[2:10] - coef_pool[2:10] 
coef.dev.I1 <- coef.dev.I1 %>% mutate(quantile = seq(0.1, 0.9, 0.1))

coef.dev.I2 <- I2.coef[2:10] - coef_pool[2:10] 
coef.dev.I2 <- coef.dev.I2 %>% mutate(quantile = seq(0.1, 0.9, 0.1))

coef.dev.I3 <- I3.coef[2:10] - coef_pool[2:10] 
coef.dev.I3 <- coef.dev.I3 %>% mutate(quantile = seq(0.1, 0.9, 0.1))

coef.dev.I4 <- I4.coef[2:10] - coef_pool[2:10] 
coef.dev.I4 <- coef.dev.I4 %>% mutate(quantile = seq(0.1, 0.9, 0.1))

coef.dev.I5 <- I5.coef[2:10] - coef_pool[2:10] 
coef.dev.I5 <- coef.dev.I5 %>% mutate(quantile = seq(0.1, 0.9, 0.1))

coef.dev.I6 <- I6.coef[2:10] - coef_pool[2:10] 
coef.dev.I6 <- coef.dev.I6 %>% mutate(quantile = seq(0.1, 0.9, 0.1))

coef.dev.I7 <- I7.coef[2:10] - coef_pool[2:10] 
coef.dev.I7 <- coef.dev.I7 %>% mutate(quantile = seq(0.1, 0.9, 0.1))

coef.dev.I8 <- I8.coef[2:10] - coef_pool[2:10] 
coef.dev.I8 <- coef.dev.I8 %>% mutate(quantile = seq(0.1, 0.9, 0.1))

coef.dev.I9 <- I9.coef[2:10] - coef_pool[2:10] 
coef.dev.I9 <- coef.dev.I9 %>% mutate(quantile = seq(0.1, 0.9, 0.1))

coef.dev.I10 <- I10.coef[2:10] - coef_pool[2:10] 
coef.dev.I10 <- coef.dev.I10 %>% mutate(quantile = seq(0.1, 0.9, 0.1))

coef.dev.I11 <- I11.coef[2:10] - coef_pool[2:10] 
coef.dev.I11 <- coef.dev.I11 %>% mutate(quantile = seq(0.1, 0.9, 0.1))

coef.dev.I12 <- I12.coef[2:10] - coef_pool[2:10] 
coef.dev.I12 <- coef.dev.I12 %>% mutate(quantile = seq(0.1, 0.9, 0.1))

coef.dev.I13 <- I13.coef[2:10] - coef_pool[2:10] 
coef.dev.I13 <- coef.dev.I13 %>% mutate(quantile = seq(0.1, 0.9, 0.1))

coef.dev.I14 <- I14.coef[2:10] - coef_pool[2:10] 
coef.dev.I14 <- coef.dev.I14 %>% mutate(quantile = seq(0.1, 0.9, 0.1))

coef.dev.I15 <- I15.coef[2:10] - coef_pool[2:10] 
coef.dev.I15 <- coef.dev.I15 %>% mutate(quantile = seq(0.1 ,0.9, 0.1)) 

coef.dev.I16 <- I16.coef[2:10] - coef_pool[2:10] 
coef.dev.I16 <- coef.dev.I16 %>% mutate(quantile = seq(0.1 ,0.9, 0.1))

coef.dev.I17 <- I17.coef[2:10] - coef_pool[2:10] 
coef.dev.I17 <- coef.dev.I17 %>% mutate(quantile = seq(0.1 ,0.9, 0.1))

coef.dev.I18 <- I18.coef[2:10] - coef_pool[2:10] 
coef.dev.I18 <- coef.dev.I18 %>% mutate(quantile = seq(0.1 ,0.9, 0.1))

coef.dev.I19 <- I19.coef[2:10] - coef_pool[2:10] 
coef.dev.I19 <- coef.dev.I19 %>% mutate(quantile = seq(0.1 ,0.9, 0.1))

coef.dev.I20 <- I20.coef[2:10] - coef_pool[2:10] 
coef.dev.I20 <- coef.dev.I20 %>% mutate(quantile = seq(0.1 ,0.9, 0.1))

coef.dev.I21 <- I21.coef[2:10] - coef_pool[2:10] 
coef.dev.I21 <- coef.dev.I21 %>% mutate(quantile = seq(0.1 ,0.9, 0.1))

coef.dev.I22 <- I22.coef[2:10] - coef_pool[2:10] 
coef.dev.I22 <- coef.dev.I22 %>% mutate(quantile = seq(0.1 ,0.9, 0.1))

coef.dev.I23 <- I23.coef[2:10] - coef_pool[2:10] 
coef.dev.I23 <- coef.dev.I23 %>% mutate(quantile = seq(0.1 ,0.9, 0.1))

coef.dev.I24 <- I24.coef[2:10] - coef_pool[2:10] 
coef.dev.I24 <- coef.dev.I24 %>% mutate(quantile = seq(0.1 ,0.9, 0.1))

coef.dev.I25 <- I25.coef[2:10] - coef_pool[2:10] 
coef.dev.I25 <- coef.dev.I25 %>% mutate(quantile = seq(0.1 ,0.9, 0.1))

#combine all coef dev matrices into one giant thing
all.coef.dev <- coef.dev.I1 %>% full_join(coef.dev.I2)  %>% full_join(coef.dev.I3) %>% full_join(coef.dev.I4) %>% full_join(coef.dev.I5) %>% full_join(coef.dev.I6) %>% full_join(coef.dev.I7) %>% full_join(coef.dev.I8) %>% full_join(coef.dev.I9) %>% full_join(coef.dev.I10) %>% full_join(coef.dev.I11) %>% full_join(coef.dev.I12) %>% full_join(coef.dev.I13) %>% full_join(coef.dev.I14) %>% full_join(coef.dev.I15) %>% full_join(coef.dev.I16)  %>% full_join(coef.dev.I17) %>% full_join(coef.dev.I18) %>% full_join(coef.dev.I19) %>% full_join(coef.dev.I20) %>% full_join(coef.dev.I21) %>% full_join(coef.dev.I22) %>% full_join(coef.dev.I23) %>% full_join(coef.dev.I24) %>% full_join(coef.dev.I25) 
 
#separate VCOV and ceof.dev by quantile because I am a bad coder
all.coef.dev.q1 <- all.coef.dev %>% filter(quantile == 0.1) %>% select (-quantile) %>% as.matrix()
all.coef.dev.q2 <- all.coef.dev %>% filter(quantile == 0.2) %>% select (-quantile) %>% as.matrix()
all.coef.dev.q3 <- all.coef.dev %>% filter(quantile == "0.3")  %>% select (-quantile) %>% as.matrix() #not sure why it won't give anything with just the numbers
all.coef.dev.q4 <- all.coef.dev %>% filter(quantile == 0.4) %>% select (-quantile) %>% as.matrix()
all.coef.dev.q5 <- all.coef.dev %>% filter(quantile == 0.5) %>% select (-quantile) %>% as.matrix()
all.coef.dev.q6 <- all.coef.dev %>% filter(quantile == 0.6)%>% select (-quantile) %>% as.matrix()
all.coef.dev.q7 <- all.coef.dev %>% filter(quantile == "0.7")%>% select (-quantile) %>% as.matrix()
all.coef.dev.q8 <- all.coef.dev %>% filter(quantile == 0.8) %>% select (-quantile) %>% as.matrix()
all.coef.dev.q9 <- all.coef.dev %>% filter(quantile == 0.9)%>% select (-quantile) %>% as.matrix()

all.cov.q1 <- cov_pool %>% filter(quantile == 0.1) %>% select(-c("rowname", "quantile")) %>% as.matrix()
all.cov.q2 <- cov_pool %>% filter(quantile == 0.2) %>% select(-c("rowname", "quantile")) %>% as.matrix()
all.cov.q3 <- cov_pool %>% filter(quantile == 0.3) %>% select(-c("rowname", "quantile")) %>% as.matrix()
all.cov.q4 <- cov_pool %>% filter(quantile == 0.4) %>% select(-c("rowname", "quantile")) %>% as.matrix()
all.cov.q5 <- cov_pool %>% filter(quantile == 0.5) %>% select(-c("rowname", "quantile")) %>% as.matrix()
all.cov.q6 <- cov_pool %>% filter(quantile == 0.6) %>% select(-c("rowname", "quantile")) %>% as.matrix()
all.cov.q7 <- cov_pool %>% filter(quantile == 0.7) %>% select(-c("rowname", "quantile")) %>% as.matrix()
all.cov.q8 <- cov_pool %>% filter(quantile == 0.8) %>% select(-c("rowname", "quantile")) %>% as.matrix()
all.cov.q9 <- cov_pool %>% filter(quantile == 0.9) %>% select(-c("rowname", "quantile")) %>% as.matrix()

#multiply matrix by its inverse, do this separately per quantile
coef.dev2.q1 <- t(all.coef.dev.q1) %*% all.coef.dev.q1
coef.dev2.q2 <- t(all.coef.dev.q2) %*% all.coef.dev.q2
coef.dev2.q3 <- t(all.coef.dev.q3) %*% all.coef.dev.q3
coef.dev2.q4 <- t(all.coef.dev.q4) %*% all.coef.dev.q4
coef.dev2.q5 <- t(all.coef.dev.q5) %*% all.coef.dev.q5
coef.dev2.q6 <- t(all.coef.dev.q6) %*% all.coef.dev.q6
coef.dev2.q7 <- t(all.coef.dev.q7) %*% all.coef.dev.q7
coef.dev2.q8 <- t(all.coef.dev.q8) %*% all.coef.dev.q8
coef.dev2.q9 <- t(all.coef.dev.q9) %*% all.coef.dev.q9

#divide the sum of the above by #of imputations - 1 (14). to get missing data number of imputations error
mdni.error.q1 <- coef.dev2.q1 / 24
mdni.error.q2 <- coef.dev2.q2 / 24
mdni.error.q3 <- coef.dev2.q3 / 24
mdni.error.q4 <- coef.dev2.q4 / 24
mdni.error.q5 <- coef.dev2.q5 / 24
mdni.error.q6 <- coef.dev2.q6 / 24
mdni.error.q7 <- coef.dev2.q7 / 24
mdni.error.q8 <- coef.dev2.q8 / 24
mdni.error.q9 <- coef.dev2.q9 / 24

#divide the mdni  by total number of imputations to get error due to finite sampling
mdfs.error.q1 <- mdni.error.q1 / 25
mdfs.error.q2 <- mdni.error.q2 / 25
mdfs.error.q3 <- mdni.error.q3 / 25
mdfs.error.q4 <- mdni.error.q4 / 25
mdfs.error.q5 <- mdni.error.q5 / 25
mdfs.error.q6 <- mdni.error.q6 / 25
mdfs.error.q7 <- mdni.error.q7 / 25
mdfs.error.q8 <- mdni.error.q8 / 25
mdfs.error.q9 <- mdni.error.q9 / 25

#add the three error components to get total error
total.error.q1 <- all.cov.q1 + mdni.error.q1 + mdfs.error.q1
total.error.q2 <- all.cov.q2 + mdni.error.q2 + mdfs.error.q2
total.error.q3 <- all.cov.q3 + mdni.error.q3 + mdfs.error.q3
total.error.q4 <- all.cov.q4 + mdni.error.q4 + mdfs.error.q4
total.error.q5 <- all.cov.q5 + mdni.error.q5 + mdfs.error.q5
total.error.q6 <- all.cov.q6 + mdni.error.q6 + mdfs.error.q6
total.error.q7 <- all.cov.q7 + mdni.error.q7 + mdfs.error.q7
total.error.q8 <- all.cov.q8 + mdni.error.q8 + mdfs.error.q8
total.error.q9 <- all.cov.q9 + mdni.error.q9 + mdfs.error.q9

fmi.q1 <- (diag(mdni.error.q1) + diag(mdfs.error.q1)) / diag(total.error.q1)
fmi.q2 <- (diag(mdni.error.q2) + diag(mdfs.error.q2)) / diag(total.error.q2)
fmi.q3 <- (diag(mdni.error.q3) + diag(mdfs.error.q3)) / diag(total.error.q3)
fmi.q4 <- (diag(mdni.error.q4) + diag(mdfs.error.q4)) / diag(total.error.q4)
fmi.q5 <- (diag(mdni.error.q5) + diag(mdfs.error.q5)) / diag(total.error.q5)
fmi.q6 <- (diag(mdni.error.q6) + diag(mdfs.error.q6)) / diag(total.error.q6)
fmi.q7 <- (diag(mdni.error.q7) + diag(mdfs.error.q7)) / diag(total.error.q7)
fmi.q8 <- (diag(mdni.error.q8) + diag(mdfs.error.q8)) / diag(total.error.q8)
fmi.q9 <- (diag(mdni.error.q9) + diag(mdfs.error.q9)) / diag(total.error.q9)


#then calculate the appropriate degrees of freedom
#dfimputation=1/1m−1FMI2+1(1−FMI)(dfcomplete+1dfcomplete+3)dfcomplete

df_com <- sum.I1$rdf %>% as.numeric() #same for all quantiles

v.q1 <- (24) / fmi.q1^2
vhat.q1 <- (1 - fmi.q1) * ((df_com + 1) / (df_com + 3)) * df_com
df_imp.q1 <- (v.q1^(-1) + vhat.q1^(-1))^(-1)

v.q2 <- (24) / fmi.q2^2
vhat.q2 <- (1 - fmi.q2) * ((df_com + 1) / (df_com + 3)) * df_com
df_imp.q2 <- (v.q2^(-1) + vhat.q2^(-1))^(-1)

v.q3 <- (24) / fmi.q3^2
vhat.q3 <- (1 - fmi.q3) * ((df_com + 1) / (df_com + 3)) * df_com
df_imp.q3 <- (v.q3^(-1) + vhat.q3^(-1))^(-1)

v.q4 <- (24) / fmi.q4^2
vhat.q4 <- (1 - fmi.q4) * ((df_com + 1) / (df_com + 3)) * df_com
df_imp.q4 <- (v.q4^(-1) + vhat.q4^(-1))^(-1)

v.q5 <- (24) / fmi.q5^2
vhat.q5 <- (1 - fmi.q5) * ((df_com + 1) / (df_com + 3)) * df_com
df_imp.q5 <- (v.q5^(-1) + vhat.q5^(-1))^(-1)

v.q6 <- (24) / fmi.q6^2
vhat.q6 <- (1 - fmi.q6) * ((df_com + 1) / (df_com + 3)) * df_com
df_imp.q6 <- (v.q6^(-1) + vhat.q6^(-1))^(-1)

v.q7 <- (24) / fmi.q7^2
vhat.q7 <- (1 - fmi.q7) * ((df_com + 1) / (df_com + 3)) * df_com
df_imp.q7 <- (v.q7^(-1) + vhat.q4^(-1))^(-1)

v.q8 <- (24) / fmi.q8^2
vhat.q8 <- (1 - fmi.q8) * ((df_com + 1) / (df_com + 3)) * df_com
df_imp.q8 <- (v.q8^(-1) + vhat.q8^(-1))^(-1)

v.q9 <- (24) / fmi.q9^2
vhat.q9 <- (1 - fmi.q9) * ((df_com + 1) / (df_com + 3)) * df_com
df_imp.q9 <- (v.q9^(-1) + vhat.q9^(-1))^(-1)

#now get the SE, t, and p values
(se.pool.q1 <- sqrt(diag(total.error.q1)))
t.pool.q1 <- coef_pool[1,2:10] / se.pool.q1 
t.pool.q1.abs <- abs(t.pool.q1) %>% as.matrix()
p.pool.q1 <- 2 * pt(q = t.pool.q1.abs, df = df_imp.q1, lower.tail = F)

(se.pool.q2 <- sqrt(diag(total.error.q2)))
t.pool.q2 <- coef_pool[2,2:10] / se.pool.q2 
t.pool.q2.abs <- abs(t.pool.q2) %>% as.matrix()
p.pool.q2 <- 2 * pt(q = t.pool.q2.abs, df = df_imp.q2, lower.tail = F)

(se.pool.q3 <- sqrt(diag(total.error.q3)))
t.pool.q3 <- coef_pool[3,2:10] / se.pool.q3 
t.pool.q3.abs <- abs(t.pool.q3) %>% as.matrix()
p.pool.q3 <- 2 * pt(q = t.pool.q3.abs, df = df_imp.q3, lower.tail = F)

(se.pool.q4 <- sqrt(diag(total.error.q4)))
t.pool.q4 <- coef_pool[4,2:10] / se.pool.q4 
t.pool.q4.abs <- abs(t.pool.q4) %>% as.matrix()
p.pool.q4 <- 2 * pt(q = t.pool.q4.abs, df = df_imp.q4, lower.tail = F)

(se.pool.q5 <- sqrt(diag(total.error.q5)))
t.pool.q5 <- coef_pool[5,2:10] / se.pool.q5 
t.pool.q5.abs <- abs(t.pool.q5) %>% as.matrix()
p.pool.q5 <- 2 * pt(q = t.pool.q5.abs, df = df_imp.q5, lower.tail = F)

(se.pool.q6 <- sqrt(diag(total.error.q6)))
t.pool.q6 <- coef_pool[6,2:10] / se.pool.q6 
t.pool.q6.abs <- abs(t.pool.q6) %>% as.matrix()
p.pool.q6 <- 2 * pt(q = t.pool.q6.abs, df = df_imp.q6, lower.tail = F)

(se.pool.q7 <- sqrt(diag(total.error.q7)))
t.pool.q7 <- coef_pool[7,2:10] / se.pool.q7 
t.pool.q7.abs <- abs(t.pool.q7) %>% as.matrix()
p.pool.q7 <- 2 * pt(q = t.pool.q7.abs, df = df_imp.q7, lower.tail = F)

(se.pool.q8 <- sqrt(diag(total.error.q8)))
t.pool.q8 <- coef_pool[8,2:10] / se.pool.q8 
t.pool.q8.abs <- abs(t.pool.q8) %>% as.matrix()
p.pool.q8 <- 2 * pt(q = t.pool.q8.abs, df = df_imp.q8, lower.tail = F)

(se.pool.q9 <- sqrt(diag(total.error.q9)))
t.pool.q9 <- coef_pool[9,2:10] / se.pool.q9 
t.pool.q9.abs <- abs(t.pool.q9) %>% as.matrix()
p.pool.q9 <- 2 * pt(q = t.pool.q9.abs, df = df_imp.q9, lower.tail = F)

# As well as our confidence intervals for parameter estimation.
# 
crit.pool.q1 <- qt(p = 1 - (.05 / 2), df = df_imp.q1, lower.tail = T)
hw.pool.q1 <- se.pool.q1 * crit.pool.q1
ci_pool.q1 <- cbind("lwr" = coef_pool[1,2:10] - hw.pool.q1, "upr" = coef_pool[1,2:10] + hw.pool.q1)

crit.pool.q2 <- qt(p = 1 - (.05 / 2), df = df_imp.q2, lower.tail = T)
hw.pool.q2 <- se.pool.q2 * crit.pool.q2
ci_pool.q2 <- cbind("lwr" = coef_pool[2,2:10] - hw.pool.q2, "upr" = coef_pool[2,2:10] + hw.pool.q2)

crit.pool.q3 <- qt(p = 1 - (.05 / 2), df = df_imp.q3, lower.tail = T)
hw.pool.q3 <- se.pool.q3 * crit.pool.q3
ci_pool.q3 <- cbind("lwr" = coef_pool[3,2:10] - hw.pool.q3, "upr" = coef_pool[3,2:10] + hw.pool.q3)

crit.pool.q4 <- qt(p = 1 - (.05 / 2), df = df_imp.q4, lower.tail = T)
hw.pool.q4 <- se.pool.q4 * crit.pool.q4
ci_pool.q4 <- cbind("lwr" = coef_pool[4,2:10] - hw.pool.q4, "upr" = coef_pool[4,2:10] + hw.pool.q4)

crit.pool.q5 <- qt(p = 1 - (.05 / 2), df = df_imp.q5, lower.tail = T)
hw.pool.q5 <- se.pool.q5 * crit.pool.q5
ci_pool.q5 <- cbind("lwr" = coef_pool[5,2:10] - hw.pool.q5, "upr" = coef_pool[5,2:10] + hw.pool.q5)

crit.pool.q6 <- qt(p = 1 - (.05 / 2), df = df_imp.q6, lower.tail = T)
hw.pool.q6 <- se.pool.q6 * crit.pool.q6
ci_pool.q6 <- cbind("lwr" = coef_pool[6,2:10] - hw.pool.q6, "upr" = coef_pool[6,2:10] + hw.pool.q6)

crit.pool.q7 <- qt(p = 1 - (.05 / 2), df = df_imp.q7, lower.tail = T)
hw.pool.q7 <- se.pool.q7 * crit.pool.q7
ci_pool.q7 <- cbind("lwr" = coef_pool[7,2:10] - hw.pool.q7, "upr" = coef_pool[7,2:10] + hw.pool.q7)

crit.pool.q8 <- qt(p = 1 - (.05 / 2), df = df_imp.q8, lower.tail = T)
hw.pool.q8 <- se.pool.q8 * crit.pool.q8
ci_pool.q8 <- cbind("lwr" = coef_pool[8,2:10] - hw.pool.q8, "upr" = coef_pool[8,2:10] + hw.pool.q8)

crit.pool.q9 <- qt(p = 1 - (.05 / 2), df = df_imp.q9, lower.tail = T)
hw.pool.q9 <- se.pool.q9 * crit.pool.q9
ci_pool.q9 <- cbind("lwr" = coef_pool[9,2:10] - hw.pool.q9, "upr" = coef_pool[9,2:10] + hw.pool.q9)

# Do the B-H correction on all the adjusted p-values
##First get all p-values into one dataframe
### Coefficients for this model are: "(Intercept)", "treatment", "P201", "P301", "P501", "P601", "P901", "LW_Factor_1", "PV_Factor_1"
## Each quantile has 9 coefficients, so 9*9 = 81 coefficients with p values in total.
p_values_RQ1_PV <- p.pool.q1 %>% rbind(p.pool.q2) %>% rbind(p.pool.q3) %>% rbind(p.pool.q4) %>% rbind(p.pool.q5) %>% rbind(p.pool.q6) %>% rbind(p.pool.q7) %>% rbind(p.pool.q8) %>% rbind (p.pool.q9)  
fdrs_RQ1_PV <- p.adjust(p_values_RQ1_PV, method="BH") 
p_values_adj_RQ1_PV <- matrix(unlist(fdrs_RQ1_PV), nrow=9) %>% round(3) %>% as.data.frame()  %>% mutate(quant=c(1, 2, 3, 4, 5, 6, 7, 8, 9))
colnames(p_values_adj_RQ1_PV) <- c("(Intercept)_adj", "treatment_adj", "P201_adj", "P301_adj", "P501_adj", "P601_adj", "P901_adj", "LW_Factor_1_adj", "PV_Factor_1_adj", "Quantile")
p_values_RQ1_PV_wquant <- p_values_RQ1_PV %>% as.data.frame() %>% mutate(quant=c(1, 2, 3, 4, 5, 6, 7, 8, 9))

(p_values_RQ1_PV_all <- p_values_RQ1_PV_wquant %>% cbind(p_values_adj_RQ1_PV) %>% select(1, 11, 2, 12, 3, 13, 4, 14, 5, 15, 6, 16, 7, 17, 8, 18, 9, 19, 10, 20)  %>% round(3))

all_ci <- ci_pool.q1 %>% rbind(ci_pool.q2) %>% rbind(ci_pool.q3) %>% rbind(ci_pool.q4) %>% rbind(ci_pool.q5) %>% rbind(ci_pool.q6) %>% rbind(ci_pool.q7) %>% rbind(ci_pool.q8) %>% rbind(ci_pool.q9)

(all_new <- all_ci %>% cbind(coef_pool))

## get the statistically significant effects of interest from previous line (I'm doing this by hand)
#treatment = not statistically significant
#LW pre test skills = not statistically significant
#PV pre test skills = statistically significant for quartiles 1-6 only
```
```{r LW_Rq1}
#Do all the above things for LWID at Wave 3 as outcome
RQ1letterid_I1 <- lqmm(LW_Factor_3 ~ treatment + P201 + P301 + P501 + P601 + P901 + LW_Factor_1 + PV_Factor_1, random = ~1, group = TID, tau = c(1:9/10), data = QR_data_imp_I1, control = list(UP_max_iter = 1000, UP_tol = 1e-6))
RQ1letterid_I2 <- lqmm(LW_Factor_3 ~ treatment + P201 + P301 + P501 + P601 + P901 + LW_Factor_1 + PV_Factor_1, random = ~1, group = TID, tau = c(1:9/10), data = QR_data_imp_I2, control = list(UP_max_iter = 1000, UP_tol = 1e-6))
RQ1letterid_I3 <- lqmm(LW_Factor_3 ~ treatment + P201 + P301 + P501 + P601 + P901 + LW_Factor_1 + PV_Factor_1, random = ~1, group = TID, tau = c(1:9/10), data = QR_data_imp_I3, control = list(UP_max_iter = 1000, UP_tol = 1e-6))
RQ1letterid_I4 <- lqmm(LW_Factor_3 ~ treatment + P201 + P301 + P501 + P601 + P901 + LW_Factor_1 + PV_Factor_1, random = ~1, group = TID, tau = c(1:9/10), data = QR_data_imp_I4, control = list(UP_max_iter = 1000, UP_tol = 1e-6))
RQ1letterid_I5 <- lqmm(LW_Factor_3 ~ treatment + P201 + P301 + P501 + P601 + P901 + LW_Factor_1 + PV_Factor_1, random = ~1, group = TID, tau = c(1:9/10), data = QR_data_imp_I5, control = list(UP_max_iter = 1000, UP_tol = 1e-6))
RQ1letterid_I6 <- lqmm(LW_Factor_3 ~ treatment + P201 + P301 + P501 + P601 + P901 + LW_Factor_1 + PV_Factor_1, random = ~1, group = TID, tau = c(1:9/10), data = QR_data_imp_I6, control = list(UP_max_iter = 1000, UP_tol = 1e-7))
RQ1letterid_I7 <- lqmm(LW_Factor_3 ~ treatment + P201 + P301 + P501 + P601 + P901 + LW_Factor_1 + PV_Factor_1, random = ~1, group = TID, tau = c(1:9/10), data = QR_data_imp_I7, control = list(UP_max_iter = 1000, UP_tol = 1e-7))
RQ1letterid_I8 <- lqmm(LW_Factor_3 ~ treatment + P201 + P301 + P501 + P601 + P901 + LW_Factor_1 + PV_Factor_1, random = ~1, group = TID, tau = c(1:9/10), data = QR_data_imp_I8, control = list(UP_max_iter = 1000, UP_tol = 1e-6))
RQ1letterid_I9 <- lqmm(LW_Factor_3 ~ treatment + P201 + P301 + P501 + P601 + P901 + LW_Factor_1 + PV_Factor_1, random = ~1, group = TID, tau = c(1:9/10), data = QR_data_imp_I9, control = list(LP_max_iter = 1000, LP_tol = 1e-6))
RQ1letterid_I10 <- lqmm(LW_Factor_3 ~ treatment + P201 + P301 + P501 + P601 + P901 + LW_Factor_1 + PV_Factor_1, random = ~1, group = TID, tau = c(1:9/10), data = QR_data_imp_I10, control = list(UP_max_iter = 1000, UP_tol = 1e-6))
RQ1letterid_I11 <- lqmm(LW_Factor_3 ~ treatment + P201 + P301 + P501 + P601 + P901 + LW_Factor_1 + PV_Factor_1, random = ~1, group = TID, tau = c(1:9/10), data = QR_data_imp_I11, control = list(UP_max_iter = 1000, UP_tol = 1e-7))
RQ1letterid_I12 <- lqmm(LW_Factor_3 ~ treatment + P201 + P301 + P501 + P601 + P901 + LW_Factor_1 + PV_Factor_1, random = ~1, group = TID, tau = c(1:9/10), data = QR_data_imp_I12, control = list(UP_max_iter = 1000, UP_tol = 1e-6))
RQ1letterid_I13 <- lqmm(LW_Factor_3 ~ treatment + P201 + P301 + P501 + P601 + P901 + LW_Factor_1 + PV_Factor_1, random = ~1, group = TID, tau = c(1:9/10), data = QR_data_imp_I13, control = list(UP_max_iter = 1000, UP_tol = 1e-7))
RQ1letterid_I14 <- lqmm(LW_Factor_3 ~ treatment + P201 + P301 + P501 + P601 + P901 + LW_Factor_1 + PV_Factor_1, random = ~1, group = TID, tau = c(1:9/10), data = QR_data_imp_I14, control = list(UP_max_iter = 1000, UP_tol = 1e-6))
RQ1letterid_I15 <- lqmm(LW_Factor_3 ~ treatment + P201 + P301 + P501 + P601 + P901 + LW_Factor_1 + PV_Factor_1, random = ~1, group = TID, tau = c(1:9/10), data = QR_data_imp_I15, control = list(UP_max_iter = 1000, UP_tol = 1e-6))
RQ1letterid_I16 <- lqmm(LW_Factor_3 ~ treatment + P201 + P301 + P501 + P601 + P901 + LW_Factor_1 + PV_Factor_1, random = ~1, group = TID, tau = c(1:9/10), data = QR_data_imp_I16, control = list(UP_max_iter = 1000, UP_tol = 1e-7))
RQ1letterid_I17 <- lqmm(LW_Factor_3 ~ treatment + P201 + P301 + P501 + P601 + P901 + LW_Factor_1 + PV_Factor_1, random = ~1, group = TID, tau = c(1:9/10), data = QR_data_imp_I17, control = list(UP_max_iter = 1000, UP_tol = 1e-7))
RQ1letterid_I18 <- lqmm(LW_Factor_3 ~ treatment + P201 + P301 + P501 + P601 + P901 + LW_Factor_1 + PV_Factor_1, random = ~1, group = TID, tau = c(1:9/10), data = QR_data_imp_I18, control = list(UP_max_iter = 1000, UP_tol = 1e-6))
RQ1letterid_I19 <- lqmm(LW_Factor_3 ~ treatment + P201 + P301 + P501 + P601 + P901 + LW_Factor_1 + PV_Factor_1, random = ~1, group = TID, tau = c(1:9/10), data = QR_data_imp_I19, control = list(LP_max_iter = 1000, LP_tol = 1e-6))
RQ1letterid_I20 <- lqmm(LW_Factor_3 ~ treatment + P201 + P301 + P501 + P601 + P901 + LW_Factor_1 + PV_Factor_1, random = ~1, group = TID, tau = c(1:9/10), data = QR_data_imp_I20, control = list(UP_max_iter = 1000, UP_tol = 1e-6))
RQ1letterid_I21 <- lqmm(LW_Factor_3 ~ treatment + P201 + P301 + P501 + P601 + P901 + LW_Factor_1 + PV_Factor_1, random = ~1, group = TID, tau = c(1:9/10), data = QR_data_imp_I21, control = list(UP_max_iter = 1000, UP_tol = 1e-7))
RQ1letterid_I22 <- lqmm(LW_Factor_3 ~ treatment + P201 + P301 + P501 + P601 + P901 + LW_Factor_1 + PV_Factor_1, random = ~1, group = TID, tau = c(1:9/10), data = QR_data_imp_I22, control = list(UP_max_iter = 1000, UP_tol = 1e-6))
RQ1letterid_I23 <- lqmm(LW_Factor_3 ~ treatment + P201 + P301 + P501 + P601 + P901 + LW_Factor_1 + PV_Factor_1, random = ~1, group = TID, tau = c(1:9/10), data = QR_data_imp_I23, control = list(UP_max_iter = 1000, UP_tol = 1e-7))
RQ1letterid_I24 <- lqmm(LW_Factor_3 ~ treatment + P201 + P301 + P501 + P601 + P901 + LW_Factor_1 + PV_Factor_1, random = ~1, group = TID, tau = c(1:9/10), data = QR_data_imp_I24, control = list(UP_max_iter = 1000, UP_tol = 1e-6))
RQ1letterid_I25 <- lqmm(LW_Factor_3 ~ treatment + P201 + P301 + P501 + P601 + P901 + LW_Factor_1 + PV_Factor_1, random = ~1, group = TID, tau = c(1:9/10), data = QR_data_imp_I25, control = list(UP_max_iter = 1000, UP_tol = 1e-6))

#Extract the regression coefficients for each model
VI1.coef <- coef.lqmm(RQ1letterid_I1) %>% t() %>% as.data.frame() %>% rownames_to_column() #transpose and make a datafrome
VI2.coef <- coef.lqmm(RQ1letterid_I2) %>% t() %>% as.data.frame() %>% rownames_to_column()
VI3.coef <- coef.lqmm(RQ1letterid_I3) %>% t() %>% as.data.frame() %>% rownames_to_column()
VI4.coef <- coef.lqmm(RQ1letterid_I4) %>% t() %>% as.data.frame() %>% rownames_to_column()
VI5.coef <- coef.lqmm(RQ1letterid_I5) %>% t() %>% as.data.frame() %>% rownames_to_column()
VI6.coef <- coef.lqmm(RQ1letterid_I6) %>% t() %>% as.data.frame() %>% rownames_to_column()
VI7.coef <- coef.lqmm(RQ1letterid_I7) %>% t() %>% as.data.frame() %>% rownames_to_column()
VI8.coef <- coef.lqmm(RQ1letterid_I8) %>% t() %>% as.data.frame() %>% rownames_to_column()
VI9.coef <- coef.lqmm(RQ1letterid_I9) %>% t() %>% as.data.frame() %>% rownames_to_column()
VI10.coef <- coef.lqmm(RQ1letterid_I10) %>% t() %>% as.data.frame() %>% rownames_to_column()
VI11.coef <- coef.lqmm(RQ1letterid_I11) %>% t() %>% as.data.frame() %>% rownames_to_column()
VI12.coef <- coef.lqmm(RQ1letterid_I12) %>% t() %>% as.data.frame() %>% rownames_to_column()
VI13.coef <- coef.lqmm(RQ1letterid_I13) %>% t() %>% as.data.frame() %>% rownames_to_column()
VI14.coef <- coef.lqmm(RQ1letterid_I14) %>% t() %>% as.data.frame() %>% rownames_to_column()
VI15.coef <- coef.lqmm(RQ1letterid_I15) %>% t() %>% as.data.frame() %>% rownames_to_column()
VI16.coef <- coef.lqmm(RQ1letterid_I16) %>% t() %>% as.data.frame() %>% rownames_to_column()
VI17.coef <- coef.lqmm(RQ1letterid_I17) %>% t() %>% as.data.frame() %>% rownames_to_column()
VI18.coef <- coef.lqmm(RQ1letterid_I18) %>% t() %>% as.data.frame() %>% rownames_to_column()
VI19.coef <- coef.lqmm(RQ1letterid_I19) %>% t() %>% as.data.frame() %>% rownames_to_column()
VI20.coef <- coef.lqmm(RQ1letterid_I20) %>% t() %>% as.data.frame() %>% rownames_to_column()
VI21.coef <- coef.lqmm(RQ1letterid_I21) %>% t() %>% as.data.frame() %>% rownames_to_column()
VI22.coef <- coef.lqmm(RQ1letterid_I22) %>% t() %>% as.data.frame() %>% rownames_to_column()
VI23.coef <- coef.lqmm(RQ1letterid_I23) %>% t() %>% as.data.frame() %>% rownames_to_column()
VI24.coef <- coef.lqmm(RQ1letterid_I24) %>% t() %>% as.data.frame() %>% rownames_to_column()
VI25.coef <- coef.lqmm(RQ1letterid_I25) %>% t() %>% as.data.frame() %>% rownames_to_column()
#combine all to one dataframe
All.coef.V <- VI1.coef %>% full_join(VI2.coef) %>% full_join(VI3.coef) %>% full_join(VI4.coef) %>% full_join(VI5.coef) %>% full_join(VI6.coef) %>% full_join(VI7.coef) %>% full_join(VI8.coef) %>% full_join(VI9.coef) %>% full_join(VI10.coef) %>% full_join(VI11.coef) %>% full_join(VI12.coef) %>% full_join(VI13.coef) %>% full_join(VI14.coef) %>% full_join(VI15.coef) %>% full_join(VI16.coef) %>% full_join(VI17.coef) %>% full_join(VI18.coef) %>% full_join(VI19.coef) %>% full_join(VI20.coef) %>% full_join(VI21.coef) %>% full_join(VI22.coef) %>% full_join(VI23.coef) %>% full_join(VI24.coef) %>% full_join(VI25.coef)

#average the coefficients
coef_pool.V <- All.coef.V  %>% group_by(rowname) %>% summarise(across(everything(), mean)) %>% ungroup()
print(coef_pool.V)

#Extract covariance matrix for each model
sum.VI1 <-summary(RQ1letterid_I1, covariance = T, R = 50)
Cov.VI1 <-as.data.frame(sum.VI1[["Cov"]]) %>% as.data.frame() %>% rownames_to_column() %>% pivot_longer(cols= !rowname, names_to = c(".value", "quantile"), names_sep = -3)

sum.VI2 <-summary(RQ1letterid_I2, covariance = T, R = 50) 
Cov.VI2 <-as.data.frame(sum.VI2[["Cov"]]) %>% as.data.frame() %>% rownames_to_column() %>% pivot_longer(cols= !rowname, names_to = c(".value", "quantile"), names_sep = -3)
 
sum.VI3 <-summary(RQ1letterid_I3, covariance = T, R = 50)
Cov.VI3 <-as.data.frame(sum.VI3[["Cov"]]) %>% as.data.frame() %>% rownames_to_column() %>% pivot_longer(cols= !rowname, names_to = c(".value", "quantile"), names_sep = -3)

sum.VI4 <-summary(RQ1letterid_I4, covariance = T, R = 50)
Cov.VI4 <-as.data.frame(sum.VI4[["Cov"]]) %>% as.data.frame() %>% rownames_to_column() %>% pivot_longer(cols= !rowname, names_to = c(".value", "quantile"), names_sep = -3)

sum.VI5 <-summary(RQ1letterid_I5, covariance = T, R = 50)
Cov.VI5 <-as.data.frame(sum.VI5[["Cov"]]) %>% as.data.frame() %>% rownames_to_column() %>% pivot_longer(cols= !rowname, names_to = c(".value", "quantile"), names_sep = -3)

sum.VI6 <-summary(RQ1letterid_I6, covariance = T, R = 50)
Cov.VI6 <-as.data.frame(sum.VI6[["Cov"]]) %>% as.data.frame() %>% rownames_to_column() %>% pivot_longer(cols= !rowname, names_to = c(".value", "quantile"), names_sep = -3)

sum.VI7 <-summary(RQ1letterid_I7, covariance = T, R = 50)
Cov.VI7 <-as.data.frame(sum.VI7[["Cov"]]) %>% as.data.frame() %>% rownames_to_column() %>% pivot_longer(cols= !rowname, names_to = c(".value", "quantile"), names_sep = -3)

sum.VI8 <-summary(RQ1letterid_I8, covariance = T, R = 50)
Cov.VI8 <-as.data.frame(sum.VI8[["Cov"]]) %>% as.data.frame() %>% rownames_to_column() %>% pivot_longer(cols= !rowname, names_to = c(".value", "quantile"), names_sep = -3)

sum.VI9 <-summary(RQ1letterid_I9, covariance = T, R = 50)
Cov.VI9 <-as.data.frame(sum.VI9[["Cov"]]) %>% as.data.frame() %>% rownames_to_column() %>% pivot_longer(cols= !rowname, names_to = c(".value", "quantile"), names_sep = -3)

sum.VI10 <-summary(RQ1letterid_I10, covariance = T, R = 50)
Cov.VI10 <-as.data.frame(sum.VI10[["Cov"]]) %>% as.data.frame() %>% rownames_to_column() %>% pivot_longer(cols= !rowname, names_to = c(".value", "quantile"), names_sep = -3)

sum.VI11 <-summary(RQ1letterid_I11, covariance = T, R = 50)
Cov.VI11 <-as.data.frame(sum.VI11[["Cov"]]) %>% as.data.frame() %>% rownames_to_column() %>% pivot_longer(cols= !rowname, names_to = c(".value", "quantile"), names_sep = -3)

sum.VI12 <-summary(RQ1letterid_I12, covariance = T, R = 50)
Cov.VI12 <-as.data.frame(sum.VI12[["Cov"]]) %>% as.data.frame() %>% rownames_to_column() %>% pivot_longer(cols= !rowname, names_to = c(".value", "quantile"), names_sep = -3)

sum.VI13 <-summary(RQ1letterid_I13, covariance = T, R = 50)
Cov.VI13 <-as.data.frame(sum.VI13[["Cov"]]) %>% as.data.frame() %>% rownames_to_column() %>% pivot_longer(cols= !rowname, names_to = c(".value", "quantile"), names_sep = -3)

sum.VI14 <-summary(RQ1letterid_I14, covariance = T, R = 50)
Cov.VI14 <-as.data.frame(sum.VI14[["Cov"]]) %>% as.data.frame() %>% rownames_to_column() %>% pivot_longer(cols= !rowname, names_to = c(".value", "quantile"), names_sep = -3)

sum.VI15 <-summary(RQ1letterid_I15, covariance = T, R = 50)
Cov.VI15 <-as.data.frame(sum.VI15[["Cov"]]) %>% as.data.frame() %>% rownames_to_column() %>% pivot_longer(cols= !rowname, names_to = c(".value", "quantile"), names_sep = -3)

sum.VI16 <-summary(RQ1letterid_I16, covariance = T, R = 50)
Cov.VI16 <-as.data.frame(sum.VI16[["Cov"]]) %>% as.data.frame() %>% rownames_to_column() %>% pivot_longer(cols= !rowname, names_to = c(".value", "quantile"), names_sep = -3)

sum.VI17 <-summary(RQ1letterid_I17, covariance = T, R = 50)
Cov.VI17 <-as.data.frame(sum.VI17[["Cov"]]) %>% as.data.frame() %>% rownames_to_column() %>% pivot_longer(cols= !rowname, names_to = c(".value", "quantile"), names_sep = -3)

sum.VI18 <-summary(RQ1letterid_I18, covariance = T, R = 50)
Cov.VI18 <-as.data.frame(sum.VI18[["Cov"]]) %>% as.data.frame() %>% rownames_to_column() %>% pivot_longer(cols= !rowname, names_to = c(".value", "quantile"), names_sep = -3)

sum.VI19 <-summary(RQ1letterid_I19, covariance = T, R = 50)
Cov.VI19 <-as.data.frame(sum.VI19[["Cov"]]) %>% as.data.frame() %>% rownames_to_column() %>% pivot_longer(cols= !rowname, names_to = c(".value", "quantile"), names_sep = -3)

sum.VI20 <-summary(RQ1letterid_I20, covariance = T, R = 50)
Cov.VI20 <-as.data.frame(sum.VI20[["Cov"]]) %>% as.data.frame() %>% rownames_to_column() %>% pivot_longer(cols= !rowname, names_to = c(".value", "quantile"), names_sep = -3)

sum.VI21 <-summary(RQ1letterid_I21, covariance = T, R = 50)
Cov.VI21 <-as.data.frame(sum.VI21[["Cov"]]) %>% as.data.frame() %>% rownames_to_column() %>% pivot_longer(cols= !rowname, names_to = c(".value", "quantile"), names_sep = -3)

sum.VI22 <-summary(RQ1letterid_I22, covariance = T, R = 50)
Cov.VI22 <-as.data.frame(sum.VI22[["Cov"]]) %>% as.data.frame() %>% rownames_to_column() %>% pivot_longer(cols= !rowname, names_to = c(".value", "quantile"), names_sep = -3)

sum.VI23 <-summary(RQ1letterid_I23, covariance = T, R = 50)
Cov.VI23 <-as.data.frame(sum.VI23[["Cov"]]) %>% as.data.frame() %>% rownames_to_column() %>% pivot_longer(cols= !rowname, names_to = c(".value", "quantile"), names_sep = -3)

sum.VI24 <-summary(RQ1letterid_I24, covariance = T, R = 50)
Cov.VI24 <-as.data.frame(sum.VI24[["Cov"]]) %>% as.data.frame() %>% rownames_to_column() %>% pivot_longer(cols= !rowname, names_to = c(".value", "quantile"), names_sep = -3)

sum.VI25 <-summary(RQ1letterid_I25, covariance = T, R = 50)
Cov.VI25 <-as.data.frame(sum.VI25[["Cov"]]) %>% as.data.frame() %>% rownames_to_column() %>% pivot_longer(cols= !rowname, names_to = c(".value", "quantile"), names_sep = -3)


#combine all COV matrices into one giant thing
all.cov.V <- Cov.VI1 %>% full_join(Cov.VI2) %>% full_join(Cov.VI3) %>% full_join(Cov.VI4) %>% full_join(Cov.VI5) %>% full_join(Cov.VI6) %>% full_join(Cov.VI7) %>% full_join(Cov.VI8) %>% full_join(Cov.VI9) %>% full_join(Cov.VI10) %>% full_join(Cov.VI11) %>% full_join(Cov.VI12) %>% full_join(Cov.VI13) %>% full_join(Cov.VI14) %>% full_join(Cov.VI15) %>% full_join(Cov.VI16) %>% full_join(Cov.VI17) %>% full_join(Cov.VI18) %>% full_join(Cov.VI19) %>% full_join(Cov.VI20) %>% full_join(Cov.VI21) %>% full_join(Cov.VI22) %>% full_join(Cov.VI23) %>% full_join(Cov.VI24) %>% full_join(Cov.VI25) %>% filter(rowname != "reStruct1") %>% filter(rowname != "scale") %>% select(-c("reStruct1.", "scale."))



#get averages per variable and per quantile # need to resort the rowname variable to preserve original order
cov_pool.V <- all.cov.V  %>% group_by(rowname, quantile) %>% summarise(across(everything(), mean)) %>% ungroup() %>% arrange(factor(rowname, levels = (c("(Intercept)", "treatment", "P201", "P301", "P501", "P601", "P901", "LW_Factor_1", "PV_Factor_1"))))
print(cov_pool.V)

#quantify uncertainty due to missing data
  #VCOVmissing.data=∑mi=1(bi−bpool)T(bi−bpool)m−1
#calculate the difference for each coefficient from the pooled coefficient
coef.dev.VI1 <- VI1.coef[2:10] - coef_pool.V[2:10] 
coef.dev.VI1 <- coef.dev.VI1 %>% mutate(quantile = seq(0.1, 0.9, 0.1))

coef.dev.VI2 <- VI2.coef[2:10] - coef_pool.V[2:10] 
coef.dev.VI2 <- coef.dev.VI2 %>% mutate(quantile = seq(0.1, 0.9, 0.1))

coef.dev.VI3 <- VI3.coef[2:10] - coef_pool.V[2:10] 
coef.dev.VI3 <- coef.dev.VI3 %>% mutate(quantile = seq(0.1, 0.9, 0.1))

coef.dev.VI4 <- VI4.coef[2:10] - coef_pool.V[2:10] 
coef.dev.VI4 <- coef.dev.VI4 %>% mutate(quantile = seq(0.1, 0.9, 0.1))

coef.dev.VI5 <- VI5.coef[2:10] - coef_pool.V[2:10] 
coef.dev.VI5 <- coef.dev.VI5 %>% mutate(quantile = seq(0.1, 0.9, 0.1))

coef.dev.VI6 <- VI6.coef[2:10] - coef_pool.V[2:10] 
coef.dev.VI6 <- coef.dev.VI6 %>% mutate(quantile = seq(0.1, 0.9, 0.1))

coef.dev.VI7 <- VI7.coef[2:10] - coef_pool.V[2:10] 
coef.dev.VI7 <- coef.dev.VI7 %>% mutate(quantile = seq(0.1, 0.9, 0.1))

coef.dev.VI8 <- VI8.coef[2:10] - coef_pool.V[2:10] 
coef.dev.VI8 <- coef.dev.VI8 %>% mutate(quantile = seq(0.1, 0.9, 0.1))

coef.dev.VI9 <- VI9.coef[2:10] - coef_pool.V[2:10] 
coef.dev.VI9 <- coef.dev.VI9 %>% mutate(quantile = seq(0.1, 0.9, 0.1))

coef.dev.VI10 <- VI10.coef[2:10] - coef_pool.V[2:10] 
coef.dev.VI10 <- coef.dev.VI10 %>% mutate(quantile = seq(0.1, 0.9, 0.1))

coef.dev.VI11 <- VI11.coef[2:10] - coef_pool.V[2:10] 
coef.dev.VI11 <- coef.dev.VI11 %>% mutate(quantile = seq(0.1, 0.9, 0.1))

coef.dev.VI12 <- VI12.coef[2:10] - coef_pool.V[2:10] 
coef.dev.VI12 <- coef.dev.VI12 %>% mutate(quantile = seq(0.1, 0.9, 0.1))

coef.dev.VI13 <- VI13.coef[2:10] - coef_pool.V[2:10] 
coef.dev.VI13 <- coef.dev.VI13 %>% mutate(quantile = seq(0.1, 0.9, 0.1))

coef.dev.VI14 <- VI14.coef[2:10] - coef_pool.V[2:10] 
coef.dev.VI14 <- coef.dev.VI14 %>% mutate(quantile = seq(0.1, 0.9, 0.1))

coef.dev.VI15 <- VI15.coef[2:10] - coef_pool.V[2:10] 
coef.dev.VI15 <- coef.dev.VI15 %>% mutate(quantile = seq(0.1 ,0.9, 0.1))

coef.dev.VI16 <- VI16.coef[2:10] - coef_pool.V[2:10] 
coef.dev.VI16 <- coef.dev.VI16 %>% mutate(quantile = seq(0.1, 0.9, 0.1))

coef.dev.VI17 <- VI17.coef[2:10] - coef_pool.V[2:10] 
coef.dev.VI17 <- coef.dev.VI17 %>% mutate(quantile = seq(0.1, 0.9, 0.1))

coef.dev.VI18 <- VI18.coef[2:10] - coef_pool.V[2:10] 
coef.dev.VI18 <- coef.dev.VI18 %>% mutate(quantile = seq(0.1, 0.9, 0.1))

coef.dev.VI19 <- VI19.coef[2:10] - coef_pool.V[2:10] 
coef.dev.VI19 <- coef.dev.VI19 %>% mutate(quantile = seq(0.1, 0.9, 0.1))

coef.dev.VI20 <- VI20.coef[2:10] - coef_pool.V[2:10] 
coef.dev.VI20 <- coef.dev.VI20 %>% mutate(quantile = seq(0.1, 0.9, 0.1))

coef.dev.VI21 <- VI21.coef[2:10] - coef_pool.V[2:10] 
coef.dev.VI21 <- coef.dev.VI21 %>% mutate(quantile = seq(0.1, 0.9, 0.1))

coef.dev.VI22 <- VI22.coef[2:10] - coef_pool.V[2:10] 
coef.dev.VI22 <- coef.dev.VI22 %>% mutate(quantile = seq(0.1, 0.9, 0.1))

coef.dev.VI23 <- VI23.coef[2:10] - coef_pool.V[2:10] 
coef.dev.VI23 <- coef.dev.VI23 %>% mutate(quantile = seq(0.1, 0.9, 0.1))

coef.dev.VI24 <- VI24.coef[2:10] - coef_pool.V[2:10] 
coef.dev.VI24 <- coef.dev.VI24 %>% mutate(quantile = seq(0.1, 0.9, 0.1))

coef.dev.VI25 <- VI25.coef[2:10] - coef_pool.V[2:10] 
coef.dev.VI25 <- coef.dev.VI25 %>% mutate(quantile = seq(0.1 ,0.9, 0.1))

#combine all coef dev matrices into one giant thing
all.coef.dev.V <- coef.dev.VI1 %>% full_join(coef.dev.VI2)  %>% full_join(coef.dev.VI3) %>% full_join(coef.dev.VI4) %>% full_join(coef.dev.VI5) %>% full_join(coef.dev.VI6) %>% full_join(coef.dev.VI7) %>% full_join(coef.dev.VI8) %>% full_join(coef.dev.VI9) %>% full_join(coef.dev.VI10) %>%full_join(coef.dev.VI16) %>% full_join(coef.dev.VI17) %>% full_join(coef.dev.VI18) %>% full_join(coef.dev.VI19) %>% full_join(coef.dev.VI20) %>% full_join(coef.dev.VI21) %>% full_join(coef.dev.VI22) %>% full_join(coef.dev.VI23) %>% full_join(coef.dev.VI24) %>% full_join(coef.dev.VI25) 

#separate VCOV and ceof.dev by quantile because I am a bad coder
all.coef.dev.q1.V <- all.coef.dev.V %>% filter(quantile == 0.1) %>% select (-quantile) %>% as.matrix()
all.coef.dev.q2.V <- all.coef.dev.V %>% filter(quantile == 0.2) %>% select (-quantile) %>% as.matrix()
all.coef.dev.q3.V <- all.coef.dev.V %>% filter(quantile == "0.3")  %>% select (-quantile) %>% as.matrix() #not sure why it won't give anything with just the numbers
all.coef.dev.q4.V <- all.coef.dev.V %>% filter(quantile == 0.4) %>% select (-quantile) %>% as.matrix()
all.coef.dev.q5.V <- all.coef.dev.V %>% filter(quantile == 0.5) %>% select (-quantile) %>% as.matrix()
all.coef.dev.q6.V <- all.coef.dev.V %>% filter(quantile == 0.6)%>% select (-quantile) %>% as.matrix()
all.coef.dev.q7.V <- all.coef.dev.V %>% filter(quantile == "0.7")%>% select (-quantile) %>% as.matrix()
all.coef.dev.q8.V <- all.coef.dev.V %>% filter(quantile == 0.8) %>% select (-quantile) %>% as.matrix()
all.coef.dev.q9.V <- all.coef.dev.V %>% filter(quantile == 0.9)%>% select (-quantile) %>% as.matrix()

all.cov.q1.V <- cov_pool.V %>% filter(quantile == 0.1) %>% select(-c("rowname", "quantile")) %>% as.matrix()
all.cov.q2.V <- cov_pool.V %>% filter(quantile == 0.2) %>% select(-c("rowname", "quantile")) %>% as.matrix()
all.cov.q3.V <- cov_pool.V %>% filter(quantile == 0.3) %>% select(-c("rowname", "quantile")) %>% as.matrix()
all.cov.q4.V <- cov_pool.V %>% filter(quantile == 0.4) %>% select(-c("rowname", "quantile")) %>% as.matrix()
all.cov.q5.V <- cov_pool.V %>% filter(quantile == 0.5) %>% select(-c("rowname", "quantile")) %>% as.matrix()
all.cov.q6.V <- cov_pool.V %>% filter(quantile == 0.6) %>% select(-c("rowname", "quantile")) %>% as.matrix()
all.cov.q7.V <- cov_pool.V %>% filter(quantile == 0.7) %>% select(-c("rowname", "quantile")) %>% as.matrix()
all.cov.q8.V <- cov_pool.V %>% filter(quantile == 0.8) %>% select(-c("rowname", "quantile")) %>% as.matrix()
all.cov.q9.V <- cov_pool.V %>% filter(quantile == 0.9) %>% select(-c("rowname", "quantile")) %>% as.matrix()

#multiply matrix by its inverse, do this separately per quantile
coef.dev2.q1.V <- t(all.coef.dev.q1.V) %*% all.coef.dev.q1.V
coef.dev2.q2.V <- t(all.coef.dev.q2.V) %*% all.coef.dev.q2.V
coef.dev2.q3.V <- t(all.coef.dev.q3.V) %*% all.coef.dev.q3.V
coef.dev2.q4.V <- t(all.coef.dev.q4.V) %*% all.coef.dev.q4.V
coef.dev2.q5.V <- t(all.coef.dev.q5.V) %*% all.coef.dev.q5.V
coef.dev2.q6.V <- t(all.coef.dev.q6.V) %*% all.coef.dev.q6.V
coef.dev2.q7.V <- t(all.coef.dev.q7.V) %*% all.coef.dev.q7.V
coef.dev2.q8.V <- t(all.coef.dev.q8.V) %*% all.coef.dev.q8.V
coef.dev2.q9.V <- t(all.coef.dev.q9.V) %*% all.coef.dev.q9.V

#divide the sum of the above by #of imputations - 1 (14). to get missing data number of imputations error
mdni.error.q1.V <- coef.dev2.q1.V / 24
mdni.error.q2.V <- coef.dev2.q2.V / 24
mdni.error.q3.V <- coef.dev2.q3.V / 24
mdni.error.q4.V <- coef.dev2.q4.V / 24
mdni.error.q5.V <- coef.dev2.q5.V / 24
mdni.error.q6.V <- coef.dev2.q6.V / 24
mdni.error.q7.V <- coef.dev2.q7.V / 24
mdni.error.q8.V <- coef.dev2.q8.V / 24
mdni.error.q9.V <- coef.dev2.q9.V / 24

#divide the mdni  by total number of imputations to get error due to finite sampling
mdfs.error.q1.V <- mdni.error.q1.V / 25
mdfs.error.q2.V <- mdni.error.q2.V / 25
mdfs.error.q3.V <- mdni.error.q3.V / 25
mdfs.error.q4.V <- mdni.error.q4.V / 25
mdfs.error.q5.V <- mdni.error.q5.V / 25
mdfs.error.q6.V <- mdni.error.q6.V / 25
mdfs.error.q7.V <- mdni.error.q7.V / 25
mdfs.error.q8.V <- mdni.error.q8.V / 25
mdfs.error.q9.V <- mdni.error.q9.V / 25

#add the three error components to get total error
total.error.q1.V <- all.cov.q1.V + mdni.error.q1.V + mdfs.error.q1.V
total.error.q2.V <- all.cov.q2.V + mdni.error.q2.V + mdfs.error.q2.V
total.error.q3.V <- all.cov.q3.V + mdni.error.q3.V + mdfs.error.q3.V
total.error.q4.V <- all.cov.q4.V + mdni.error.q4.V + mdfs.error.q4.V
total.error.q5.V <- all.cov.q5.V + mdni.error.q5.V + mdfs.error.q5.V
total.error.q6.V <- all.cov.q6.V + mdni.error.q6.V + mdfs.error.q6.V
total.error.q7.V <- all.cov.q7.V + mdni.error.q7.V + mdfs.error.q7.V
total.error.q8.V <- all.cov.q8.V + mdni.error.q8.V + mdfs.error.q8.V
total.error.q9.V <- all.cov.q9.V + mdni.error.q9.V + mdfs.error.q9.V

fmi.q1.V <- (diag(mdni.error.q1.V) + diag(mdfs.error.q1.V)) / diag(total.error.q1.V)
fmi.q2.V <- (diag(mdni.error.q2.V) + diag(mdfs.error.q2.V)) / diag(total.error.q2.V)
fmi.q3.V <- (diag(mdni.error.q3.V) + diag(mdfs.error.q3.V)) / diag(total.error.q3.V)
fmi.q4.V <- (diag(mdni.error.q4.V) + diag(mdfs.error.q4.V)) / diag(total.error.q4.V)
fmi.q5.V <- (diag(mdni.error.q5.V) + diag(mdfs.error.q5.V)) / diag(total.error.q5.V)
fmi.q6.V <- (diag(mdni.error.q6.V) + diag(mdfs.error.q6.V)) / diag(total.error.q6.V)
fmi.q7.V <- (diag(mdni.error.q7.V) + diag(mdfs.error.q7.V)) / diag(total.error.q7.V)
fmi.q8.V <- (diag(mdni.error.q8.V) + diag(mdfs.error.q8.V)) / diag(total.error.q8.V)
fmi.q9.V <- (diag(mdni.error.q9.V) + diag(mdfs.error.q9.V)) / diag(total.error.q9.V)

#then calculate the approapriate degrees of freedom
#dfimputation=1/1m−1FMI2+1(1−FMI)(dfcomplete+1dfcomplete+3)dfcomplete

df_com.V <- sum.VI1$rdf %>% as.numeric() #same for all quantiles

v.q1.V <- (24) / fmi.q1.V^2
vhat.q1.V <- (1 - fmi.q1.V) * ((df_com.V + 1) / (df_com.V + 3)) * df_com.V
df_imp.q1.V <- (v.q1.V^(-1) + vhat.q1.V^(-1))^(-1)

v.q2.V <- (24) / fmi.q2.V^2
vhat.q2.V <- (1 - fmi.q2.V) * ((df_com.V + 1) / (df_com.V + 3)) * df_com.V
df_imp.q2.V <- (v.q2.V^(-1) + vhat.q2.V^(-1))^(-1)

v.q3.V <- (24) / fmi.q3.V^2
vhat.q3.V <- (1 - fmi.q3.V) * ((df_com.V + 1) / (df_com.V + 3)) * df_com.V
df_imp.q3.V <- (v.q3.V^(-1) + vhat.q3.V^(-1))^(-1)

v.q4.V <- (24) / fmi.q4.V^2
vhat.q4.V <- (1 - fmi.q4.V) * ((df_com.V + 1) / (df_com.V + 3)) * df_com.V
df_imp.q4.V <- (v.q4.V^(-1) + vhat.q4.V^(-1))^(-1)

v.q5.V <- (24) / fmi.q5.V^2
vhat.q5.V <- (1 - fmi.q5.V) * ((df_com.V + 1) / (df_com.V + 3)) * df_com.V
df_imp.q5.V <- (v.q5.V^(-1) + vhat.q5.V^(-1))^(-1)

v.q6.V <- (24) / fmi.q6.V^2
vhat.q6.V <- (1 - fmi.q6.V) * ((df_com.V + 1) / (df_com.V + 3)) * df_com.V
df_imp.q6.V <- (v.q6.V^(-1) + vhat.q6.V^(-1))^(-1)

v.q7.V <- (24) / fmi.q7.V^2
vhat.q7.V <- (1 - fmi.q7.V) * ((df_com.V + 1) / (df_com.V + 3)) * df_com.V
df_imp.q7.V <- (v.q7.V^(-1) + vhat.q4.V^(-1))^(-1)

v.q8.V <- (24) / fmi.q8.V^2
vhat.q8.V <- (1 - fmi.q8.V) * ((df_com.V + 1) / (df_com.V + 3)) * df_com.V
df_imp.q8.V <- (v.q8.V^(-1) + vhat.q8.V^(-1))^(-1)

v.q9.V <- (24) / fmi.q9.V^2
vhat.q9.V <- (1 - fmi.q9.V) * ((df_com.V + 1) / (df_com.V + 3)) * df_com.V
df_imp.q9.V <- (v.q9.V^(-1) + vhat.q9.V^(-1))^(-1)

#now get the SE, t, and p values
(se.pool.q1.V <- sqrt(diag(total.error.q1.V)))
t.pool.q1.V <- coef_pool.V[1,2:10] / se.pool.q1.V 
t.pool.q1.V.abs <- abs(t.pool.q1.V) %>% as.matrix()
p.pool.q1.V <- 2 * pt(q = t.pool.q1.V.abs, df = df_imp.q1.V, lower.tail = F)

(se.pool.q2.V <- sqrt(diag(total.error.q2.V)))
t.pool.q2.V <- coef_pool.V[2,2:10] / se.pool.q2.V 
t.pool.q2.V.abs <- abs(t.pool.q2.V) %>% as.matrix()
p.pool.q2.V <- 2 * pt(q = t.pool.q2.V.abs, df = df_imp.q2.V, lower.tail = F)

(se.pool.q3.V <- sqrt(diag(total.error.q3.V)))
t.pool.q3.V <- coef_pool.V[3,2:10] / se.pool.q3.V 
t.pool.q3.V.abs <- abs(t.pool.q3.V) %>% as.matrix()
p.pool.q3.V <- 2 * pt(q = t.pool.q3.V.abs, df = df_imp.q3.V, lower.tail = F)

(se.pool.q4.V <- sqrt(diag(total.error.q4.V)))
t.pool.q4.V <- coef_pool.V[4,2:10] / se.pool.q4.V 
t.pool.q4.V.abs <- abs(t.pool.q4.V) %>% as.matrix()
p.pool.q4.V <- 2 * pt(q = t.pool.q4.V.abs, df = df_imp.q4.V, lower.tail = F)

(se.pool.q5.V <- sqrt(diag(total.error.q5.V)))
t.pool.q5.V <- coef_pool.V[5,2:10] / se.pool.q5.V
t.pool.q5.V.abs <- abs(t.pool.q5.V) %>% as.matrix()
p.pool.q5.V <- 2 * pt(q = t.pool.q5.V.abs, df = df_imp.q5.V, lower.tail = F)

(se.pool.q6.V <- sqrt(diag(total.error.q6.V)))
t.pool.q6.V <- coef_pool.V[6,2:10] / se.pool.q6.V 
t.pool.q6.V.abs <- abs(t.pool.q6.V) %>% as.matrix()
p.pool.q6.V <- 2 * pt(q = t.pool.q6.V.abs, df = df_imp.q6.V, lower.tail = F)

(se.pool.q7.V <- sqrt(diag(total.error.q7.V)))
t.pool.q7.V <- coef_pool.V[7,2:10] / se.pool.q7.V 
t.pool.q7.V.abs <- abs(t.pool.q7.V) %>% as.matrix()
p.pool.q7.V <- 2 * pt(q = t.pool.q7.V.abs, df = df_imp.q7.V, lower.tail = F)

(se.pool.q8.V <- sqrt(diag(total.error.q8.V)))
t.pool.q8.V <- coef_pool.V[8,2:10] / se.pool.q8.V 
t.pool.q8.V.abs <- abs(t.pool.q8.V) %>% as.matrix()
p.pool.q8.V <- 2 * pt(q = t.pool.q8.V.abs, df = df_imp.q8.V, lower.tail = F)

(se.pool.q9.V <- sqrt(diag(total.error.q9.V)))
t.pool.q9.V <- coef_pool.V[9,2:10] / se.pool.q9.V 
t.pool.q9.V.abs <- abs(t.pool.q9.V) %>% as.matrix()
p.pool.q9.V <- 2 * pt(q = t.pool.q9.V.abs, df = df_imp.q9.V, lower.tail = F)

crit.pool.q1.V <- qt(p = 1 - (.05 / 2), df = df_imp.q1.V, lower.tail = T)
hw.pool.q1.V <- se.pool.q1.V * crit.pool.q1.V
ci_pool.q1.V <- cbind("lwr" = coef_pool.V[1,2:10] - hw.pool.q1.V, "upr" = coef_pool.V[1,2:10] + hw.pool.q1.V)

crit.pool.q2.V <- qt(p = 1 - (.05 / 2), df = df_imp.q2.V, lower.tail = T)
hw.pool.q2.V <- se.pool.q2.V * crit.pool.q2.V
ci_pool.q2.V <- cbind("lwr" = coef_pool.V[2,2:10] - hw.pool.q2.V, "upr" = coef_pool.V[2,2:10] + hw.pool.q2.V)

crit.pool.q3.V <- qt(p = 1 - (.05 / 2), df = df_imp.q3.V, lower.tail = T)
hw.pool.q3.V <- se.pool.q3.V * crit.pool.q3.V
ci_pool.q3.V <- cbind("lwr" = coef_pool.V[3,2:10] - hw.pool.q3.V, "upr" = coef_pool.V[3,2:10] + hw.pool.q3.V)

crit.pool.q4.V <- qt(p = 1 - (.05 / 2), df = df_imp.q4.V, lower.tail = T)
hw.pool.q4.V <- se.pool.q4.V * crit.pool.q4.V
ci_pool.q4.V <- cbind("lwr" = coef_pool.V[4,2:10] - hw.pool.q4.V, "upr" = coef_pool.V[4,2:10] + hw.pool.q4.V)

crit.pool.q5.V <- qt(p = 1 - (.05 / 2), df = df_imp.q5.V, lower.tail = T)
hw.pool.q5.V <- se.pool.q5.V * crit.pool.q5.V
ci_pool.q5.V <- cbind("lwr" = coef_pool.V[5,2:10] - hw.pool.q5.V, "upr" = coef_pool.V[5,2:10] + hw.pool.q5.V)

crit.pool.q6.V <- qt(p = 1 - (.05 / 2), df = df_imp.q6.V, lower.tail = T)
hw.pool.q6.V <- se.pool.q6.V * crit.pool.q6.V
ci_pool.q6.V <- cbind("lwr" = coef_pool.V[6,2:10] - hw.pool.q6.V, "upr" = coef_pool.V[6,2:10] + hw.pool.q6.V)

crit.pool.q7.V <- qt(p = 1 - (.05 / 2), df = df_imp.q7.V, lower.tail = T)
hw.pool.q7.V <- se.pool.q7.V * crit.pool.q7.V
ci_pool.q7.V <- cbind("lwr" = coef_pool.V[7,2:10] - hw.pool.q7.V, "upr" = coef_pool.V[7,2:10] + hw.pool.q7.V)

crit.pool.q8.V <- qt(p = 1 - (.05 / 2), df = df_imp.q8.V, lower.tail = T)
hw.pool.q8.V <- se.pool.q8.V * crit.pool.q8.V
ci_pool.q8.V <- cbind("lwr" = coef_pool.V[8,2:10] - hw.pool.q8.V, "upr" = coef_pool.V[8,2:10] + hw.pool.q8.V)

crit.pool.q9.V <- qt(p = 1 - (.05 / 2), df = df_imp.q9.V, lower.tail = T)
hw.pool.q9.V <- se.pool.q9.V * crit.pool.q9.V
ci_pool.q9.V <- cbind("lwr" = coef_pool.V[9,2:10] - hw.pool.q9.V, "upr" = coef_pool.V[9,2:10] + hw.pool.q9.V)


##### BH adjustments
p_values_RQ1_LW <- p.pool.q1.V %>% rbind(p.pool.q2.V) %>% rbind(p.pool.q3.V) %>% rbind(p.pool.q4.V) %>% rbind(p.pool.q5.V) %>% rbind(p.pool.q6.V) %>% rbind(p.pool.q7.V) %>% rbind(p.pool.q8.V) %>% rbind (p.pool.q9.V)  
fdrs_RQ1_LW <- p.adjust(p_values_RQ1_LW, method="BH") 
p_values_adj_RQ1_LW <- matrix(unlist(fdrs_RQ1_LW), nrow=9) %>% round(3) %>% as.data.frame()  %>% mutate(quant=c(1, 2, 3, 4, 5, 6, 7, 8, 9))
colnames(p_values_adj_RQ1_LW) <- c("(Intercept)_adj", "treatment_adj", "P201_adj", "P301_adj", "P501_adj", "P601_adj", "P901_adj", "LW_Factor_1_adj", "PV_Factor_1_adj", "Quantile")
p_values_RQ1_LW_wquant <- p_values_RQ1_LW %>% as.data.frame() %>% mutate(quant=c(1, 2, 3, 4, 5, 6, 7, 8, 9))

(p_values_RQ1_LW_all <- p_values_RQ1_LW_wquant %>% cbind(p_values_adj_RQ1_LW) %>% select(1, 11, 2, 12, 3, 13, 4, 14, 5, 15, 6, 16, 7, 17, 8, 18, 9, 19, 10, 20)  %>% round(3))

all_ci_V <- ci_pool.q1.V %>% rbind(ci_pool.q2.V) %>% rbind(ci_pool.q3.V) %>% rbind(ci_pool.q4.V) %>% rbind(ci_pool.q5.V) %>% rbind(ci_pool.q6.V) %>% rbind(ci_pool.q7.V) %>% rbind(ci_pool.q8.V) %>% rbind(ci_pool.q9.V)

(all_new_V <- all_ci_V %>% cbind(coef_pool.V))


## get the statistically significant effects of interest from previous line (I'm doing this by hand)
#treatment = not statistically significant
#LW pre test skills = statistically significant
#PV pre test skills = NOT statistically significant 
```
```{r RQ2Vocab}
#Then run all the analyses
#control functions to c convergence
RQ2vocab_I1 <- lqmm(PV_Factor_3 ~ treatment*(LW_Factor_1 + PV_Factor_1) + P201 + P301 + P501 + P601 + P901, random = ~1, group = TID, tau = c(1:9/10), data = QR_data_imp_I1, control = list(UP_max_iter = 1000, UP_tol = 1e-6))
RQ2vocab_I2 <- lqmm(PV_Factor_3 ~ treatment*(LW_Factor_1 + PV_Factor_1) + P201 + P301 + P501 + P601 + P901, random = ~1, group = TID, tau = c(1:9/10), data = QR_data_imp_I2, control = list(UP_max_iter = 1000, UP_tol = 1e-6))
RQ2vocab_I3 <- lqmm(PV_Factor_3 ~ treatment*(LW_Factor_1 + PV_Factor_1) + P201 + P301 + P501 + P601 + P901, random = ~1, group = TID, tau = c(1:9/10), data = QR_data_imp_I3, control = list(UP_max_iter = 1000, UP_tol = 1e-6))
RQ2vocab_I4 <- lqmm(PV_Factor_3 ~ treatment*(LW_Factor_1 + PV_Factor_1) + P201 + P301 + P501 + P601 + P901, random = ~1, group = TID, tau = c(1:9/10), data = QR_data_imp_I4, control = list(LP_max_iter = 10000, lP_tol = 1e-10))
RQ2vocab_I5 <- lqmm(PV_Factor_3 ~ treatment*(LW_Factor_1 + PV_Factor_1) + P201 + P301 + P501 + P601 + P901, random = ~1, group = TID, tau = c(1:9/10), data = QR_data_imp_I5, control = list(LP_max_iter = 1000, LP_tol = 1e-6))
RQ2vocab_I6 <- lqmm(PV_Factor_3 ~ treatment*(LW_Factor_1 + PV_Factor_1) + P201 + P301 + P501 + P601 + P901, random = ~1, group = TID, tau = c(1:9/10), data = QR_data_imp_I6, control = list(UP_max_iter = 1000, UP_tol = 1e-7))
RQ2vocab_I7 <- lqmm(PV_Factor_3 ~ treatment*(LW_Factor_1 + PV_Factor_1) + P201 + P301 + P501 + P601 + P901, random = ~1, group = TID, tau = c(1:9/10), data = QR_data_imp_I7, control = list(UP_max_iter = 1000, UP_tol = 1e-7))
RQ2vocab_I8 <- lqmm(PV_Factor_3 ~ treatment*(LW_Factor_1 + PV_Factor_1) + P201 + P301 + P501 + P601 + P901, random = ~1, group = TID, tau = c(1:9/10), data = QR_data_imp_I8, control = list(UP_max_iter = 1000, UP_tol = 1e-6))
RQ2vocab_I9 <- lqmm(PV_Factor_3 ~ treatment*(LW_Factor_1 + PV_Factor_1) + P201 + P301 + P501 + P601 + P901, random = ~1, group = TID, tau = c(1:9/10), data = QR_data_imp_I9, control = list(UP_max_iter = 1000, UP_tol = 1e-6))
RQ2vocab_I10 <- lqmm(PV_Factor_3 ~ treatment*(LW_Factor_1 + PV_Factor_1) + P201 + P301 + P501 + P601 + P901, random = ~1, group = TID, tau = c(1:9/10), data = QR_data_imp_I10, control = list(UP_max_iter = 1000, UP_tol = 1e-6))
RQ2vocab_I11 <- lqmm(PV_Factor_3 ~ treatment*(LW_Factor_1 + PV_Factor_1) + P201 + P301 + P501 + P601 + P901, random = ~1, group = TID, tau = c(1:9/10), data = QR_data_imp_I11, control = list(UP_max_iter = 1000, UP_tol = 1e-7))
RQ2vocab_I12 <- lqmm(PV_Factor_3 ~ treatment*(LW_Factor_1 + PV_Factor_1) + P201 + P301 + P501 + P601 + P901, random = ~1, group = TID, tau = c(1:9/10), data = QR_data_imp_I12, control = list(UP_max_iter = 1000, UP_tol = 1e-6))
RQ2vocab_I13 <- lqmm(PV_Factor_3 ~ treatment*(LW_Factor_1 + PV_Factor_1) + P201 + P301 + P501 + P601 + P901, random = ~1, group = TID, tau = c(1:9/10), data = QR_data_imp_I13, control = list(UP_max_iter = 1000, UP_tol = 1e-7))
RQ2vocab_I14 <- lqmm(PV_Factor_3 ~ treatment*(LW_Factor_1 + PV_Factor_1) + P201 + P301 + P501 + P601 + P901, random = ~1, group = TID, tau = c(1:9/10), data = QR_data_imp_I14, control = list(UP_max_iter = 1000, UP_tol = 1e-6))
RQ2vocab_I15 <- lqmm(PV_Factor_3 ~ treatment*(LW_Factor_1 + PV_Factor_1) + P201 + P301 + P501 + P601 + P901, random = ~1, group = TID, tau = c(1:9/10), data = QR_data_imp_I15, control = list(UP_max_iter = 1000, UP_tol = 1e-6))
RQ2vocab_I16 <- lqmm(PV_Factor_3 ~ treatment*(LW_Factor_1 + PV_Factor_1) + P201 + P301 + P501 + P601 + P901, random = ~1, group = TID, tau = c(1:9/10), data = QR_data_imp_I16, control = list(UP_max_iter = 1000, UP_tol = 1e-7))
RQ2vocab_I17 <- lqmm(PV_Factor_3 ~ treatment*(LW_Factor_1 + PV_Factor_1) + P201 + P301 + P501 + P601 + P901, random = ~1, group = TID, tau = c(1:9/10), data = QR_data_imp_I17, control = list(UP_max_iter = 1000, UP_tol = 1e-7))
RQ2vocab_I18 <- lqmm(PV_Factor_3 ~ treatment*(LW_Factor_1 + PV_Factor_1) + P201 + P301 + P501 + P601 + P901, random = ~1, group = TID, tau = c(1:9/10), data = QR_data_imp_I18, control = list(UP_max_iter = 1000, UP_tol = 1e-6))
RQ2vocab_I19 <- lqmm(PV_Factor_3 ~ treatment*(LW_Factor_1 + PV_Factor_1) + P201 + P301 + P501 + P601 + P901, random = ~1, group = TID, tau = c(1:9/10), data = QR_data_imp_I19, control = list(UP_max_iter = 1000, UP_tol = 1e-6))
RQ2vocab_I20 <- lqmm(PV_Factor_3 ~ treatment*(LW_Factor_1 + PV_Factor_1) + P201 + P301 + P501 + P601 + P901, random = ~1, group = TID, tau = c(1:9/10), data = QR_data_imp_I20, control = list(UP_max_iter = 1000, UP_tol = 1e-6))
RQ2vocab_I21 <- lqmm(PV_Factor_3 ~ treatment*(LW_Factor_1 + PV_Factor_1) + P201 + P301 + P501 + P601 + P901, random = ~1, group = TID, tau = c(1:9/10), data = QR_data_imp_I21, control = list(UP_max_iter = 1000, UP_tol = 1e-7))
RQ2vocab_I22 <- lqmm(PV_Factor_3 ~ treatment*(LW_Factor_1 + PV_Factor_1) + P201 + P301 + P501 + P601 + P901, random = ~1, group = TID, tau = c(1:9/10), data = QR_data_imp_I22, control = list(UP_max_iter = 1000, UP_tol = 1e-6))
RQ2vocab_I23 <- lqmm(PV_Factor_3 ~ treatment*(LW_Factor_1 + PV_Factor_1) + P201 + P301 + P501 + P601 + P901, random = ~1, group = TID, tau = c(1:9/10), data = QR_data_imp_I23, control = list(UP_max_iter = 1000, UP_tol = 1e-7))
RQ2vocab_I24 <- lqmm(PV_Factor_3 ~ treatment*(LW_Factor_1 + PV_Factor_1) + P201 + P301 + P501 + P601 + P901, random = ~1, group = TID, tau = c(1:9/10), data = QR_data_imp_I24, control = list(UP_max_iter = 1000, UP_tol = 1e-6))
RQ2vocab_I25 <- lqmm(PV_Factor_3 ~ treatment*(LW_Factor_1 + PV_Factor_1) + P201 + P301 + P501 + P601 + P901, random = ~1, group = TID, tau = c(1:9/10), data = QR_data_imp_I25, control = list(UP_max_iter = 1000, UP_tol = 1e-6))

#Extract the regression coefficients for each model
I1.coef.I <- coef.lqmm(RQ2vocab_I1) %>% t() %>% as.data.frame() %>% rownames_to_column() #transpose and make a datafrome
I2.coef.I <- coef.lqmm(RQ2vocab_I2) %>% t() %>% as.data.frame() %>% rownames_to_column()
I3.coef.I <- coef.lqmm(RQ2vocab_I3) %>% t() %>% as.data.frame() %>% rownames_to_column()
I4.coef.I <- coef.lqmm(RQ2vocab_I4) %>% t() %>% as.data.frame() %>% rownames_to_column()
I5.coef.I <- coef.lqmm(RQ2vocab_I5) %>% t() %>% as.data.frame() %>% rownames_to_column()
I6.coef.I <- coef.lqmm(RQ2vocab_I6) %>% t() %>% as.data.frame() %>% rownames_to_column()
I7.coef.I <- coef.lqmm(RQ2vocab_I7) %>% t() %>% as.data.frame() %>% rownames_to_column()
I8.coef.I <- coef.lqmm(RQ2vocab_I8) %>% t() %>% as.data.frame() %>% rownames_to_column()
I9.coef.I <- coef.lqmm(RQ2vocab_I9) %>% t() %>% as.data.frame() %>% rownames_to_column()
I10.coef.I <- coef.lqmm(RQ2vocab_I10) %>% t() %>% as.data.frame() %>% rownames_to_column()
I11.coef.I <- coef.lqmm(RQ2vocab_I11) %>% t() %>% as.data.frame() %>% rownames_to_column()
I12.coef.I <- coef.lqmm(RQ2vocab_I12) %>% t() %>% as.data.frame() %>% rownames_to_column()
I13.coef.I <- coef.lqmm(RQ2vocab_I13) %>% t() %>% as.data.frame() %>% rownames_to_column()
I14.coef.I <- coef.lqmm(RQ2vocab_I14) %>% t() %>% as.data.frame() %>% rownames_to_column()
I15.coef.I <- coef.lqmm(RQ2vocab_I15) %>% t() %>% as.data.frame() %>% rownames_to_column()
I16.coef.I <- coef.lqmm(RQ2vocab_I16) %>% t() %>% as.data.frame() %>% rownames_to_column()
I17.coef.I <- coef.lqmm(RQ2vocab_I17) %>% t() %>% as.data.frame() %>% rownames_to_column()
I18.coef.I <- coef.lqmm(RQ2vocab_I18) %>% t() %>% as.data.frame() %>% rownames_to_column()
I19.coef.I <- coef.lqmm(RQ2vocab_I19) %>% t() %>% as.data.frame() %>% rownames_to_column()
I20.coef.I <- coef.lqmm(RQ2vocab_I20) %>% t() %>% as.data.frame() %>% rownames_to_column()
I21.coef.I <- coef.lqmm(RQ2vocab_I21) %>% t() %>% as.data.frame() %>% rownames_to_column()
I22.coef.I <- coef.lqmm(RQ2vocab_I22) %>% t() %>% as.data.frame() %>% rownames_to_column()
I23.coef.I <- coef.lqmm(RQ2vocab_I23) %>% t() %>% as.data.frame() %>% rownames_to_column()
I24.coef.I <- coef.lqmm(RQ2vocab_I24) %>% t() %>% as.data.frame() %>% rownames_to_column()
I25.coef.I <- coef.lqmm(RQ2vocab_I25) %>% t() %>% as.data.frame() %>% rownames_to_column()
#combine all to one dataframe
All.coef.I <- I1.coef.I %>% full_join(I2.coef.I) %>% full_join(I3.coef.I) %>% full_join(I4.coef.I) %>% full_join(I5.coef.I) %>% full_join(I6.coef.I) %>% full_join(I7.coef.I) %>% full_join(I8.coef.I) %>% full_join(I9.coef.I) %>% full_join(I10.coef.I) %>% full_join(I11.coef.I) %>% full_join(I12.coef.I) %>% full_join(I13.coef.I) %>% full_join(I14.coef.I) %>% full_join(I15.coef.I) %>% full_join(I16.coef.I) %>% full_join(I17.coef.I) %>% full_join(I18.coef.I) %>% full_join(I19.coef.I) %>% full_join(I20.coef.I) %>% full_join(I21.coef.I) %>% full_join(I22.coef.I) %>% full_join(I23.coef.I) %>% full_join(I24.coef.I) %>% full_join(I25.coef.I)

#average the coefficients
coef_pool.I <- All.coef.I  %>% group_by(rowname) %>% summarise(across(everything(), mean)) %>% ungroup()
print(coef_pool.I)

#Extract covariance matrix for each model
sum.I1.I <-summary(RQ2vocab_I1, covariance = T, R = 50)
Cov.I1.I <-as.data.frame(sum.I1.I[["Cov"]]) %>% as.data.frame() %>% rownames_to_column() %>% pivot_longer(cols= !rowname, names_to = c(".value", "quantile"), names_sep = -3)

sum.I2.I <-summary(RQ2vocab_I2, covariance = T, R = 50) 
Cov.I2.I<-as.data.frame(sum.I2.I[["Cov"]]) %>% as.data.frame() %>% rownames_to_column() %>% pivot_longer(cols= !rowname, names_to = c(".value", "quantile"), names_sep = -3)
 
sum.I3.I <-summary(RQ2vocab_I3, covariance = T, R = 50)
Cov.I3.I <-as.data.frame(sum.I3.I[["Cov"]]) %>% as.data.frame() %>% rownames_to_column() %>% pivot_longer(cols= !rowname, names_to = c(".value", "quantile"), names_sep = -3)

sum.I4.I <-summary(RQ2vocab_I4, covariance = T, R = 50)
Cov.I4.I <-as.data.frame(sum.I4.I[["Cov"]]) %>% as.data.frame() %>% rownames_to_column() %>% pivot_longer(cols= !rowname, names_to = c(".value", "quantile"), names_sep = -3)

sum.I5.I <-summary(RQ2vocab_I5, covariance = T, R = 50)
Cov.I5.I <-as.data.frame(sum.I5.I[["Cov"]]) %>% as.data.frame() %>% rownames_to_column() %>% pivot_longer(cols= !rowname, names_to = c(".value", "quantile"), names_sep = -3)

sum.I6.I <-summary(RQ2vocab_I6, covariance = T, R = 50)
Cov.I6.I <-as.data.frame(sum.I6.I[["Cov"]]) %>% as.data.frame() %>% rownames_to_column() %>% pivot_longer(cols= !rowname, names_to = c(".value", "quantile"), names_sep = -3)

sum.I7.I <-summary(RQ2vocab_I7, covariance = T, R = 50)
Cov.I7.I <-as.data.frame(sum.I7.I[["Cov"]]) %>% as.data.frame() %>% rownames_to_column() %>% pivot_longer(cols= !rowname, names_to = c(".value", "quantile"), names_sep = -3)

sum.I8.I <-summary(RQ2vocab_I8, covariance = T, R = 50)
Cov.I8.I <-as.data.frame(sum.I8.I[["Cov"]]) %>% as.data.frame() %>% rownames_to_column() %>% pivot_longer(cols= !rowname, names_to = c(".value", "quantile"), names_sep = -3)

sum.I9.I <-summary(RQ2vocab_I9, covariance = T, R = 50)
Cov.I9.I <-as.data.frame(sum.I9.I[["Cov"]]) %>% as.data.frame() %>% rownames_to_column() %>% pivot_longer(cols= !rowname, names_to = c(".value", "quantile"), names_sep = -3)

sum.I10.I <-summary(RQ2vocab_I10, covariance = T, R = 50)
Cov.I10.I <-as.data.frame(sum.I10.I[["Cov"]]) %>% as.data.frame() %>% rownames_to_column() %>% pivot_longer(cols= !rowname, names_to = c(".value", "quantile"), names_sep = -3)

sum.I11.I <-summary(RQ2vocab_I11, covariance = T, R = 50)
Cov.I11.I <-as.data.frame(sum.I11.I[["Cov"]]) %>% as.data.frame() %>% rownames_to_column() %>% pivot_longer(cols= !rowname, names_to = c(".value", "quantile"), names_sep = -3)

sum.I12.I <-summary(RQ2vocab_I12, covariance = T, R = 50)
Cov.I12.I <-as.data.frame(sum.I12.I[["Cov"]]) %>% as.data.frame() %>% rownames_to_column() %>% pivot_longer(cols= !rowname, names_to = c(".value", "quantile"), names_sep = -3)

sum.I13.I <-summary(RQ2vocab_I13, covariance = T, R = 50)
Cov.I13.I <-as.data.frame(sum.I13.I[["Cov"]]) %>% as.data.frame() %>% rownames_to_column() %>% pivot_longer(cols= !rowname, names_to = c(".value", "quantile"), names_sep = -3)

sum.I14.I <-summary(RQ2vocab_I14, covariance = T, R = 50)
Cov.I14.I <-as.data.frame(sum.I14.I[["Cov"]]) %>% as.data.frame() %>% rownames_to_column() %>% pivot_longer(cols= !rowname, names_to = c(".value", "quantile"), names_sep = -3)

sum.I15.I<-summary(RQ2vocab_I15, covariance = T, R = 50)
Cov.I15.I <-as.data.frame(sum.I15.I[["Cov"]]) %>% as.data.frame() %>% rownames_to_column() %>% pivot_longer(cols= !rowname, names_to = c(".value", "quantile"), names_sep = -3)

sum.I16.I <-summary(RQ2vocab_I16, covariance = T, R = 50)
Cov.I16.I <-as.data.frame(sum.I16.I[["Cov"]]) %>% as.data.frame() %>% rownames_to_column() %>% pivot_longer(cols= !rowname, names_to = c(".value", "quantile"), names_sep = -3)

sum.I17.I <-summary(RQ2vocab_I17, covariance = T, R = 50)
Cov.I17.I <-as.data.frame(sum.I17.I[["Cov"]]) %>% as.data.frame() %>% rownames_to_column() %>% pivot_longer(cols= !rowname, names_to = c(".value", "quantile"), names_sep = -3)

sum.I18.I <-summary(RQ2vocab_I18, covariance = T, R = 50)
Cov.I18.I <-as.data.frame(sum.I18.I[["Cov"]]) %>% as.data.frame() %>% rownames_to_column() %>% pivot_longer(cols= !rowname, names_to = c(".value", "quantile"), names_sep = -3)

sum.I19.I <-summary(RQ2vocab_I19, covariance = T, R = 50)
Cov.I19.I <-as.data.frame(sum.I19.I[["Cov"]]) %>% as.data.frame() %>% rownames_to_column() %>% pivot_longer(cols= !rowname, names_to = c(".value", "quantile"), names_sep = -3)

sum.I20.I <-summary(RQ2vocab_I20, covariance = T, R = 50)
Cov.I20.I <-as.data.frame(sum.I20.I[["Cov"]]) %>% as.data.frame() %>% rownames_to_column() %>% pivot_longer(cols= !rowname, names_to = c(".value", "quantile"), names_sep = -3)

sum.I21.I <-summary(RQ2vocab_I21, covariance = T, R = 50)
Cov.I21.I <-as.data.frame(sum.I21.I[["Cov"]]) %>% as.data.frame() %>% rownames_to_column() %>% pivot_longer(cols= !rowname, names_to = c(".value", "quantile"), names_sep = -3)

sum.I22.I <-summary(RQ2vocab_I22, covariance = T, R = 50)
Cov.I22.I <-as.data.frame(sum.I22.I[["Cov"]]) %>% as.data.frame() %>% rownames_to_column() %>% pivot_longer(cols= !rowname, names_to = c(".value", "quantile"), names_sep = -3)

sum.I23.I <-summary(RQ2vocab_I23, covariance = T, R = 50)
Cov.I23.I <-as.data.frame(sum.I23.I[["Cov"]]) %>% as.data.frame() %>% rownames_to_column() %>% pivot_longer(cols= !rowname, names_to = c(".value", "quantile"), names_sep = -3)

sum.I24.I <-summary(RQ2vocab_I24, covariance = T, R = 50)
Cov.I24.I <-as.data.frame(sum.I24.I[["Cov"]]) %>% as.data.frame() %>% rownames_to_column() %>% pivot_longer(cols= !rowname, names_to = c(".value", "quantile"), names_sep = -3)

sum.I25.I<-summary(RQ2vocab_I25, covariance = T, R = 50)
Cov.I25.I <-as.data.frame(sum.I25.I[["Cov"]]) %>% as.data.frame() %>% rownames_to_column() %>% pivot_longer(cols= !rowname, names_to = c(".value", "quantile"), names_sep = -3)

#combine all COV matrices into one giant thing
all.cov.I <- Cov.I1.I %>% full_join(Cov.I2.I) %>% full_join(Cov.I3.I) %>% full_join(Cov.I4.I) %>% full_join(Cov.I5.I) %>% full_join(Cov.I6.I) %>% full_join(Cov.I7.I) %>% full_join(Cov.I8.I) %>% full_join(Cov.I9.I) %>% full_join(Cov.I10.I) %>% full_join(Cov.I11.I) %>% full_join(Cov.I12.I) %>% full_join(Cov.I13.I) %>% full_join(Cov.I14.I) %>% full_join(Cov.I15.I) %>% full_join(Cov.I16.I) %>% full_join(Cov.I17.I) %>% full_join(Cov.I18.I) %>% full_join(Cov.I19.I) %>% full_join(Cov.I20.I) %>% full_join(Cov.I21.I) %>% full_join(Cov.I22.I) %>% full_join(Cov.I23.I) %>% full_join(Cov.I24.I) %>% full_join(Cov.I25.I) %>% filter(rowname != "reStruct1") %>% filter(rowname != "scale") %>% select(-c("reStruct1.", "scale."))



#get avergaes per variable and per quantile # need to resort the row name variable to preserve original order
cov_pool.I <- all.cov.I  %>% group_by(rowname, quantile) %>% summarise(across(everything(), mean)) %>% ungroup() %>% arrange(factor(rowname, levels = (c("(Intercept)", "treatment", "LW_Factor_1", "PV_Factor_1", "P201", "P301", "P501", "P601", "P901", "treatment:LW_Factor_1", "treatment:PV_Factor_1"))))
print(cov_pool.I)

#quantify uncertainty due to missing data
  #VCOVmissing.data=∑mi=1(bi−bpool)T(bi−bpool)m−1
#calculate the difference for each coefficient from the pooled coefficient
coef.dev.I1.I <- I1.coef.I[2:12] - coef_pool.I[2:12] 
coef.dev.I1.I <- coef.dev.I1.I %>% mutate(quantile = seq(0.1, 0.9, 0.1))

coef.dev.I2.I <- I2.coef.I[2:12] - coef_pool.I[2:12] 
coef.dev.I2.I <- coef.dev.I2.I %>% mutate(quantile = seq(0.1, 0.9, 0.1))

coef.dev.I3.I <- I3.coef.I[2:12] - coef_pool.I[2:12] 
coef.dev.I3.I <- coef.dev.I3.I %>% mutate(quantile = seq(0.1, 0.9, 0.1))

coef.dev.I4.I <- I4.coef.I[2:12] - coef_pool.I[2:12] 
coef.dev.I4.I <- coef.dev.I4.I %>% mutate(quantile = seq(0.1, 0.9, 0.1))

coef.dev.I5.I <- I5.coef.I[2:12] - coef_pool.I[2:12] 
coef.dev.I5.I <- coef.dev.I5.I %>% mutate(quantile = seq(0.1, 0.9, 0.1))

coef.dev.I6.I <- I6.coef.I[2:12] - coef_pool.I[2:12] 
coef.dev.I6.I <- coef.dev.I6.I %>% mutate(quantile = seq(0.1, 0.9, 0.1))

coef.dev.I7.I <- I7.coef.I[2:12] - coef_pool.I[2:12] 
coef.dev.I7.I <- coef.dev.I7.I %>% mutate(quantile = seq(0.1, 0.9, 0.1))

coef.dev.I8.I <- I8.coef.I[2:12] - coef_pool.I[2:12] 
coef.dev.I8.I <- coef.dev.I8.I %>% mutate(quantile = seq(0.1, 0.9, 0.1))

coef.dev.I9.I <- I9.coef.I[2:12] - coef_pool.I[2:12] 
coef.dev.I9.I <- coef.dev.I9.I %>% mutate(quantile = seq(0.1, 0.9, 0.1))

coef.dev.I10.I <- I10.coef.I[2:12] - coef_pool.I[2:12] 
coef.dev.I10.I <- coef.dev.I10.I %>% mutate(quantile = seq(0.1, 0.9, 0.1))

coef.dev.I11.I <- I11.coef.I[2:12] - coef_pool.I[2:12] 
coef.dev.I11.I <- coef.dev.I11.I %>% mutate(quantile = seq(0.1, 0.9, 0.1))

coef.dev.I12.I <- I12.coef.I[2:12] - coef_pool.I[2:12] 
coef.dev.I12.I <- coef.dev.I12.I %>% mutate(quantile = seq(0.1, 0.9, 0.1))

coef.dev.I13.I <- I13.coef.I[2:12] - coef_pool.I[2:12] 
coef.dev.I13.I <- coef.dev.I13.I %>% mutate(quantile = seq(0.1, 0.9, 0.1))

coef.dev.I14.I <- I14.coef.I[2:12] - coef_pool.I[2:12] 
coef.dev.I14.I <- coef.dev.I14.I %>% mutate(quantile = seq(0.1, 0.9, 0.1))

coef.dev.I15.I <- I15.coef.I[2:12] - coef_pool.I[2:12] 
coef.dev.I15.I <- coef.dev.I15.I %>% mutate(quantile = seq(0.1 ,0.9, 0.1))

coef.dev.I16.I <- I16.coef.I[2:12] - coef_pool.I[2:12] 
coef.dev.I16.I <- coef.dev.I16.I %>% mutate(quantile = seq(0.1, 0.9, 0.1))

coef.dev.I17.I <- I17.coef.I[2:12] - coef_pool.I[2:12] 
coef.dev.I17.I <- coef.dev.I17.I %>% mutate(quantile = seq(0.1, 0.9, 0.1))

coef.dev.I18.I <- I18.coef.I[2:12] - coef_pool.I[2:12] 
coef.dev.I18.I <- coef.dev.I18.I %>% mutate(quantile = seq(0.1, 0.9, 0.1))

coef.dev.I19.I <- I19.coef.I[2:12] - coef_pool.I[2:12] 
coef.dev.I19.I <- coef.dev.I19.I %>% mutate(quantile = seq(0.1, 0.9, 0.1))

coef.dev.I20.I <- I20.coef.I[2:12] - coef_pool.I[2:12] 
coef.dev.I20.I <- coef.dev.I20.I %>% mutate(quantile = seq(0.1, 0.9, 0.1))

coef.dev.I21.I <- I21.coef.I[2:12] - coef_pool.I[2:12] 
coef.dev.I21.I <- coef.dev.I21.I %>% mutate(quantile = seq(0.1, 0.9, 0.1))

coef.dev.I22.I <- I22.coef.I[2:12] - coef_pool.I[2:12] 
coef.dev.I22.I <- coef.dev.I22.I %>% mutate(quantile = seq(0.1, 0.9, 0.1))

coef.dev.I23.I <- I23.coef.I[2:12] - coef_pool.I[2:12] 
coef.dev.I23.I <- coef.dev.I23.I %>% mutate(quantile = seq(0.1, 0.9, 0.1))

coef.dev.I24.I <- I24.coef.I[2:12] - coef_pool.I[2:12] 
coef.dev.I24.I <- coef.dev.I14.I %>% mutate(quantile = seq(0.1, 0.9, 0.1))

coef.dev.I25.I <- I25.coef.I[2:12] - coef_pool.I[2:12] 
coef.dev.I25.I <- coef.dev.I15.I %>% mutate(quantile = seq(0.1 ,0.9, 0.1))

#combine all coef dev matrices into one giant thing
all.coef.dev.I <- coef.dev.I1.I %>% full_join(coef.dev.I2.I)  %>% full_join(coef.dev.I3.I) %>% full_join(coef.dev.I4.I) %>% full_join(coef.dev.I5.I) %>% full_join(coef.dev.I6.I) %>% full_join(coef.dev.I7.I) %>% full_join(coef.dev.I8.I) %>% full_join(coef.dev.I9.I) %>% full_join(coef.dev.I10.I) %>% full_join(coef.dev.I11.I) %>% full_join(coef.dev.I12.I) %>% full_join(coef.dev.I13.I) %>% full_join(coef.dev.I14.I) %>% full_join(coef.dev.I15.I) %>% full_join(coef.dev.I16.I) %>% full_join(coef.dev.I17.I) %>% full_join(coef.dev.I18.I) %>% full_join(coef.dev.I19.I) %>% full_join(coef.dev.I20.I) %>% full_join(coef.dev.I21.I) %>% full_join(coef.dev.I22.I) %>% full_join(coef.dev.I23.I) %>% full_join(coef.dev.I24.I) %>% full_join(coef.dev.I25.I) 

#separate VCOV and ceof.dev by quantile because I am a bad coder
all.coef.dev.q1.I <- all.coef.dev.I %>% filter(quantile == 0.1) %>% select (-quantile) %>% as.matrix()
all.coef.dev.q2.I <- all.coef.dev.I %>% filter(quantile == 0.2) %>% select (-quantile) %>% as.matrix()
all.coef.dev.q3.I <- all.coef.dev.I %>% filter(quantile == "0.3")  %>% select (-quantile) %>% as.matrix() #not sure why it won't give anything with just the numbers
all.coef.dev.q4.I <- all.coef.dev.I %>% filter(quantile == 0.4) %>% select (-quantile) %>% as.matrix()
all.coef.dev.q5.I <- all.coef.dev.I %>% filter(quantile == 0.5) %>% select (-quantile) %>% as.matrix()
all.coef.dev.q6.I <- all.coef.dev.I %>% filter(quantile == 0.6)%>% select (-quantile) %>% as.matrix()
all.coef.dev.q7.I <- all.coef.dev.I %>% filter(quantile == "0.7")%>% select (-quantile) %>% as.matrix()
all.coef.dev.q8.I <- all.coef.dev.I %>% filter(quantile == 0.8) %>% select (-quantile) %>% as.matrix()
all.coef.dev.q9.I <- all.coef.dev.I %>% filter(quantile == 0.9)%>% select (-quantile) %>% as.matrix()

all.cov.q1.I <- cov_pool.I %>% filter(quantile == 0.1) %>% select(-c("rowname", "quantile")) %>% as.matrix()
all.cov.q2.I <- cov_pool.I %>% filter(quantile == 0.2) %>% select(-c("rowname", "quantile")) %>% as.matrix()
all.cov.q3.I <- cov_pool.I %>% filter(quantile == 0.3) %>% select(-c("rowname", "quantile")) %>% as.matrix()
all.cov.q4.I <- cov_pool.I %>% filter(quantile == 0.4) %>% select(-c("rowname", "quantile")) %>% as.matrix()
all.cov.q5.I <- cov_pool.I %>% filter(quantile == 0.5) %>% select(-c("rowname", "quantile")) %>% as.matrix()
all.cov.q6.I <- cov_pool.I %>% filter(quantile == 0.6) %>% select(-c("rowname", "quantile")) %>% as.matrix()
all.cov.q7.I <- cov_pool.I %>% filter(quantile == 0.7) %>% select(-c("rowname", "quantile")) %>% as.matrix()
all.cov.q8.I <- cov_pool.I %>% filter(quantile == 0.8) %>% select(-c("rowname", "quantile")) %>% as.matrix()
all.cov.q9.I <- cov_pool.I %>% filter(quantile == 0.9) %>% select(-c("rowname", "quantile")) %>% as.matrix()

#multiply matrix by its inverse, do this separately per quantile
coef.dev2.q1.I <- t(all.coef.dev.q1.I) %*% all.coef.dev.q1.I
coef.dev2.q2.I <- t(all.coef.dev.q2.I) %*% all.coef.dev.q2.I
coef.dev2.q3.I <- t(all.coef.dev.q3.I) %*% all.coef.dev.q3.I
coef.dev2.q4.I <- t(all.coef.dev.q4.I) %*% all.coef.dev.q4.I
coef.dev2.q5.I <- t(all.coef.dev.q5.I) %*% all.coef.dev.q5.I
coef.dev2.q6.I <- t(all.coef.dev.q6.I) %*% all.coef.dev.q6.I
coef.dev2.q7.I <- t(all.coef.dev.q7.I) %*% all.coef.dev.q7.I
coef.dev2.q8.I <- t(all.coef.dev.q8.I) %*% all.coef.dev.q8.I
coef.dev2.q9.I <- t(all.coef.dev.q9.I) %*% all.coef.dev.q9.I

#divide the sum of the above by #of imputations - 1 (14). to get missing data number of imputations error
mdni.error.q1.I <- coef.dev2.q1.I / 24
mdni.error.q2.I <- coef.dev2.q2.I / 24
mdni.error.q3.I <- coef.dev2.q3.I / 24
mdni.error.q4.I <- coef.dev2.q4.I / 24
mdni.error.q5.I <- coef.dev2.q5.I / 24
mdni.error.q6.I <- coef.dev2.q6.I / 24
mdni.error.q7.I <- coef.dev2.q7.I / 24
mdni.error.q8.I <- coef.dev2.q8.I / 24
mdni.error.q9.I <- coef.dev2.q9.I / 24

#divide the mdni  by total number of imputations to get error due to finite sampling
mdfs.error.q1.I <- mdni.error.q1.I / 25
mdfs.error.q2.I <- mdni.error.q2.I / 25
mdfs.error.q3.I <- mdni.error.q3.I / 25
mdfs.error.q4.I <- mdni.error.q4.I / 25
mdfs.error.q5.I <- mdni.error.q5.I / 25
mdfs.error.q6.I <- mdni.error.q6.I / 25
mdfs.error.q7.I <- mdni.error.q7.I / 25
mdfs.error.q8.I <- mdni.error.q8.I / 25
mdfs.error.q9.I <- mdni.error.q9.I / 25
#add the three error components to get total error
total.error.q1.I <-  all.cov.q1.I + mdni.error.q1.I + mdfs.error.q1.I
total.error.q2.I <- all.cov.q2.I + mdni.error.q2.I + mdfs.error.q2.I
total.error.q3.I <- all.cov.q3.I + mdni.error.q3.I + mdfs.error.q3.I
total.error.q4.I <- all.cov.q4.I + mdni.error.q4.I + mdfs.error.q4.I
total.error.q5.I <- all.cov.q5.I + mdni.error.q5.I + mdfs.error.q5.I
total.error.q6.I <- all.cov.q6.I + mdni.error.q6.I + mdfs.error.q6.I
total.error.q7.I <- all.cov.q7.I + mdni.error.q7.I + mdfs.error.q7.I
total.error.q8.I <- all.cov.q8.I + mdni.error.q8.I + mdfs.error.q8.I
total.error.q9.I <- all.cov.q9.I + mdni.error.q9.I + mdfs.error.q9.I

fmi.q1.I <- (diag(mdni.error.q1.I) + diag(mdfs.error.q1.I)) / diag(total.error.q1.I)
fmi.q2.I <- (diag(mdni.error.q2.I) + diag(mdfs.error.q2.I)) / diag(total.error.q2.I)
fmi.q3.I <- (diag(mdni.error.q3.I) + diag(mdfs.error.q3.I)) / diag(total.error.q3.I)
fmi.q4.I <- (diag(mdni.error.q4.I) + diag(mdfs.error.q4.I)) / diag(total.error.q4.I)
fmi.q5.I <- (diag(mdni.error.q5.I) + diag(mdfs.error.q5.I)) / diag(total.error.q5.I)
fmi.q6.I <- (diag(mdni.error.q6.I) + diag(mdfs.error.q6.I)) / diag(total.error.q6.I)
fmi.q7.I <- (diag(mdni.error.q7.I) + diag(mdfs.error.q7.I)) / diag(total.error.q7.I)
fmi.q8.I <- (diag(mdni.error.q8.I) + diag(mdfs.error.q8.I)) / diag(total.error.q8.I)
fmi.q9.I <- (diag(mdni.error.q9.I) + diag(mdfs.error.q9.I)) / diag(total.error.q9.I)

#then calculate the approapriate degrees of freedom
#dfimputation=1/1m−1FMI2+1(1−FMI)(dfcomplete+1dfcomplete+3)dfcomplete

df_com.I <- sum.I1.I$rdf %>% as.numeric() #same for all quantiles

v.q1.I <- (24) / fmi.q1.I^2
vhat.q1.I <- (1 - fmi.q1.I) * ((df_com.I + 1) / (df_com.I + 3)) * df_com.I
df_imp.q1.I <- (v.q1.I^(-1) + vhat.q1.I^(-1))^(-1)

v.q2.I <- (24) / fmi.q2.I^2
vhat.q2.I <- (1 - fmi.q2.I) * ((df_com.I + 1) / (df_com.I + 3)) * df_com.I
df_imp.q2.I <- (v.q2.I^(-1) + vhat.q2.I^(-1))^(-1)

v.q3.I <- (24) / fmi.q3.I^2
vhat.q3.I <- (1 - fmi.q3.I) * ((df_com.I + 1) / (df_com.I + 3)) * df_com.I
df_imp.q3.I <- (v.q3.I^(-1) + vhat.q3.I^(-1))^(-1)

v.q4.I <- (24) / fmi.q4.I^2
vhat.q4.I <- (1 - fmi.q4.I) * ((df_com.I + 1) / (df_com.I + 3)) * df_com.I
df_imp.q4.I <- (v.q4.I^(-1) + vhat.q4.I^(-1))^(-1)

v.q5.I <- (24) / fmi.q5.I^2
vhat.q5.I <- (1 - fmi.q5.I) * ((df_com.I + 1) / (df_com.I + 3)) * df_com.I
df_imp.q5.I <- (v.q5.I^(-1) + vhat.q5.I^(-1))^(-1)

v.q6.I <- (24) / fmi.q6.I^2
vhat.q6.I <- (1 - fmi.q6.I) * ((df_com.I + 1) / (df_com.I + 3)) * df_com.I
df_imp.q6.I <- (v.q6.I^(-1) + vhat.q6.I^(-1))^(-1)

v.q7.I <- (24) / fmi.q7.I^2
vhat.q7.I <- (1 - fmi.q7.I) * ((df_com.I + 1) / (df_com.I + 3)) * df_com.I
df_imp.q7.I <- (v.q7.I^(-1) + vhat.q4.I^(-1))^(-1)

v.q8.I <- (24) / fmi.q8.I^2
vhat.q8.I <- (1 - fmi.q8.I) * ((df_com.I + 1) / (df_com.I + 3)) * df_com.I
df_imp.q8.I <- (v.q8.I^(-1) + vhat.q8.I^(-1))^(-1)

v.q9.I <- (24) / fmi.q9.I^2
vhat.q9.I <- (1 - fmi.q9.I) * ((df_com.I + 1) / (df_com.I + 3)) * df_com.I
df_imp.q9.I <- (v.q9.I^(-1) + vhat.q9.I^(-1))^(-1)

#now get the SE, t, and p values
se.pool.q1.I <- sqrt(diag(total.error.q1.I))
t.pool.q1.I <- coef_pool.I[1,2:12] / se.pool.q1.I 
t.pool.q1.I.abs <- abs(t.pool.q1.I) %>% as.matrix()
(p.pool.q1.I <- 2 * pt(q = t.pool.q1.I.abs, df = df_imp.q1.I, lower.tail = F))

se.pool.q2.I <- sqrt(diag(total.error.q2.I))
t.pool.q2.I <- coef_pool.I[2,2:12] / se.pool.q2.I 
t.pool.q2.I.abs <- abs(t.pool.q2.I) %>% as.matrix()
(p.pool.q2.I <- 2 * pt(q = t.pool.q2.I.abs, df = df_imp.q2.I, lower.tail = F))

se.pool.q3.I <- sqrt(diag(total.error.q3.I))
t.pool.q3.I <- coef_pool.I[3,2:12] / se.pool.q3.I 
t.pool.q3.I.abs <- abs(t.pool.q3.I) %>% as.matrix()
(p.pool.q3.I <- 2 * pt(q = t.pool.q3.I.abs, df = df_imp.q3.I, lower.tail = F))

se.pool.q4.I <- sqrt(diag(total.error.q4.I))
t.pool.q4.I <- coef_pool.I[4,2:12] / se.pool.q4.I 
t.pool.q4.I.abs <- abs(t.pool.q4.I) %>% as.matrix()
(p.pool.q4.I <- 2 * pt(q = t.pool.q4.I.abs, df = df_imp.q4.I, lower.tail = F))

se.pool.q5.I <- sqrt(diag(total.error.q5.I))
t.pool.q5.I <- coef_pool.I[5,2:12] / se.pool.q5.I
t.pool.q5.I.abs <- abs(t.pool.q5.I) %>% as.matrix()
(p.pool.q5.I <- 2 * pt(q = t.pool.q5.I.abs, df = df_imp.q5.I, lower.tail = F))

se.pool.q6.I <- sqrt(diag(total.error.q6.I))
t.pool.q6.I <- coef_pool.I[6,2:12] / se.pool.q6.I 
t.pool.q6.I.abs <- abs(t.pool.q6.I) %>% as.matrix()
(p.pool.q6.I <- 2 * pt(q = t.pool.q6.I.abs, df = df_imp.q6.I, lower.tail = F))

se.pool.q7.I <- sqrt(diag(total.error.q7.I))
t.pool.q7.I <- coef_pool.I[7,2:12] / se.pool.q7.I 
t.pool.q7.I.abs <- abs(t.pool.q7.I) %>% as.matrix()
(p.pool.q7.I <- 2 * pt(q = t.pool.q7.I.abs, df = df_imp.q7.I, lower.tail = F))

se.pool.q8.I <- sqrt(diag(total.error.q8.I))
t.pool.q8.I <- coef_pool.I[8,2:12] / se.pool.q8.I 
t.pool.q8.I.abs <- abs(t.pool.q8.I) %>% as.matrix()
(p.pool.q8.I <- 2 * pt(q = t.pool.q8.I.abs, df = df_imp.q8.I, lower.tail = F))

se.pool.q9.I <- sqrt(diag(total.error.q9.I))
t.pool.q9.I <- coef_pool.I[9,2:12] / se.pool.q9.I 
t.pool.q9.I.abs <- abs(t.pool.q9.I) %>% as.matrix()
(p.pool.q9.I <- 2 * pt(q = t.pool.q9.I.abs, df = df_imp.q9.I, lower.tail = F))

crit.pool.q1.I <- qt(p = 1 - (.05 / 2), df = df_imp.q1.I, lower.tail = T)
hw.pool.q1.I <- se.pool.q1.I * crit.pool.q1.I
ci_pool.q1.I <- cbind("lwr" = coef_pool.I[1,2:12] - hw.pool.q1.I, "upr" = coef_pool.I[1,2:12] + hw.pool.q1.I)

crit.pool.q2.I <- qt(p = 1 - (.05 / 2), df = df_imp.q2.I, lower.tail = T)
hw.pool.q2.I <- se.pool.q2.I * crit.pool.q2.I
ci_pool.q2.I <- cbind("lwr" = coef_pool.I[2,2:12] - hw.pool.q2.I, "upr" = coef_pool.I[2,2:12] + hw.pool.q2.I)

crit.pool.q3.I <- qt(p = 1 - (.05 / 2), df = df_imp.q3.I, lower.tail = T)
hw.pool.q3.I <- se.pool.q3.I * crit.pool.q3.I
ci_pool.q3.I <- cbind("lwr" = coef_pool.I[3,2:12] - hw.pool.q3.I, "upr" = coef_pool.I[3,2:12] + hw.pool.q3.I)

crit.pool.q4.I <- qt(p = 1 - (.05 / 2), df = df_imp.q4.I, lower.tail = T)
hw.pool.q4.I <- se.pool.q4.I * crit.pool.q4.I
ci_pool.q4.I <- cbind("lwr" = coef_pool.I[4,2:12] - hw.pool.q4.I, "upr" = coef_pool.I[4,2:12] + hw.pool.q4.I)

crit.pool.q5.I <- qt(p = 1 - (.05 / 2), df = df_imp.q5.I, lower.tail = T)
hw.pool.q5.I <- se.pool.q5.I * crit.pool.q5.I
ci_pool.q5.I <- cbind("lwr" = coef_pool.I[5,2:12] - hw.pool.q5.I, "upr" = coef_pool.I[5,2:12] + hw.pool.q5.I)

crit.pool.q6.I <- qt(p = 1 - (.05 / 2), df = df_imp.q6.I, lower.tail = T)
hw.pool.q6.I <- se.pool.q6.I * crit.pool.q6.I
ci_pool.q6.I <- cbind("lwr" = coef_pool.I[6,2:12] - hw.pool.q6.I, "upr" = coef_pool.I[6,2:12] + hw.pool.q6.I)

crit.pool.q7.I <- qt(p = 1 - (.05 / 2), df = df_imp.q7.I, lower.tail = T)
hw.pool.q7.I <- se.pool.q7.I * crit.pool.q7.I
ci_pool.q7.I <- cbind("lwr" = coef_pool.I[7,2:12] - hw.pool.q7.I, "upr" = coef_pool.I[7,2:12] + hw.pool.q7.I)

crit.pool.q8.I <- qt(p = 1 - (.05 / 2), df = df_imp.q8.I, lower.tail = T)
hw.pool.q8.I <- se.pool.q8.I * crit.pool.q8.I
ci_pool.q8.I <- cbind("lwr" = coef_pool.I[8,2:12] - hw.pool.q8.I, "upr" = coef_pool.I[8,2:12] + hw.pool.q8.I)

crit.pool.q9.I <- qt(p = 1 - (.05 / 2), df = df_imp.q9.I, lower.tail = T)
hw.pool.q9.I <- se.pool.q9.I * crit.pool.q9.I
ci_pool.q9.I <- cbind("lwr" = coef_pool.I[9,2:12] - hw.pool.q9.I, "upr" = coef_pool.I[9,2:12] + hw.pool.q9.I)

##### BH adjustments
p_values_RQ2_PV <- p.pool.q1.I %>% rbind(p.pool.q2.I) %>% rbind(p.pool.q3.I) %>% rbind(p.pool.q4.I) %>% rbind(p.pool.q5.I) %>% rbind(p.pool.q6.I) %>% rbind(p.pool.q7.I) %>% rbind(p.pool.q8.I) %>% rbind (p.pool.q9.I)  
fdrs_RQ2_PV <- p.adjust(p_values_RQ2_PV, method="BH") 
p_values_adj_RQ2_PV <- matrix(unlist(fdrs_RQ2_PV), nrow=9) %>% round(3) %>% as.data.frame()  %>% mutate(quant=c(1, 2, 3, 4, 5, 6, 7, 8, 9))
colnames(p_values_adj_RQ2_PV) <- c("(Intercept)_adj", "treatment_adj", "LW_Factor_1_adj", "PV_Factor_1_adj", "P201_adj", "P301_adj", "P501_adj", "P601_adj", "P901_adj", "treatment:LW_Factor_1_adj", "treatment:PV_Factor_1_adj", "Quantile")
p_values_RQ2_PV_wquant <- p_values_RQ2_PV %>% as.data.frame() %>% mutate(quant=c(1, 2, 3, 4, 5, 6, 7, 8, 9))

p_values_RQ2_PV_all <- p_values_RQ2_PV_wquant %>% cbind(p_values_adj_RQ2_PV) %>% select(1, 13, 2, 14, 3, 15, 4, 16, 5, 17, 6, 18, 7, 19, 8, 20, 9, 21, 10, 22, 11, 23)  %>% round(3)

all_ci_I <- ci_pool.q1.I %>% rbind(ci_pool.q2.I) %>% rbind(ci_pool.q3.I) %>% rbind(ci_pool.q4.I) %>% rbind(ci_pool.q5.I) %>% rbind(ci_pool.q6.I) %>% rbind(ci_pool.q7.I) %>% rbind(ci_pool.q8.I) %>% rbind(ci_pool.q9.I)

(all_new_I <- all_ci_I %>% cbind(coef_pool.I))

## get the statistically significant effects of interest from previous line (I'm doing this by hand)
#treatment = not statistically significant
#LW pre test skills = NOT statistically significant 
#PV pre test skills = statistically significant for quartiles 1-5 
#interaction of treatment with LW pre = NOT statistically significant at all quartiles
#interaction of treatment with PV pre = NOT statistically significant at all quartiles
```
```{r RQ2LWID}
#Then run all the analyses
#control functions to reah convergence
RQ2letterid_I1 <- lqmm(LW_Factor_3 ~ treatment*(LW_Factor_1 + PV_Factor_1) + P201 + P301 + P501 + P601 + P901, random = ~1, group = TID, tau = c(1:9/10), data = QR_data_imp_I1, control = list(UP_max_iter = 1000, UP_tol = 1e-6))
RQ2letterid_I2 <- lqmm(LW_Factor_3 ~ treatment*(LW_Factor_1 + PV_Factor_1) + P201 + P301 + P501 + P601 + P901, random = ~1, group = TID, tau = c(1:9/10), data = QR_data_imp_I2, control = list(UP_max_iter = 1000, UP_tol = 1e-6))
RQ2letterid_I3 <- lqmm(LW_Factor_3 ~ treatment*(LW_Factor_1 + PV_Factor_1) + P201 + P301 + P501 + P601 + P901, random = ~1, group = TID, tau = c(1:9/10), data = QR_data_imp_I3, control = list(UP_max_iter = 1000, UP_tol = 1e-6))
RQ2letterid_I4 <- lqmm(LW_Factor_3 ~ treatment*(LW_Factor_1 + PV_Factor_1) + P201 + P301 + P501 + P601 + P901, random = ~1, group = TID, tau = c(1:9/10), data = QR_data_imp_I4, control = list(LP_max_iter = 10000, lP_tol = 1e-10))
RQ2letterid_I5 <- lqmm(LW_Factor_3 ~ treatment*(LW_Factor_1 + PV_Factor_1) + P201 + P301 + P501 + P601 + P901, random = ~1, group = TID, tau = c(1:9/10), data = QR_data_imp_I5, control = list(LP_max_iter = 1000, LP_tol = 1e-6))
RQ2letterid_I6 <- lqmm(LW_Factor_3 ~ treatment*(LW_Factor_1 + PV_Factor_1) + P201 + P301 + P501 + P601 + P901, random = ~1, group = TID, tau = c(1:9/10), data = QR_data_imp_I6, control = list(UP_max_iter = 1000, UP_tol = 1e-7))
RQ2letterid_I7 <- lqmm(LW_Factor_3 ~ treatment*(LW_Factor_1 + PV_Factor_1) + P201 + P301 + P501 + P601 + P901, random = ~1, group = TID, tau = c(1:9/10), data = QR_data_imp_I7, control = list(UP_max_iter = 1000, UP_tol = 1e-7))
RQ2letterid_I8 <- lqmm(LW_Factor_3 ~ treatment*(LW_Factor_1 + PV_Factor_1) + P201 + P301 + P501 + P601 + P901, random = ~1, group = TID, tau = c(1:9/10), data = QR_data_imp_I8, control = list(UP_max_iter = 1000, UP_tol = 1e-6))
RQ2letterid_I9 <- lqmm(LW_Factor_3 ~ treatment*(LW_Factor_1 + PV_Factor_1) + P201 + P301 + P501 + P601 + P901, random = ~1, group = TID, tau = c(1:9/10), data = QR_data_imp_I9, control = list(UP_max_iter = 1000, UP_tol = 1e-6))
RQ2letterid_I10 <- lqmm(LW_Factor_3 ~ treatment*(LW_Factor_1 + PV_Factor_1) + P201 + P301 + P501 + P601 + P901, random = ~1, group = TID, tau = c(1:9/10), data = QR_data_imp_I10, control = list(UP_max_iter = 1000, UP_tol = 1e-6))
RQ2letterid_I11 <- lqmm(LW_Factor_3 ~ treatment*(LW_Factor_1 + PV_Factor_1) + P201 + P301 + P501 + P601 + P901, random = ~1, group = TID, tau = c(1:9/10), data = QR_data_imp_I11, control = list(UP_max_iter = 1000, UP_tol = 1e-7))
RQ2letterid_I12 <- lqmm(LW_Factor_3 ~ treatment*(LW_Factor_1 + PV_Factor_1) + P201 + P301 + P501 + P601 + P901, random = ~1, group = TID, tau = c(1:9/10), data = QR_data_imp_I12, control = list(UP_max_iter = 1000, UP_tol = 1e-6))
RQ2letterid_I13 <- lqmm(LW_Factor_3 ~ treatment*(LW_Factor_1 + PV_Factor_1) + P201 + P301 + P501 + P601 + P901, random = ~1, group = TID, tau = c(1:9/10), data = QR_data_imp_I13, control = list(UP_max_iter = 1000, UP_tol = 1e-7))
RQ2letterid_I14 <- lqmm(LW_Factor_3 ~ treatment*(LW_Factor_1 + PV_Factor_1) + P201 + P301 + P501 + P601 + P901, random = ~1, group = TID, tau = c(1:9/10), data = QR_data_imp_I14, control = list(UP_max_iter = 1000, UP_tol = 1e-6))
RQ2letterid_I15 <- lqmm(LW_Factor_3 ~ treatment*(LW_Factor_1 + PV_Factor_1) + P201 + P301 + P501 + P601 + P901, random = ~1, group = TID, tau = c(1:9/10), data = QR_data_imp_I15, control = list(UP_max_iter = 1000, UP_tol = 1e-6))
RQ2letterid_I16 <- lqmm(LW_Factor_3 ~ treatment*(LW_Factor_1 + PV_Factor_1) + P201 + P301 + P501 + P601 + P901, random = ~1, group = TID, tau = c(1:9/10), data = QR_data_imp_I16, control = list(UP_max_iter = 1000, UP_tol = 1e-7))
RQ2letterid_I17 <- lqmm(LW_Factor_3 ~ treatment*(LW_Factor_1 + PV_Factor_1) + P201 + P301 + P501 + P601 + P901, random = ~1, group = TID, tau = c(1:9/10), data = QR_data_imp_I17, control = list(UP_max_iter = 1000, UP_tol = 1e-7))
RQ2letterid_I18 <- lqmm(LW_Factor_3 ~ treatment*(LW_Factor_1 + PV_Factor_1) + P201 + P301 + P501 + P601 + P901, random = ~1, group = TID, tau = c(1:9/10), data = QR_data_imp_I18, control = list(UP_max_iter = 1000, UP_tol = 1e-6))
RQ2letterid_I19 <- lqmm(LW_Factor_3 ~ treatment*(LW_Factor_1 + PV_Factor_1) + P201 + P301 + P501 + P601 + P901, random = ~1, group = TID, tau = c(1:9/10), data = QR_data_imp_I19, control = list(UP_max_iter = 1000, UP_tol = 1e-6))
RQ2letterid_I20 <- lqmm(LW_Factor_3 ~ treatment*(LW_Factor_1 + PV_Factor_1) + P201 + P301 + P501 + P601 + P901, random = ~1, group = TID, tau = c(1:9/10), data = QR_data_imp_I20, control = list(UP_max_iter = 1000, UP_tol = 1e-6))
RQ2letterid_I21 <- lqmm(LW_Factor_3 ~ treatment*(LW_Factor_1 + PV_Factor_1) + P201 + P301 + P501 + P601 + P901, random = ~1, group = TID, tau = c(1:9/10), data = QR_data_imp_I21, control = list(UP_max_iter = 1000, UP_tol = 1e-7))
RQ2letterid_I22 <- lqmm(LW_Factor_3 ~ treatment*(LW_Factor_1 + PV_Factor_1) + P201 + P301 + P501 + P601 + P901, random = ~1, group = TID, tau = c(1:9/10), data = QR_data_imp_I22, control = list(UP_max_iter = 1000, UP_tol = 1e-6))
RQ2letterid_I23 <- lqmm(LW_Factor_3 ~ treatment*(LW_Factor_1 + PV_Factor_1) + P201 + P301 + P501 + P601 + P901, random = ~1, group = TID, tau = c(1:9/10), data = QR_data_imp_I23, control = list(UP_max_iter = 1000, UP_tol = 1e-7))
RQ2letterid_I24 <- lqmm(LW_Factor_3 ~ treatment*(LW_Factor_1 + PV_Factor_1) + P201 + P301 + P501 + P601 + P901, random = ~1, group = TID, tau = c(1:9/10), data = QR_data_imp_I24, control = list(UP_max_iter = 1000, UP_tol = 1e-6))
RQ2letterid_I25 <- lqmm(LW_Factor_3 ~ treatment*(LW_Factor_1 + PV_Factor_1) + P201 + P301 + P501 + P601 + P901, random = ~1, group = TID, tau = c(1:9/10), data = QR_data_imp_I25, control = list(UP_max_iter = 1000, UP_tol = 1e-6))

#Extract the regression coefficients for each model
I1.coef.IV <- coef.lqmm(RQ2letterid_I1) %>% t() %>% as.data.frame() %>% rownames_to_column() #transpose and make a datafrome
I2.coef.IV <- coef.lqmm(RQ2letterid_I2) %>% t() %>% as.data.frame() %>% rownames_to_column()
I3.coef.IV <- coef.lqmm(RQ2letterid_I3) %>% t() %>% as.data.frame() %>% rownames_to_column()
I4.coef.IV <- coef.lqmm(RQ2letterid_I4) %>% t() %>% as.data.frame() %>% rownames_to_column()
I5.coef.IV <- coef.lqmm(RQ2letterid_I5) %>% t() %>% as.data.frame() %>% rownames_to_column()
I6.coef.IV <- coef.lqmm(RQ2letterid_I6) %>% t() %>% as.data.frame() %>% rownames_to_column()
I7.coef.IV <- coef.lqmm(RQ2letterid_I7) %>% t() %>% as.data.frame() %>% rownames_to_column()
I8.coef.IV <- coef.lqmm(RQ2letterid_I8) %>% t() %>% as.data.frame() %>% rownames_to_column()
I9.coef.IV <- coef.lqmm(RQ2letterid_I9) %>% t() %>% as.data.frame() %>% rownames_to_column()
I10.coef.IV <- coef.lqmm(RQ2letterid_I10) %>% t() %>% as.data.frame() %>% rownames_to_column()
I11.coef.IV <- coef.lqmm(RQ2letterid_I11) %>% t() %>% as.data.frame() %>% rownames_to_column()
I12.coef.IV <- coef.lqmm(RQ2letterid_I12) %>% t() %>% as.data.frame() %>% rownames_to_column()
I13.coef.IV <- coef.lqmm(RQ2letterid_I13) %>% t() %>% as.data.frame() %>% rownames_to_column()
I14.coef.IV <- coef.lqmm(RQ2letterid_I14) %>% t() %>% as.data.frame() %>% rownames_to_column()
I15.coef.IV <- coef.lqmm(RQ2letterid_I15) %>% t() %>% as.data.frame() %>% rownames_to_column()
I16.coef.IV <- coef.lqmm(RQ2letterid_I16) %>% t() %>% as.data.frame() %>% rownames_to_column()
I17.coef.IV <- coef.lqmm(RQ2letterid_I17) %>% t() %>% as.data.frame() %>% rownames_to_column()
I18.coef.IV <- coef.lqmm(RQ2letterid_I18) %>% t() %>% as.data.frame() %>% rownames_to_column()
I19.coef.IV <- coef.lqmm(RQ2letterid_I19) %>% t() %>% as.data.frame() %>% rownames_to_column()
I20.coef.IV <- coef.lqmm(RQ2letterid_I20) %>% t() %>% as.data.frame() %>% rownames_to_column()
I21.coef.IV <- coef.lqmm(RQ2letterid_I21) %>% t() %>% as.data.frame() %>% rownames_to_column()
I22.coef.IV <- coef.lqmm(RQ2letterid_I22) %>% t() %>% as.data.frame() %>% rownames_to_column()
I23.coef.IV <- coef.lqmm(RQ2letterid_I23) %>% t() %>% as.data.frame() %>% rownames_to_column()
I24.coef.IV <- coef.lqmm(RQ2letterid_I24) %>% t() %>% as.data.frame() %>% rownames_to_column()
I25.coef.IV <- coef.lqmm(RQ2letterid_I25) %>% t() %>% as.data.frame() %>% rownames_to_column()

#combine all to one dataframe
All.coef.IV <- I1.coef.IV %>% full_join(I2.coef.IV) %>% full_join(I3.coef.IV) %>% full_join(I4.coef.IV) %>% full_join(I5.coef.IV) %>% full_join(I6.coef.IV) %>% full_join(I7.coef.IV) %>% full_join(I8.coef.IV) %>% full_join(I9.coef.IV) %>% full_join(I10.coef.IV) %>% full_join(I11.coef.IV) %>% full_join(I12.coef.IV) %>% full_join(I13.coef.IV) %>% full_join(I14.coef.IV) %>% full_join(I15.coef.IV) %>% full_join(I16.coef.IV) %>% full_join(I17.coef.IV) %>% full_join(I18.coef.IV) %>% full_join(I19.coef.IV) %>% full_join(I20.coef.IV) %>% full_join(I21.coef.IV) %>% full_join(I22.coef.IV) %>% full_join(I23.coef.IV) %>% full_join(I24.coef.IV) %>% full_join(I25.coef.IV)

#average the coefficients
coef_pool.IV <- All.coef.IV  %>% group_by(rowname) %>% summarise(across(everything(), mean)) %>% ungroup()
print(coef_pool.IV)

#Extract covariance matrix for each model
sum.IV1.IV <-summary(RQ2letterid_I1, covariance = T, R = 50)
Cov.IV1.IV <-as.data.frame(sum.IV1.IV[["Cov"]]) %>% as.data.frame() %>% rownames_to_column() %>% pivot_longer(cols= !rowname, names_to = c(".value", "quantile"), names_sep = -3)

sum.IV2.IV <-summary(RQ2letterid_I2, covariance = T, R = 50) 
Cov.IV2.IV<-as.data.frame(sum.IV2.IV[["Cov"]]) %>% as.data.frame() %>% rownames_to_column() %>% pivot_longer(cols= !rowname, names_to = c(".value", "quantile"), names_sep = -3)
 
sum.IV3.IV <-summary(RQ2letterid_I3, covariance = T, R = 50)
Cov.IV3.IV <-as.data.frame(sum.IV3.IV[["Cov"]]) %>% as.data.frame() %>% rownames_to_column() %>% pivot_longer(cols= !rowname, names_to = c(".value", "quantile"), names_sep = -3)

sum.IV4.IV <-summary(RQ2letterid_I4, covariance = T, R = 50)
Cov.IV4.IV <-as.data.frame(sum.IV4.IV[["Cov"]]) %>% as.data.frame() %>% rownames_to_column() %>% pivot_longer(cols= !rowname, names_to = c(".value", "quantile"), names_sep = -3)

sum.IV5.IV <-summary(RQ2letterid_I5, covariance = T, R = 50)
Cov.IV5.IV <-as.data.frame(sum.IV5.IV[["Cov"]]) %>% as.data.frame() %>% rownames_to_column() %>% pivot_longer(cols= !rowname, names_to = c(".value", "quantile"), names_sep = -3)

sum.IV6.IV <-summary(RQ2letterid_I6, covariance = T, R = 50)
Cov.IV6.IV <-as.data.frame(sum.IV6.IV[["Cov"]]) %>% as.data.frame() %>% rownames_to_column() %>% pivot_longer(cols= !rowname, names_to = c(".value", "quantile"), names_sep = -3)

sum.IV7.IV <-summary(RQ2letterid_I7, covariance = T, R = 50)
Cov.IV7.IV <-as.data.frame(sum.IV7.IV[["Cov"]]) %>% as.data.frame() %>% rownames_to_column() %>% pivot_longer(cols= !rowname, names_to = c(".value", "quantile"), names_sep = -3)

sum.IV8.IV <-summary(RQ2letterid_I8, covariance = T, R = 50)
Cov.IV8.IV <-as.data.frame(sum.IV8.IV[["Cov"]]) %>% as.data.frame() %>% rownames_to_column() %>% pivot_longer(cols= !rowname, names_to = c(".value", "quantile"), names_sep = -3)

sum.IV9.IV <-summary(RQ2letterid_I9, covariance = T, R = 50)
Cov.IV9.IV <-as.data.frame(sum.IV9.IV[["Cov"]]) %>% as.data.frame() %>% rownames_to_column() %>% pivot_longer(cols= !rowname, names_to = c(".value", "quantile"), names_sep = -3)

sum.IV10.IV <-summary(RQ2letterid_I10, covariance = T, R = 50)
Cov.IV10.IV <-as.data.frame(sum.IV10.IV[["Cov"]]) %>% as.data.frame() %>% rownames_to_column() %>% pivot_longer(cols= !rowname, names_to = c(".value", "quantile"), names_sep = -3)

sum.IV11.IV <-summary(RQ2letterid_I11, covariance = T, R = 50)
Cov.IV11.IV <-as.data.frame(sum.IV11.IV[["Cov"]]) %>% as.data.frame() %>% rownames_to_column() %>% pivot_longer(cols= !rowname, names_to = c(".value", "quantile"), names_sep = -3)

sum.IV12.IV <-summary(RQ2letterid_I12, covariance = T, R = 50)
Cov.IV12.IV <-as.data.frame(sum.IV12.IV[["Cov"]]) %>% as.data.frame() %>% rownames_to_column() %>% pivot_longer(cols= !rowname, names_to = c(".value", "quantile"), names_sep = -3)

sum.IV13.IV <-summary(RQ2letterid_I13, covariance = T, R = 50)
Cov.IV13.IV <-as.data.frame(sum.IV13.IV[["Cov"]]) %>% as.data.frame() %>% rownames_to_column() %>% pivot_longer(cols= !rowname, names_to = c(".value", "quantile"), names_sep = -3)

sum.IV14.IV <-summary(RQ2letterid_I14, covariance = T, R = 50)
Cov.IV14.IV <-as.data.frame(sum.IV14.IV[["Cov"]]) %>% as.data.frame() %>% rownames_to_column() %>% pivot_longer(cols= !rowname, names_to = c(".value", "quantile"), names_sep = -3)

sum.IV15.IV<-summary(RQ2letterid_I15, covariance = T, R = 50)
Cov.IV15.IV <-as.data.frame(sum.IV15.IV[["Cov"]]) %>% as.data.frame() %>% rownames_to_column() %>% pivot_longer(cols= !rowname, names_to = c(".value", "quantile"), names_sep = -3)

sum.IV16.IV <-summary(RQ2letterid_I16, covariance = T, R = 50)
Cov.IV16.IV <-as.data.frame(sum.IV16.IV[["Cov"]]) %>% as.data.frame() %>% rownames_to_column() %>% pivot_longer(cols= !rowname, names_to = c(".value", "quantile"), names_sep = -3)

sum.IV17.IV <-summary(RQ2letterid_I17, covariance = T, R = 50)
Cov.IV17.IV <-as.data.frame(sum.IV17.IV[["Cov"]]) %>% as.data.frame() %>% rownames_to_column() %>% pivot_longer(cols= !rowname, names_to = c(".value", "quantile"), names_sep = -3)

sum.IV18.IV <-summary(RQ2letterid_I18, covariance = T, R = 50)
Cov.IV18.IV <-as.data.frame(sum.IV18.IV[["Cov"]]) %>% as.data.frame() %>% rownames_to_column() %>% pivot_longer(cols= !rowname, names_to = c(".value", "quantile"), names_sep = -3)

sum.IV19.IV <-summary(RQ2letterid_I19, covariance = T, R = 50)
Cov.IV19.IV <-as.data.frame(sum.IV19.IV[["Cov"]]) %>% as.data.frame() %>% rownames_to_column() %>% pivot_longer(cols= !rowname, names_to = c(".value", "quantile"), names_sep = -3)

sum.IV20.IV <-summary(RQ2letterid_I20, covariance = T, R = 50)
Cov.IV20.IV <-as.data.frame(sum.IV20.IV[["Cov"]]) %>% as.data.frame() %>% rownames_to_column() %>% pivot_longer(cols= !rowname, names_to = c(".value", "quantile"), names_sep = -3)

sum.IV21.IV <-summary(RQ2letterid_I21, covariance = T, R = 50)
Cov.IV21.IV <-as.data.frame(sum.IV21.IV[["Cov"]]) %>% as.data.frame() %>% rownames_to_column() %>% pivot_longer(cols= !rowname, names_to = c(".value", "quantile"), names_sep = -3)

sum.IV22.IV <-summary(RQ2letterid_I22, covariance = T, R = 50)
Cov.IV22.IV <-as.data.frame(sum.IV22.IV[["Cov"]]) %>% as.data.frame() %>% rownames_to_column() %>% pivot_longer(cols= !rowname, names_to = c(".value", "quantile"), names_sep = -3)

sum.IV23.IV <-summary(RQ2letterid_I23, covariance = T, R = 50)
Cov.IV23.IV <-as.data.frame(sum.IV23.IV[["Cov"]]) %>% as.data.frame() %>% rownames_to_column() %>% pivot_longer(cols= !rowname, names_to = c(".value", "quantile"), names_sep = -3)

sum.IV24.IV <-summary(RQ2letterid_I24, covariance = T, R = 50)
Cov.IV24.IV <-as.data.frame(sum.IV24.IV[["Cov"]]) %>% as.data.frame() %>% rownames_to_column() %>% pivot_longer(cols= !rowname, names_to = c(".value", "quantile"), names_sep = -3)

sum.IV25.IV<-summary(RQ2letterid_I25, covariance = T, R = 50)
Cov.IV25.IV <-as.data.frame(sum.IV25.IV[["Cov"]]) %>% as.data.frame() %>% rownames_to_column() %>% pivot_longer(cols= !rowname, names_to = c(".value", "quantile"), names_sep = -3)

#combine all COV matrices into one giant thing
all.cov.IV <- Cov.IV1.IV %>% full_join(Cov.IV2.IV) %>% full_join(Cov.IV3.IV) %>% full_join(Cov.IV4.IV) %>% full_join(Cov.IV5.IV) %>% full_join(Cov.IV6.IV) %>% full_join(Cov.IV7.IV) %>% full_join(Cov.IV8.IV) %>% full_join(Cov.IV9.IV) %>% full_join(Cov.IV10.IV) %>% full_join(Cov.IV11.IV) %>% full_join(Cov.IV12.IV) %>% full_join(Cov.IV13.IV) %>% full_join(Cov.IV14.IV) %>% full_join(Cov.IV15.IV)  %>% full_join(Cov.IV16.IV) %>% full_join(Cov.IV17.IV) %>% full_join(Cov.IV18.IV) %>% full_join(Cov.IV19.IV) %>% full_join(Cov.IV20.IV) %>% full_join(Cov.IV21.IV) %>% full_join(Cov.IV22.IV) %>% full_join(Cov.IV23.IV) %>% full_join(Cov.IV24.IV) %>% full_join(Cov.IV25.IV) %>% filter(rowname != "reStruct1") %>% filter(rowname != "scale") %>% select(-c("reStruct1.", "scale."))



#get avergaes per variable and per quantile # need to resoirt the rowname variable to preserve original order
cov_pool.IV <- all.cov.IV  %>% group_by(rowname, quantile) %>% summarise(across(everything(), mean)) %>% ungroup() %>% arrange(factor(rowname, levels = (c("(Intercept)", "treatment", "LW_Factor_1", "PV_Factor_1", "P201", "P301", "P501", "P601", "P901", "treatment:LW_Factor_1", "treatment:PV_Factor_1"))))
print(cov_pool.IV)

#quantify uncertainty due to missing data
  #VCOVmissing.data=∑mi=1(bi−bpool)T(bi−bpool)m−1
#calculate the difference for each coefficient from the pooled coefficient
coef.dev.IV1.IV <- I1.coef.IV[2:12] - coef_pool.IV[2:12] 
coef.dev.IV1.IV <- coef.dev.IV1.IV %>% mutate(quantile = seq(0.1, 0.9, 0.1))

coef.dev.IV2.IV <- I2.coef.IV[2:12] - coef_pool.IV[2:12] 
coef.dev.IV2.IV <- coef.dev.IV2.IV %>% mutate(quantile = seq(0.1, 0.9, 0.1))

coef.dev.IV3.IV <- I3.coef.IV[2:12] - coef_pool.IV[2:12] 
coef.dev.IV3.IV <- coef.dev.IV3.IV %>% mutate(quantile = seq(0.1, 0.9, 0.1))

coef.dev.IV4.IV <- I4.coef.IV[2:12] - coef_pool.IV[2:12] 
coef.dev.IV4.IV <- coef.dev.IV4.IV %>% mutate(quantile = seq(0.1, 0.9, 0.1))

coef.dev.IV5.IV <- I5.coef.IV[2:12] - coef_pool.IV[2:12] 
coef.dev.IV5.IV <- coef.dev.IV5.IV %>% mutate(quantile = seq(0.1, 0.9, 0.1))

coef.dev.IV6.IV <- I6.coef.IV[2:12] - coef_pool.IV[2:12] 
coef.dev.IV6.IV <- coef.dev.IV6.IV %>% mutate(quantile = seq(0.1, 0.9, 0.1))

coef.dev.IV7.IV <- I7.coef.IV[2:12] - coef_pool.IV[2:12] 
coef.dev.IV7.IV <- coef.dev.IV7.IV %>% mutate(quantile = seq(0.1, 0.9, 0.1))

coef.dev.IV8.IV <- I8.coef.IV[2:12] - coef_pool.IV[2:12] 
coef.dev.IV8.IV <- coef.dev.IV8.IV %>% mutate(quantile = seq(0.1, 0.9, 0.1))

coef.dev.IV9.IV <- I9.coef.IV[2:12] - coef_pool.IV[2:12] 
coef.dev.IV9.IV <- coef.dev.IV9.IV %>% mutate(quantile = seq(0.1, 0.9, 0.1))

coef.dev.IV10.IV <- I10.coef.IV[2:12] - coef_pool.IV[2:12] 
coef.dev.IV10.IV <- coef.dev.IV10.IV %>% mutate(quantile = seq(0.1, 0.9, 0.1))

coef.dev.IV11.IV <- I11.coef.IV[2:12] - coef_pool.IV[2:12] 
coef.dev.IV11.IV <- coef.dev.IV11.IV %>% mutate(quantile = seq(0.1, 0.9, 0.1))

coef.dev.IV12.IV <- I12.coef.IV[2:12] - coef_pool.IV[2:12] 
coef.dev.IV12.IV <- coef.dev.IV12.IV %>% mutate(quantile = seq(0.1, 0.9, 0.1))

coef.dev.IV13.IV <- I13.coef.IV[2:12] - coef_pool.IV[2:12] 
coef.dev.IV13.IV <- coef.dev.IV13.IV %>% mutate(quantile = seq(0.1, 0.9, 0.1))

coef.dev.IV14.IV <- I14.coef.IV[2:12] - coef_pool.IV[2:12] 
coef.dev.IV14.IV <- coef.dev.IV14.IV %>% mutate(quantile = seq(0.1, 0.9, 0.1))

coef.dev.IV15.IV <- I15.coef.IV[2:12] - coef_pool.IV[2:12] 
coef.dev.IV15.IV <- coef.dev.IV15.IV %>% mutate(quantile = seq(0.1 ,0.9, 0.1))

coef.dev.IV16.IV <- I16.coef.IV[2:12] - coef_pool.IV[2:12] 
coef.dev.IV16.IV <- coef.dev.IV16.IV %>% mutate(quantile = seq(0.1, 0.9, 0.1))

coef.dev.IV17.IV <- I17.coef.IV[2:12] - coef_pool.IV[2:12] 
coef.dev.IV17.IV <- coef.dev.IV17.IV %>% mutate(quantile = seq(0.1, 0.9, 0.1))

coef.dev.IV18.IV <- I18.coef.IV[2:12] - coef_pool.IV[2:12] 
coef.dev.IV18.IV <- coef.dev.IV18.IV %>% mutate(quantile = seq(0.1, 0.9, 0.1))

coef.dev.IV19.IV <- I19.coef.IV[2:12] - coef_pool.IV[2:12] 
coef.dev.IV19.IV <- coef.dev.IV19.IV %>% mutate(quantile = seq(0.1, 0.9, 0.1))

coef.dev.IV20.IV <- I20.coef.IV[2:12] - coef_pool.IV[2:12] 
coef.dev.IV20.IV <- coef.dev.IV20.IV %>% mutate(quantile = seq(0.1, 0.9, 0.1))

coef.dev.IV21.IV <- I21.coef.IV[2:12] - coef_pool.IV[2:12] 
coef.dev.IV21.IV <- coef.dev.IV21.IV %>% mutate(quantile = seq(0.1, 0.9, 0.1))

coef.dev.IV22.IV <- I22.coef.IV[2:12] - coef_pool.IV[2:12] 
coef.dev.IV22.IV <- coef.dev.IV22.IV %>% mutate(quantile = seq(0.1, 0.9, 0.1))

coef.dev.IV23.IV <- I23.coef.IV[2:12] - coef_pool.IV[2:12] 
coef.dev.IV23.IV <- coef.dev.IV23.IV %>% mutate(quantile = seq(0.1, 0.9, 0.1))

coef.dev.IV24.IV <- I24.coef.IV[2:12] - coef_pool.IV[2:12] 
coef.dev.IV24.IV <- coef.dev.IV24.IV %>% mutate(quantile = seq(0.1, 0.9, 0.1))

coef.dev.IV25.IV <- I25.coef.IV[2:12] - coef_pool.IV[2:12] 
coef.dev.IV25.IV <- coef.dev.IV25.IV %>% mutate(quantile = seq(0.1 ,0.9, 0.1))

#combine all coef dev matrices into one giant thing
all.coef.dev.IV <- coef.dev.IV1.IV %>% full_join(coef.dev.IV2.IV)  %>% full_join(coef.dev.IV3.IV) %>% full_join(coef.dev.IV4.IV) %>% full_join(coef.dev.IV5.IV) %>% full_join(coef.dev.IV6.IV) %>% full_join(coef.dev.IV7.IV) %>% full_join(coef.dev.IV8.IV) %>% full_join(coef.dev.IV9.IV) %>% full_join(coef.dev.IV10.IV) %>% full_join(coef.dev.IV11.IV) %>% full_join(coef.dev.IV12.IV) %>% full_join(coef.dev.IV13.IV) %>% full_join(coef.dev.IV14.IV) %>% full_join(coef.dev.IV15.IV) %>% full_join(coef.dev.IV16.IV) %>% full_join(coef.dev.IV17.IV) %>% full_join(coef.dev.IV18.IV) %>% full_join(coef.dev.IV19.IV) %>% full_join(coef.dev.IV20.IV) %>% full_join(coef.dev.IV21.IV) %>% full_join(coef.dev.IV22.IV) %>% full_join(coef.dev.IV23.IV) %>% full_join(coef.dev.IV24.IV) %>% full_join(coef.dev.IV25.IV) 

#separate VCOV and ceof.dev by quantile because I am a bad coder
all.coef.dev.q1.IV <- all.coef.dev.IV %>% filter(quantile == 0.1) %>% select (-quantile) %>% as.matrix()
all.coef.dev.q2.IV <- all.coef.dev.IV %>% filter(quantile == 0.2) %>% select (-quantile) %>% as.matrix()
all.coef.dev.q3.IV <- all.coef.dev.IV %>% filter(quantile == "0.3")  %>% select (-quantile) %>% as.matrix() #not sure why it won't give anything with just the numbers
all.coef.dev.q4.IV <- all.coef.dev.IV %>% filter(quantile == 0.4) %>% select (-quantile) %>% as.matrix()
all.coef.dev.q5.IV <- all.coef.dev.IV %>% filter(quantile == 0.5) %>% select (-quantile) %>% as.matrix()
all.coef.dev.q6.IV <- all.coef.dev.IV %>% filter(quantile == 0.6)%>% select (-quantile) %>% as.matrix()
all.coef.dev.q7.IV <- all.coef.dev.IV %>% filter(quantile == "0.7")%>% select (-quantile) %>% as.matrix()
all.coef.dev.q8.IV <- all.coef.dev.IV %>% filter(quantile == 0.8) %>% select (-quantile) %>% as.matrix()
all.coef.dev.q9.IV <- all.coef.dev.IV %>% filter(quantile == 0.9)%>% select (-quantile) %>% as.matrix()

all.cov.q1.IV <- cov_pool.IV %>% filter(quantile == 0.1) %>% select(-c("rowname", "quantile")) %>% as.matrix()
all.cov.q2.IV <- cov_pool.IV %>% filter(quantile == 0.2) %>% select(-c("rowname", "quantile")) %>% as.matrix()
all.cov.q3.IV <- cov_pool.IV %>% filter(quantile == 0.3) %>% select(-c("rowname", "quantile")) %>% as.matrix()
all.cov.q4.IV <- cov_pool.IV %>% filter(quantile == 0.4) %>% select(-c("rowname", "quantile")) %>% as.matrix()
all.cov.q5.IV <- cov_pool.IV %>% filter(quantile == 0.5) %>% select(-c("rowname", "quantile")) %>% as.matrix()
all.cov.q6.IV <- cov_pool.IV %>% filter(quantile == 0.6) %>% select(-c("rowname", "quantile")) %>% as.matrix()
all.cov.q7.IV <- cov_pool.IV %>% filter(quantile == 0.7) %>% select(-c("rowname", "quantile")) %>% as.matrix()
all.cov.q8.IV <- cov_pool.IV %>% filter(quantile == 0.8) %>% select(-c("rowname", "quantile")) %>% as.matrix()
all.cov.q9.IV <- cov_pool.IV %>% filter(quantile == 0.9) %>% select(-c("rowname", "quantile")) %>% as.matrix()

#multiply matrix by its inverse, do this separately per quantile
coef.dev2.q1.IV <- t(all.coef.dev.q1.IV) %*% all.coef.dev.q1.IV
coef.dev2.q2.IV <- t(all.coef.dev.q2.IV) %*% all.coef.dev.q2.IV
coef.dev2.q3.IV <- t(all.coef.dev.q3.IV) %*% all.coef.dev.q3.IV
coef.dev2.q4.IV <- t(all.coef.dev.q4.IV) %*% all.coef.dev.q4.IV
coef.dev2.q5.IV <- t(all.coef.dev.q5.IV) %*% all.coef.dev.q5.IV
coef.dev2.q6.IV <- t(all.coef.dev.q6.IV) %*% all.coef.dev.q6.IV
coef.dev2.q7.IV <- t(all.coef.dev.q7.IV) %*% all.coef.dev.q7.IV
coef.dev2.q8.IV <- t(all.coef.dev.q8.IV) %*% all.coef.dev.q8.IV
coef.dev2.q9.IV <- t(all.coef.dev.q9.IV) %*% all.coef.dev.q9.IV

#divide the sum of the above by #of imputations - 1 (14). to get missing data number of imputations error
mdni.error.q1.IV <- coef.dev2.q1.IV / 24
mdni.error.q2.IV <- coef.dev2.q2.IV / 24
mdni.error.q3.IV <- coef.dev2.q3.IV / 24
mdni.error.q4.IV <- coef.dev2.q4.IV / 24
mdni.error.q5.IV <- coef.dev2.q5.IV / 24
mdni.error.q6.IV <- coef.dev2.q6.IV / 24
mdni.error.q7.IV <- coef.dev2.q7.IV / 24
mdni.error.q8.IV <- coef.dev2.q8.IV / 24
mdni.error.q9.IV <- coef.dev2.q9.IV / 24

#divide the mdni  by total number of imputations to get error due to finite sampling
mdfs.error.q1.IV <- mdni.error.q1.IV / 25
mdfs.error.q2.IV <- mdni.error.q2.IV / 25
mdfs.error.q3.IV <- mdni.error.q3.IV / 25
mdfs.error.q4.IV <- mdni.error.q4.IV / 25
mdfs.error.q5.IV <- mdni.error.q5.IV / 25
mdfs.error.q6.IV <- mdni.error.q6.IV / 25
mdfs.error.q7.IV <- mdni.error.q7.IV / 25
mdfs.error.q8.IV <- mdni.error.q8.IV / 25
mdfs.error.q9.IV <- mdni.error.q9.IV / 25

#add the three error components to get total error
total.error.q1.IV <- all.cov.q1.IV + mdni.error.q1.IV + mdfs.error.q1.IV
total.error.q2.IV <- all.cov.q2.IV + mdni.error.q2.IV + mdfs.error.q2.IV
total.error.q3.IV <- all.cov.q3.IV + mdni.error.q3.IV + mdfs.error.q3.IV
total.error.q4.IV <- all.cov.q4.IV + mdni.error.q4.IV + mdfs.error.q4.IV
total.error.q5.IV <- all.cov.q5.IV + mdni.error.q5.IV + mdfs.error.q5.IV
total.error.q6.IV <- all.cov.q6.IV + mdni.error.q6.IV + mdfs.error.q6.IV
total.error.q7.IV <- all.cov.q7.IV + mdni.error.q7.IV + mdfs.error.q7.IV
total.error.q8.IV <- all.cov.q8.IV + mdni.error.q8.IV + mdfs.error.q8.IV
total.error.q9.IV <- all.cov.q9.IV + mdni.error.q9.IV + mdfs.error.q9.IV

fmi.q1.IV <- (diag(mdni.error.q1.IV) + diag(mdfs.error.q1.IV)) / diag(total.error.q1.IV)
fmi.q2.IV <- (diag(mdni.error.q2.IV) + diag(mdfs.error.q2.IV)) / diag(total.error.q2.IV)
fmi.q3.IV <- (diag(mdni.error.q3.IV) + diag(mdfs.error.q3.IV)) / diag(total.error.q3.IV)
fmi.q4.IV <- (diag(mdni.error.q4.IV) + diag(mdfs.error.q4.IV)) / diag(total.error.q4.IV)
fmi.q5.IV <- (diag(mdni.error.q5.IV) + diag(mdfs.error.q5.IV)) / diag(total.error.q5.IV)
fmi.q6.IV <- (diag(mdni.error.q6.IV) + diag(mdfs.error.q6.IV)) / diag(total.error.q6.IV)
fmi.q7.IV <- (diag(mdni.error.q7.IV) + diag(mdfs.error.q7.IV)) / diag(total.error.q7.IV)
fmi.q8.IV <- (diag(mdni.error.q8.IV) + diag(mdfs.error.q8.IV)) / diag(total.error.q8.IV)
fmi.q9.IV <- (diag(mdni.error.q9.IV) + diag(mdfs.error.q9.IV)) / diag(total.error.q9.IV)

#then calculate the appropriate degrees of freedom
#dfimputation=1/1m−1FMI2+1(1−FMI)(dfcomplete+1dfcomplete+3)dfcomplete

df_com.IV <- sum.IV1.IV$rdf %>% as.numeric() #same for all quantiles

v.q1.IV <- (24) / fmi.q1.IV^2
vhat.q1.IV <- (1 - fmi.q1.IV) * ((df_com.IV + 1) / (df_com.IV + 3)) * df_com.IV
df_imp.q1.IV <- (v.q1.IV^(-1) + vhat.q1.IV^(-1))^(-1)

v.q2.IV <- (24) / fmi.q2.IV^2
vhat.q2.IV <- (1 - fmi.q2.IV) * ((df_com.IV + 1) / (df_com.IV + 3)) * df_com.IV
df_imp.q2.IV <- (v.q2.IV^(-1) + vhat.q2.IV^(-1))^(-1)

v.q3.IV <- (24) / fmi.q3.IV^2
vhat.q3.IV <- (1 - fmi.q3.IV) * ((df_com.IV + 1) / (df_com.IV + 3)) * df_com.IV
df_imp.q3.IV <- (v.q3.IV^(-1) + vhat.q3.IV^(-1))^(-1)

v.q4.IV <- (24) / fmi.q4.IV^2
vhat.q4.IV <- (1 - fmi.q4.IV) * ((df_com.IV + 1) / (df_com.IV + 3)) * df_com.IV
df_imp.q4.IV <- (v.q4.IV^(-1) + vhat.q4.IV^(-1))^(-1)

v.q5.IV <- (24) / fmi.q5.IV^2
vhat.q5.IV <- (1 - fmi.q5.IV) * ((df_com.IV + 1) / (df_com.IV + 3)) * df_com.IV
df_imp.q5.IV <- (v.q5.IV^(-1) + vhat.q5.IV^(-1))^(-1)

v.q6.IV <- (24) / fmi.q6.IV^2
vhat.q6.IV <- (1 - fmi.q6.IV) * ((df_com.IV + 1) / (df_com.IV + 3)) * df_com.IV
df_imp.q6.IV <- (v.q6.IV^(-1) + vhat.q6.IV^(-1))^(-1)

v.q7.IV <- (24) / fmi.q7.IV^2
vhat.q7.IV <- (1 - fmi.q7.IV) * ((df_com.IV + 1) / (df_com.IV + 3)) * df_com.IV
df_imp.q7.IV <- (v.q7.IV^(-1) + vhat.q4.IV^(-1))^(-1)

v.q8.IV <- (24) / fmi.q8.IV^2
vhat.q8.IV <- (1 - fmi.q8.IV) * ((df_com.IV + 1) / (df_com.IV + 3)) * df_com.IV
df_imp.q8.IV <- (v.q8.IV^(-1) + vhat.q8.IV^(-1))^(-1)

v.q9.IV <- (24) / fmi.q9.IV^2
vhat.q9.IV <- (1 - fmi.q9.IV) * ((df_com.IV + 1) / (df_com.IV + 3)) * df_com.IV
df_imp.q9.IV <- (v.q9.IV^(-1) + vhat.q9.IV^(-1))^(-1)

#now get the SE, t, and p values
se.pool.q1.IV <- sqrt(diag(total.error.q1.IV))
t.pool.q1.IV <- coef_pool.IV[1,2:12] / se.pool.q1.IV 
t.pool.q1.IV.abs <- abs(t.pool.q1.IV) %>% as.matrix()
(p.pool.q1.IV <- 2 * pt(q = t.pool.q1.IV.abs, df = df_imp.q1.IV, lower.tail = F))

se.pool.q2.IV <- sqrt(diag(total.error.q2.IV))
t.pool.q2.IV <- coef_pool.IV[2,2:12] / se.pool.q2.IV 
t.pool.q2.IV.abs <- abs(t.pool.q2.IV) %>% as.matrix()
(p.pool.q2.IV <- 2 * pt(q = t.pool.q2.IV.abs, df = df_imp.q2.IV, lower.tail = F))

se.pool.q3.IV <- sqrt(diag(total.error.q3.IV))
t.pool.q3.IV <- coef_pool.IV[3,2:12] / se.pool.q3.IV 
t.pool.q3.IV.abs <- abs(t.pool.q3.IV) %>% as.matrix()
(p.pool.q3.IV <- 2 * pt(q = t.pool.q3.IV.abs, df = df_imp.q3.IV, lower.tail = F))

se.pool.q4.IV <- sqrt(diag(total.error.q4.IV))
t.pool.q4.IV <- coef_pool.IV[4,2:12] / se.pool.q4.IV 
t.pool.q4.IV.abs <- abs(t.pool.q4.IV) %>% as.matrix()
(p.pool.q4.IV <- 2 * pt(q = t.pool.q4.IV.abs, df = df_imp.q4.IV, lower.tail = F))

se.pool.q5.IV <- sqrt(diag(total.error.q5.IV))
t.pool.q5.IV <- coef_pool.IV[5,2:12] / se.pool.q5.IV
t.pool.q5.IV.abs <- abs(t.pool.q5.IV) %>% as.matrix()
(p.pool.q5.IV <- 2 * pt(q = t.pool.q5.IV.abs, df = df_imp.q5.IV, lower.tail = F))

se.pool.q6.IV <- sqrt(diag(total.error.q6.IV))
t.pool.q6.IV <- coef_pool.IV[6,2:12] / se.pool.q6.IV 
t.pool.q6.IV.abs <- abs(t.pool.q6.IV) %>% as.matrix()
(p.pool.q6.IV <- 2 * pt(q = t.pool.q6.IV.abs, df = df_imp.q6.IV, lower.tail = F))

se.pool.q7.IV <- sqrt(diag(total.error.q7.IV))
t.pool.q7.IV <- coef_pool.IV[7,2:12] / se.pool.q7.IV 
t.pool.q7.IV.abs <- abs(t.pool.q7.IV) %>% as.matrix()
(p.pool.q7.IV <- 2 * pt(q = t.pool.q7.IV.abs, df = df_imp.q7.IV, lower.tail = F))

se.pool.q8.IV <- sqrt(diag(total.error.q8.IV))
t.pool.q8.IV <- coef_pool.IV[8,2:12] / se.pool.q8.IV 
t.pool.q8.IV.abs <- abs(t.pool.q8.IV) %>% as.matrix()
(p.pool.q8.IV <- 2 * pt(q = t.pool.q8.IV.abs, df = df_imp.q8.IV, lower.tail = F))

se.pool.q9.IV <- sqrt(diag(total.error.q9.IV))
t.pool.q9.IV <- coef_pool.IV[9,2:12] / se.pool.q9.IV 
t.pool.q9.IV.abs <- abs(t.pool.q9.IV) %>% as.matrix()
(p.pool.q9.IV <- 2 * pt(q = t.pool.q9.IV.abs, df = df_imp.q9.IV, lower.tail = F))

crit.pool.q1.IV <- qt(p = 1 - (.05 / 2), df = df_imp.q1.IV, lower.tail = T)
hw.pool.q1.IV <- se.pool.q1.IV * crit.pool.q1.IV
ci_pool.q1.IV <- cbind("lwr" = coef_pool.IV[1,2:12] - hw.pool.q1.IV, "upr" = coef_pool.IV[1,2:12] + hw.pool.q1.IV) %>% mutate(quantile = "0.1")

crit.pool.q2.IV <- qt(p = 1 - (.05 / 2), df = df_imp.q2.IV, lower.tail = T)
hw.pool.q2.IV <- se.pool.q2.IV * crit.pool.q2.IV
ci_pool.q2.IV <- cbind("lwr" = coef_pool.IV[2,2:12] - hw.pool.q2.IV, "upr" = coef_pool.IV[2,2:12] + hw.pool.q2.IV) %>% mutate(quantile = "0.2")

crit.pool.q3.IV <- qt(p = 1 - (.05 / 2), df = df_imp.q3.IV, lower.tail = T)
hw.pool.q3.IV <- se.pool.q3.IV * crit.pool.q3.IV
ci_pool.q3.IV <- cbind("lwr" = coef_pool.IV[3,2:12] - hw.pool.q3.IV, "upr" = coef_pool.IV[3,2:12] + hw.pool.q3.IV) %>% mutate(quantile = "0.3")

crit.pool.q4.IV <- qt(p = 1 - (.05 / 2), df = df_imp.q4.IV, lower.tail = T)
hw.pool.q4.IV <- se.pool.q4.IV * crit.pool.q4.IV
ci_pool.q4.IV <- cbind("lwr" = coef_pool.IV[4,2:12] - hw.pool.q4.IV, "upr" = coef_pool.IV[4,2:12] + hw.pool.q4.IV) %>% mutate(quantile = "0.4")

crit.pool.q5.IV <- qt(p = 1 - (.05 / 2), df = df_imp.q5.IV, lower.tail = T)
hw.pool.q5.IV <- se.pool.q5.IV * crit.pool.q5.IV
ci_pool.q5.IV <- cbind("lwr" = coef_pool.IV[5,2:12] - hw.pool.q5.IV, "upr" = coef_pool.IV[5,2:12] + hw.pool.q5.IV) %>% mutate(quantile = "0.5")

crit.pool.q6.IV <- qt(p = 1 - (.05 / 2), df = df_imp.q6.IV, lower.tail = T)
hw.pool.q6.IV <- se.pool.q6.IV * crit.pool.q6.IV

ci_pool.q6.IV <- cbind("lwr" = coef_pool.IV[6,2:12] - hw.pool.q6.IV, "upr" = coef_pool.IV[6,2:12] + hw.pool.q6.IV) %>% mutate(quantile = "0.6")

crit.pool.q7.IV <- qt(p = 1 - (.05 / 2), df = df_imp.q7.IV, lower.tail = T)
hw.pool.q7.IV <- se.pool.q7.IV * crit.pool.q7.IV
ci_pool.q7.IV <- cbind("lwr" = coef_pool.IV[7,2:12] - hw.pool.q7.IV, "upr" = coef_pool.IV[7,2:12] + hw.pool.q7.IV) %>% mutate(quantile = "0.7")

crit.pool.q8.IV <- qt(p = 1 - (.05 / 2), df = df_imp.q8.IV, lower.tail = T)
hw.pool.q8.IV <- se.pool.q8.IV * crit.pool.q8.IV
ci_pool.q8.IV <- cbind("lwr" = coef_pool.IV[8,2:12] - hw.pool.q8.IV, "upr" = coef_pool.IV[8,2:12] + hw.pool.q8.IV) %>% mutate(quantile = "0.8")

crit.pool.q9.IV <- qt(p = 1 - (.05 / 2), df = df_imp.q9.IV, lower.tail = T)
hw.pool.q9.IV <- se.pool.q9.IV * crit.pool.q9.IV
ci_pool.q9.IV <- cbind("lwr" = coef_pool.IV[9,2:12] - hw.pool.q9.IV, "upr" = coef_pool.IV[9,2:12] + hw.pool.q9.IV) %>% mutate(quantile = "0.9")

all_ci_IV <- ci_pool.q1.IV %>% rbind(ci_pool.q2.IV) %>% rbind(ci_pool.q3.IV) %>% rbind(ci_pool.q4.IV) %>% rbind(ci_pool.q5.IV) %>% rbind(ci_pool.q6.IV) %>% rbind(ci_pool.q7.IV) %>% rbind(ci_pool.q8.IV) %>% rbind(ci_pool.q9.IV)

##### BH adjustments
p_values_RQ2_LW <- p.pool.q1.IV %>% rbind(p.pool.q2.IV) %>% rbind(p.pool.q3.IV) %>% rbind(p.pool.q4.IV) %>% rbind(p.pool.q5.IV) %>% rbind(p.pool.q6.IV) %>% rbind(p.pool.q7.IV) %>% rbind(p.pool.q8.IV) %>% rbind (p.pool.q9.IV)   
fdrs_RQ2_LW <- p.adjust(p_values_RQ2_LW, method="BH") 
p_values_adj_RQ2_LW <- matrix(unlist(fdrs_RQ2_LW), nrow=9) %>% round(3) %>% as.data.frame()  %>% mutate(quant=c(1, 2, 3, 4, 5, 6, 7, 8, 9))
colnames(p_values_adj_RQ2_LW) <- c("(Intercept)_adj", "treatment_adj", "LW_Factor_1_adj", "PV_Factor_1_adj", "P201_adj", "P301_adj", "P501_adj", "P601_adj", "P901_adj", "treatment:LW_Factor_1_adj", "treatment:PV_Factor_1_adj", "Quantile")
p_values_RQ2_LW_wquant <- p_values_RQ2_LW %>% as.data.frame() %>% mutate(quant=c(1, 2, 3, 4, 5, 6, 7, 8, 9))

p_values_RQ2_LW_all <- p_values_RQ2_LW_wquant %>% cbind(p_values_adj_RQ2_LW) %>% select(1, 13, 2, 14, 3, 15, 4, 16, 5, 17, 6, 18, 7, 19, 8, 20, 9, 21, 10, 22, 11, 23)  %>% round(3)

(all_new_VI <- all_ci_IV %>% full_join(coef_pool.IV, join_by(quantile == rowname)))

## get the statistically significant effects of interest from previous line (I'm doing this by hand)
#treatment = not statistically significant
#LW pre test skills = statistically significant for all quartiles
#PV pre test skills = NOT statistically significant 
#interaction of treatment with LW pre = NOT statistically significant at all quartiles
#interaction of treatment with PV pre = NOT statistically significant at all quartiles
```
```{r for graphs}
library(jtools)
(RQ1_PV_intercept_plot <- ggplot(all_new, aes(x=rowname))  + geom_ribbon(aes(ymin = `lwr.(Intercept)`, 
                                    ymax = `upr.(Intercept)`, group = 1), alpha = 0.5) +
  geom_line(aes(y = `(Intercept)`, group = 1)) +
  theme_apa())

(RQ1_PV_treatmenteffect_plot <- ggplot(all_new, aes(x=rowname))  + geom_ribbon(aes(ymin = lwr.treatment, 
                                    ymax = upr.treatment, group = 1), alpha = 0.5) +
  geom_line(aes(y = treatment, group = 1)) +
  theme_apa())

(RQ1_PV_PVpretest_plot <- ggplot(all_new, aes(x=rowname))  + geom_ribbon(aes(ymin = lwr.PV_Factor_1, 
                                    ymax = upr.PV_Factor_1, group = 1), alpha = 0.5) +
  geom_line(aes(y = PV_Factor_1, group = 1)) +
  theme_apa())

(RQ2_PV_intercept_plot <- ggplot(all_new_I, aes(x=rowname))  + geom_ribbon(aes(ymin = `lwr.(Intercept)`, 
                                    ymax = `upr.(Intercept)`, group = 1), alpha = 0.5) +
  geom_line(aes(y = `(Intercept)`, group = 1)) +
  theme_apa())

(RQ2_PV_treatmenteffect_plot <- ggplot(all_new_I, aes(x=rowname))  + geom_ribbon(aes(ymin = lwr.treatment, 
                                    ymax = upr.treatment, group = 1), alpha = 0.5) +
  geom_line(aes(y = treatment, group = 1)) +
  theme_apa())

(RQ2_PV_PVpretest_plot <- ggplot(all_new_I, aes(x=rowname))  + geom_ribbon(aes(ymin = lwr.PV_Factor_1, 
                                    ymax = upr.PV_Factor_1, group = 1), alpha = 0.5) +
  geom_line(aes(y = PV_Factor_1, group = 1)) +
  theme_apa())


(RQ1_LW_intercept_plot <- ggplot(all_new_V, aes(x=rowname))  + geom_ribbon(aes(ymin = `lwr.(Intercept)`, 
                                    ymax = `upr.(Intercept)`, group = 1), alpha = 0.5) +
  geom_line(aes(y = `(Intercept)`, group = 1)) +
  theme_apa())

(RQ1_LW_treatmenteffect_plot <- ggplot(all_new_V, aes(x=rowname))  + geom_ribbon(aes(ymin = lwr.treatment, 
                                    ymax = upr.treatment, group = 1), alpha = 0.5) +
  geom_line(aes(y = treatment, group = 1)) +
  theme_apa())

(RQ1_LW_LWpretest_plot <- ggplot(all_new_V, aes(x=rowname))  + geom_ribbon(aes(ymin = lwr.LW_Factor_1, 
                                    ymax = upr.LW_Factor_1, group = 1), alpha = 0.5) +
  geom_line(aes(y = LW_Factor_1, group = 1)) +
  theme_apa())


(RQ2_LW_intercept_plot <- ggplot(all_new_VI, aes(x=quantile))  + geom_ribbon(aes(ymin = `lwr.(Intercept)`, 
                                    ymax = `upr.(Intercept)`, group = 1), alpha = 0.5) +
  geom_line(aes(y = `(Intercept)`, group = 1)) +
  theme_apa())

(RQ2_LW_treatmenteffect_plot <- ggplot(all_new_VI, aes(x=quantile))  + geom_ribbon(aes(ymin = lwr.treatment, 
                                    ymax = upr.treatment, group = 1), alpha = 0.5) +
  geom_line(aes(y = treatment, group = 1)) +
  theme_apa())

(RQ2_LW_LWpretest_plot <- ggplot(all_new_VI, aes(x=quantile))  + geom_ribbon(aes(ymin = lwr.LW_Factor_1, 
                                    ymax = upr.LW_Factor_1, group = 1), alpha = 0.5) +
  geom_line(aes(y = LW_Factor_1, group = 1)) +
  theme_apa())



```
```{r SensitivityOLS}
library(lme4)
library(mitml)

QR_data_imp_Sens_1 <- QR_data_imp_1 %>% long2mitml.list(split = ".imp") 


RQ1vocab_OLS <- with(QR_data_imp_Sens_1, lme4::lmer(PV_Factor_3 ~ treatment + P201 + P301 + P501 + P601 + P901 + LW_Factor_1 + PV_Factor_1 + (1| sID/TID)))
(RQ1vocab_OLS_est <- testEstimates(RQ1vocab_OLS, extra.pars = T))

RQ1vocab_OLS_p <- with(QR_data_imp_Sens_1, lme4::lmer(PV_Factor_3 ~ treatment*(P201 + P501 + P601 + P901) + P301 + LW_Factor_1 + PV_Factor_1 + (1| sID/TID)))
(RQ1vocab_OLS_p_est <- testEstimates(RQ1vocab_OLS_p, extra.pars = T))

RQ1lwid_OLS <- with(QR_data_imp_Sens_1, lme4::lmer(LW_Factor_3 ~ treatment + P201 + P301 + P501 + P601 + P901 + LW_Factor_1 + PV_Factor_1 + (1| sID/TID)))
(RQ1lwid_OLS_est <- testEstimates(RQ1lwid_OLS, extra.pars = T))

RQ1lwid_OLS_p <- with(QR_data_imp_Sens_1, lme4::lmer(LW_Factor_3 ~ treatment*(P201  + P501 + P601 + P901) + P301 + LW_Factor_1 + PV_Factor_1 + (1| sID/TID)))
(RQ1lwid_OLS_p_est <- testEstimates(RQ1lwid_OLS_p, extra.pars = T))


RQ2vocab_OLS <- with(QR_data_imp_Sens_1, lme4::lmer(PV_Factor_3 ~ treatment*(LW_Factor_1 + PV_Factor_1) + P201 + P301 + P501 + P601 + P901 + (1| sID/TID)))
(RQ2vocab_OLS_est <- testEstimates(RQ2vocab_OLS, extra.pars = T))

RQ2vocab_OLS_p <- with(QR_data_imp_Sens_1, lme4::lmer(PV_Factor_3 ~ treatment*(LW_Factor_1 + PV_Factor_1 + P201 + P501 + P601 + P901) + P301  + (1| sID/TID)))
(RQ2vocab_OLS_p_est <- testEstimates(RQ2vocab_OLS_p, extra.pars = T))

RQ2lwid_OLS <- with(QR_data_imp_Sens_1, lme4::lmer(LW_Factor_3 ~ treatment*(LW_Factor_1 + PV_Factor_1) + P201 + P301 + P501 + P601 + P901  + (1| sID/TID)))
(RQ2lwid_OLS_est <- testEstimates(RQ2lwid_OLS, extra.pars = T))

RQ2lwid_OLS_p <- with(QR_data_imp_Sens_1, lme4::lmer(LW_Factor_3 ~ treatment*(LW_Factor_1 + PV_Factor_1 + P201  + P501 + P601 + P901) + P301 + (1| sID/TID)))
(RQ2lwid_OLS_p_est <- testEstimates(RQ2lwid_OLS_p, extra.pars = T))

```
```{r sensitivityProject PV}
RQ1vocab_I_p <- lqmm(PV_Factor_3 ~ treatment*(P201 + P501 + P601 + P901) + P301 + LW_Factor_1 + PV_Factor_1, random = ~1, group = TID, tau = c(1:9/10), data = QR_data_imp_I1, control = lqmmControl(method ="df", UP_max_iter = 1000, UP_tol = 1e-6))
RQ1vocab_I2_p <- lqmm(PV_Factor_3 ~ treatment*(P201  + P501 + P601 + P901) + P301 + LW_Factor_1 + PV_Factor_1, random = ~1, group = TID, tau = c(1:9/10), data = QR_data_imp_I2, control = list(UP_max_iter = 1000, UP_tol = 1e-6))
RQ1vocab_I3_p <- lqmm(PV_Factor_3 ~ treatment*(P201  + P501 + P601 + P901) + P301 + LW_Factor_1 + PV_Factor_1, random = ~1, group = TID, tau = c(1:9/10), data = QR_data_imp_I3, control = list(UP_max_iter = 1000, UP_tol = 1e-6))
RQ1vocab_I4_p <- lqmm(PV_Factor_3 ~ treatment*(P201  + P501 + P601 + P901) + P301 + LW_Factor_1 + PV_Factor_1, random = ~1, group = TID, tau = c(1:9/10), data = QR_data_imp_I4, control = list(LP_max_iter = 1000, lP_tol = 1e-6))
RQ1vocab_I5_p <- lqmm(PV_Factor_3 ~ treatment*(P201  + P501 + P601 + P901) + P301 + LW_Factor_1 + PV_Factor_1, random = ~1, group = TID, tau = c(1:9/10), data = QR_data_imp_I5, control = list(LP_max_iter = 1000, LP_tol = 1e-6))
RQ1vocab_I6_p <- lqmm(PV_Factor_3 ~ treatment*(P201  + P501 + P601 + P901) + P301 + LW_Factor_1 + PV_Factor_1, random = ~1, group = TID, tau = c(1:9/10), data = QR_data_imp_I6, control = list(UP_max_iter = 1000, UP_tol = 1e-7))
RQ1vocab_I7_p <- lqmm(PV_Factor_3 ~ treatment*(P201  + P501 + P601 + P901) + P301 + LW_Factor_1 + PV_Factor_1, random = ~1, group = TID, tau = c(1:9/10), data = QR_data_imp_I7, control = list(UP_max_iter = 1000, UP_tol = 1e-7))
RQ1vocab_I8_p <- lqmm(PV_Factor_3 ~ treatment*(P201  + P501 + P601 + P901) + P301 + LW_Factor_1 + PV_Factor_1, random = ~1, group = TID, tau = c(1:9/10), data = QR_data_imp_I8, control = list(UP_max_iter = 1000, UP_tol = 1e-6))
RQ1vocab_I9_p <- lqmm(PV_Factor_3 ~ treatment*(P201  + P501 + P601 + P901) + P301 + LW_Factor_1 + PV_Factor_1, random = ~1, group = TID, tau = c(1:9/10), data = QR_data_imp_I9, control = list(UP_max_iter = 1000, UP_tol = 1e-6))
RQ1vocab_I10_p <- lqmm(PV_Factor_3 ~ treatment*(P201  + P501 + P601 + P901) + P301 + LW_Factor_1 + PV_Factor_1, random = ~1, group = TID, tau = c(1:9/10), data = QR_data_imp_I10, control = list(UP_max_iter = 1000, UP_tol = 1e-6))
RQ1vocab_I11_p <- lqmm(PV_Factor_3 ~ treatment*(P201  + P501 + P601 + P901) + P301 + LW_Factor_1 + PV_Factor_1, random = ~1, group = TID, tau = c(1:9/10), data = QR_data_imp_I11, control = list(UP_max_iter = 1000, UP_tol = 1e-7))
RQ1vocab_I12_p <- lqmm(PV_Factor_3 ~ treatment*(P201 + P501 + P601 + P901) + P301 + LW_Factor_1 + PV_Factor_1, random = ~1, group = TID, tau = c(1:9/10), data = QR_data_imp_I12, control = list(UP_max_iter = 1000, UP_tol = 1e-6))
RQ1vocab_I13_p <- lqmm(PV_Factor_3 ~ treatment*(P201  + P501 + P601 + P901) + P301 + LW_Factor_1 + PV_Factor_1, random = ~1, group = TID, tau = c(1:9/10), data = QR_data_imp_I13, control = list(UP_max_iter = 1000, UP_tol = 1e-7))
RQ1vocab_I14_p <- lqmm(PV_Factor_3 ~ treatment*(P201  + P501 + P601 + P901) + P301 + LW_Factor_1 + PV_Factor_1, random = ~1, group = TID, tau = c(1:9/10), data = QR_data_imp_I14, control = list(UP_max_iter = 1000, UP_tol = 1e-6))
RQ1vocab_I15_p <- lqmm(PV_Factor_3 ~ treatment*(P201  + P501 + P601 + P901) + P301 + LW_Factor_1 + PV_Factor_1, random = ~1, group = TID, tau = c(1:9/10), data = QR_data_imp_I15, control = list(UP_max_iter = 1000, UP_tol = 1e-6))
RQ1vocab_I16_p <- lqmm(PV_Factor_3 ~ treatment*(P201  + P501 + P601 + P901) + P301 + LW_Factor_1 + PV_Factor_1, random = ~1, group = TID, tau = c(1:9/10), data = QR_data_imp_I16, control = list(UP_max_iter = 1000, UP_tol = 1e-6))
RQ1vocab_I17_p <- lqmm(PV_Factor_3 ~ treatment*(P201  + P501 + P601 + P901) + P301 + LW_Factor_1 + PV_Factor_1, random = ~1, group = TID, tau = c(1:9/10), data = QR_data_imp_I17, control = list(UP_max_iter = 1000, UP_tol = 1e-6))
RQ1vocab_I18_p <- lqmm(PV_Factor_3 ~ treatment*(P201 + P501 + P601 + P901) + P301+ LW_Factor_1 + PV_Factor_1, random = ~1, group = TID, tau = c(1:9/10), data = QR_data_imp_I18, control = list(UP_max_iter = 1000, UP_tol = 1e-6))
RQ1vocab_I19_p <- lqmm(PV_Factor_3 ~ treatment*(P201  + P501 + P601 + P901) + P301 + LW_Factor_1 + PV_Factor_1, random = ~1, group = TID, tau = c(1:9/10), data = QR_data_imp_I19, control = list(UP_max_iter = 1000, UP_tol = 1e-6))
RQ1vocab_I20_p <- lqmm(PV_Factor_3 ~ treatment*(P201 + P501 + P601 + P901) + P301 + LW_Factor_1 + PV_Factor_1, random = ~1, group = TID, tau = c(1:9/10), data = QR_data_imp_I20, control = list(UP_max_iter = 1000, UP_tol = 1e-6))
RQ1vocab_I21_p <- lqmm(PV_Factor_3 ~ treatment*(P201 + P501 + P601 + P901) + P301 + LW_Factor_1 + PV_Factor_1, random = ~1, group = TID, tau = c(1:9/10), data = QR_data_imp_I21, control = list(UP_max_iter = 1000, UP_tol = 1e-6))
RQ1vocab_I22_p <- lqmm(PV_Factor_3 ~ treatment*(P201  + P501 + P601 + P901) + P301 + LW_Factor_1 + PV_Factor_1, random = ~1, group = TID, tau = c(1:9/10), data = QR_data_imp_I22, control = list(UP_max_iter = 1000, UP_tol = 1e-6))
RQ1vocab_I23_p <- lqmm(PV_Factor_3 ~ treatment*(P201  + P501 + P601 + P901) + P301 + LW_Factor_1 + PV_Factor_1, random = ~1, group = TID, tau = c(1:9/10), data = QR_data_imp_I23, control = list(UP_max_iter = 1000, UP_tol = 1e-6))
RQ1vocab_I24_p <- lqmm(PV_Factor_3 ~ treatment*(P201  + P501 + P601 + P901) + P301 + LW_Factor_1 + PV_Factor_1, random = ~1, group = TID, tau = c(1:9/10), data = QR_data_imp_I24, control = list(UP_max_iter = 1000, UP_tol = 1e-6))
RQ1vocab_I25_p <- lqmm(PV_Factor_3 ~ treatment*(P201  + P501 + P601 + P901) + P301 + LW_Factor_1 + PV_Factor_1, random = ~1, group = TID, tau = c(1:9/10), data = QR_data_imp_I25, control = list(UP_max_iter = 1000, UP_tol = 1e-6))

#Extract the regression coefficients for each model
I1_p.coef <- coef.lqmm(RQ1vocab_I_p) %>% t() %>% as.data.frame() %>% rownames_to_column() #transpose and make a datafrome
I2_p.coef <- coef.lqmm(RQ1vocab_I2_p) %>% t() %>% as.data.frame() %>% rownames_to_column()
I3_p.coef <- coef.lqmm(RQ1vocab_I3_p) %>% t() %>% as.data.frame() %>% rownames_to_column()
I4_p.coef <- coef.lqmm(RQ1vocab_I4_p) %>% t() %>% as.data.frame() %>% rownames_to_column()
I5_p.coef <- coef.lqmm(RQ1vocab_I5_p) %>% t() %>% as.data.frame() %>% rownames_to_column()
I6_p.coef <- coef.lqmm(RQ1vocab_I6_p) %>% t() %>% as.data.frame() %>% rownames_to_column()
I7_p.coef <- coef.lqmm(RQ1vocab_I7_p) %>% t() %>% as.data.frame() %>% rownames_to_column()
I8_p.coef <- coef.lqmm(RQ1vocab_I8_p) %>% t() %>% as.data.frame() %>% rownames_to_column()
I9_p.coef <- coef.lqmm(RQ1vocab_I9_p) %>% t() %>% as.data.frame() %>% rownames_to_column()
I10_p.coef <- coef.lqmm(RQ1vocab_I10_p) %>% t() %>% as.data.frame() %>% rownames_to_column()
I11_p.coef <- coef.lqmm(RQ1vocab_I11_p) %>% t() %>% as.data.frame() %>% rownames_to_column()
I12_p.coef <- coef.lqmm(RQ1vocab_I12_p) %>% t() %>% as.data.frame() %>% rownames_to_column()
I13_p.coef <- coef.lqmm(RQ1vocab_I13_p) %>% t() %>% as.data.frame() %>% rownames_to_column()
I14_p.coef <- coef.lqmm(RQ1vocab_I14_p) %>% t() %>% as.data.frame() %>% rownames_to_column()
I15_p.coef <- coef.lqmm(RQ1vocab_I15_p) %>% t() %>% as.data.frame() %>% rownames_to_column()
I16_p.coef <- coef.lqmm(RQ1vocab_I16_p) %>% t() %>% as.data.frame() %>% rownames_to_column()
I17_p.coef <- coef.lqmm(RQ1vocab_I17_p) %>% t() %>% as.data.frame() %>% rownames_to_column()
I18_p.coef <- coef.lqmm(RQ1vocab_I18_p) %>% t() %>% as.data.frame() %>% rownames_to_column()
I19_p.coef <- coef.lqmm(RQ1vocab_I19_p) %>% t() %>% as.data.frame() %>% rownames_to_column()
I20_p.coef <- coef.lqmm(RQ1vocab_I20_p) %>% t() %>% as.data.frame() %>% rownames_to_column()
I21_p.coef <- coef.lqmm(RQ1vocab_I21_p) %>% t() %>% as.data.frame() %>% rownames_to_column()
I22_p.coef <- coef.lqmm(RQ1vocab_I22_p) %>% t() %>% as.data.frame() %>% rownames_to_column()
I23_p.coef <- coef.lqmm(RQ1vocab_I23_p) %>% t() %>% as.data.frame() %>% rownames_to_column()
I24_p.coef <- coef.lqmm(RQ1vocab_I24_p) %>% t() %>% as.data.frame() %>% rownames_to_column()
I25_p.coef <- coef.lqmm(RQ1vocab_I25_p) %>% t() %>% as.data.frame() %>% rownames_to_column()
#combine all to one dataframe
All.coef_p <- I1_p.coef %>% full_join(I2_p.coef) %>% full_join(I3_p.coef) %>% full_join(I4_p.coef) %>% full_join(I5_p.coef) %>% full_join(I6_p.coef) %>% full_join(I7_p.coef) %>% full_join(I8_p.coef) %>% full_join(I9_p.coef) %>% full_join(I10_p.coef) %>% full_join(I11_p.coef) %>% full_join(I12_p.coef) %>% full_join(I13_p.coef) %>% full_join(I14_p.coef) %>% full_join(I15_p.coef) %>% full_join(I16_p.coef) %>% full_join(I1_p.coef) %>% full_join(I18_p.coef) %>% full_join(I19_p.coef) %>% full_join(I20_p.coef) %>% full_join(I21_p.coef) %>% full_join(I22_p.coef) %>% full_join(I23_p.coef) %>% full_join(I24_p.coef) %>% full_join(I25_p.coef)

#average the coefficients
coef_pool_p <- All.coef_p  %>% group_by(rowname) %>% summarise(across(everything(), mean)) %>% ungroup()
print(coef_pool_p)

#Extract covariance matrix for each model
sum.I1_p <-summary(RQ1vocab_I_p, covariance = T, R = 50)
Cov.I1_p <-as.data.frame(sum.I1_p[["Cov"]]) %>% as.data.frame() %>% rownames_to_column() %>% pivot_longer(cols= !rowname, names_to = c(".value", "quantile"), names_sep = -3)

sum.I2_p <-summary(RQ1vocab_I2_p, covariance = T, R = 50) 
Cov.I2_p <-as.data.frame(sum.I2_p[["Cov"]]) %>% as.data.frame() %>% rownames_to_column() %>% pivot_longer(cols= !rowname, names_to = c(".value", "quantile"), names_sep = -3)
 
sum.I3_p <-summary(RQ1vocab_I3_p, covariance = T, R = 50)
Cov.I3_p <-as.data.frame(sum.I3_p[["Cov"]]) %>% as.data.frame() %>% rownames_to_column() %>% pivot_longer(cols= !rowname, names_to = c(".value", "quantile"), names_sep = -3)

sum.I4_p <-summary(RQ1vocab_I4_p, covariance = T, R = 50)
Cov.I4_p <-as.data.frame(sum.I4_p[["Cov"]]) %>% as.data.frame() %>% rownames_to_column() %>% pivot_longer(cols= !rowname, names_to = c(".value", "quantile"), names_sep = -3)

sum.I5_p <-summary(RQ1vocab_I5_p, covariance = T, R = 50)
Cov.I5_p <-as.data.frame(sum.I5_p[["Cov"]]) %>% as.data.frame() %>% rownames_to_column() %>% pivot_longer(cols= !rowname, names_to = c(".value", "quantile"), names_sep = -3)

sum.I6_p <-summary(RQ1vocab_I6_p, covariance = T, R = 50)
Cov.I6_p <-as.data.frame(sum.I6_p[["Cov"]]) %>% as.data.frame() %>% rownames_to_column() %>% pivot_longer(cols= !rowname, names_to = c(".value", "quantile"), names_sep = -3)

sum.I7_p <-summary(RQ1vocab_I7_p, covariance = T, R = 50)
Cov.I7_p <-as.data.frame(sum.I7_p[["Cov"]]) %>% as.data.frame() %>% rownames_to_column() %>% pivot_longer(cols= !rowname, names_to = c(".value", "quantile"), names_sep = -3)

sum.I8_p <-summary(RQ1vocab_I8_p, covariance = T, R = 50)
Cov.I8_p <-as.data.frame(sum.I8_p[["Cov"]]) %>% as.data.frame() %>% rownames_to_column() %>% pivot_longer(cols= !rowname, names_to = c(".value", "quantile"), names_sep = -3)

sum.I9_p <-summary(RQ1vocab_I9_p, covariance = T, R = 50)
Cov.I9_p <-as.data.frame(sum.I9_p[["Cov"]]) %>% as.data.frame() %>% rownames_to_column() %>% pivot_longer(cols= !rowname, names_to = c(".value", "quantile"), names_sep = -3)

sum.I10_p <-summary(RQ1vocab_I10_p, covariance = T, R = 50)
Cov.I10_p <-as.data.frame(sum.I10_p[["Cov"]]) %>% as.data.frame() %>% rownames_to_column() %>% pivot_longer(cols= !rowname, names_to = c(".value", "quantile"), names_sep = -3)

sum.I11_p <-summary(RQ1vocab_I11_p, covariance = T, R = 50)
Cov.I11_p <-as.data.frame(sum.I11_p[["Cov"]]) %>% as.data.frame() %>% rownames_to_column() %>% pivot_longer(cols= !rowname, names_to = c(".value", "quantile"), names_sep = -3)

sum.I12_p <-summary(RQ1vocab_I12_p, covariance = T, R = 50)
Cov.I12_p <-as.data.frame(sum.I12_p[["Cov"]]) %>% as.data.frame() %>% rownames_to_column() %>% pivot_longer(cols= !rowname, names_to = c(".value", "quantile"), names_sep = -3)

sum.I13_p <-summary(RQ1vocab_I13_p, covariance = T, R = 50)
Cov.I13_p <-as.data.frame(sum.I13_p[["Cov"]]) %>% as.data.frame() %>% rownames_to_column() %>% pivot_longer(cols= !rowname, names_to = c(".value", "quantile"), names_sep = -3)

sum.I14_p <-summary(RQ1vocab_I14_p, covariance = T, R = 50)
Cov.I14_p <-as.data.frame(sum.I14_p[["Cov"]]) %>% as.data.frame() %>% rownames_to_column() %>% pivot_longer(cols= !rowname, names_to = c(".value", "quantile"), names_sep = -3)

sum.I15_p <-summary(RQ1vocab_I15_p, covariance = T, R = 50)
Cov.I15_p <-as.data.frame(sum.I15_p[["Cov"]]) %>% as.data.frame() %>% rownames_to_column() %>% pivot_longer(cols= !rowname, names_to = c(".value", "quantile"), names_sep = -3)

sum.I16_p <-summary(RQ1vocab_I16_p, covariance = T, R = 50)
Cov.I16_p <-as.data.frame(sum.I16_p[["Cov"]]) %>% as.data.frame() %>% rownames_to_column() %>% pivot_longer(cols= !rowname, names_to = c(".value", "quantile"), names_sep = -3)

sum.I17_p <-summary(RQ1vocab_I17_p, covariance = T, R = 50)
Cov.I17_p <-as.data.frame(sum.I17_p[["Cov"]]) %>% as.data.frame() %>% rownames_to_column() %>% pivot_longer(cols= !rowname, names_to = c(".value", "quantile"), names_sep = -3)

sum.I18_p <-summary(RQ1vocab_I18_p, covariance = T, R = 50)
Cov.I18_p <-as.data.frame(sum.I18_p[["Cov"]]) %>% as.data.frame() %>% rownames_to_column() %>% pivot_longer(cols= !rowname, names_to = c(".value", "quantile"), names_sep = -3)

sum.I19_p <-summary(RQ1vocab_I19_p, covariance = T, R = 50)
Cov.I19_p <-as.data.frame(sum.I19_p[["Cov"]]) %>% as.data.frame() %>% rownames_to_column() %>% pivot_longer(cols= !rowname, names_to = c(".value", "quantile"), names_sep = -3)

sum.I20_p <-summary(RQ1vocab_I20_p, covariance = T, R = 50)
Cov.I20_p <-as.data.frame(sum.I20_p[["Cov"]]) %>% as.data.frame() %>% rownames_to_column() %>% pivot_longer(cols= !rowname, names_to = c(".value", "quantile"), names_sep = -3)

sum.I21_p <-summary(RQ1vocab_I21_p, covariance = T, R = 50)
Cov.I21_p <-as.data.frame(sum.I21_p[["Cov"]]) %>% as.data.frame() %>% rownames_to_column() %>% pivot_longer(cols= !rowname, names_to = c(".value", "quantile"), names_sep = -3)

sum.I22_p <-summary(RQ1vocab_I22_p, covariance = T, R = 50)
Cov.I22_p <-as.data.frame(sum.I22_p[["Cov"]]) %>% as.data.frame() %>% rownames_to_column() %>% pivot_longer(cols= !rowname, names_to = c(".value", "quantile"), names_sep = -3)

sum.I23_p <-summary(RQ1vocab_I23_p, covariance = T, R = 50)
Cov.I23_p <-as.data.frame(sum.I23_p[["Cov"]]) %>% as.data.frame() %>% rownames_to_column() %>% pivot_longer(cols= !rowname, names_to = c(".value", "quantile"), names_sep = -3)

sum.I24_p <-summary(RQ1vocab_I24_p, covariance = T, R = 50)
Cov.I24_p <-as.data.frame(sum.I24_p[["Cov"]]) %>% as.data.frame() %>% rownames_to_column() %>% pivot_longer(cols= !rowname, names_to = c(".value", "quantile"), names_sep = -3)

sum.I25_p <-summary(RQ1vocab_I25_p, covariance = T, R = 50)
Cov.I25_p <-as.data.frame(sum.I25_p[["Cov"]]) %>% as.data.frame() %>% rownames_to_column() %>% pivot_longer(cols= !rowname, names_to = c(".value", "quantile"), names_sep = -3)

#combine all COV matrices into one giant thing
all.cov_p <- Cov.I1_p %>% full_join(Cov.I2_p) %>% full_join(Cov.I3_p) %>% full_join(Cov.I4_p) %>% full_join(Cov.I5_p) %>% full_join(Cov.I6_p) %>% full_join(Cov.I7_p) %>% full_join(Cov.I8_p) %>% full_join(Cov.I9_p) %>% full_join(Cov.I10_p) %>% full_join(Cov.I11_p) %>% full_join(Cov.I12_p) %>% full_join(Cov.I13_p) %>% full_join(Cov.I14_p) %>% full_join(Cov.I15_p) %>% full_join(Cov.I16_p) %>% full_join(Cov.I17_p) %>% full_join(Cov.I18_p) %>% full_join(Cov.I19_p) %>% full_join(Cov.I20_p) %>% full_join(Cov.I21_p) %>% full_join(Cov.I22_p) %>% full_join(Cov.I23_p) %>% full_join(Cov.I24_p) %>% full_join(Cov.I25_p) %>% filter(rowname != "reStruct1") %>% filter(rowname != "scale") %>% select(-c("reStruct1.", "scale."))
                                   
#get avergaes per variable and per quantile # need to resoirt the rowname variable to preserve original order

cov_pool_p <- all.cov_p  %>% group_by(rowname, quantile) %>% summarise(across(everything(), mean)) %>% ungroup() %>% arrange(factor(rowname, levels = (c("(Intercept)", "treatment", "P201", "P301", "P501", "P601", "P901", "LW_Factor_1", "PV_Factor_1", "treatment:P201", "treatment:P501", "treatment:P601", "treatment:P901")))) %>% ungroup()


#quantify uncertainty due to missing data
  #VCOVmissing.data=∑mi=1(bi−bpool)T(bi−bpool)m−1
#calculate the difference for each coefficient from the pooled coefficient
coef.dev.I1_p <- I1_p.coef[2:14] - coef_pool_p[2:14] 
coef.dev.I1_p <- coef.dev.I1_p %>% mutate(quantile = seq(0.1, 0.9, 0.1))

coef.dev.I2_p <- I2_p.coef[2:14] - coef_pool_p[2:14] 
coef.dev.I2_p <- coef.dev.I2_p %>% mutate(quantile = seq(0.1, 0.9, 0.1))

coef.dev.I3_p <- I3_p.coef[2:14] - coef_pool_p[2:14] 
coef.dev.I3_p <- coef.dev.I3_p %>% mutate(quantile = seq(0.1, 0.9, 0.1))

coef.dev.I4_p <- I4_p.coef[2:14] - coef_pool_p[2:14] 
coef.dev.I4_p <- coef.dev.I4_p %>% mutate(quantile = seq(0.1, 0.9, 0.1))

coef.dev.I5_p <- I5_p.coef[2:14] - coef_pool_p[2:14] 
coef.dev.I5_p <- coef.dev.I5_p %>% mutate(quantile = seq(0.1, 0.9, 0.1))

coef.dev.I6_p <- I6_p.coef[2:14] - coef_pool_p[2:14] 
coef.dev.I6_p <- coef.dev.I6_p %>% mutate(quantile = seq(0.1, 0.9, 0.1))

coef.dev.I7_p <- I7_p.coef[2:14] - coef_pool_p[2:14] 
coef.dev.I7_p <- coef.dev.I7_p %>% mutate(quantile = seq(0.1, 0.9, 0.1))

coef.dev.I8_p <- I8_p.coef[2:14] - coef_pool_p[2:14] 
coef.dev.I8_p <- coef.dev.I8_p %>% mutate(quantile = seq(0.1, 0.9, 0.1))

coef.dev.I9_p <- I9_p.coef[2:14] - coef_pool_p[2:14] 
coef.dev.I9_p <- coef.dev.I9_p %>% mutate(quantile = seq(0.1, 0.9, 0.1))

coef.dev.I10_p <- I10_p.coef[2:14] - coef_pool_p[2:14] 
coef.dev.I10_p <- coef.dev.I10_p %>% mutate(quantile = seq(0.1, 0.9, 0.1))

coef.dev.I11_p <- I11_p.coef[2:14] - coef_pool_p[2:14] 
coef.dev.I11_p <- coef.dev.I11_p %>% mutate(quantile = seq(0.1, 0.9, 0.1))

coef.dev.I12_p <- I12_p.coef[2:14] - coef_pool_p[2:14] 
coef.dev.I12_p <- coef.dev.I12_p %>% mutate(quantile = seq(0.1, 0.9, 0.1))

coef.dev.I13_p <- I13_p.coef[2:14] - coef_pool_p[2:14] 
coef.dev.I13_p <- coef.dev.I13_p %>% mutate(quantile = seq(0.1, 0.9, 0.1))

coef.dev.I14_p <- I14_p.coef[2:14] - coef_pool_p[2:14] 
coef.dev.I14_p <- coef.dev.I14_p %>% mutate(quantile = seq(0.1, 0.9, 0.1))

coef.dev.I15_p <- I15_p.coef[2:14] - coef_pool_p[2:14] 
coef.dev.I15_p <- coef.dev.I15_p %>% mutate(quantile = seq(0.1 ,0.9, 0.1)) 

coef.dev.I16_p <- I16_p.coef[2:14] - coef_pool_p[2:14] 
coef.dev.I16_p <- coef.dev.I16_p %>% mutate(quantile = seq(0.1 ,0.9, 0.1))

coef.dev.I17_p <- I17_p.coef[2:14] - coef_pool_p[2:14] 
coef.dev.I17_p <- coef.dev.I17_p %>% mutate(quantile = seq(0.1 ,0.9, 0.1))

coef.dev.I18_p <- I18_p.coef[2:14] - coef_pool_p[2:14]
coef.dev.I18_p <- coef.dev.I18_p %>% mutate(quantile = seq(0.1 ,0.9, 0.1))

coef.dev.I19_p <- I19_p.coef[2:14] - coef_pool_p[2:14] 
coef.dev.I19_p <- coef.dev.I19_p %>% mutate(quantile = seq(0.1 ,0.9, 0.1))

coef.dev.I20_p <- I20_p.coef[2:14] - coef_pool_p[2:14] 
coef.dev.I20_p <- coef.dev.I20_p %>% mutate(quantile = seq(0.1 ,0.9, 0.1))

coef.dev.I21_p <- I21_p.coef[2:14] - coef_pool_p[2:14] 
coef.dev.I21_p <- coef.dev.I21_p %>% mutate(quantile = seq(0.1 ,0.9, 0.1))

coef.dev.I22_p <- I22_p.coef[2:14] - coef_pool_p[2:14] 
coef.dev.I22_p <- coef.dev.I22_p %>% mutate(quantile = seq(0.1 ,0.9, 0.1))

coef.dev.I23_p <- I23_p.coef[2:14] - coef_pool_p[2:14] 
coef.dev.I23_p <- coef.dev.I23_p %>% mutate(quantile = seq(0.1 ,0.9, 0.1))

coef.dev.I24_p <- I24_p.coef[2:14] - coef_pool_p[2:14] 
coef.dev.I24_p <- coef.dev.I24_p %>% mutate(quantile = seq(0.1 ,0.9, 0.1))

coef.dev.I25_p <- I25_p.coef[2:14] - coef_pool_p[2:14] 
coef.dev.I25_p <- coef.dev.I25_p %>% mutate(quantile = seq(0.1 ,0.9, 0.1))

#combine all coef dev matrices into one giant thing
all.coef.dev_p <- coef.dev.I1_p %>% full_join(coef.dev.I2_p)  %>% full_join(coef.dev.I3_p) %>% full_join(coef.dev.I4_p) %>% full_join(coef.dev.I5_p) %>% full_join(coef.dev.I6_p) %>% full_join(coef.dev.I7_p) %>% full_join(coef.dev.I8_p) %>% full_join(coef.dev.I9_p) %>% full_join(coef.dev.I10_p) %>% full_join(coef.dev.I11_p) %>% full_join(coef.dev.I12_p) %>% full_join(coef.dev.I13_p) %>% full_join(coef.dev.I14_p) %>% full_join(coef.dev.I15_p) %>% full_join(coef.dev.I16_p)  %>% full_join(coef.dev.I17_p) %>% full_join(coef.dev.I18_p) %>% full_join(coef.dev.I19_p) %>% full_join(coef.dev.I20_p) %>% full_join(coef.dev.I21_p) %>% full_join(coef.dev.I22_p) %>% full_join(coef.dev.I23_p) %>% full_join(coef.dev.I24_p) %>% full_join(coef.dev.I25_p) 

#separate VCOV and ceof.dev by quantile because I am a bad coder
all.coef.dev.q1_p <- all.coef.dev_p %>% filter(quantile == 0.1) %>% select (-quantile) %>% as.matrix()
all.coef.dev.q2_p <- all.coef.dev_p %>% filter(quantile == 0.2) %>% select (-quantile) %>% as.matrix()
all.coef.dev.q3_p <- all.coef.dev_p %>% filter(quantile == "0.3")  %>% select (-quantile) %>% as.matrix() #not sure why it won't give anything with just the numbers
all.coef.dev.q4_p <- all.coef.dev_p %>% filter(quantile == 0.4) %>% select (-quantile) %>% as.matrix()
all.coef.dev.q5_p <- all.coef.dev_p %>% filter(quantile == 0.5) %>% select (-quantile) %>% as.matrix()
all.coef.dev.q6_p <- all.coef.dev_p %>% filter(quantile == 0.6)%>% select (-quantile) %>% as.matrix()
all.coef.dev.q7_p <- all.coef.dev_p %>% filter(quantile == "0.7")%>% select (-quantile) %>% as.matrix()
all.coef.dev.q8_p <- all.coef.dev_p %>% filter(quantile == 0.8) %>% select (-quantile) %>% as.matrix()
all.coef.dev.q9_p <- all.coef.dev_p %>% filter(quantile == 0.9)%>% select (-quantile) %>% as.matrix()

all.cov.q1_p <- cov_pool_p %>% filter(quantile == 0.1) %>% select(-c("rowname", "quantile")) %>% as.matrix()
all.cov.q2_p <- cov_pool_p %>% filter(quantile == 0.2) %>% select(-c("rowname", "quantile")) %>% as.matrix()
all.cov.q3_p <- cov_pool_p %>% filter(quantile == 0.3) %>% select(-c("rowname", "quantile")) %>% as.matrix()
all.cov.q4_p <- cov_pool_p %>% filter(quantile == 0.4) %>% select(-c("rowname", "quantile")) %>% as.matrix()
all.cov.q5_p <- cov_pool_p %>% filter(quantile == 0.5) %>% select(-c("rowname", "quantile")) %>% as.matrix()
all.cov.q6_p <- cov_pool_p %>% filter(quantile == 0.6) %>% select(-c("rowname", "quantile")) %>% as.matrix()
all.cov.q7_p <- cov_pool_p %>% filter(quantile == 0.7) %>% select(-c("rowname", "quantile")) %>% as.matrix()
all.cov.q8_p <- cov_pool_p %>% filter(quantile == 0.8) %>% select(-c("rowname", "quantile")) %>% as.matrix()
all.cov.q9_p <- cov_pool_p %>% filter(quantile == 0.9) %>% select(-c("rowname", "quantile")) %>% as.matrix()

#multiply matrix by its inverse, do this separately per quantile
coef.dev2.q1_p <- t(all.coef.dev.q1_p) %*% all.coef.dev.q1_p
coef.dev2.q2_p <- t(all.coef.dev.q2_p) %*% all.coef.dev.q2_p
coef.dev2.q3_p <- t(all.coef.dev.q3_p) %*% all.coef.dev.q3_p
coef.dev2.q4_p <- t(all.coef.dev.q4_p) %*% all.coef.dev.q4_p
coef.dev2.q5_p <- t(all.coef.dev.q5_p) %*% all.coef.dev.q5_p
coef.dev2.q6_p <- t(all.coef.dev.q6_p) %*% all.coef.dev.q6_p
coef.dev2.q7_p <- t(all.coef.dev.q7_p) %*% all.coef.dev.q7_p
coef.dev2.q8_p <- t(all.coef.dev.q8_p) %*% all.coef.dev.q8_p
coef.dev2.q9_p <- t(all.coef.dev.q9_p) %*% all.coef.dev.q9_p

#divide the sum of the above by #of imputations - 1 (14). to get missing data number of imputations error
mdni.error.q1_p <- coef.dev2.q1_p / 24
mdni.error.q2_p <- coef.dev2.q2_p / 24
mdni.error.q3_p <- coef.dev2.q3_p / 24
mdni.error.q4_p <- coef.dev2.q4_p / 24
mdni.error.q5_p <- coef.dev2.q5_p / 24
mdni.error.q6_p <- coef.dev2.q6_p / 24
mdni.error.q7_p <- coef.dev2.q7_p / 24
mdni.error.q8_p <- coef.dev2.q8_p / 24
mdni.error.q9_p <- coef.dev2.q9_p / 24

#divide the mdni  by total number of imputations to get error due to finite sampling
mdfs.error.q1_p <- mdni.error.q1_p / 25
mdfs.error.q2_p <- mdni.error.q2_p / 25
mdfs.error.q3_p <- mdni.error.q3_p / 25
mdfs.error.q4_p <- mdni.error.q4_p / 25
mdfs.error.q5_p <- mdni.error.q5_p / 25
mdfs.error.q6_p <- mdni.error.q6_p / 25
mdfs.error.q7_p <- mdni.error.q7_p / 25
mdfs.error.q8_p <- mdni.error.q8_p / 25
mdfs.error.q9_p <- mdni.error.q9_p / 25

#add the three error components to get total error
total.error.q1_p <- all.cov.q1_p + mdni.error.q1_p + mdfs.error.q1_p
total.error.q2_p <- all.cov.q2_p + mdni.error.q2_p + mdfs.error.q2_p
total.error.q3_p <- all.cov.q3_p + mdni.error.q3_p + mdfs.error.q3_p
total.error.q4_p <- all.cov.q4_p + mdni.error.q4_p + mdfs.error.q4_p
total.error.q5_p <- all.cov.q5_p + mdni.error.q5_p + mdfs.error.q5_p
total.error.q6_p <- all.cov.q6_p + mdni.error.q6_p + mdfs.error.q6_p
total.error.q7_p <- all.cov.q7_p + mdni.error.q7_p + mdfs.error.q7_p
total.error.q8_p <- all.cov.q8_p + mdni.error.q8_p + mdfs.error.q8_p
total.error.q9_p <- all.cov.q9_p + mdni.error.q9_p + mdfs.error.q9_p

fmi.q1_p <- (diag(mdni.error.q1_p) + diag(mdfs.error.q1_p)) / diag(total.error.q1_p)
fmi.q2_p <- (diag(mdni.error.q2_p) + diag(mdfs.error.q2_p)) / diag(total.error.q2_p)
fmi.q3_p <- (diag(mdni.error.q3_p) + diag(mdfs.error.q3_p)) / diag(total.error.q3_p)
fmi.q4_p <- (diag(mdni.error.q4_p) + diag(mdfs.error.q4_p)) / diag(total.error.q4_p)
fmi.q5_p <- (diag(mdni.error.q5_p) + diag(mdfs.error.q5_p)) / diag(total.error.q5_p)
fmi.q6_p <- (diag(mdni.error.q6_p) + diag(mdfs.error.q6_p)) / diag(total.error.q6_p)
fmi.q7_p <- (diag(mdni.error.q7_p) + diag(mdfs.error.q7_p)) / diag(total.error.q7_p)
fmi.q8_p <- (diag(mdni.error.q8_p) + diag(mdfs.error.q8_p)) / diag(total.error.q8_p)
fmi.q9_p <- (diag(mdni.error.q9_p) + diag(mdfs.error.q9_p)) / diag(total.error.q9_p)


#then calculate the appropriate degrees of freedom
#dfimputation=1/1m−1FMI2+1(1−FMI)(dfcomplete+1dfcomplete+3)dfcomplete

df_com_p <- sum.I1_p$rdf %>% as.numeric() #same for all quantiles

v.q1_p <- (24) / fmi.q1_p^2
vhat.q1_p <- (1 - fmi.q1_p) * ((df_com_p + 1) / (df_com_p + 3)) * df_com_p
df_imp.q1_p <- (v.q1_p^(-1) + vhat.q1_p^(-1))^(-1)

v.q2_p <- (24) / fmi.q2_p^2
vhat.q2_p <- (1 - fmi.q2_p) * ((df_com_p + 1) / (df_com_p + 3)) * df_com_p
df_imp.q2_p <- (v.q2_p^(-1) + vhat.q2_p^(-1))^(-1)

v.q3_p <- (24) / fmi.q3_p^2
vhat.q3_p <- (1 - fmi.q3_p) * ((df_com_p + 1) / (df_com_p + 3)) * df_com_p
df_imp.q3_p <- (v.q3_p^(-1) + vhat.q3_p^(-1))^(-1)

v.q4_p <- (24) / fmi.q4_p^2
vhat.q4_p <- (1 - fmi.q4_p) * ((df_com_p + 1) / (df_com_p + 3)) * df_com_p
df_imp.q4_p <- (v.q4_p^(-1) + vhat.q4_p^(-1))^(-1)

v.q5_p <- (24) / fmi.q5_p^2
vhat.q5_p <- (1 - fmi.q5_p) * ((df_com_p + 1) / (df_com_p + 3)) * df_com_p
df_imp.q5_p <- (v.q5_p^(-1) + vhat.q5_p^(-1))^(-1)

v.q6_p <- (24) / fmi.q6_p^2
vhat.q6_p <- (1 - fmi.q6_p) * ((df_com_p + 1) / (df_com_p + 3)) * df_com_p
df_imp.q6_p <- (v.q6_p^(-1) + vhat.q6_p^(-1))^(-1)

v.q7_p <- (24) / fmi.q7_p^2
vhat.q7_p <- (1 - fmi.q7_p) * ((df_com_p + 1) / (df_com_p + 3)) * df_com_p
df_imp.q7_p <- (v.q7_p^(-1) + vhat.q4_p^(-1))^(-1)

v.q8_p <- (24) / fmi.q8_p^2
vhat.q8_p <- (1 - fmi.q8_p) * ((df_com_p + 1) / (df_com_p + 3)) * df_com_p
df_imp.q8_p <- (v.q8_p^(-1) + vhat.q8_p^(-1))^(-1)

v.q9_p <- (24) / fmi.q9_p^2
vhat.q9_p <- (1 - fmi.q9_p) * ((df_com_p + 1) / (df_com_p + 3)) * df_com_p
df_imp.q9_p <- (v.q9_p^(-1) + vhat.q9_p^(-1))^(-1)

#now get the SE, t, and p values
(se.pool.q1_p <- sqrt(diag(total.error.q1_p)))
t.pool.q1_p <- coef_pool_p[1,2:14] / se.pool.q1_p 
t.pool.q1.abs_p <- abs(t.pool.q1_p) %>% as.matrix()
p.pool.q1_p <- 2 * pt(q = t.pool.q1.abs_p, df = df_imp.q1_p, lower.tail = F)

(se.pool.q2_p <- sqrt(diag(total.error.q2_p)))
t.pool.q2_p <- coef_pool_p[2,2:14] / se.pool.q2_p 
t.pool.q2.abs_p <- abs(t.pool.q2_p) %>% as.matrix()
p.pool.q2_p <- 2 * pt(q = t.pool.q2.abs_p, df = df_imp.q2_p, lower.tail = F)

(se.pool.q3_p <- sqrt(diag(total.error.q3_p)))
t.pool.q3_p <- coef_pool_p[3,2:14] / se.pool.q3_p
t.pool.q3.abs_p <- abs(t.pool.q3_p) %>% as.matrix()
p.pool.q3_p <- 2 * pt(q = t.pool.q3.abs_p, df = df_imp.q3_p, lower.tail = F)

(se.pool.q4_p <- sqrt(diag(total.error.q4_p)))
t.pool.q4_p <- coef_pool_p[4,2:14] / se.pool.q4_p 
t.pool.q4.abs_p <- abs(t.pool.q4_p) %>% as.matrix()
p.pool.q4_p <- 2 * pt(q = t.pool.q4.abs_p, df = df_imp.q4_p, lower.tail = F)

(se.pool.q5_p <- sqrt(diag(total.error.q5_p)))
t.pool.q5_p <- coef_pool_p[5,2:14] / se.pool.q5_p 
t.pool.q5.abs_p <- abs(t.pool.q5_p) %>% as.matrix()
p.pool.q5_p <- 2 * pt(q = t.pool.q5.abs_p, df = df_imp.q5_p, lower.tail = F)

(se.pool.q6_p <- sqrt(diag(total.error.q6_p)))
t.pool.q6_p <- coef_pool_p[6,2:14] / se.pool.q6_p 
t.pool.q6.abs_p <- abs(t.pool.q6_p) %>% as.matrix()
p.pool.q6_p <- 2 * pt(q = t.pool.q6.abs_p, df = df_imp.q6_p, lower.tail = F)

(se.pool.q7_p <- sqrt(diag(total.error.q7_p)))
t.pool.q7_p <- coef_pool_p[7,2:14] / se.pool.q7_p
t.pool.q7.abs_p <- abs(t.pool.q7_p) %>% as.matrix()
p.pool.q7_p <- 2 * pt(q = t.pool.q7.abs_p, df = df_imp.q7_p, lower.tail = F)

(se.pool.q8_p <- sqrt(diag(total.error.q8_p)))
t.pool.q8_p <- coef_pool_p[8,2:14] / se.pool.q8_p
t.pool.q8.abs_p <- abs(t.pool.q8_p) %>% as.matrix()
p.pool.q8_p <- 2 * pt(q = t.pool.q8.abs_p, df = df_imp.q8_p, lower.tail = F)

(se.pool.q9_p <- sqrt(diag(total.error.q9_p)))
t.pool.q9_p <- coef_pool_p[9,2:14] / se.pool.q9_p
t.pool.q9.abs_p <- abs(t.pool.q9_p) %>% as.matrix()
p.pool.q9_p <- 2 * pt(q = t.pool.q9.abs_p, df = df_imp.q9_p, lower.tail = F)

# As well as our confidence intervals for parameter estimation.
# 
crit.pool.q1_p <- qt(p = 1 - (.05 / 2), df = df_imp.q1_p, lower.tail = T)
hw.pool.q1_p <- se.pool.q1 * crit.pool.q1
ci_pool.q1_p <- cbind("lwr" = coef_pool_p[1,2:14] - hw.pool.q1_p, "upr" = coef_pool_p[1,2:14] + hw.pool.q1_p) %>% mutate(quantile = "0.1")

crit.pool.q2_p <- qt(p = 1 - (.05 / 2), df = df_imp.q2_p, lower.tail = T)
hw.pool.q2_p <- se.pool.q2_p * crit.pool.q2_p
ci_pool.q2_p <- cbind("lwr" = coef_pool_p[2,2:14] - hw.pool.q2_p, "upr" = coef_pool_p[2,2:14] + hw.pool.q2_p) %>% mutate(quantile = "0.2")

crit.pool.q3_p <- qt(p = 1 - (.05 / 2), df = df_imp.q3_p, lower.tail = T)
hw.pool.q3_p <- se.pool.q3_p * crit.pool.q3_p
ci_pool.q3_p <- cbind("lwr" = coef_pool_p[3,2:14] - hw.pool.q3_p, "upr" = coef_pool_p[3,2:14] + hw.pool.q3_p) %>% mutate(quantile = "0.3")

crit.pool.q4_p <- qt(p = 1 - (.05 / 2), df = df_imp.q4_p, lower.tail = T)
hw.pool.q4_p <- se.pool.q4_p * crit.pool.q4_p
ci_pool.q4_p <- cbind("lwr" = coef_pool_p[4,2:14] - hw.pool.q4_p, "upr" = coef_pool_p[4,2:14] + hw.pool.q4_p) %>% mutate(quantile = "0.4")

crit.pool.q5_p <- qt(p = 1 - (.05 / 2), df = df_imp.q5_p, lower.tail = T)
hw.pool.q5_p <- se.pool.q5_p * crit.pool.q5_p
ci_pool.q5_p <- cbind("lwr" = coef_pool_p[5,2:14] - hw.pool.q5_p, "upr" = coef_pool_p[5,2:14] + hw.pool.q5_p) %>% mutate(quantile = "0.5")

crit.pool.q6_p <- qt(p = 1 - (.05 / 2), df = df_imp.q6_p, lower.tail = T)
hw.pool.q6_p <- se.pool.q6_p * crit.pool.q6_p
ci_pool.q6_p <- cbind("lwr" = coef_pool_p[6,2:14] - hw.pool.q6_p, "upr" = coef_pool_p[6,2:14] + hw.pool.q6_p) %>% mutate(quantile = "0.6")

crit.pool.q7_p <- qt(p = 1 - (.05 / 2), df = df_imp.q7_p, lower.tail = T)
hw.pool.q7_p <- se.pool.q7_p * crit.pool.q7_p
ci_pool.q7_p <- cbind("lwr" = coef_pool_p[7,2:14] - hw.pool.q7_p, "upr" = coef_pool_p[7,2:14] + hw.pool.q7_p) %>% mutate(quantile = "0.7")

crit.pool.q8_p <- qt(p = 1 - (.05 / 2), df = df_imp.q8_p, lower.tail = T)
hw.pool.q8_p <- se.pool.q8_p * crit.pool.q8_p
ci_pool.q8_p <- cbind("lwr" = coef_pool_p[8,2:14] - hw.pool.q8_p, "upr" = coef_pool_p[8,2:14] + hw.pool.q8_p) %>% mutate(quantile = "0.8")

crit.pool.q9_p <- qt(p = 1 - (.05 / 2), df = df_imp.q9_p, lower.tail = T)
hw.pool.q9_p <- se.pool.q9_p * crit.pool.q9_p
ci_pool.q9_p <- cbind("lwr" = coef_pool_p[9,2:14] - hw.pool.q9_p, "upr" = coef_pool_p[9,2:14] + hw.pool.q9_p) %>% mutate(quantile = "0.9")

# Do the B-H correction on all the adjusted p-values
##First get all p-values into one dataframe

p_values_RQ1_PV_p<- p.pool.q1_p %>% rbind(p.pool.q2_p) %>% rbind(p.pool.q3_p) %>% rbind(p.pool.q4_p) %>% rbind(p.pool.q5_p) %>% rbind(p.pool.q6_p) %>% rbind(p.pool.q7_p) %>% rbind(p.pool.q8_p) %>% rbind (p.pool.q9_p)  
fdrs_RQ1_PV_p <- p.adjust(p_values_RQ1_PV_p, method="BH") 
p_values_adj_RQ1_PV_p <- matrix(unlist(fdrs_RQ1_PV_p), nrow=9) %>% round(3) %>% as.data.frame()  %>% mutate(quant=c(1, 2, 3, 4, 5, 6, 7, 8, 9))
colnames(p_values_adj_RQ1_PV_p) <- c("(Intercept)_adj", "treatment_adj", "P201_adj", "P501_adj", "P601_adj", "P901_adj", "P301_adj", "LW_Factor_1_adj", "PV_Factor_1_adj", "treatment:P201",  "treatment:P501", "treatment:P601", "treatment:P901", "Quantile")
p_values_RQ1_PV_wquant_p <- p_values_RQ1_PV_p %>% as.data.frame() %>% mutate(quant=c(1, 2, 3, 4, 5, 6, 7, 8, 9))

(p_values_RQ1_PV_all_p <- p_values_RQ1_PV_wquant_p %>% cbind(p_values_adj_RQ1_PV_p))

all_ci_p <- ci_pool.q1_p %>% rbind(ci_pool.q2_p) %>% rbind(ci_pool.q3_p) %>% rbind(ci_pool.q4_p) %>% rbind(ci_pool.q5_p) %>% rbind(ci_pool.q6_p) %>% rbind(ci_pool.q7_p) %>% rbind(ci_pool.q8_p) %>% rbind(ci_pool.q9_p)

(all_new_p <- all_ci_p %>% full_join(coef_pool_p, join_by(quantile == rowname)))

```
```{r sensitivityProject LWID}
RQ1letterid_I_p <- lqmm(LW_Factor_3 ~ treatment*(P201 + P501 + P601 + P901) + P301 + LW_Factor_1 + PV_Factor_1, random = ~1, group = TID, tau = c(1:9/10), data = QR_data_imp_I1, control = list(UP_max_iter = 1000, UP_tol = 1e-6))
RQ1letterid_I2_p <- lqmm(LW_Factor_3 ~ treatment*(P201 + P501 + P601 + P901) + P301 + LW_Factor_1 + PV_Factor_1, random = ~1, group = TID, tau = c(1:9/10), data = QR_data_imp_I2, control = list(UP_max_iter = 1000, UP_tol = 1e-6))
RQ1letterid_I3_p <- lqmm(LW_Factor_3 ~ treatment*(P201 + P501 + P601 + P901) + P301 + LW_Factor_1 + PV_Factor_1, random = ~1, group = TID, tau = c(1:9/10), data = QR_data_imp_I3, control = list(UP_max_iter = 1000, UP_tol = 1e-6))
RQ1letterid_I4_p <- lqmm(LW_Factor_3 ~ treatment*(P201 + P501 + P601 + P901) + P301 + LW_Factor_1 + PV_Factor_1, random = ~1, group = TID, tau = c(1:9/10), data = QR_data_imp_I4, control = list(LP_max_iter = 1000, lP_tol = 1e-6))
RQ1letterid_I5_p <- lqmm(LW_Factor_3 ~ treatment*(P201 + P501 + P601 + P901) + P301 + LW_Factor_1 + PV_Factor_1, random = ~1, group = TID, tau = c(1:9/10), data = QR_data_imp_I5, control = list(LP_max_iter = 1000, LP_tol = 1e-6))
RQ1letterid_I6_p <- lqmm(LW_Factor_3 ~ treatment*(P201 + P501 + P601 + P901) + P301 + LW_Factor_1 + PV_Factor_1, random = ~1, group = TID, tau = c(1:9/10), data = QR_data_imp_I6, control = list(UP_max_iter = 1000, UP_tol = 1e-7))
RQ1letterid_I7_p <- lqmm(LW_Factor_3 ~ treatment*(P201 + P501 + P601 + P901) + P301 + LW_Factor_1 + PV_Factor_1, random = ~1, group = TID, tau = c(1:9/10), data = QR_data_imp_I7, control = list(UP_max_iter = 1000, UP_tol = 1e-7))
RQ1letterid_I8_p <- lqmm(LW_Factor_3 ~ treatment*(P201 + P501 + P601 + P901) + P301 + LW_Factor_1 + PV_Factor_1, random = ~1, group = TID, tau = c(1:9/10), data = QR_data_imp_I8, control = list(UP_max_iter = 1000, UP_tol = 1e-6))
RQ1letterid_I9_p <- lqmm(LW_Factor_3 ~ treatment*(P201 + P501 + P601 + P901) + P301 + LW_Factor_1 + PV_Factor_1, random = ~1, group = TID, tau = c(1:9/10), data = QR_data_imp_I9, control = list(UP_max_iter = 1000, UP_tol = 1e-6))
RQ1letterid_I10_p <- lqmm(LW_Factor_3 ~ treatment*(P201 + P501 + P601 + P901) + P301 + LW_Factor_1 + PV_Factor_1, random = ~1, group = TID, tau = c(1:9/10), data = QR_data_imp_I10, control = list(UP_max_iter = 1000, UP_tol = 1e-6))
RQ1letterid_I11_p <- lqmm(LW_Factor_3 ~ treatment*(P201 + P501 + P601 + P901) + P301 + LW_Factor_1 + PV_Factor_1, random = ~1, group = TID, tau = c(1:9/10), data = QR_data_imp_I11, control = list(UP_max_iter = 1000, UP_tol = 1e-7))
RQ1letterid_I12_p <- lqmm(LW_Factor_3 ~ treatment*(P201 + P501 + P601 + P901) + P301 + LW_Factor_1 + PV_Factor_1, random = ~1, group = TID, tau = c(1:9/10), data = QR_data_imp_I12, control = list(UP_max_iter = 1000, UP_tol = 1e-6))
RQ1letterid_I13_p <- lqmm(LW_Factor_3 ~ treatment*(P201 + P501 + P601 + P901) + P301 + LW_Factor_1 + PV_Factor_1, random = ~1, group = TID, tau = c(1:9/10), data = QR_data_imp_I13, control = list(UP_max_iter = 1000, UP_tol = 1e-7))
RQ1letterid_I14_p <- lqmm(LW_Factor_3 ~ treatment*(P201 + P501 + P601 + P901) + P301 + LW_Factor_1 + PV_Factor_1, random = ~1, group = TID, tau = c(1:9/10), data = QR_data_imp_I14, control = list(UP_max_iter = 1000, UP_tol = 1e-6))
RQ1letterid_I15_p <- lqmm(LW_Factor_3 ~ treatment*(P201 + P501 + P601 + P901) + P301 + LW_Factor_1 + PV_Factor_1, random = ~1, group = TID, tau = c(1:9/10), data = QR_data_imp_I15, control = list(UP_max_iter = 1000, UP_tol = 1e-6))
RQ1letterid_I16_p <- lqmm(LW_Factor_3 ~ treatment*(P201 + P501 + P601 + P901) + P301 + LW_Factor_1 + PV_Factor_1, random = ~1, group = TID, tau = c(1:9/10), data = QR_data_imp_I16, control = list(UP_max_iter = 1000, UP_tol = 1e-6))
RQ1letterid_I17_p <- lqmm(LW_Factor_3 ~ treatment*(P201 + P501 + P601 + P901) + P301 + LW_Factor_1 + PV_Factor_1, random = ~1, group = TID, tau = c(1:9/10), data = QR_data_imp_I17, control = list(UP_max_iter = 1000, UP_tol = 1e-6))
RQ1letterid_I18_p <- lqmm(LW_Factor_3 ~ treatment*(P201 + P501 + P601 + P901) + P301 + LW_Factor_1 + PV_Factor_1, random = ~1, group = TID, tau = c(1:9/10), data = QR_data_imp_I18, control = list(UP_max_iter = 1000, UP_tol = 1e-6))
RQ1letterid_I19_p <- lqmm(LW_Factor_3 ~ treatment*(P201 + P501 + P601 + P901) + P301 + LW_Factor_1 + PV_Factor_1, random = ~1, group = TID, tau = c(1:9/10), data = QR_data_imp_I19, control = list(UP_max_iter = 1000, UP_tol = 1e-6))
RQ1letterid_I20_p <- lqmm(LW_Factor_3 ~ treatment*(P201 + P501 + P601 + P901) + P301 + LW_Factor_1 + PV_Factor_1, random = ~1, group = TID, tau = c(1:9/10), data = QR_data_imp_I20, control = list(UP_max_iter = 1000, UP_tol = 1e-6))
RQ1letterid_I21_p <- lqmm(LW_Factor_3 ~ treatment*(P201 + P501 + P601 + P901)+ P301 + LW_Factor_1 + PV_Factor_1, random = ~1, group = TID, tau = c(1:9/10), data = QR_data_imp_I21, control = list(UP_max_iter = 1000, UP_tol = 1e-6))
RQ1letterid_I22_p <- lqmm(LW_Factor_3 ~ treatment*(P201 + P501 + P601 + P901) + P301 + LW_Factor_1 + PV_Factor_1, random = ~1, group = TID, tau = c(1:9/10), data = QR_data_imp_I22, control = list(UP_max_iter = 1000, UP_tol = 1e-6))
RQ1letterid_I23_p <- lqmm(LW_Factor_3 ~ treatment*(P201 + P501 + P601 + P901) + P301 + LW_Factor_1 + PV_Factor_1, random = ~1, group = TID, tau = c(1:9/10), data = QR_data_imp_I23, control = list(UP_max_iter = 1000, UP_tol = 1e-6))
RQ1letterid_I24_p <- lqmm(LW_Factor_3 ~ treatment*(P201 + P501 + P601 + P901) + P301 + LW_Factor_1 + PV_Factor_1, random = ~1, group = TID, tau = c(1:9/10), data = QR_data_imp_I24, control = list(UP_max_iter = 1000, UP_tol = 1e-6))
RQ1letterid_I25_p <- lqmm(LW_Factor_3 ~ treatment*(P201 + P501 + P601 + P901) + P301 + LW_Factor_1 + PV_Factor_1, random = ~1, group = TID, tau = c(1:9/10), data = QR_data_imp_I25, control = list(UP_max_iter = 1000, UP_tol = 1e-6))

#Extract the regression coefficients for each model
VI1_p.coef <- coef.lqmm(RQ1letterid_I_p) %>% t() %>% as.data.frame() %>% rownames_to_column() #transpose and make a datafrome
VI2_p.coef <- coef.lqmm(RQ1letterid_I2_p) %>% t() %>% as.data.frame() %>% rownames_to_column()
VI3_p.coef <- coef.lqmm(RQ1letterid_I3_p) %>% t() %>% as.data.frame() %>% rownames_to_column()
VI4_p.coef <- coef.lqmm(RQ1letterid_I4_p) %>% t() %>% as.data.frame() %>% rownames_to_column()
VI5_p.coef <- coef.lqmm(RQ1letterid_I5_p) %>% t() %>% as.data.frame() %>% rownames_to_column()
VI6_p.coef <- coef.lqmm(RQ1letterid_I6_p) %>% t() %>% as.data.frame() %>% rownames_to_column()
VI7_p.coef <- coef.lqmm(RQ1letterid_I7_p) %>% t() %>% as.data.frame() %>% rownames_to_column()
VI8_p.coef <- coef.lqmm(RQ1letterid_I8_p) %>% t() %>% as.data.frame() %>% rownames_to_column()
VI9_p.coef <- coef.lqmm(RQ1letterid_I9_p) %>% t() %>% as.data.frame() %>% rownames_to_column()
VI10_p.coef <- coef.lqmm(RQ1letterid_I10_p) %>% t() %>% as.data.frame() %>% rownames_to_column()
VI11_p.coef <- coef.lqmm(RQ1letterid_I11_p) %>% t() %>% as.data.frame() %>% rownames_to_column()
VI12_p.coef <- coef.lqmm(RQ1letterid_I12_p) %>% t() %>% as.data.frame() %>% rownames_to_column()
VI13_p.coef <- coef.lqmm(RQ1letterid_I13_p) %>% t() %>% as.data.frame() %>% rownames_to_column()
VI14_p.coef <- coef.lqmm(RQ1letterid_I14_p) %>% t() %>% as.data.frame() %>% rownames_to_column()
VI15_p.coef <- coef.lqmm(RQ1letterid_I15_p) %>% t() %>% as.data.frame() %>% rownames_to_column()
VI16_p.coef <- coef.lqmm(RQ1letterid_I16_p) %>% t() %>% as.data.frame() %>% rownames_to_column()
VI17_p.coef <- coef.lqmm(RQ1letterid_I17_p) %>% t() %>% as.data.frame() %>% rownames_to_column()
VI18_p.coef <- coef.lqmm(RQ1letterid_I18_p) %>% t() %>% as.data.frame() %>% rownames_to_column()
VI19_p.coef <- coef.lqmm(RQ1letterid_I19_p) %>% t() %>% as.data.frame() %>% rownames_to_column()
VI20_p.coef <- coef.lqmm(RQ1letterid_I20_p) %>% t() %>% as.data.frame() %>% rownames_to_column()
VI21_p.coef <- coef.lqmm(RQ1letterid_I21_p) %>% t() %>% as.data.frame() %>% rownames_to_column()
VI22_p.coef <- coef.lqmm(RQ1letterid_I22_p) %>% t() %>% as.data.frame() %>% rownames_to_column()
VI23_p.coef <- coef.lqmm(RQ1letterid_I23_p) %>% t() %>% as.data.frame() %>% rownames_to_column()
VI24_p.coef <- coef.lqmm(RQ1letterid_I24_p) %>% t() %>% as.data.frame() %>% rownames_to_column()
VI25_p.coef <- coef.lqmm(RQ1letterid_I25_p) %>% t() %>% as.data.frame() %>% rownames_to_column()
#combine all to one dataframe
All.coef.V_p <- VI1_p.coef %>% full_join(VI2_p.coef) %>% full_join(VI3_p.coef) %>% full_join(VI4_p.coef) %>% full_join(VI5_p.coef) %>% full_join(VI6_p.coef) %>% full_join(VI7_p.coef) %>% full_join(VI8_p.coef) %>% full_join(VI9_p.coef) %>% full_join(VI10_p.coef) %>% full_join(VI11_p.coef) %>% full_join(VI12_p.coef) %>% full_join(VI13_p.coef) %>% full_join(VI14_p.coef) %>% full_join(VI15_p.coef) %>% full_join(VI16_p.coef) %>% full_join(VI1_p.coef) %>% full_join(VI18_p.coef) %>% full_join(VI19_p.coef) %>% full_join(VI20_p.coef) %>% full_join(VI21_p.coef) %>% full_join(VI22_p.coef) %>% full_join(VI23_p.coef) %>% full_join(VI24_p.coef) %>% full_join(VI25_p.coef)

#average the coefficients
coef_pool.V_p <- All.coef.V_p  %>% group_by(rowname) %>% summarise(across(everything(), mean)) %>% ungroup()
print(coef_pool.V_p)

#Extract covariance matrix for each model
sum.VI1_p <-summary(RQ1letterid_I_p, covariance = T, R = 50)
Cov.VI1_p <-as.data.frame(sum.VI1_p[["Cov"]]) %>% as.data.frame() %>% rownames_to_column() %>% pivot_longer(cols= !rowname, names_to = c(".value", "quantile"), names_sep = -3)

sum.VI2_p <-summary(RQ1letterid_I2_p, covariance = T, R = 50) 
Cov.VI2_p <-as.data.frame(sum.VI2_p[["Cov"]]) %>% as.data.frame() %>% rownames_to_column() %>% pivot_longer(cols= !rowname, names_to = c(".value", "quantile"), names_sep = -3)
 
sum.VI3_p <-summary(RQ1letterid_I3_p, covariance = T, R = 50)
Cov.VI3_p <-as.data.frame(sum.VI3_p[["Cov"]]) %>% as.data.frame() %>% rownames_to_column() %>% pivot_longer(cols= !rowname, names_to = c(".value", "quantile"), names_sep = -3)

sum.VI4_p <-summary(RQ1letterid_I4_p, covariance = T, R = 50)
Cov.VI4_p <-as.data.frame(sum.VI4_p[["Cov"]]) %>% as.data.frame() %>% rownames_to_column() %>% pivot_longer(cols= !rowname, names_to = c(".value", "quantile"), names_sep = -3)

sum.VI5_p <-summary(RQ1letterid_I5_p, covariance = T, R = 50)
Cov.VI5_p <-as.data.frame(sum.VI5_p[["Cov"]]) %>% as.data.frame() %>% rownames_to_column() %>% pivot_longer(cols= !rowname, names_to = c(".value", "quantile"), names_sep = -3)

sum.VI6_p <-summary(RQ1letterid_I6_p, covariance = T, R = 50)
Cov.VI6_p <-as.data.frame(sum.VI6_p[["Cov"]]) %>% as.data.frame() %>% rownames_to_column() %>% pivot_longer(cols= !rowname, names_to = c(".value", "quantile"), names_sep = -3)

sum.VI7_p <-summary(RQ1letterid_I7_p, covariance = T, R = 50)
Cov.VI7_p <-as.data.frame(sum.VI7_p[["Cov"]]) %>% as.data.frame() %>% rownames_to_column() %>% pivot_longer(cols= !rowname, names_to = c(".value", "quantile"), names_sep = -3)

sum.VI8_p <-summary(RQ1letterid_I8_p, covariance = T, R = 50)
Cov.VI8_p <-as.data.frame(sum.VI8_p[["Cov"]]) %>% as.data.frame() %>% rownames_to_column() %>% pivot_longer(cols= !rowname, names_to = c(".value", "quantile"), names_sep = -3)

sum.VI9_p <-summary(RQ1letterid_I9_p, covariance = T, R = 50)
Cov.VI9_p <-as.data.frame(sum.VI9_p[["Cov"]]) %>% as.data.frame() %>% rownames_to_column() %>% pivot_longer(cols= !rowname, names_to = c(".value", "quantile"), names_sep = -3)

sum.VI10_p <-summary(RQ1letterid_I10_p, covariance = T, R = 50)
Cov.VI10_p <-as.data.frame(sum.VI10_p[["Cov"]]) %>% as.data.frame() %>% rownames_to_column() %>% pivot_longer(cols= !rowname, names_to = c(".value", "quantile"), names_sep = -3)

sum.VI11_p <-summary(RQ1letterid_I11_p, covariance = T, R = 50)
Cov.VI11_p <-as.data.frame(sum.VI11_p[["Cov"]]) %>% as.data.frame() %>% rownames_to_column() %>% pivot_longer(cols= !rowname, names_to = c(".value", "quantile"), names_sep = -3)

sum.VI12_p <-summary(RQ1letterid_I12_p, covariance = T, R = 50)
Cov.VI12_p <-as.data.frame(sum.VI12_p[["Cov"]]) %>% as.data.frame() %>% rownames_to_column() %>% pivot_longer(cols= !rowname, names_to = c(".value", "quantile"), names_sep = -3)

sum.VI13_p <-summary(RQ1letterid_I13_p, covariance = T, R = 50)
Cov.VI13_p <-as.data.frame(sum.VI13_p[["Cov"]]) %>% as.data.frame() %>% rownames_to_column() %>% pivot_longer(cols= !rowname, names_to = c(".value", "quantile"), names_sep = -3)

sum.VI14_p <-summary(RQ1letterid_I14_p, covariance = T, R = 50)
Cov.VI14_p <-as.data.frame(sum.VI14_p[["Cov"]]) %>% as.data.frame() %>% rownames_to_column() %>% pivot_longer(cols= !rowname, names_to = c(".value", "quantile"), names_sep = -3)

sum.VI15_p <-summary(RQ1letterid_I15_p, covariance = T, R = 50)
Cov.VI15_p <-as.data.frame(sum.VI15_p[["Cov"]]) %>% as.data.frame() %>% rownames_to_column() %>% pivot_longer(cols= !rowname, names_to = c(".value", "quantile"), names_sep = -3)

sum.VI16_p <-summary(RQ1letterid_I16_p, covariance = T, R = 50)
Cov.VI16_p <-as.data.frame(sum.VI16_p[["Cov"]]) %>% as.data.frame() %>% rownames_to_column() %>% pivot_longer(cols= !rowname, names_to = c(".value", "quantile"), names_sep = -3)

sum.VI17_p <-summary(RQ1letterid_I17_p, covariance = T, R = 50)
Cov.VI17_p <-as.data.frame(sum.VI17_p[["Cov"]]) %>% as.data.frame() %>% rownames_to_column() %>% pivot_longer(cols= !rowname, names_to = c(".value", "quantile"), names_sep = -3)

sum.VI18_p <-summary(RQ1letterid_I18_p, covariance = T, R = 50)
Cov.VI18_p <-as.data.frame(sum.VI18_p[["Cov"]]) %>% as.data.frame() %>% rownames_to_column() %>% pivot_longer(cols= !rowname, names_to = c(".value", "quantile"), names_sep = -3)

sum.VI19_p <-summary(RQ1letterid_I19_p, covariance = T, R = 50)
Cov.VI19_p <-as.data.frame(sum.VI19_p[["Cov"]]) %>% as.data.frame() %>% rownames_to_column() %>% pivot_longer(cols= !rowname, names_to = c(".value", "quantile"), names_sep = -3)

sum.VI20_p <-summary(RQ1letterid_I20_p, covariance = T, R = 50)
Cov.VI20_p <-as.data.frame(sum.VI20_p[["Cov"]]) %>% as.data.frame() %>% rownames_to_column() %>% pivot_longer(cols= !rowname, names_to = c(".value", "quantile"), names_sep = -3)

sum.VI21_p <-summary(RQ1letterid_I21_p, covariance = T, R = 50)
Cov.VI21_p <-as.data.frame(sum.VI21_p[["Cov"]]) %>% as.data.frame() %>% rownames_to_column() %>% pivot_longer(cols= !rowname, names_to = c(".value", "quantile"), names_sep = -3)

sum.VI22_p <-summary(RQ1letterid_I22_p, covariance = T, R = 50)
Cov.VI22_p <-as.data.frame(sum.VI22_p[["Cov"]]) %>% as.data.frame() %>% rownames_to_column() %>% pivot_longer(cols= !rowname, names_to = c(".value", "quantile"), names_sep = -3)

sum.VI23_p <-summary(RQ1letterid_I23_p, covariance = T, R = 50)
Cov.VI23_p <-as.data.frame(sum.VI23_p[["Cov"]]) %>% as.data.frame() %>% rownames_to_column() %>% pivot_longer(cols= !rowname, names_to = c(".value", "quantile"), names_sep = -3)

sum.VI24_p <-summary(RQ1letterid_I24_p, covariance = T, R = 50)
Cov.VI24_p <-as.data.frame(sum.VI24_p[["Cov"]]) %>% as.data.frame() %>% rownames_to_column() %>% pivot_longer(cols= !rowname, names_to = c(".value", "quantile"), names_sep = -3)

sum.VI25_p <-summary(RQ1letterid_I25_p, covariance = T, R = 50)
Cov.VI25_p <-as.data.frame(sum.VI25_p[["Cov"]]) %>% as.data.frame() %>% rownames_to_column() %>% pivot_longer(cols= !rowname, names_to = c(".value", "quantile"), names_sep = -3)

#combine all COV matrices into one giant thing
all.cov.V_p <- Cov.VI1_p %>% full_join(Cov.VI2_p) %>% full_join(Cov.VI3_p) %>% full_join(Cov.VI4_p) %>% full_join(Cov.VI5_p) %>% full_join(Cov.VI6_p) %>% full_join(Cov.VI7_p) %>% full_join(Cov.VI8_p) %>% full_join(Cov.VI9_p) %>% full_join(Cov.VI10_p) %>% full_join(Cov.VI11_p) %>% full_join(Cov.VI12_p) %>% full_join(Cov.VI13_p) %>% full_join(Cov.VI14_p) %>% full_join(Cov.VI15_p) %>% full_join(Cov.VI16_p) %>% full_join(Cov.VI17_p) %>% full_join(Cov.VI18_p) %>% full_join(Cov.VI19_p) %>% full_join(Cov.VI20_p) %>% full_join(Cov.VI21_p) %>% full_join(Cov.VI22_p) %>% full_join(Cov.VI23_p) %>% full_join(Cov.VI24_p) %>% full_join(Cov.VI25_p) %>% filter(rowname != "reStruct1") %>% filter(rowname != "scale") %>% select(-c("reStruct1.", "scale."))
                                   
#get avergaes per variable and per quantile # need to resoirt the rowname variable to preserve original order

cov_pool.V_p <- all.cov.V_p  %>% group_by(rowname, quantile) %>% summarise(across(everything(), mean)) %>% ungroup() %>% arrange(factor(rowname, levels = (c("(Intercept)", "treatment", "P201", "P301", "P501", "P601", "P901", "LW_Factor_1", "PV_Factor_1", "treatment:P201", "treatment:P301", "treatment:P501", "treatment:P601", "treatment:P901")))) %>% ungroup()


#quantify uncertainty due to missing data
  #VCOVmissing.data=∑mi=1(bi−bpool)T(bi−bpool)m−1
#calculate the difference for each coefficient from the pooled coefficient
coef.dev.VI1_p <- VI1_p.coef[2:14] - coef_pool.V_p[2:14] 
coef.dev.VI1_p <- coef.dev.VI1_p %>% mutate(quantile = seq(0.1, 0.9, 0.1))

coef.dev.VI2_p <- VI2_p.coef[2:14] - coef_pool.V_p[2:14] 
coef.dev.VI2_p <- coef.dev.VI2_p %>% mutate(quantile = seq(0.1, 0.9, 0.1))

coef.dev.VI3_p <- VI3_p.coef[2:14] - coef_pool.V_p[2:14] 
coef.dev.VI3_p <- coef.dev.VI3_p %>% mutate(quantile = seq(0.1, 0.9, 0.1))

coef.dev.VI4_p <- VI4_p.coef[2:14] - coef_pool.V_p[2:14] 
coef.dev.VI4_p <- coef.dev.VI4_p %>% mutate(quantile = seq(0.1, 0.9, 0.1))

coef.dev.VI5_p <- VI5_p.coef[2:14] - coef_pool.V_p[2:14] 
coef.dev.VI5_p <- coef.dev.VI5_p %>% mutate(quantile = seq(0.1, 0.9, 0.1))

coef.dev.VI6_p <- VI6_p.coef[2:14] - coef_pool.V_p[2:14] 
coef.dev.VI6_p <- coef.dev.VI6_p %>% mutate(quantile = seq(0.1, 0.9, 0.1))

coef.dev.VI7_p <- VI7_p.coef[2:14] - coef_pool.V_p[2:14] 
coef.dev.VI7_p <- coef.dev.VI7_p %>% mutate(quantile = seq(0.1, 0.9, 0.1))

coef.dev.VI8_p <- VI8_p.coef[2:14] - coef_pool.V_p[2:14] 
coef.dev.VI8_p <- coef.dev.VI8_p %>% mutate(quantile = seq(0.1, 0.9, 0.1))

coef.dev.VI9_p <- VI9_p.coef[2:14] - coef_pool.V_p[2:14] 
coef.dev.VI9_p <- coef.dev.VI9_p %>% mutate(quantile = seq(0.1, 0.9, 0.1))

coef.dev.VI10_p <- VI10_p.coef[2:14] - coef_pool.V_p[2:14] 
coef.dev.VI10_p <- coef.dev.VI10_p %>% mutate(quantile = seq(0.1, 0.9, 0.1))

coef.dev.VI11_p <- VI11_p.coef[2:14] - coef_pool.V_p[2:14] 
coef.dev.VI11_p <- coef.dev.VI11_p %>% mutate(quantile = seq(0.1, 0.9, 0.1))

coef.dev.VI12_p <- VI12_p.coef[2:14] - coef_pool.V_p[2:14] 
coef.dev.VI12_p <- coef.dev.VI12_p %>% mutate(quantile = seq(0.1, 0.9, 0.1))

coef.dev.VI13_p <- VI13_p.coef[2:14] - coef_pool.V_p[2:14] 
coef.dev.VI13_p <- coef.dev.VI13_p %>% mutate(quantile = seq(0.1, 0.9, 0.1))

coef.dev.VI14_p <- VI14_p.coef[2:14] - coef_pool.V_p[2:14] 
coef.dev.VI14_p <- coef.dev.VI14_p %>% mutate(quantile = seq(0.1, 0.9, 0.1))

coef.dev.VI15_p <- VI15_p.coef[2:14] - coef_pool.V_p[2:14] 
coef.dev.VI15_p <- coef.dev.VI15_p %>% mutate(quantile = seq(0.1 ,0.9, 0.1)) 

coef.dev.VI16_p <- VI16_p.coef[2:14] - coef_pool.V_p[2:14] 
coef.dev.VI16_p <- coef.dev.VI16_p %>% mutate(quantile = seq(0.1 ,0.9, 0.1))

coef.dev.VI17_p <- VI17_p.coef[2:14] - coef_pool.V_p[2:14] 
coef.dev.VI17_p <- coef.dev.VI17_p %>% mutate(quantile = seq(0.1 ,0.9, 0.1))

coef.dev.VI18_p <- VI18_p.coef[2:14] - coef_pool.V_p[2:14]
coef.dev.VI18_p <- coef.dev.VI18_p %>% mutate(quantile = seq(0.1 ,0.9, 0.1))

coef.dev.VI19_p <- VI19_p.coef[2:14] - coef_pool.V_p[2:14] 
coef.dev.VI19_p <- coef.dev.VI19_p %>% mutate(quantile = seq(0.1 ,0.9, 0.1))

coef.dev.VI20_p <- VI20_p.coef[2:14] - coef_pool.V_p[2:14] 
coef.dev.VI20_p <- coef.dev.VI20_p %>% mutate(quantile = seq(0.1 ,0.9, 0.1))

coef.dev.VI21_p <- VI21_p.coef[2:14] - coef_pool.V_p[2:14] 
coef.dev.VI21_p <- coef.dev.VI21_p %>% mutate(quantile = seq(0.1 ,0.9, 0.1))

coef.dev.VI22_p <- VI22_p.coef[2:14] - coef_pool.V_p[2:14] 
coef.dev.VI22_p <- coef.dev.VI22_p %>% mutate(quantile = seq(0.1 ,0.9, 0.1))

coef.dev.VI23_p <- VI23_p.coef[2:14] - coef_pool.V_p[2:14] 
coef.dev.VI23_p <- coef.dev.VI23_p %>% mutate(quantile = seq(0.1 ,0.9, 0.1))

coef.dev.VI24_p <- VI24_p.coef[2:14] - coef_pool.V_p[2:14] 
coef.dev.VI24_p <- coef.dev.VI24_p %>% mutate(quantile = seq(0.1 ,0.9, 0.1))

coef.dev.VI25_p <- VI25_p.coef[2:14] - coef_pool.V_p[2:14] 
coef.dev.VI25_p <- coef.dev.VI25_p %>% mutate(quantile = seq(0.1 ,0.9, 0.1))

#combine all coef dev matrices into one giant thing
all.coef.dev.V_p <- coef.dev.VI1_p %>% full_join(coef.dev.VI2_p)  %>% full_join(coef.dev.VI3_p) %>% full_join(coef.dev.VI4_p) %>% full_join(coef.dev.VI5_p) %>% full_join(coef.dev.VI6_p) %>% full_join(coef.dev.VI7_p) %>% full_join(coef.dev.VI8_p) %>% full_join(coef.dev.VI9_p) %>% full_join(coef.dev.VI10_p) %>% full_join(coef.dev.VI11_p) %>% full_join(coef.dev.VI12_p) %>% full_join(coef.dev.VI13_p) %>% full_join(coef.dev.VI14_p) %>% full_join(coef.dev.VI15_p) %>% full_join(coef.dev.VI16_p)  %>% full_join(coef.dev.VI17_p) %>% full_join(coef.dev.VI18_p) %>% full_join(coef.dev.VI19_p) %>% full_join(coef.dev.VI20_p) %>% full_join(coef.dev.VI21_p) %>% full_join(coef.dev.VI22_p) %>% full_join(coef.dev.VI23_p) %>% full_join(coef.dev.VI24_p) %>% full_join(coef.dev.VI25_p) 

#separate VCOV and ceof.dev by quantile because I am a bad coder
all.coef.dev.q1.V_p <- all.coef.dev.V_p %>% filter(quantile == 0.1) %>% select (-quantile) %>% as.matrix()
all.coef.dev.q2.V_p <- all.coef.dev.V_p %>% filter(quantile == 0.2) %>% select (-quantile) %>% as.matrix()
all.coef.dev.q3.V_p <- all.coef.dev.V_p %>% filter(quantile == "0.3")  %>% select (-quantile) %>% as.matrix() #not sure why it won't give anything with just the numbers
all.coef.dev.q4.V_p <- all.coef.dev.V_p %>% filter(quantile == 0.4) %>% select (-quantile) %>% as.matrix()
all.coef.dev.q5.V_p <- all.coef.dev.V_p %>% filter(quantile == 0.5) %>% select (-quantile) %>% as.matrix()
all.coef.dev.q6.V_p <- all.coef.dev.V_p %>% filter(quantile == 0.6)%>% select (-quantile) %>% as.matrix()
all.coef.dev.q7.V_p <- all.coef.dev.V_p %>% filter(quantile == "0.7")%>% select (-quantile) %>% as.matrix()
all.coef.dev.q8.V_p <- all.coef.dev.V_p %>% filter(quantile == 0.8) %>% select (-quantile) %>% as.matrix()
all.coef.dev.q9.V_p <- all.coef.dev.V_p %>% filter(quantile == 0.9)%>% select (-quantile) %>% as.matrix()

all.cov.q1.V_p <- cov_pool.V_p %>% filter(quantile == 0.1) %>% select(-c("rowname", "quantile")) %>% as.matrix()
all.cov.q2.V_p <- cov_pool.V_p %>% filter(quantile == 0.2) %>% select(-c("rowname", "quantile")) %>% as.matrix()
all.cov.q3.V_p <- cov_pool.V_p %>% filter(quantile == 0.3) %>% select(-c("rowname", "quantile")) %>% as.matrix()
all.cov.q4.V_p <- cov_pool.V_p %>% filter(quantile == 0.4) %>% select(-c("rowname", "quantile")) %>% as.matrix()
all.cov.q5.V_p <- cov_pool.V_p %>% filter(quantile == 0.5) %>% select(-c("rowname", "quantile")) %>% as.matrix()
all.cov.q6.V_p <- cov_pool.V_p %>% filter(quantile == 0.6) %>% select(-c("rowname", "quantile")) %>% as.matrix()
all.cov.q7.V_p <- cov_pool.V_p %>% filter(quantile == 0.7) %>% select(-c("rowname", "quantile")) %>% as.matrix()
all.cov.q8.V_p <- cov_pool.V_p %>% filter(quantile == 0.8) %>% select(-c("rowname", "quantile")) %>% as.matrix()
all.cov.q9.V_p <- cov_pool.V_p %>% filter(quantile == 0.9) %>% select(-c("rowname", "quantile")) %>% as.matrix()

#multiply matrix by its inverse, do this separately per quantile
coef.dev2.q1.V_p <- t(all.coef.dev.q1.V_p) %*% all.coef.dev.q1.V_p
coef.dev2.q2.V_p <- t(all.coef.dev.q2.V_p) %*% all.coef.dev.q2.V_p
coef.dev2.q3.V_p <- t(all.coef.dev.q3.V_p) %*% all.coef.dev.q3.V_p
coef.dev2.q4.V_p <- t(all.coef.dev.q4.V_p) %*% all.coef.dev.q4.V_p
coef.dev2.q5.V_p <- t(all.coef.dev.q5.V_p) %*% all.coef.dev.q5.V_p
coef.dev2.q6.V_p <- t(all.coef.dev.q6.V_p) %*% all.coef.dev.q6.V_p
coef.dev2.q7.V_p <- t(all.coef.dev.q7.V_p) %*% all.coef.dev.q7.V_p
coef.dev2.q8.V_p <- t(all.coef.dev.q8.V_p) %*% all.coef.dev.q8.V_p
coef.dev2.q9.V_p <- t(all.coef.dev.q9.V_p) %*% all.coef.dev.q9.V_p

#divide the sum of the above by #of imputations - 1 (14). to get missing data number of imputations error
mdni.error.q1.V_p <- coef.dev2.q1.V_p / 24
mdni.error.q2.V_p <- coef.dev2.q2.V_p / 24
mdni.error.q3.V_p <- coef.dev2.q3.V_p / 24
mdni.error.q4.V_p <- coef.dev2.q4.V_p / 24
mdni.error.q5.V_p <- coef.dev2.q5.V_p / 24
mdni.error.q6.V_p <- coef.dev2.q6.V_p / 24
mdni.error.q7.V_p <- coef.dev2.q7.V_p / 24
mdni.error.q8.V_p <- coef.dev2.q8.V_p / 24
mdni.error.q9.V_p <- coef.dev2.q9.V_p / 24

#divide the mdni  by total number of imputations to get error due to finite sampling
mdfs.error.q1.V_p <- mdni.error.q1.V_p / 25
mdfs.error.q2.V_p <- mdni.error.q2.V_p / 25
mdfs.error.q3.V_p <- mdni.error.q3.V_p / 25
mdfs.error.q4.V_p <- mdni.error.q4.V_p / 25
mdfs.error.q5.V_p <- mdni.error.q5.V_p / 25
mdfs.error.q6.V_p <- mdni.error.q6.V_p / 25
mdfs.error.q7.V_p <- mdni.error.q7.V_p / 25
mdfs.error.q8.V_p <- mdni.error.q8.V_p / 25
mdfs.error.q9.V_p <- mdni.error.q9.V_p / 25

#add the three error components to get total error
total.error.q1.V_p <- all.cov.q1.V_p + mdni.error.q1.V_p + mdfs.error.q1.V_p
total.error.q2.V_p <- all.cov.q2.V_p + mdni.error.q2.V_p + mdfs.error.q2.V_p
total.error.q3.V_p <- all.cov.q3.V_p + mdni.error.q3.V_p + mdfs.error.q3.V_p
total.error.q4.V_p <- all.cov.q4.V_p + mdni.error.q4.V_p + mdfs.error.q4.V_p
total.error.q5.V_p <- all.cov.q5.V_p + mdni.error.q5.V_p + mdfs.error.q5.V_p
total.error.q6.V_p <- all.cov.q6.V_p + mdni.error.q6.V_p + mdfs.error.q6.V_p
total.error.q7.V_p <- all.cov.q7.V_p + mdni.error.q7.V_p + mdfs.error.q7.V_p
total.error.q8.V_p <- all.cov.q8.V_p + mdni.error.q8.V_p + mdfs.error.q8.V_p
total.error.q9.V_p <- all.cov.q9.V_p + mdni.error.q9.V_p + mdfs.error.q9.V_p

fmi.q1.V_p <- (diag(mdni.error.q1.V_p) + diag(mdfs.error.q1.V_p)) / diag(total.error.q1.V_p)
fmi.q2.V_p <- (diag(mdni.error.q2.V_p) + diag(mdfs.error.q2.V_p)) / diag(total.error.q2.V_p)
fmi.q3.V_p <- (diag(mdni.error.q3.V_p) + diag(mdfs.error.q3.V_p)) / diag(total.error.q3.V_p)
fmi.q4.V_p <- (diag(mdni.error.q4.V_p) + diag(mdfs.error.q4.V_p)) / diag(total.error.q4.V_p)
fmi.q5.V_p <- (diag(mdni.error.q5.V_p) + diag(mdfs.error.q5.V_p)) / diag(total.error.q5.V_p)
fmi.q6.V_p <- (diag(mdni.error.q6.V_p) + diag(mdfs.error.q6.V_p)) / diag(total.error.q6.V_p)
fmi.q7.V_p <- (diag(mdni.error.q7.V_p) + diag(mdfs.error.q7.V_p)) / diag(total.error.q7.V_p)
fmi.q8.V_p <- (diag(mdni.error.q8.V_p) + diag(mdfs.error.q8.V_p)) / diag(total.error.q8.V_p)
fmi.q9.V_p <- (diag(mdni.error.q9.V_p) + diag(mdfs.error.q9.V_p)) / diag(total.error.q9.V_p)


#then calculate the appropriate degrees of freedom
#dfimputation=1/1m−1FMI2+1(1−FMI)(dfcomplete+1dfcomplete+3)dfcomplete

df_com.V_p <- sum.VI1_p$rdf %>% as.numeric() #same for all quantiles

v.q1.V_p <- (24) / fmi.q1.V_p^2
vhat.q1.V_p <- (1 - fmi.q1.V_p) * ((df_com.V_p + 1) / (df_com.V_p + 3)) * df_com.V_p
df_imp.q1.V_p <- (v.q1.V_p^(-1) + vhat.q1.V_p^(-1))^(-1)

v.q2.V_p <- (24) / fmi.q2.V_p^2
vhat.q2.V_p <- (1 - fmi.q2.V_p) * ((df_com.V_p + 1) / (df_com.V_p + 3)) * df_com.V_p
df_imp.q2.V_p <- (v.q2.V_p^(-1) + vhat.q2.V_p^(-1))^(-1)

v.q3.V_p <- (24) / fmi.q3.V_p^2
vhat.q3.V_p <- (1 - fmi.q3.V_p) * ((df_com.V_p + 1) / (df_com.V_p + 3)) * df_com.V_p
df_imp.q3.V_p <- (v.q3.V_p^(-1) + vhat.q3.V_p^(-1))^(-1)

v.q4.V_p <- (24) / fmi.q4.V_p^2
vhat.q4.V_p <- (1 - fmi.q4.V_p) * ((df_com.V_p + 1) / (df_com.V_p + 3)) * df_com.V_p
df_imp.q4.V_p <- (v.q4.V_p^(-1) + vhat.q4.V_p^(-1))^(-1)

v.q5.V_p <- (24) / fmi.q5.V_p^2
vhat.q5.V_p <- (1 - fmi.q5.V_p) * ((df_com.V_p + 1) / (df_com.V_p + 3)) * df_com.V_p
df_imp.q5.V_p <- (v.q5.V_p^(-1) + vhat.q5.V_p^(-1))^(-1)

v.q6.V_p <- (24) / fmi.q6.V_p^2
vhat.q6.V_p <- (1 - fmi.q6.V_p) * ((df_com.V_p + 1) / (df_com.V_p + 3)) * df_com.V_p
df_imp.q6.V_p <- (v.q6.V_p^(-1) + vhat.q6.V_p^(-1))^(-1)

v.q7.V_p <- (24) / fmi.q7.V_p^2
vhat.q7.V_p <- (1 - fmi.q7.V_p) * ((df_com.V_p + 1) / (df_com.V_p + 3)) * df_com.V_p
df_imp.q7.V_p <- (v.q7.V_p^(-1) + vhat.q4.V_p^(-1))^(-1)

v.q8.V_p <- (24) / fmi.q8.V_p^2
vhat.q8.V_p <- (1 - fmi.q8.V_p) * ((df_com.V_p + 1) / (df_com.V_p + 3)) * df_com.V_p
df_imp.q8.V_p <- (v.q8.V_p^(-1) + vhat.q8.V_p^(-1))^(-1)

v.q9.V_p <- (24) / fmi.q9.V_p^2
vhat.q9.V_p <- (1 - fmi.q9.V_p) * ((df_com.V_p + 1) / (df_com.V_p + 3)) * df_com.V_p
df_imp.q9.V_p <- (v.q9.V_p^(-1) + vhat.q9.V_p^(-1))^(-1)

#now get the SE, t, and p values
(se.pool.q1.V_p <- sqrt(diag(total.error.q1.V_p)))
t.pool.q1.V_p <- coef_pool.V_p[1,2:14] / se.pool.q1.V_p 
t.pool.q1.V.abs_p <- abs(t.pool.q1.V_p) %>% as.matrix()
p.pool.q1.V_p <- 2 * pt(q = t.pool.q1.V.abs_p, df = df_imp.q1.V_p, lower.tail = F)

(se.pool.q2.V_p <- sqrt(diag(total.error.q2.V_p)))
t.pool.q2.V_p <- coef_pool.V_p[2,2:14] / se.pool.q2.V_p 
t.pool.q2.V.abs_p <- abs(t.pool.q2.V_p) %>% as.matrix()
p.pool.q2.V_p <- 2 * pt(q = t.pool.q2.V.abs_p, df = df_imp.q2.V_p, lower.tail = F)

(se.pool.q3.V_p <- sqrt(diag(total.error.q3.V_p)))
t.pool.q3.V_p <- coef_pool.V_p[3,2:14] / se.pool.q3.V_p
t.pool.q3.V.abs_p <- abs(t.pool.q3.V_p) %>% as.matrix()
p.pool.q3.V_p <- 2 * pt(q = t.pool.q3.V.abs_p, df = df_imp.q3.V_p, lower.tail = F)

(se.pool.q4.V_p <- sqrt(diag(total.error.q4.V_p)))
t.pool.q4.V_p <- coef_pool.V_p[4,2:14] / se.pool.q4.V_p 
t.pool.q4.V.abs_p <- abs(t.pool.q4.V_p) %>% as.matrix()
p.pool.q4.V_p <- 2 * pt(q = t.pool.q4.V.abs_p, df = df_imp.q4.V_p, lower.tail = F)

(se.pool.q5.V_p <- sqrt(diag(total.error.q5.V_p)))
t.pool.q5.V_p <- coef_pool.V_p[5,2:14] / se.pool.q5.V_p 
t.pool.q5.V.abs_p <- abs(t.pool.q5.V_p) %>% as.matrix()
p.pool.q5.V_p <- 2 * pt(q = t.pool.q5.V.abs_p, df = df_imp.q5.V_p, lower.tail = F)

(se.pool.q6.V_p <- sqrt(diag(total.error.q6.V_p)))
t.pool.q6.V_p <- coef_pool.V_p[6,2:14] / se.pool.q6.V_p 
t.pool.q6.V.abs_p <- abs(t.pool.q6.V_p) %>% as.matrix()
p.pool.q6.V_p <- 2 * pt(q = t.pool.q6.V.abs_p, df = df_imp.q6.V_p, lower.tail = F)

(se.pool.q7.V_p <- sqrt(diag(total.error.q7.V_p)))
t.pool.q7.V_p <- coef_pool.V_p[7,2:14] / se.pool.q7.V_p
t.pool.q7.V.abs_p <- abs(t.pool.q7.V_p) %>% as.matrix()
p.pool.q7.V_p <- 2 * pt(q = t.pool.q7.V.abs_p, df = df_imp.q7.V_p, lower.tail = F)

(se.pool.q8.V_p <- sqrt(diag(total.error.q8.V_p)))
t.pool.q8.V_p <- coef_pool.V_p[8,2:14] / se.pool.q8.V_p
t.pool.q8.V.abs_p <- abs(t.pool.q8.V_p) %>% as.matrix()
p.pool.q8.V_p <- 2 * pt(q = t.pool.q8.V.abs_p, df = df_imp.q8.V_p, lower.tail = F)

(se.pool.q9.V_p <- sqrt(diag(total.error.q9.V_p)))
t.pool.q9.V_p <- coef_pool.V_p[9,2:14] / se.pool.q9.V_p
t.pool.q9.V.abs_p <- abs(t.pool.q9.V_p) %>% as.matrix()
p.pool.q9.V_p <- 2 * pt(q = t.pool.q9.V.abs_p, df = df_imp.q9.V_p, lower.tail = F)

# As well as our confidence intervals for parameter estimation.
# 
crit.pool.q1.V_p <- qt(p = 1 - (.05 / 2), df = df_imp.q1.V_p, lower.tail = T)
hw.pool.q1.V_p <- se.pool.q1.V * crit.pool.q1.V
ci_pool.q1.V_p <- cbind("lwr" = coef_pool.V_p[1,2:14] - hw.pool.q1.V_p, "upr" = coef_pool.V_p[1,2:14] + hw.pool.q1.V_p) %>% mutate(quantile = "0.1")

crit.pool.q2.V_p <- qt(p = 1 - (.05 / 2), df = df_imp.q2.V_p, lower.tail = T)
hw.pool.q2.V_p <- se.pool.q2.V_p * crit.pool.q2.V_p
ci_pool.q2.V_p <- cbind("lwr" = coef_pool.V_p[2,2:14] - hw.pool.q2.V_p, "upr" = coef_pool.V_p[2,2:14] + hw.pool.q2.V_p)%>% mutate(quantile = "0.2")

crit.pool.q3.V_p <- qt(p = 1 - (.05 / 2), df = df_imp.q3.V_p, lower.tail = T)
hw.pool.q3.V_p <- se.pool.q3.V_p * crit.pool.q3.V_p
ci_pool.q3.V_p <- cbind("lwr" = coef_pool.V_p[3,2:14] - hw.pool.q3.V_p, "upr" = coef_pool.V_p[3,2:14] + hw.pool.q3.V_p) %>% mutate(quantile = "0.3")

crit.pool.q4.V_p <- qt(p = 1 - (.05 / 2), df = df_imp.q4.V_p, lower.tail = T)
hw.pool.q4.V_p <- se.pool.q4.V_p * crit.pool.q4.V_p
ci_pool.q4.V_p <- cbind("lwr" = coef_pool.V_p[4,2:14] - hw.pool.q4.V_p, "upr" = coef_pool.V_p[4,2:14] + hw.pool.q4.V_p) %>% mutate(quantile = "0.4")

crit.pool.q5.V_p <- qt(p = 1 - (.05 / 2), df = df_imp.q5.V_p, lower.tail = T)
hw.pool.q5.V_p <- se.pool.q5.V_p * crit.pool.q5.V_p
ci_pool.q5.V_p <- cbind("lwr" = coef_pool.V_p[5,2:14] - hw.pool.q5.V_p, "upr" = coef_pool.V_p[5,2:14] + hw.pool.q5.V_p) %>% mutate(quantile = "0.5")

crit.pool.q6.V_p <- qt(p = 1 - (.05 / 2), df = df_imp.q6.V_p, lower.tail = T)
hw.pool.q6.V_p <- se.pool.q6.V_p * crit.pool.q6.V_p
ci_pool.q6.V_p <- cbind("lwr" = coef_pool.V_p[6,2:14] - hw.pool.q6.V_p, "upr" = coef_pool.V_p[6,2:14] + hw.pool.q6.V_p) %>% mutate(quantile = "0.6")

crit.pool.q7.V_p <- qt(p = 1 - (.05 / 2), df = df_imp.q7.V_p, lower.tail = T)
hw.pool.q7.V_p <- se.pool.q7.V_p * crit.pool.q7.V_p
ci_pool.q7.V_p <- cbind("lwr" = coef_pool.V_p[7,2:14] - hw.pool.q7.V_p, "upr" = coef_pool.V_p[7,2:14] + hw.pool.q7.V_p) %>% mutate(quantile = "0.7")

crit.pool.q8.V_p <- qt(p = 1 - (.05 / 2), df = df_imp.q8.V_p, lower.tail = T)
hw.pool.q8.V_p <- se.pool.q8.V_p * crit.pool.q8.V_p
ci_pool.q8.V_p <- cbind("lwr" = coef_pool.V_p[8,2:14] - hw.pool.q8.V_p, "upr" = coef_pool.V_p[8,2:14] + hw.pool.q8.V_p) %>% mutate(quantile = "0.8")

crit.pool.q9.V_p <- qt(p = 1 - (.05 / 2), df = df_imp.q9.V_p, lower.tail = T)
hw.pool.q9.V_p <- se.pool.q9.V_p * crit.pool.q9.V_p
ci_pool.q9.V_p <- cbind("lwr" = coef_pool.V_p[9,2:14] - hw.pool.q9.V_p, "upr" = coef_pool.V_p[9,2:14] + hw.pool.q9.V_p) %>% mutate(quantile = "0.9")

# Do the B-H correction on all the adjusted p-values
##First get all p-values into one dataframe
### Coefficients for this model are: "(Intercept)", "treatment", "P201", "P301", "P501", "P601", "P901", "LW_Factor_1", "PV_Factor_1"
## Each quantile has 9 coefficients, so 9*9 = 81 coefficients with p values in total.
p_values_RQ1_LW_p<- p.pool.q1.V_p %>% rbind(p.pool.q2.V_p) %>% rbind(p.pool.q3.V_p) %>% rbind(p.pool.q4.V_p) %>% rbind(p.pool.q5.V_p) %>% rbind(p.pool.q6.V_p) %>% rbind(p.pool.q7.V_p) %>% rbind(p.pool.q8.V_p) %>% rbind (p.pool.q9.V_p)  
fdrs_RQ1_LW_p <- p.adjust(p_values_RQ1_LW_p, method="BH") 
p_values_adj_RQ1_LW_p <- matrix(unlist(fdrs_RQ1_LW_p), nrow=9) %>% round(3) %>% as.data.frame()  %>% mutate(quant=c(1, 2, 3, 4, 5, 6, 7, 8, 9))
colnames(p_values_adj_RQ1_LW_p) <- c("(Intercept)_adj", "treatment_adj", "P201_adj", "P501_adj", "P601_adj", "P901_adj", "P301_adj", "LW_Factor_1_adj", "PV_Factor_1_adj", "treatment:P201_adj", "treatment:P501_adj", "treatment:P601_adj", "treatment:P901_adj", "Quantile")
p_values_RQ1_LW_wquant_p <- p_values_RQ1_LW_p %>% as.data.frame() %>% mutate(quant=c(1, 2, 3, 4, 5, 6, 7, 8, 9))

(p_values_RQ1_LW_all_p <- p_values_RQ1_LW_wquant_p %>% cbind(p_values_adj_RQ1_LW_p))

all_ci.V_p <- ci_pool.q1.V_p %>% rbind(ci_pool.q2.V_p) %>% rbind(ci_pool.q3.V_p) %>% rbind(ci_pool.q4.V_p) %>% rbind(ci_pool.q5.V_p) %>% rbind(ci_pool.q6.V_p) %>% rbind(ci_pool.q7.V_p) %>% rbind(ci_pool.q8.V_p) %>% rbind(ci_pool.q9.V_p)

(all_new.V_p <- all_ci.V_p %>% full_join(coef_pool.V_p, join_by(quantile == rowname)))

```
```{r sensitivityProject RQ2Vocab}
RQ2vocab_I1_p <- lqmm(PV_Factor_3 ~ treatment*(LW_Factor_1 + PV_Factor_1 + P201 + P501 + P601 + P901) + P301 , random = ~1, group = TID, tau = c(1:9/10), data = QR_data_imp_I1, control = list(UP_max_iter = 1000, UP_tol = 1e-6))
RQ2vocab_I2_p <- lqmm(PV_Factor_3 ~ treatment*(LW_Factor_1 + PV_Factor_1 + P201 + P501 + P601 + P901) + P301 , random = ~1, group = TID, tau = c(1:9/10), data = QR_data_imp_I2, control = list(UP_max_iter = 1000, UP_tol = 1e-6))
RQ2vocab_I3_p <- lqmm(PV_Factor_3 ~ treatment*(LW_Factor_1 + PV_Factor_1 + P201 + P501 + P601 + P901) + P301 , random = ~1, group = TID, tau = c(1:9/10), data = QR_data_imp_I3, control = list(UP_max_iter = 1000, UP_tol = 1e-6))
RQ2vocab_I4_p <- lqmm(PV_Factor_3 ~ treatment*(LW_Factor_1 + PV_Factor_1 + P201 + P501 + P601 + P901) + P301 , random = ~1, group = TID, tau = c(1:9/10), data = QR_data_imp_I4, control = list(LP_max_iter = 10000, lP_tol = 1e-10))
RQ2vocab_I5_p <- lqmm(PV_Factor_3 ~ treatment*(LW_Factor_1 + PV_Factor_1 + P201 + P501 + P601 + P901) + P301 , random = ~1, group = TID, tau = c(1:9/10), data = QR_data_imp_I5, control = list(LP_max_iter = 1000, LP_tol = 1e-6))
RQ2vocab_I6_p <- lqmm(PV_Factor_3 ~ treatment*(LW_Factor_1 + PV_Factor_1 + P201 + P501 + P601 + P901) + P301 , random = ~1, group = TID, tau = c(1:9/10), data = QR_data_imp_I6, control = list(UP_max_iter = 1000, UP_tol = 1e-7))
RQ2vocab_I7_p <- lqmm(PV_Factor_3 ~ treatment*(LW_Factor_1 + PV_Factor_1 + P201 + P501 + P601 + P901) + P301 , random = ~1, group = TID, tau = c(1:9/10), data = QR_data_imp_I7, control = list(UP_max_iter = 1000, UP_tol = 1e-7))
RQ2vocab_I8_p <- lqmm(PV_Factor_3 ~ treatment*(LW_Factor_1 + PV_Factor_1 + P201 + P501 + P601 + P901) + P301 , random = ~1, group = TID, tau = c(1:9/10), data = QR_data_imp_I8, control = list(UP_max_iter = 1000, UP_tol = 1e-6))
RQ2vocab_I9_p <- lqmm(PV_Factor_3 ~ treatment*(LW_Factor_1 + PV_Factor_1 + P201 + P501 + P601 + P901) + P301 , random = ~1, group = TID, tau = c(1:9/10), data = QR_data_imp_I9, control = list(UP_max_iter = 1000, UP_tol = 1e-6))
RQ2vocab_I10_p <- lqmm(PV_Factor_3 ~ treatment*(LW_Factor_1 + PV_Factor_1 + P201 + P501 + P601 + P901) + P301 , random = ~1, group = TID, tau = c(1:9/10), data = QR_data_imp_I10, control = list(UP_max_iter = 1000, UP_tol = 1e-6))
RQ2vocab_I11_p <- lqmm(PV_Factor_3 ~ treatment*(LW_Factor_1 + PV_Factor_1 + P201 + P501 + P601 + P901) + P301 , random = ~1, group = TID, tau = c(1:9/10), data = QR_data_imp_I11, control = list(UP_max_iter = 1000, UP_tol = 1e-7))
RQ2vocab_I12_p <- lqmm(PV_Factor_3 ~ treatment*(LW_Factor_1 + PV_Factor_1 + P201 + P501 + P601 + P901) + P301 , random = ~1, group = TID, tau = c(1:9/10), data = QR_data_imp_I12, control = list(UP_max_iter = 1000, UP_tol = 1e-6))
RQ2vocab_I13_p <- lqmm(PV_Factor_3 ~ treatment*(LW_Factor_1 + PV_Factor_1 + P201 + P501 + P601 + P901) + P301 , random = ~1, group = TID, tau = c(1:9/10), data = QR_data_imp_I13, control = list(UP_max_iter = 1000, UP_tol = 1e-7))
RQ2vocab_I14_p <- lqmm(PV_Factor_3 ~ treatment*(LW_Factor_1 + PV_Factor_1 + P201 + P501 + P601 + P901) + P301 , random = ~1, group = TID, tau = c(1:9/10), data = QR_data_imp_I14, control = list(UP_max_iter = 1000, UP_tol = 1e-6))
RQ2vocab_I15_p <- lqmm(PV_Factor_3 ~ treatment*(LW_Factor_1 + PV_Factor_1 + P201 + P501 + P601 + P901) + P301 , random = ~1, group = TID, tau = c(1:9/10), data = QR_data_imp_I15, control = list(UP_max_iter = 1000, UP_tol = 1e-6))
RQ2vocab_I16_p <- lqmm(PV_Factor_3 ~ treatment*(LW_Factor_1 + PV_Factor_1 + P201 + P501 + P601 + P901) + P301 , random = ~1, group = TID, tau = c(1:9/10), data = QR_data_imp_I16, control = list(UP_max_iter = 1000, UP_tol = 1e-7))
RQ2vocab_I17_p <- lqmm(PV_Factor_3 ~ treatment*(LW_Factor_1 + PV_Factor_1 + P201 + P501 + P601 + P901) + P301 , random = ~1, group = TID, tau = c(1:9/10), data = QR_data_imp_I17, control = list(UP_max_iter = 1000, UP_tol = 1e-7))
RQ2vocab_I18_p <- lqmm(PV_Factor_3 ~ treatment*(LW_Factor_1 + PV_Factor_1 + P201 + P501 + P601 + P901) + P301 , random = ~1, group = TID, tau = c(1:9/10), data = QR_data_imp_I18, control = list(UP_max_iter = 1000, UP_tol = 1e-6))
RQ2vocab_I19_p <- lqmm(PV_Factor_3 ~ treatment*(LW_Factor_1 + PV_Factor_1 + P201 + P501 + P601 + P901) + P301 , random = ~1, group = TID, tau = c(1:9/10), data = QR_data_imp_I19, control = list(UP_max_iter = 1000, UP_tol = 1e-6))
RQ2vocab_I20_p <- lqmm(PV_Factor_3 ~ treatment*(LW_Factor_1 + PV_Factor_1 + P201 + P501 + P601 + P901) + P301 , random = ~1, group = TID, tau = c(1:9/10), data = QR_data_imp_I20, control = list(UP_max_iter = 1000, UP_tol = 1e-6))
RQ2vocab_I21_p <- lqmm(PV_Factor_3 ~ treatment*(LW_Factor_1 + PV_Factor_1 + P201 + P501 + P601 + P901) + P301 , random = ~1, group = TID, tau = c(1:9/10), data = QR_data_imp_I21, control = list(UP_max_iter = 1000, UP_tol = 1e-7))
RQ2vocab_I22_p <- lqmm(PV_Factor_3 ~ treatment*(LW_Factor_1 + PV_Factor_1 + P201 + P501 + P601 + P901) + P301 , random = ~1, group = TID, tau = c(1:9/10), data = QR_data_imp_I22, control = list(UP_max_iter = 1000, UP_tol = 1e-6))
RQ2vocab_I23_p <- lqmm(PV_Factor_3 ~ treatment*(LW_Factor_1 + PV_Factor_1 + P201 + P501 + P601 + P901) + P301 , random = ~1, group = TID, tau = c(1:9/10), data = QR_data_imp_I23, control = list(UP_max_iter = 1000, UP_tol = 1e-7))
RQ2vocab_I24_p <- lqmm(PV_Factor_3 ~ treatment*(LW_Factor_1 + PV_Factor_1 + P201 + P501 + P601 + P901) + P301 , random = ~1, group = TID, tau = c(1:9/10), data = QR_data_imp_I24, control = list(UP_max_iter = 1000, UP_tol = 1e-6))
RQ2vocab_I25_p <- lqmm(PV_Factor_3 ~ treatment*(LW_Factor_1 + PV_Factor_1 + P201 + P501 + P601 + P901) + P301 , random = ~1, group = TID, tau = c(1:9/10), data = QR_data_imp_I25, control = list(UP_max_iter = 1000, UP_tol = 1e-6))

#Extract the regression coefficients for each model
I1.coef.I_p <- coef.lqmm(RQ2vocab_I1_p) %>% t() %>% as.data.frame() %>% rownames_to_column() #transpose and make a datafrome
I2.coef.I_p <- coef.lqmm(RQ2vocab_I2_p) %>% t() %>% as.data.frame() %>% rownames_to_column()
I3.coef.I_p <- coef.lqmm(RQ2vocab_I3_p) %>% t() %>% as.data.frame() %>% rownames_to_column()
I4.coef.I_p <- coef.lqmm(RQ2vocab_I4_p) %>% t() %>% as.data.frame() %>% rownames_to_column()
I5.coef.I_p <- coef.lqmm(RQ2vocab_I5_p) %>% t() %>% as.data.frame() %>% rownames_to_column()
I6.coef.I_p <- coef.lqmm(RQ2vocab_I6_p) %>% t() %>% as.data.frame() %>% rownames_to_column()
I7.coef.I_p <- coef.lqmm(RQ2vocab_I7_p) %>% t() %>% as.data.frame() %>% rownames_to_column()
I8.coef.I_p <- coef.lqmm(RQ2vocab_I8_p) %>% t() %>% as.data.frame() %>% rownames_to_column()
I9.coef.I_p <- coef.lqmm(RQ2vocab_I9_p) %>% t() %>% as.data.frame() %>% rownames_to_column()
I10.coef.I_p <- coef.lqmm(RQ2vocab_I10_p) %>% t() %>% as.data.frame() %>% rownames_to_column()
I11.coef.I_p <- coef.lqmm(RQ2vocab_I11_p) %>% t() %>% as.data.frame() %>% rownames_to_column()
I12.coef.I_p <- coef.lqmm(RQ2vocab_I12_p) %>% t() %>% as.data.frame() %>% rownames_to_column()
I13.coef.I_p <- coef.lqmm(RQ2vocab_I13_p) %>% t() %>% as.data.frame() %>% rownames_to_column()
I14.coef.I_p <- coef.lqmm(RQ2vocab_I14_p) %>% t() %>% as.data.frame() %>% rownames_to_column()
I15.coef.I_p <- coef.lqmm(RQ2vocab_I15_p) %>% t() %>% as.data.frame() %>% rownames_to_column()
I16.coef.I_p <- coef.lqmm(RQ2vocab_I16_p) %>% t() %>% as.data.frame() %>% rownames_to_column()
I17.coef.I_p <- coef.lqmm(RQ2vocab_I17_p) %>% t() %>% as.data.frame() %>% rownames_to_column()
I18.coef.I_p <- coef.lqmm(RQ2vocab_I18_p) %>% t() %>% as.data.frame() %>% rownames_to_column()
I19.coef.I_p <- coef.lqmm(RQ2vocab_I19_p) %>% t() %>% as.data.frame() %>% rownames_to_column()
I20.coef.I_p <- coef.lqmm(RQ2vocab_I20_p) %>% t() %>% as.data.frame() %>% rownames_to_column()
I21.coef.I_p <- coef.lqmm(RQ2vocab_I21_p) %>% t() %>% as.data.frame() %>% rownames_to_column()
I22.coef.I_p <- coef.lqmm(RQ2vocab_I22_p) %>% t() %>% as.data.frame() %>% rownames_to_column()
I23.coef.I_p <- coef.lqmm(RQ2vocab_I23_p) %>% t() %>% as.data.frame() %>% rownames_to_column()
I24.coef.I_p <- coef.lqmm(RQ2vocab_I24_p) %>% t() %>% as.data.frame() %>% rownames_to_column()
I25.coef.I_p <- coef.lqmm(RQ2vocab_I25_p) %>% t() %>% as.data.frame() %>% rownames_to_column()
#combine all to one dataframe
All.coef.I_p <- I1.coef.I_p %>% full_join(I2.coef.I_p) %>% full_join(I3.coef.I_p) %>% full_join(I4.coef.I_p) %>% full_join(I5.coef.I_p) %>% full_join(I6.coef.I_p) %>% full_join(I7.coef.I_p) %>% full_join(I8.coef.I_p) %>% full_join(I9.coef.I_p) %>% full_join(I10.coef.I_p) %>% full_join(I11.coef.I_p) %>% full_join(I12.coef.I_p) %>% full_join(I13.coef.I_p) %>% full_join(I14.coef.I_p) %>% full_join(I15.coef.I_p) %>% full_join(I16.coef.I_p) %>% full_join(I17.coef.I_p) %>% full_join(I18.coef.I_p) %>% full_join(I19.coef.I_p) %>% full_join(I20.coef.I_p) %>% full_join(I21.coef.I_p) %>% full_join(I22.coef.I_p) %>% full_join(I23.coef.I_p) %>% full_join(I24.coef.I_p) %>% full_join(I25.coef.I_p)

#average the coefficients
coef_pool.I_p <- All.coef.I_p  %>% group_by(rowname) %>% summarise(across(everything(), mean)) %>% ungroup()
print(coef_pool.I_p)

#Extract covariance matrix for each model
sum.I1.I_p <-summary(RQ2vocab_I1_p, covariance = T, R = 50)
Cov.I1.I_p <-as.data.frame(sum.I1.I_p[["Cov"]]) %>% as.data.frame() %>% rownames_to_column() %>% pivot_longer(cols= !rowname, names_to = c(".value", "quantile"), names_sep = -3)

sum.I2.I_p <-summary(RQ2vocab_I2_p, covariance = T, R = 50) 
Cov.I2.I_p <-as.data.frame(sum.I2.I_p[["Cov"]]) %>% as.data.frame() %>% rownames_to_column() %>% pivot_longer(cols= !rowname, names_to = c(".value", "quantile"), names_sep = -3)
 
sum.I3.I_p <-summary(RQ2vocab_I3_p, covariance = T, R = 50)
Cov.I3.I_p <-as.data.frame(sum.I3.I_p[["Cov"]]) %>% as.data.frame() %>% rownames_to_column() %>% pivot_longer(cols= !rowname, names_to = c(".value", "quantile"), names_sep = -3)

sum.I4.I_p <-summary(RQ2vocab_I4_p, covariance = T, R = 50)
Cov.I4.I_p <-as.data.frame(sum.I4.I_p[["Cov"]]) %>% as.data.frame() %>% rownames_to_column() %>% pivot_longer(cols= !rowname, names_to = c(".value", "quantile"), names_sep = -3)

sum.I5.I_p <-summary(RQ2vocab_I5_p, covariance = T, R = 50)
Cov.I5.I_p <-as.data.frame(sum.I5.I_p[["Cov"]]) %>% as.data.frame() %>% rownames_to_column() %>% pivot_longer(cols= !rowname, names_to = c(".value", "quantile"), names_sep = -3)

sum.I6.I_p <-summary(RQ2vocab_I6_p, covariance = T, R = 50)
Cov.I6.I_p <-as.data.frame(sum.I6.I_p[["Cov"]]) %>% as.data.frame() %>% rownames_to_column() %>% pivot_longer(cols= !rowname, names_to = c(".value", "quantile"), names_sep = -3)

sum.I7.I_p <-summary(RQ2vocab_I7_p, covariance = T, R = 50)
Cov.I7.I_p <-as.data.frame(sum.I7.I_p[["Cov"]]) %>% as.data.frame() %>% rownames_to_column() %>% pivot_longer(cols= !rowname, names_to = c(".value", "quantile"), names_sep = -3)

sum.I8.I_p <-summary(RQ2vocab_I8_p, covariance = T, R = 50)
Cov.I8.I_p <-as.data.frame(sum.I8.I_p[["Cov"]]) %>% as.data.frame() %>% rownames_to_column() %>% pivot_longer(cols= !rowname, names_to = c(".value", "quantile"), names_sep = -3)

sum.I9.I_p <-summary(RQ2vocab_I9_p, covariance = T, R = 50)
Cov.I9.I_p <-as.data.frame(sum.I9.I_p[["Cov"]]) %>% as.data.frame() %>% rownames_to_column() %>% pivot_longer(cols= !rowname, names_to = c(".value", "quantile"), names_sep = -3)

sum.I10.I_p <-summary(RQ2vocab_I10_p, covariance = T, R = 50)
Cov.I10.I_p <-as.data.frame(sum.I10.I_p[["Cov"]]) %>% as.data.frame() %>% rownames_to_column() %>% pivot_longer(cols= !rowname, names_to = c(".value", "quantile"), names_sep = -3)

sum.I11.I_p <-summary(RQ2vocab_I11_p, covariance = T, R = 50)
Cov.I11.I_p <-as.data.frame(sum.I11.I_p[["Cov"]]) %>% as.data.frame() %>% rownames_to_column() %>% pivot_longer(cols= !rowname, names_to = c(".value", "quantile"), names_sep = -3)

sum.I12.I_p <-summary(RQ2vocab_I12_p, covariance = T, R = 50)
Cov.I12.I_p <-as.data.frame(sum.I12.I_p[["Cov"]]) %>% as.data.frame() %>% rownames_to_column() %>% pivot_longer(cols= !rowname, names_to = c(".value", "quantile"), names_sep = -3)

sum.I13.I_p <-summary(RQ2vocab_I13_p, covariance = T, R = 50)
Cov.I13.I_p <-as.data.frame(sum.I13.I_p[["Cov"]]) %>% as.data.frame() %>% rownames_to_column() %>% pivot_longer(cols= !rowname, names_to = c(".value", "quantile"), names_sep = -3)

sum.I14.I_p <-summary(RQ2vocab_I14_p, covariance = T, R = 50)
Cov.I14.I_p <-as.data.frame(sum.I14.I_p[["Cov"]]) %>% as.data.frame() %>% rownames_to_column() %>% pivot_longer(cols= !rowname, names_to = c(".value", "quantile"), names_sep = -3)

sum.I15.I_p <-summary(RQ2vocab_I15_p, covariance = T, R = 50)
Cov.I15.I_p <-as.data.frame(sum.I15.I_p[["Cov"]]) %>% as.data.frame() %>% rownames_to_column() %>% pivot_longer(cols= !rowname, names_to = c(".value", "quantile"), names_sep = -3)

sum.I16.I_p <-summary(RQ2vocab_I16_p, covariance = T, R = 50)
Cov.I16.I_p <-as.data.frame(sum.I16.I_p[["Cov"]]) %>% as.data.frame() %>% rownames_to_column() %>% pivot_longer(cols= !rowname, names_to = c(".value", "quantile"), names_sep = -3)

sum.I17.I_p <-summary(RQ2vocab_I17_p, covariance = T, R = 50)
Cov.I17.I_p <-as.data.frame(sum.I17.I_p[["Cov"]]) %>% as.data.frame() %>% rownames_to_column() %>% pivot_longer(cols= !rowname, names_to = c(".value", "quantile"), names_sep = -3)

sum.I18.I_p <-summary(RQ2vocab_I18_p, covariance = T, R = 50)
Cov.I18.I_p <-as.data.frame(sum.I18.I_p[["Cov"]]) %>% as.data.frame() %>% rownames_to_column() %>% pivot_longer(cols= !rowname, names_to = c(".value", "quantile"), names_sep = -3)

sum.I19.I_p <-summary(RQ2vocab_I19_p, covariance = T, R = 50)
Cov.I19.I_p <-as.data.frame(sum.I19.I_p[["Cov"]]) %>% as.data.frame() %>% rownames_to_column() %>% pivot_longer(cols= !rowname, names_to = c(".value", "quantile"), names_sep = -3)

sum.I20.I_p <-summary(RQ2vocab_I20_p, covariance = T, R = 50)
Cov.I20.I_p <-as.data.frame(sum.I20.I_p[["Cov"]]) %>% as.data.frame() %>% rownames_to_column() %>% pivot_longer(cols= !rowname, names_to = c(".value", "quantile"), names_sep = -3)

sum.I21.I_p <-summary(RQ2vocab_I21_p, covariance = T, R = 50)
Cov.I21.I_p <-as.data.frame(sum.I21.I_p[["Cov"]]) %>% as.data.frame() %>% rownames_to_column() %>% pivot_longer(cols= !rowname, names_to = c(".value", "quantile"), names_sep = -3)

sum.I22.I_p <-summary(RQ2vocab_I22_p, covariance = T, R = 50)
Cov.I22.I_p <-as.data.frame(sum.I22.I_p[["Cov"]]) %>% as.data.frame() %>% rownames_to_column() %>% pivot_longer(cols= !rowname, names_to = c(".value", "quantile"), names_sep = -3)

sum.I23.I_p <-summary(RQ2vocab_I23_p, covariance = T, R = 50)
Cov.I23.I_p <-as.data.frame(sum.I23.I_p[["Cov"]]) %>% as.data.frame() %>% rownames_to_column() %>% pivot_longer(cols= !rowname, names_to = c(".value", "quantile"), names_sep = -3)

sum.I24.I_p <-summary(RQ2vocab_I24_p, covariance = T, R = 50)
Cov.I24.I_p <-as.data.frame(sum.I24.I_p[["Cov"]]) %>% as.data.frame() %>% rownames_to_column() %>% pivot_longer(cols= !rowname, names_to = c(".value", "quantile"), names_sep = -3)

sum.I25.I_p <-summary(RQ2vocab_I25_p, covariance = T, R = 50)
Cov.I25.I_p <-as.data.frame(sum.I25.I_p[["Cov"]]) %>% as.data.frame() %>% rownames_to_column() %>% pivot_longer(cols= !rowname, names_to = c(".value", "quantile"), names_sep = -3)

#combine all COV matrices into one giant thing
all.cov.I_p <- Cov.I1.I_p %>% full_join(Cov.I2.I_p) %>% full_join(Cov.I3.I_p) %>% full_join(Cov.I4.I_p) %>% full_join(Cov.I5.I_p) %>% full_join(Cov.I6.I_p) %>% full_join(Cov.I7.I_p) %>% full_join(Cov.I8.I_p) %>% full_join(Cov.I9.I_p) %>% full_join(Cov.I10.I_p) %>% full_join(Cov.I11.I_p) %>% full_join(Cov.I12.I_p) %>% full_join(Cov.I13.I_p) %>% full_join(Cov.I14.I_p) %>% full_join(Cov.I15.I_p) %>% full_join(Cov.I16.I_p) %>% full_join(Cov.I17.I_p) %>% full_join(Cov.I18.I_p) %>% full_join(Cov.I19.I_p) %>% full_join(Cov.I20.I_p) %>% full_join(Cov.I21.I_p) %>% full_join(Cov.I22.I_p) %>% full_join(Cov.I23.I_p) %>% full_join(Cov.I24.I_p) %>% full_join(Cov.I25.I_p) %>% filter(rowname != "reStruct1") %>% filter(rowname != "scale") %>% select(-c("reStruct1.", "scale."))



#get avergaes per variable and per quantile # need to resort the row name variable to preserve original order
cov_pool.I_p <- all.cov.I_p  %>% group_by(rowname, quantile) %>% summarise(across(everything(), mean)) %>% ungroup() %>% arrange(factor(rowname, levels = (c("(Intercept)", "treatment", "LW_Factor_1", "PV_Factor_1", "P201", "P301", "P501", "P601", "P901", "treatment:LW_Factor_1", "treatment:PV_Factor_1", "treatment:P201", "treatment:P501", "treatment:P601", "treatment:P901"))))
print(cov_pool.I_p)

#quantify uncertainty due to missing data
  #VCOVmissing.data=∑mi=1(bi−bpool)T(bi−bpool)m−1
#calculate the difference for each coefficient from the pooled coefficient
coef.dev.I1.I_p <- I1.coef.I_p[2:16] - coef_pool.I_p[2:16] 
coef.dev.I1.I_p <- coef.dev.I1.I_p %>% mutate(quantile = seq(0.1, 0.9, 0.1))

coef.dev.I2.I_p <- I2.coef.I_p[2:16] - coef_pool.I_p[2:16] 
coef.dev.I2.I_p <- coef.dev.I2.I_p %>% mutate(quantile = seq(0.1, 0.9, 0.1))

coef.dev.I3.I_p <- I3.coef.I_p[2:16] - coef_pool.I_p[2:16] 
coef.dev.I3.I_p <- coef.dev.I3.I_p %>% mutate(quantile = seq(0.1, 0.9, 0.1))

coef.dev.I4.I_p <- I4.coef.I_p[2:16] - coef_pool.I_p[2:16] 
coef.dev.I4.I_p <- coef.dev.I4.I_p %>% mutate(quantile = seq(0.1, 0.9, 0.1))

coef.dev.I5.I_p <- I5.coef.I_p[2:16] - coef_pool.I_p[2:16] 
coef.dev.I5.I_p <- coef.dev.I5.I_p %>% mutate(quantile = seq(0.1, 0.9, 0.1))

coef.dev.I6.I_p <- I6.coef.I_p[2:16] - coef_pool.I_p[2:16] 
coef.dev.I6.I_p <- coef.dev.I6.I_p %>% mutate(quantile = seq(0.1, 0.9, 0.1))

coef.dev.I7.I_p <- I7.coef.I_p[2:16] - coef_pool.I_p[2:16] 
coef.dev.I7.I_p <- coef.dev.I7.I_p %>% mutate(quantile = seq(0.1, 0.9, 0.1))

coef.dev.I8.I_p <- I8.coef.I_p[2:16] - coef_pool.I_p[2:16] 
coef.dev.I8.I_p <- coef.dev.I8.I_p %>% mutate(quantile = seq(0.1, 0.9, 0.1))

coef.dev.I9.I_p <- I9.coef.I_p[2:16] - coef_pool.I_p[2:16] 
coef.dev.I9.I_p <- coef.dev.I9.I_p %>% mutate(quantile = seq(0.1, 0.9, 0.1))

coef.dev.I10.I_p <- I10.coef.I_p[2:16] - coef_pool.I_p[2:16] 
coef.dev.I10.I_p <- coef.dev.I10.I_p %>% mutate(quantile = seq(0.1, 0.9, 0.1))

coef.dev.I11.I_p <- I11.coef.I_p[2:16] - coef_pool.I_p[2:16] 
coef.dev.I11.I_p <- coef.dev.I11.I_p %>% mutate(quantile = seq(0.1, 0.9, 0.1))

coef.dev.I12.I_p <- I12.coef.I_p[2:16] - coef_pool.I_p[2:16] 
coef.dev.I12.I_p <- coef.dev.I12.I_p %>% mutate(quantile = seq(0.1, 0.9, 0.1))

coef.dev.I13.I_p <- I13.coef.I_p[2:16] - coef_pool.I_p[2:16] 
coef.dev.I13.I_p <- coef.dev.I13.I_p %>% mutate(quantile = seq(0.1, 0.9, 0.1))

coef.dev.I14.I_p <- I14.coef.I_p[2:16] - coef_pool.I_p[2:16] 
coef.dev.I14.I_p <- coef.dev.I14.I_p %>% mutate(quantile = seq(0.1, 0.9, 0.1))

coef.dev.I15.I_p <- I15.coef.I_p[2:16] - coef_pool.I_p[2:16] 
coef.dev.I15.I_p <- coef.dev.I15.I_p %>% mutate(quantile = seq(0.1 ,0.9, 0.1))

coef.dev.I16.I_p <- I16.coef.I_p[2:16] - coef_pool.I_p[2:16] 
coef.dev.I16.I_p <- coef.dev.I16.I_p %>% mutate(quantile = seq(0.1, 0.9, 0.1))

coef.dev.I17.I_p <- I17.coef.I_p[2:16] - coef_pool.I_p[2:16] 
coef.dev.I17.I_p <- coef.dev.I17.I_p %>% mutate(quantile = seq(0.1, 0.9, 0.1))

coef.dev.I18.I_p <- I18.coef.I_p[2:16] - coef_pool.I_p[2:16] 
coef.dev.I18.I_p <- coef.dev.I18.I_p %>% mutate(quantile = seq(0.1, 0.9, 0.1))

coef.dev.I19.I_p <- I19.coef.I_p[2:16] - coef_pool.I_p[2:16] 
coef.dev.I19.I_p <- coef.dev.I19.I_p %>% mutate(quantile = seq(0.1, 0.9, 0.1))

coef.dev.I20.I_p <- I20.coef.I_p[2:16] - coef_pool.I_p[2:16] 
coef.dev.I20.I_p <- coef.dev.I20.I_p %>% mutate(quantile = seq(0.1, 0.9, 0.1))

coef.dev.I21.I_p <- I21.coef.I_p[2:16] - coef_pool.I_p[2:16] 
coef.dev.I21.I_p <- coef.dev.I21.I_p %>% mutate(quantile = seq(0.1, 0.9, 0.1))

coef.dev.I22.I_p <- I22.coef.I_p[2:16] - coef_pool.I_p[2:16] 
coef.dev.I22.I_p <- coef.dev.I22.I_p %>% mutate(quantile = seq(0.1, 0.9, 0.1))

coef.dev.I23.I_p <- I23.coef.I_p[2:16] - coef_pool.I_p[2:16] 
coef.dev.I23.I_p <- coef.dev.I23.I_p %>% mutate(quantile = seq(0.1, 0.9, 0.1))

coef.dev.I24.I_p <- I24.coef.I_p[2:16] - coef_pool.I_p[2:16] 
coef.dev.I24.I_p <- coef.dev.I14.I_p %>% mutate(quantile = seq(0.1, 0.9, 0.1))

coef.dev.I25.I_p <- I25.coef.I_p[2:16] - coef_pool.I_p[2:16] 
coef.dev.I25.I_p <- coef.dev.I15.I_p %>% mutate(quantile = seq(0.1 ,0.9, 0.1))

#combine all coef dev matrices into one giant thing
all.coef.dev.I_p <- coef.dev.I1.I_p %>% full_join(coef.dev.I2.I_p)  %>% full_join(coef.dev.I3.I_p) %>% full_join(coef.dev.I4.I_p) %>% full_join(coef.dev.I5.I_p) %>% full_join(coef.dev.I6.I_p) %>% full_join(coef.dev.I7.I_p) %>% full_join(coef.dev.I8.I_p) %>% full_join(coef.dev.I9.I_p) %>% full_join(coef.dev.I10.I_p) %>% full_join(coef.dev.I11.I_p) %>% full_join(coef.dev.I12.I_p) %>% full_join(coef.dev.I13.I_p) %>% full_join(coef.dev.I14.I_p) %>% full_join(coef.dev.I15.I_p) %>% full_join(coef.dev.I16.I_p) %>% full_join(coef.dev.I17.I_p) %>% full_join(coef.dev.I18.I_p) %>% full_join(coef.dev.I19.I_p) %>% full_join(coef.dev.I20.I_p) %>% full_join(coef.dev.I21.I_p) %>% full_join(coef.dev.I22.I_p) %>% full_join(coef.dev.I23.I_p) %>% full_join(coef.dev.I24.I_p) %>% full_join(coef.dev.I25.I_p) 

#separate VCOV and ceof.dev by quantile because I am a bad coder
all.coef.dev.q1.I_p <- all.coef.dev.I_p %>% filter(quantile == 0.1) %>% select (-quantile) %>% as.matrix()
all.coef.dev.q2.I_p <- all.coef.dev.I_p %>% filter(quantile == 0.2) %>% select (-quantile) %>% as.matrix()
all.coef.dev.q3.I_p <- all.coef.dev.I_p %>% filter(quantile == "0.3")  %>% select (-quantile) %>% as.matrix() #not sure why it won't give anything with just the numbers
all.coef.dev.q4.I_p <- all.coef.dev.I_p %>% filter(quantile == 0.4) %>% select (-quantile) %>% as.matrix()
all.coef.dev.q5.I_p <- all.coef.dev.I_p %>% filter(quantile == 0.5) %>% select (-quantile) %>% as.matrix()
all.coef.dev.q6.I_p <- all.coef.dev.I_p %>% filter(quantile == 0.6)%>% select (-quantile) %>% as.matrix()
all.coef.dev.q7.I_p <- all.coef.dev.I_p %>% filter(quantile == "0.7")%>% select (-quantile) %>% as.matrix()
all.coef.dev.q8.I_p <- all.coef.dev.I_p %>% filter(quantile == 0.8) %>% select (-quantile) %>% as.matrix()
all.coef.dev.q9.I_p <- all.coef.dev.I_p %>% filter(quantile == 0.9)%>% select (-quantile) %>% as.matrix()

all.cov.q1.I_p <- cov_pool.I_p %>% filter(quantile == 0.1) %>% select(-c("rowname", "quantile")) %>% as.matrix()
all.cov.q2.I_p <- cov_pool.I_p %>% filter(quantile == 0.2) %>% select(-c("rowname", "quantile")) %>% as.matrix()
all.cov.q3.I_p <- cov_pool.I_p %>% filter(quantile == 0.3) %>% select(-c("rowname", "quantile")) %>% as.matrix()
all.cov.q4.I_p <- cov_pool.I_p %>% filter(quantile == 0.4) %>% select(-c("rowname", "quantile")) %>% as.matrix()
all.cov.q5.I_p <- cov_pool.I_p %>% filter(quantile == 0.5) %>% select(-c("rowname", "quantile")) %>% as.matrix()
all.cov.q6.I_p <- cov_pool.I_p %>% filter(quantile == 0.6) %>% select(-c("rowname", "quantile")) %>% as.matrix()
all.cov.q7.I_p <- cov_pool.I_p %>% filter(quantile == 0.7) %>% select(-c("rowname", "quantile")) %>% as.matrix()
all.cov.q8.I_p <- cov_pool.I_p %>% filter(quantile == 0.8) %>% select(-c("rowname", "quantile")) %>% as.matrix()
all.cov.q9.I_p <- cov_pool.I_p %>% filter(quantile == 0.9) %>% select(-c("rowname", "quantile")) %>% as.matrix()

#multiply matrix by its inverse, do this separately per quantile
coef.dev2.q1.I_p <- t(all.coef.dev.q1.I_p) %*% all.coef.dev.q1.I_p
coef.dev2.q2.I_p <- t(all.coef.dev.q2.I_p) %*% all.coef.dev.q2.I_p
coef.dev2.q3.I_p <- t(all.coef.dev.q3.I_p) %*% all.coef.dev.q3.I_p
coef.dev2.q4.I_p <- t(all.coef.dev.q4.I_p) %*% all.coef.dev.q4.I_p
coef.dev2.q5.I_p <- t(all.coef.dev.q5.I_p) %*% all.coef.dev.q5.I_p
coef.dev2.q6.I_p <- t(all.coef.dev.q6.I_p) %*% all.coef.dev.q6.I_p
coef.dev2.q7.I_p <- t(all.coef.dev.q7.I_p) %*% all.coef.dev.q7.I_p
coef.dev2.q8.I_p <- t(all.coef.dev.q8.I_p) %*% all.coef.dev.q8.I_p
coef.dev2.q9.I_p <- t(all.coef.dev.q9.I_p) %*% all.coef.dev.q9.I_p

#divide the sum of the above by #of imputations - 1 (24). to get missing data number of imputations error
mdni.error.q1.I_p <- coef.dev2.q1.I_p / 24
mdni.error.q2.I_p <- coef.dev2.q2.I_p / 24
mdni.error.q3.I_p <- coef.dev2.q3.I_p / 24
mdni.error.q4.I_p <- coef.dev2.q4.I_p / 24
mdni.error.q5.I_p <- coef.dev2.q5.I_p / 24
mdni.error.q6.I_p <- coef.dev2.q6.I_p / 24
mdni.error.q7.I_p <- coef.dev2.q7.I_p / 24
mdni.error.q8.I_p <- coef.dev2.q8.I_p / 24
mdni.error.q9.I_p <- coef.dev2.q9.I_p / 24

#divide the mdni  by total number of imputations to get error due to finite sampling
mdfs.error.q1.I_p <- mdni.error.q1.I_p / 25
mdfs.error.q2.I_p <- mdni.error.q2.I_p / 25
mdfs.error.q3.I_p <- mdni.error.q3.I_p / 25
mdfs.error.q4.I_p <- mdni.error.q4.I_p / 25
mdfs.error.q5.I_p <- mdni.error.q5.I_p / 25
mdfs.error.q6.I_p <- mdni.error.q6.I_p / 25
mdfs.error.q7.I_p <- mdni.error.q7.I_p / 25
mdfs.error.q8.I_p <- mdni.error.q8.I_p / 25
mdfs.error.q9.I_p <- mdni.error.q9.I_p / 25
#add the three error components to get total error
total.error.q1.I_p <- all.cov.q1.I_p + mdni.error.q1.I_p + mdfs.error.q1.I_p
total.error.q2.I_p <- all.cov.q2.I_p + mdni.error.q2.I_p + mdfs.error.q2.I_p
total.error.q3.I_p <- all.cov.q3.I_p + mdni.error.q3.I_p + mdfs.error.q3.I_p
total.error.q4.I_p <- all.cov.q4.I_p + mdni.error.q4.I_p + mdfs.error.q4.I_p
total.error.q5.I_p <- all.cov.q5.I_p + mdni.error.q5.I_p + mdfs.error.q5.I_p
total.error.q6.I_p <- all.cov.q6.I_p + mdni.error.q6.I_p + mdfs.error.q6.I_p
total.error.q7.I_p <- all.cov.q7.I_p + mdni.error.q7.I_p + mdfs.error.q7.I_p
total.error.q8.I_p <- all.cov.q8.I_p + mdni.error.q8.I_p + mdfs.error.q8.I_p
total.error.q9.I_p <- all.cov.q9.I_p + mdni.error.q9.I_p + mdfs.error.q9.I_p

fmi.q1.I_p <- (diag(mdni.error.q1.I_p) + diag(mdfs.error.q1.I_p)) / diag(total.error.q1.I_p)
fmi.q2.I_p <- (diag(mdni.error.q2.I_p) + diag(mdfs.error.q2.I_p)) / diag(total.error.q2.I_p)
fmi.q3.I_p <- (diag(mdni.error.q3.I_p) + diag(mdfs.error.q3.I_p)) / diag(total.error.q3.I_p)
fmi.q4.I_p <- (diag(mdni.error.q4.I_p) + diag(mdfs.error.q4.I_p)) / diag(total.error.q4.I_p)
fmi.q5.I_p <- (diag(mdni.error.q5.I_p) + diag(mdfs.error.q5.I_p)) / diag(total.error.q5.I_p)
fmi.q6.I_p <- (diag(mdni.error.q6.I_p) + diag(mdfs.error.q6.I_p)) / diag(total.error.q6.I_p)
fmi.q7.I_p <- (diag(mdni.error.q7.I_p) + diag(mdfs.error.q7.I_p)) / diag(total.error.q7.I_p)
fmi.q8.I_p <- (diag(mdni.error.q8.I_p) + diag(mdfs.error.q8.I_p)) / diag(total.error.q8.I_p)
fmi.q9.I_p <- (diag(mdni.error.q9.I_p) + diag(mdfs.error.q9.I_p)) / diag(total.error.q9.I_p)

#then calculate the approapriate degrees of freedom
#dfimputation=1/1m−1FMI2+1(1−FMI)(dfcomplete+1dfcomplete+3)dfcomplete

df_com.I_p <- sum.I1.I_p$rdf %>% as.numeric() #same for all quantiles

v.q1.I_p <- (24) / fmi.q1.I_p^2
vhat.q1.I_p <- (1 - fmi.q1.I_p) * ((df_com.I_p + 1) / (df_com.I_p + 3)) * df_com.I_p
df_imp.q1.I_p <- (v.q1.I_p^(-1) + vhat.q1.I_p^(-1))^(-1)

v.q2.I_p <- (24) / fmi.q2.I_p^2
vhat.q2.I_p <- (1 - fmi.q2.I_p) * ((df_com.I_p + 1) / (df_com.I_p + 3)) * df_com.I_p
df_imp.q2.I_p <- (v.q2.I_p^(-1) + vhat.q2.I_p^(-1))^(-1)

v.q3.I_p <- (24) / fmi.q3.I_p^2
vhat.q3.I_p <- (1 - fmi.q3.I_p) * ((df_com.I_p + 1) / (df_com.I_p + 3)) * df_com.I_p
df_imp.q3.I_p <- (v.q3.I_p^(-1) + vhat.q3.I_p^(-1))^(-1)

v.q4.I_p <- (24) / fmi.q4.I_p^2
vhat.q4.I_p <- (1 - fmi.q4.I_p) * ((df_com.I_p + 1) / (df_com.I_p + 3)) * df_com.I_p
df_imp.q4.I_p <- (v.q4.I_p^(-1) + vhat.q4.I_p^(-1))^(-1)

v.q5.I_p <- (24) / fmi.q5.I_p^2
vhat.q5.I_p <- (1 - fmi.q5.I_p) * ((df_com.I_p + 1) / (df_com.I_p + 3)) * df_com.I_p
df_imp.q5.I_p <- (v.q5.I_p^(-1) + vhat.q5.I_p^(-1))^(-1)

v.q6.I_p <- (24) / fmi.q6.I_p^2
vhat.q6.I_p <- (1 - fmi.q6.I_p) * ((df_com.I_p + 1) / (df_com.I_p + 3)) * df_com.I_p
df_imp.q6.I_p <- (v.q6.I_p^(-1) + vhat.q6.I_p^(-1))^(-1)

v.q7.I_p <- (24) / fmi.q7.I_p^2
vhat.q7.I_p <- (1 - fmi.q7.I_p) * ((df_com.I_p + 1) / (df_com.I_p + 3)) * df_com.I_p
df_imp.q7.I_p <- (v.q7.I_p^(-1) + vhat.q4.I_p^(-1))^(-1)

v.q8.I_p <- (24) / fmi.q8.I_p^2
vhat.q8.I_p <- (1 - fmi.q8.I_p) * ((df_com.I_p + 1) / (df_com.I_p + 3)) * df_com.I_p
df_imp.q8.I_p <- (v.q8.I_p^(-1) + vhat.q8.I_p^(-1))^(-1)

v.q9.I_p <- (24) / fmi.q9.I_p^2
vhat.q9.I_p <- (1 - fmi.q9.I_p) * ((df_com.I_p + 1) / (df_com.I_p + 3)) * df_com.I_p
df_imp.q9.I_p <- (v.q9.I_p^(-1) + vhat.q9.I_p^(-1))^(-1)

#now get the SE, t, and p values
se.pool.q1.I_p <- sqrt(diag(total.error.q1.I_p))
t.pool.q1.I_p <- coef_pool.I_p[1,2:16] / se.pool.q1.I_p 
t.pool.q1.I.abs_p <- abs(t.pool.q1.I_p) %>% as.matrix()
(p.pool.q1.I_p <- 2 * pt(q = t.pool.q1.I.abs_p, df = df_imp.q1.I_p, lower.tail = F))

se.pool.q2.I_p <- sqrt(diag(total.error.q2.I_p))
t.pool.q2.I_p <- coef_pool.I_p[2,2:16] / se.pool.q2.I_p 
t.pool.q2.I.abs_p <- abs(t.pool.q2.I_p) %>% as.matrix()
(p.pool.q2.I_p <- 2 * pt(q = t.pool.q2.I.abs_p, df = df_imp.q2.I_p, lower.tail = F))

se.pool.q3.I_p <- sqrt(diag(total.error.q3.I_p))
t.pool.q3.I_p <- coef_pool.I_p[3,2:16] / se.pool.q3.I_p 
t.pool.q3.I.abs_p <- abs(t.pool.q3.I_p) %>% as.matrix()
(p.pool.q3.I_p <- 2 * pt(q = t.pool.q3.I.abs_p, df = df_imp.q3.I_p, lower.tail = F))

se.pool.q4.I_p <- sqrt(diag(total.error.q4.I_p))
t.pool.q4.I_p <- coef_pool.I_p[4,2:16] / se.pool.q4.I_p 
t.pool.q4.I.abs_p <- abs(t.pool.q4.I_p) %>% as.matrix()
(p.pool.q4.I_p <- 2 * pt(q = t.pool.q4.I.abs_p, df = df_imp.q4.I_p, lower.tail = F))

se.pool.q5.I_p <- sqrt(diag(total.error.q5.I_p))
t.pool.q5.I_p <- coef_pool.I_p[5,2:16] / se.pool.q5.I_p
t.pool.q5.I.abs_p <- abs(t.pool.q5.I_p) %>% as.matrix()
(p.pool.q5.I_p <- 2 * pt(q = t.pool.q5.I.abs_p, df = df_imp.q5.I_p, lower.tail = F))

se.pool.q6.I_p <- sqrt(diag(total.error.q6.I_p))
t.pool.q6.I_p <- coef_pool.I_p[6,2:16] / se.pool.q6.I_p 
t.pool.q6.I.abs_p <- abs(t.pool.q6.I_p) %>% as.matrix()
(p.pool.q6.I_p <- 2 * pt(q = t.pool.q6.I.abs_p, df = df_imp.q6.I_p, lower.tail = F))

se.pool.q7.I_p <- sqrt(diag(total.error.q7.I_p))
t.pool.q7.I_p <- coef_pool.I_p[7,2:16] / se.pool.q7.I_p
t.pool.q7.I.abs_p <- abs(t.pool.q7.I_p) %>% as.matrix()
(p.pool.q7.I_p <- 2 * pt(q = t.pool.q7.I.abs_p, df = df_imp.q7.I_p, lower.tail = F))

se.pool.q8.I_p <- sqrt(diag(total.error.q8.I_p))
t.pool.q8.I_p <- coef_pool.I_p[8,2:16] / se.pool.q8.I_p 
t.pool.q8.I.abs_p <- abs(t.pool.q8.I_p) %>% as.matrix()
(p.pool.q8.I_p <- 2 * pt(q = t.pool.q8.I.abs_p, df = df_imp.q8.I_p, lower.tail = F))

se.pool.q9.I_p <- sqrt(diag(total.error.q9.I_p))
t.pool.q9.I_p <- coef_pool.I_p[9,2:16] / se.pool.q9.I_p 
t.pool.q9.I.abs_p <- abs(t.pool.q9.I_p) %>% as.matrix()
(p.pool.q9.I_p <- 2 * pt(q = t.pool.q9.I.abs_p, df = df_imp.q9.I_p, lower.tail = F))

crit.pool.q1.I_p <- qt(p = 1 - (.05 / 2), df = df_imp.q1.I_p, lower.tail = T)
hw.pool.q1.I_p <- se.pool.q1.I_p * crit.pool.q1.I_p
ci_pool.q1.I_p <- cbind("lwr" = coef_pool.I_p[1,2:16] - hw.pool.q1.I_p, "upr" = coef_pool.I_p[1,2:16] + hw.pool.q1.I_p)

crit.pool.q2.I_p <- qt(p = 1 - (.05 / 2), df = df_imp.q2.I_p, lower.tail = T)
hw.pool.q2.I_p <- se.pool.q2.I_p * crit.pool.q2.I_p
ci_pool.q2.I_p <- cbind("lwr" = coef_pool.I_p[2,2:16] - hw.pool.q2.I_p, "upr" = coef_pool.I_p[2,2:16] + hw.pool.q2.I_p)

crit.pool.q3.I_p <- qt(p = 1 - (.05 / 2), df = df_imp.q3.I_p, lower.tail = T)
hw.pool.q3.I_p <- se.pool.q3.I_p * crit.pool.q3.I_p
ci_pool.q3.I_p <- cbind("lwr" = coef_pool.I_p[3,2:16] - hw.pool.q3.I_p, "upr" = coef_pool.I_p[3,2:16] + hw.pool.q3.I_p)

crit.pool.q4.I_p <- qt(p = 1 - (.05 / 2), df = df_imp.q4.I_p, lower.tail = T)
hw.pool.q4.I_p <- se.pool.q4.I_p * crit.pool.q4.I_p
ci_pool.q4.I_p <- cbind("lwr" = coef_pool.I_p[4,2:16] - hw.pool.q4.I_p, "upr" = coef_pool.I_p[4,2:16] + hw.pool.q4.I_p)

crit.pool.q5.I_p <- qt(p = 1 - (.05 / 2), df = df_imp.q5.I_p, lower.tail = T)
hw.pool.q5.I_p <- se.pool.q5.I_p * crit.pool.q5.I_p
ci_pool.q5.I_p <- cbind("lwr" = coef_pool.I_p[5,2:16] - hw.pool.q5.I_p, "upr" = coef_pool.I_p[5,2:16] + hw.pool.q5.I_p)

crit.pool.q6.I_p <- qt(p = 1 - (.05 / 2), df = df_imp.q6.I_p, lower.tail = T)
hw.pool.q6.I_p <- se.pool.q6.I_p * crit.pool.q6.I_p
ci_pool.q6.I_p <- cbind("lwr" = coef_pool.I_p[6,2:16] - hw.pool.q6.I_p, "upr" = coef_pool.I_p[6,2:16] + hw.pool.q6.I_p)

crit.pool.q7.I_p <- qt(p = 1 - (.05 / 2), df = df_imp.q7.I_p, lower.tail = T)
hw.pool.q7.I_p <- se.pool.q7.I_p * crit.pool.q7.I_p
ci_pool.q7.I_p <- cbind("lwr" = coef_pool.I_p[7,2:16] - hw.pool.q7.I_p, "upr" = coef_pool.I_p[7,2:16] + hw.pool.q7.I_p)

crit.pool.q8.I_p <- qt(p = 1 - (.05 / 2), df = df_imp.q8.I_p, lower.tail = T)
hw.pool.q8.I_p <- se.pool.q8.I_p * crit.pool.q8.I_p
ci_pool.q8.I_p <- cbind("lwr" = coef_pool.I_p[8,2:16] - hw.pool.q8.I_p, "upr" = coef_pool.I_p[8,2:16] + hw.pool.q8.I_p)

crit.pool.q9.I_p <- qt(p = 1 - (.05 / 2), df = df_imp.q9.I_p, lower.tail = T)
hw.pool.q9.I_p <- se.pool.q9.I_p * crit.pool.q9.I_p
ci_pool.q9.I_p <- cbind("lwr" = coef_pool.I_p[9,2:16] - hw.pool.q9.I_p, "upr" = coef_pool.I_p[9,2:16] + hw.pool.q9.I_p)

##### BH adjustments
p_values_RQ2_PV_p <- p.pool.q1.I_p %>% rbind(p.pool.q2.I_p) %>% rbind(p.pool.q3.I_p) %>% rbind(p.pool.q4.I_p) %>% rbind(p.pool.q5.I_p) %>% rbind(p.pool.q6.I_p) %>% rbind(p.pool.q7.I_p) %>% rbind(p.pool.q8.I_p) %>% rbind (p.pool.q9.I_p)  
fdrs_RQ2_PV_p <- p.adjust(p_values_RQ2_PV_p, method="BH") 
p_values_adj_RQ2_PV_p <- matrix(unlist(fdrs_RQ2_PV_p), nrow=9) %>% round(3) %>% as.data.frame()  %>% mutate(quant=c(1, 2, 3, 4, 5, 6, 7, 8, 9))
colnames(p_values_adj_RQ2_PV_p) <- (c("(Intercept)_adj", "treatment_adj", "LW_Factor_1_adj", "PV_Factor_1_adj", "P201_adj", "P501_adj",  "P601_adj", "P901_adj",  "P301_adj", "treatment:LW_Factor_1_adj", "treatment:PV_Factor_1_adj", "treatment:P201_adj", "treatment:P501_adj", "treatment:P601_adj", "treatment:P901_adj", "Quantile"))
p_values_RQ2_PV_wquant_p <- p_values_RQ2_PV_p %>% as.data.frame() %>% mutate(quant=c(1, 2, 3, 4, 5, 6, 7, 8, 9))

(p_values_RQ2_PV_all_p <- p_values_RQ2_PV_wquant_p %>% cbind(p_values_adj_RQ2_PV_p) %>% dplyr::select(1, 17, 2, 18, 3, 19, 4, 20, 5, 21, 6, 22, 7, 23, 8, 24, 9, 25, 10, 26, 11, 27, 12, 28, 13, 29, 14, 30, 15, 31, 16, 32)  %>% round(3))


all_ci_I_p <- ci_pool.q1.I_p %>% rbind(ci_pool.q2.I_p) %>% rbind(ci_pool.q3.I_p) %>% rbind(ci_pool.q4.I_p) %>% rbind(ci_pool.q5.I_p) %>% rbind(ci_pool.q6.I_p) %>% rbind(ci_pool.q7.I_p) %>% rbind(ci_pool.q8.I_p) %>% rbind(ci_pool.q9.I_p)

(all_new_I_p <- all_ci_I_p %>% cbind(coef_pool.I_p))

## get the statistically significant effects of interest from previous line (I'm doing this by hand)
#treatment = not statistically significant
#LW pre test skills = NOT statistically significant 
#PV pre test skills = statistically significant for quartiles 1-5 
#interaction of treatment with LW pre = NOT statistically significant at all quartiles
#interaction of treatment with PV pre = NOT statistically significant at all quartiles
```
```{r sensitivityProject RQ2LWID}
#Then run all the analyses
#control functions to reah convergence
RQ2letterid_I1_p <- lqmm(LW_Factor_3 ~ treatment*(LW_Factor_1 + PV_Factor_1 + P201 + P501 + P601 + P901) + P301 , random = ~1, group = TID, tau = c(1:9/10), data = QR_data_imp_I1, control = lqmmControl(method ="df", UP_max_iter = 1000, UP_tol = 1e-6))
RQ2letterid_I2_p  <- lqmm(LW_Factor_3 ~ treatment*(LW_Factor_1 + PV_Factor_1 + P201 + P501 + P601 + P901) + P301 , random = ~1, group = TID, tau = c(1:9/10), data = QR_data_imp_I2, control = list(UP_max_iter = 1000, UP_tol = 1e-6))
RQ2letterid_I3_p  <- lqmm(LW_Factor_3 ~ treatment*(LW_Factor_1 + PV_Factor_1 + P201 + P501 + P601 + P901) + P301 , random = ~1, group = TID, tau = c(1:9/10), data = QR_data_imp_I3, control = list(UP_max_iter = 1000, UP_tol = 1e-6))
RQ2letterid_I4_p  <- lqmm(LW_Factor_3 ~ treatment*(LW_Factor_1 + PV_Factor_1 + P201 + P501 + P601 + P901) + P301 , random = ~1, group = TID, tau = c(1:9/10), data = QR_data_imp_I4, control = list(LP_max_iter = 10000, lP_tol = 1e-10))
RQ2letterid_I5_p  <- lqmm(LW_Factor_3 ~ treatment*(LW_Factor_1 + PV_Factor_1 + P201 + P501 + P601 + P901) + P301 , random = ~1, group = TID, tau = c(1:9/10), data = QR_data_imp_I5, control = list(LP_max_iter = 1000, LP_tol = 1e-6))
RQ2letterid_I6_p  <- lqmm(LW_Factor_3 ~ treatment*(LW_Factor_1 + PV_Factor_1 + P201 + P501 + P601 + P901) + P301 , random = ~1, group = TID, tau = c(1:9/10), data = QR_data_imp_I6, control = list(UP_max_iter = 1000, UP_tol = 1e-7))
RQ2letterid_I7_p  <- lqmm(LW_Factor_3 ~ treatment*(LW_Factor_1 + PV_Factor_1 + P201 + P501 + P601 + P901) + P301 , random = ~1, group = TID, tau = c(1:9/10), data = QR_data_imp_I7, control = list(UP_max_iter = 1000, UP_tol = 1e-7))
RQ2letterid_I8_p  <- lqmm(LW_Factor_3 ~ treatment*(LW_Factor_1 + PV_Factor_1 + P201 + P501 + P601 + P901) + P301 , random = ~1, group = TID, tau = c(1:9/10), data = QR_data_imp_I8, control = list(UP_max_iter = 1000, UP_tol = 1e-6))
RQ2letterid_I9_p  <- lqmm(LW_Factor_3 ~ treatment*(LW_Factor_1 + PV_Factor_1 + P201 + P501 + P601 + P901)+ P301 , random = ~1, group = TID, tau = c(1:9/10), data = QR_data_imp_I9, control = list(UP_max_iter = 1000, UP_tol = 1e-6))
RQ2letterid_I10_p  <- lqmm(LW_Factor_3 ~ treatment*(LW_Factor_1 + PV_Factor_1 + P201 + P501 + P601 + P901) + P301 , random = ~1, group = TID, tau = c(1:9/10), data = QR_data_imp_I10, control = list(UP_max_iter = 1000, UP_tol = 1e-6))
RQ2letterid_I11_p  <- lqmm(LW_Factor_3 ~ treatment*(LW_Factor_1 + PV_Factor_1 + P201 + P501 + P601 + P901) + P301 , random = ~1, group = TID, tau = c(1:9/10), data = QR_data_imp_I11, control = list(UP_max_iter = 1000, UP_tol = 1e-7))
RQ2letterid_I12_p  <- lqmm(LW_Factor_3 ~ treatment*(LW_Factor_1 + PV_Factor_1 + P201 + P501 + P601 + P901) + P301 , random = ~1, group = TID, tau = c(1:9/10), data = QR_data_imp_I12, control = list(UP_max_iter = 1000, UP_tol = 1e-6))
RQ2letterid_I13_p  <- lqmm(LW_Factor_3 ~ treatment*(LW_Factor_1 + PV_Factor_1 + P201 + P501 + P601 + P901) + P301 , random = ~1, group = TID, tau = c(1:9/10), data = QR_data_imp_I13, control = list(UP_max_iter = 1000, UP_tol = 1e-7))
RQ2letterid_I14_p  <- lqmm(LW_Factor_3 ~ treatment*(LW_Factor_1 + PV_Factor_1 + P201 + P501 + P601 + P901) + P301 , random = ~1, group = TID, tau = c(1:9/10), data = QR_data_imp_I14, control = list(UP_max_iter = 1000, UP_tol = 1e-6))
RQ2letterid_I15_p  <- lqmm(LW_Factor_3 ~ treatment*(LW_Factor_1 + PV_Factor_1 + P201 + P501 + P601 + P901) + P301 , random = ~1, group = TID, tau = c(1:9/10), data = QR_data_imp_I15, control = list(UP_max_iter = 1000, UP_tol = 1e-6))
RQ2letterid_I16_p  <- lqmm(LW_Factor_3 ~ treatment*(LW_Factor_1 + PV_Factor_1 + P201 + P501 + P601 + P901) + P301 , random = ~1, group = TID, tau = c(1:9/10), data = QR_data_imp_I16, control = list(UP_max_iter = 1000, UP_tol = 1e-7))
RQ2letterid_I17_p  <- lqmm(LW_Factor_3 ~ treatment*(LW_Factor_1 + PV_Factor_1 + P201 + P501 + P601 + P901) + P301 , random = ~1, group = TID, tau = c(1:9/10), data = QR_data_imp_I17, control = list(UP_max_iter = 1000, UP_tol = 1e-7))
RQ2letterid_I18_p  <- lqmm(LW_Factor_3 ~ treatment*(LW_Factor_1 + PV_Factor_1 + P201 + P501 + P601 + P901) + P301 , random = ~1, group = TID, tau = c(1:9/10), data = QR_data_imp_I18, control = list(UP_max_iter = 1000, UP_tol = 1e-6))
RQ2letterid_I19_p  <- lqmm(LW_Factor_3 ~ treatment*(LW_Factor_1 + PV_Factor_1 + P201 + P501 + P601 + P901) + P301 , random = ~1, group = TID, tau = c(1:9/10), data = QR_data_imp_I19, control = list(UP_max_iter = 1000, UP_tol = 1e-6))
RQ2letterid_I20_p  <- lqmm(LW_Factor_3 ~ treatment*(LW_Factor_1 + PV_Factor_1 + P201 + P501 + P601 + P901) + P301 , random = ~1, group = TID, tau = c(1:9/10), data = QR_data_imp_I20, control = list(UP_max_iter = 1000, UP_tol = 1e-6))
RQ2letterid_I21_p  <- lqmm(LW_Factor_3 ~ treatment*(LW_Factor_1 + PV_Factor_1 + P201 + P501 + P601 + P901) + P301 , random = ~1, group = TID, tau = c(1:9/10), data = QR_data_imp_I21, control = list(UP_max_iter = 1000, UP_tol = 1e-7))
RQ2letterid_I22_p  <- lqmm(LW_Factor_3 ~ treatment*(LW_Factor_1 + PV_Factor_1 + P201 + P501 + P601 + P901) + P301 , random = ~1, group = TID, tau = c(1:9/10), data = QR_data_imp_I22, control = list(UP_max_iter = 1000, UP_tol = 1e-6))
RQ2letterid_I23_p  <- lqmm(LW_Factor_3 ~ treatment*(LW_Factor_1 + PV_Factor_1 + P201 + P501 + P601 + P901) + P301 , random = ~1, group = TID, tau = c(1:9/10), data = QR_data_imp_I23, control = list(UP_max_iter = 1000, UP_tol = 1e-7))
RQ2letterid_I24_p  <- lqmm(LW_Factor_3 ~ treatment*(LW_Factor_1 + PV_Factor_1 + P201 + P501 + P601 + P901) + P301 , random = ~1, group = TID, tau = c(1:9/10), data = QR_data_imp_I24, control = list(UP_max_iter = 1000, UP_tol = 1e-6))
RQ2letterid_I25_p  <- lqmm(LW_Factor_3 ~ treatment*(LW_Factor_1 + PV_Factor_1 + P201 + P501 + P601 + P901) + P301 , random = ~1, group = TID, tau = c(1:9/10), data = QR_data_imp_I25, control = list(UP_max_iter = 1000, UP_tol = 1e-6))

#Extract the regression coefficients for each model
I1.coef.IV_p  <- coef.lqmm(RQ2letterid_I1_p) %>% t() %>% as.data.frame() %>% rownames_to_column() #transpose and make a datafrome
I2.coef.IV_p  <- coef.lqmm(RQ2letterid_I2_p ) %>% t() %>% as.data.frame() %>% rownames_to_column()
I3.coef.IV_p  <- coef.lqmm(RQ2letterid_I3_p ) %>% t() %>% as.data.frame() %>% rownames_to_column()
I4.coef.IV_p  <- coef.lqmm(RQ2letterid_I4_p ) %>% t() %>% as.data.frame() %>% rownames_to_column()
I5.coef.IV_p  <- coef.lqmm(RQ2letterid_I5_p ) %>% t() %>% as.data.frame() %>% rownames_to_column()
I6.coef.IV_p  <- coef.lqmm(RQ2letterid_I6_p ) %>% t() %>% as.data.frame() %>% rownames_to_column()
I7.coef.IV_p  <- coef.lqmm(RQ2letterid_I7_p ) %>% t() %>% as.data.frame() %>% rownames_to_column()
I8.coef.IV_p  <- coef.lqmm(RQ2letterid_I8_p ) %>% t() %>% as.data.frame() %>% rownames_to_column()
I9.coef.IV_p  <- coef.lqmm(RQ2letterid_I9_p ) %>% t() %>% as.data.frame() %>% rownames_to_column()
I10.coef.IV_p  <- coef.lqmm(RQ2letterid_I10_p ) %>% t() %>% as.data.frame() %>% rownames_to_column()
I11.coef.IV_p  <- coef.lqmm(RQ2letterid_I11_p ) %>% t() %>% as.data.frame() %>% rownames_to_column()
I12.coef.IV_p  <- coef.lqmm(RQ2letterid_I12_p ) %>% t() %>% as.data.frame() %>% rownames_to_column()
I13.coef.IV_p  <- coef.lqmm(RQ2letterid_I13_p ) %>% t() %>% as.data.frame() %>% rownames_to_column()
I14.coef.IV_p  <- coef.lqmm(RQ2letterid_I14_p ) %>% t() %>% as.data.frame() %>% rownames_to_column()
I15.coef.IV_p  <- coef.lqmm(RQ2letterid_I15_p ) %>% t() %>% as.data.frame() %>% rownames_to_column()
I16.coef.IV_p  <- coef.lqmm(RQ2letterid_I16_p ) %>% t() %>% as.data.frame() %>% rownames_to_column()
I17.coef.IV_p  <- coef.lqmm(RQ2letterid_I17_p ) %>% t() %>% as.data.frame() %>% rownames_to_column()
I18.coef.IV_p  <- coef.lqmm(RQ2letterid_I18_p ) %>% t() %>% as.data.frame() %>% rownames_to_column()
I19.coef.IV_p  <- coef.lqmm(RQ2letterid_I19_p ) %>% t() %>% as.data.frame() %>% rownames_to_column()
I20.coef.IV_p  <- coef.lqmm(RQ2letterid_I20_p ) %>% t() %>% as.data.frame() %>% rownames_to_column()
I21.coef.IV_p  <- coef.lqmm(RQ2letterid_I21_p ) %>% t() %>% as.data.frame() %>% rownames_to_column()
I22.coef.IV_p  <- coef.lqmm(RQ2letterid_I22_p ) %>% t() %>% as.data.frame() %>% rownames_to_column()
I23.coef.IV_p  <- coef.lqmm(RQ2letterid_I23_p ) %>% t() %>% as.data.frame() %>% rownames_to_column()
I24.coef.IV_p  <- coef.lqmm(RQ2letterid_I24_p ) %>% t() %>% as.data.frame() %>% rownames_to_column()
I25.coef.IV_p  <- coef.lqmm(RQ2letterid_I25_p ) %>% t() %>% as.data.frame() %>% rownames_to_column()

#combine all to one dataframe
All.coef.IV_p  <- I1.coef.IV_p  %>% full_join(I2.coef.IV_p ) %>% full_join(I3.coef.IV_p ) %>% full_join(I4.coef.IV_p ) %>% full_join(I5.coef.IV_p ) %>% full_join(I6.coef.IV_p ) %>% full_join(I7.coef.IV_p ) %>% full_join(I8.coef.IV_p ) %>% full_join(I9.coef.IV_p ) %>% full_join(I10.coef.IV_p ) %>% full_join(I11.coef.IV_p ) %>% full_join(I12.coef.IV_p ) %>% full_join(I13.coef.IV_p ) %>% full_join(I14.coef.IV_p ) %>% full_join(I15.coef.IV_p ) %>% full_join(I16.coef.IV_p ) %>% full_join(I17.coef.IV_p ) %>% full_join(I18.coef.IV_p ) %>% full_join(I19.coef.IV_p ) %>% full_join(I20.coef.IV_p ) %>% full_join(I21.coef.IV_p ) %>% full_join(I22.coef.IV_p ) %>% full_join(I23.coef.IV_p ) %>% full_join(I24.coef.IV_p ) %>% full_join(I25.coef.IV_p )

#average the coefficients
coef_pool.IV_p  <- All.coef.IV_p   %>% group_by(rowname) %>% summarise(across(everything(), mean)) %>% ungroup()
print(coef_pool.IV_p )

#Extract covariance matrix for each model
sum.IV1.IV_p  <-summary(RQ2letterid_I1_p , covariance = T, R = 50)
Cov.IV1.IV_p  <-as.data.frame(sum.IV1.IV_p [["Cov"]]) %>% as.data.frame() %>% rownames_to_column() %>% pivot_longer(cols= !rowname, names_to = c(".value", "quantile"), names_sep = -3)

sum.IV2.IV_p  <-summary(RQ2letterid_I2_p , covariance = T, R = 50) 
Cov.IV2.IV_p <-as.data.frame(sum.IV2.IV_p [["Cov"]]) %>% as.data.frame() %>% rownames_to_column() %>% pivot_longer(cols= !rowname, names_to = c(".value", "quantile"), names_sep = -3)
 
sum.IV3.IV_p  <-summary(RQ2letterid_I3_p , covariance = T, R = 50)
Cov.IV3.IV_p  <-as.data.frame(sum.IV3.IV_p [["Cov"]]) %>% as.data.frame() %>% rownames_to_column() %>% pivot_longer(cols= !rowname, names_to = c(".value", "quantile"), names_sep = -3)

sum.IV4.IV_p  <-summary(RQ2letterid_I4_p , covariance = T, R = 50)
Cov.IV4.IV_p  <-as.data.frame(sum.IV4.IV_p [["Cov"]]) %>% as.data.frame() %>% rownames_to_column() %>% pivot_longer(cols= !rowname, names_to = c(".value", "quantile"), names_sep = -3)

sum.IV5.IV_p  <-summary(RQ2letterid_I5_p , covariance = T, R = 50)
Cov.IV5.IV_p  <-as.data.frame(sum.IV5.IV_p [["Cov"]]) %>% as.data.frame() %>% rownames_to_column() %>% pivot_longer(cols= !rowname, names_to = c(".value", "quantile"), names_sep = -3)

sum.IV6.IV_p  <-summary(RQ2letterid_I6_p , covariance = T, R = 50)
Cov.IV6.IV_p  <-as.data.frame(sum.IV6.IV_p [["Cov"]]) %>% as.data.frame() %>% rownames_to_column() %>% pivot_longer(cols= !rowname, names_to = c(".value", "quantile"), names_sep = -3)

sum.IV7.IV_p  <-summary(RQ2letterid_I7_p , covariance = T, R = 50)
Cov.IV7.IV_p  <-as.data.frame(sum.IV7.IV_p [["Cov"]]) %>% as.data.frame() %>% rownames_to_column() %>% pivot_longer(cols= !rowname, names_to = c(".value", "quantile"), names_sep = -3)

sum.IV8.IV_p  <-summary(RQ2letterid_I8_p , covariance = T, R = 50)
Cov.IV8.IV_p  <-as.data.frame(sum.IV8.IV_p [["Cov"]]) %>% as.data.frame() %>% rownames_to_column() %>% pivot_longer(cols= !rowname, names_to = c(".value", "quantile"), names_sep = -3)

sum.IV9.IV_p  <-summary(RQ2letterid_I9_p , covariance = T, R = 50)
Cov.IV9.IV_p  <-as.data.frame(sum.IV9.IV_p [["Cov"]]) %>% as.data.frame() %>% rownames_to_column() %>% pivot_longer(cols= !rowname, names_to = c(".value", "quantile"), names_sep = -3)

sum.IV10.IV_p  <-summary(RQ2letterid_I10_p , covariance = T, R = 50)
Cov.IV10.IV_p  <-as.data.frame(sum.IV10.IV_p [["Cov"]]) %>% as.data.frame() %>% rownames_to_column() %>% pivot_longer(cols= !rowname, names_to = c(".value", "quantile"), names_sep = -3)

sum.IV11.IV_p  <-summary(RQ2letterid_I11_p , covariance = T, R = 50)
Cov.IV11.IV_p  <-as.data.frame(sum.IV11.IV_p [["Cov"]]) %>% as.data.frame() %>% rownames_to_column() %>% pivot_longer(cols= !rowname, names_to = c(".value", "quantile"), names_sep = -3)

sum.IV12.IV_p  <-summary(RQ2letterid_I12_p , covariance = T, R = 50)
Cov.IV12.IV_p  <-as.data.frame(sum.IV12.IV_p [["Cov"]]) %>% as.data.frame() %>% rownames_to_column() %>% pivot_longer(cols= !rowname, names_to = c(".value", "quantile"), names_sep = -3)

sum.IV13.IV_p  <-summary(RQ2letterid_I13_p , covariance = T, R = 50)
Cov.IV13.IV_p  <-as.data.frame(sum.IV13.IV_p [["Cov"]]) %>% as.data.frame() %>% rownames_to_column() %>% pivot_longer(cols= !rowname, names_to = c(".value", "quantile"), names_sep = -3)

sum.IV14.IV_p  <-summary(RQ2letterid_I14_p , covariance = T, R = 50)
Cov.IV14.IV_p  <-as.data.frame(sum.IV14.IV_p [["Cov"]]) %>% as.data.frame() %>% rownames_to_column() %>% pivot_longer(cols= !rowname, names_to = c(".value", "quantile"), names_sep = -3)

sum.IV15.IV_p <-summary(RQ2letterid_I15_p , covariance = T, R = 50)
Cov.IV15.IV_p  <-as.data.frame(sum.IV15.IV_p [["Cov"]]) %>% as.data.frame() %>% rownames_to_column() %>% pivot_longer(cols= !rowname, names_to = c(".value", "quantile"), names_sep = -3)

sum.IV16.IV_p  <-summary(RQ2letterid_I16_p , covariance = T, R = 50)
Cov.IV16.IV_p  <-as.data.frame(sum.IV16.IV_p [["Cov"]]) %>% as.data.frame() %>% rownames_to_column() %>% pivot_longer(cols= !rowname, names_to = c(".value", "quantile"), names_sep = -3)

sum.IV17.IV_p  <-summary(RQ2letterid_I17_p , covariance = T, R = 50)
Cov.IV17.IV_p  <-as.data.frame(sum.IV17.IV_p [["Cov"]]) %>% as.data.frame() %>% rownames_to_column() %>% pivot_longer(cols= !rowname, names_to = c(".value", "quantile"), names_sep = -3)

sum.IV18.IV_p  <-summary(RQ2letterid_I18_p , covariance = T, R = 50)
Cov.IV18.IV_p  <-as.data.frame(sum.IV18.IV_p [["Cov"]]) %>% as.data.frame() %>% rownames_to_column() %>% pivot_longer(cols= !rowname, names_to = c(".value", "quantile"), names_sep = -3)

sum.IV19.IV_p  <-summary(RQ2letterid_I19_p , covariance = T, R = 50)
Cov.IV19.IV_p  <-as.data.frame(sum.IV19.IV_p [["Cov"]]) %>% as.data.frame() %>% rownames_to_column() %>% pivot_longer(cols= !rowname, names_to = c(".value", "quantile"), names_sep = -3)

sum.IV20.IV_p  <-summary(RQ2letterid_I20_p,  covariance = T, R = 50)
Cov.IV20.IV_p <-as.data.frame(sum.IV20.IV_p [["Cov"]]) %>% as.data.frame() %>% rownames_to_column() %>% pivot_longer(cols= !rowname, names_to = c(".value", "quantile"), names_sep = -3)

sum.IV21.IV_p  <-summary(RQ2letterid_I21_p , covariance = T, R = 50)
Cov.IV21.IV_p  <-as.data.frame(sum.IV21.IV_p [["Cov"]]) %>% as.data.frame() %>% rownames_to_column() %>% pivot_longer(cols= !rowname, names_to = c(".value", "quantile"), names_sep = -3)

sum.IV22.IV_p  <-summary(RQ2letterid_I22_p , covariance = T, R = 50)
Cov.IV22.IV_p  <-as.data.frame(sum.IV22.IV_p [["Cov"]]) %>% as.data.frame() %>% rownames_to_column() %>% pivot_longer(cols= !rowname, names_to = c(".value", "quantile"), names_sep = -3)

sum.IV23.IV_p  <-summary(RQ2letterid_I23_p , covariance = T, R = 50)
Cov.IV23.IV_p  <-as.data.frame(sum.IV23.IV_p [["Cov"]]) %>% as.data.frame() %>% rownames_to_column() %>% pivot_longer(cols= !rowname, names_to = c(".value", "quantile"), names_sep = -3)

sum.IV24.IV_p  <-summary(RQ2letterid_I24_p , covariance = T, R = 50)
Cov.IV24.IV_p  <-as.data.frame(sum.IV24.IV_p [["Cov"]]) %>% as.data.frame() %>% rownames_to_column() %>% pivot_longer(cols= !rowname, names_to = c(".value", "quantile"), names_sep = -3)

sum.IV25.IV_p <-summary(RQ2letterid_I25_p , covariance = T, R = 50)
Cov.IV25.IV_p  <-as.data.frame(sum.IV25.IV_p [["Cov"]]) %>% as.data.frame() %>% rownames_to_column() %>% pivot_longer(cols= !rowname, names_to = c(".value", "quantile"), names_sep = -3)

#combine all COV matrices into one giant thing
all.cov.IV_p <- Cov.IV1.IV_p %>% full_join(Cov.IV2.IV_p) %>% full_join(Cov.IV3.IV_p) %>% full_join(Cov.IV4.IV_p) %>% full_join(Cov.IV5.IV_p) %>% full_join(Cov.IV6.IV_p) %>% full_join(Cov.IV7.IV_p) %>% full_join(Cov.IV8.IV_p) %>% full_join(Cov.IV9.IV_p) %>% full_join(Cov.IV10.IV_p) %>% full_join(Cov.IV11.IV_p) %>% full_join(Cov.IV12.IV_p) %>% full_join(Cov.IV13.IV_p) %>% full_join(Cov.IV14.IV_p) %>% full_join(Cov.IV15.IV_p)  %>% full_join(Cov.IV16.IV_p) %>% full_join(Cov.IV17.IV_p) %>% full_join(Cov.IV18.IV_p) %>% full_join(Cov.IV19.IV_p) %>% full_join(Cov.IV20.IV_p) %>% full_join(Cov.IV21.IV_p) %>% full_join(Cov.IV22.IV_p) %>% full_join(Cov.IV23.IV_p) %>% full_join(Cov.IV24.IV_p) %>% full_join(Cov.IV25.IV_p) %>% filter(rowname != "reStruct1") %>% filter(rowname != "scale") %>% select(-c("reStruct1.", "scale."))



#get avergaes per variable and per quantile # need to resoirt the rowname variable to preserve original order
cov_pool.IV_p <- all.cov.IV_p  %>% group_by(rowname, quantile) %>% summarise(across(everything(), mean)) %>% ungroup() %>% arrange(factor(rowname, levels = (c("(Intercept)", "treatment", "LW_Factor_1", "PV_Factor_1", "P201",  "P501", "P601", "P901", "P301", "treatment:LW_Factor_1_adj", "treatment:PV_Factor_1_adj", "treatment:P201_adj",  "treatment:P501_adj", "treatment:P601_adj", "treatment:P901_adj"))))
print(cov_pool.IV_p)

#quantify uncertainty due to missing data
  #VCOVmissing.data=∑mi=1(bi−bpool)T(bi−bpool)m−1
#calculate the difference for each coefficient from the pooled coefficient
coef.dev.IV1.IV_p <- I1.coef.IV_p[2:16] - coef_pool.IV_p[2:16] 
coef.dev.IV1.IV_p <- coef.dev.IV1.IV_p %>% mutate(quantile = seq(0.1, 0.9, 0.1))

coef.dev.IV2.IV_p <- I2.coef.IV_p[2:16] - coef_pool.IV_p[2:16] 
coef.dev.IV2.IV_p <- coef.dev.IV2.IV_p %>% mutate(quantile = seq(0.1, 0.9, 0.1))

coef.dev.IV3.IV_p <- I3.coef.IV_p[2:16] - coef_pool.IV_p[2:16] 
coef.dev.IV3.IV_p <- coef.dev.IV3.IV_p %>% mutate(quantile = seq(0.1, 0.9, 0.1))

coef.dev.IV4.IV_p <- I4.coef.IV_p[2:16] - coef_pool.IV_p[2:16] 
coef.dev.IV4.IV_p <- coef.dev.IV4.IV_p %>% mutate(quantile = seq(0.1, 0.9, 0.1))

coef.dev.IV5.IV_p <- I5.coef.IV_p[2:16] - coef_pool.IV_p[2:16] 
coef.dev.IV5.IV_p <- coef.dev.IV5.IV_p %>% mutate(quantile = seq(0.1, 0.9, 0.1))

coef.dev.IV6.IV_p <- I6.coef.IV_p[2:16] - coef_pool.IV_p[2:16] 
coef.dev.IV6.IV_p <- coef.dev.IV6.IV_p %>% mutate(quantile = seq(0.1, 0.9, 0.1))

coef.dev.IV7.IV_p <- I7.coef.IV_p[2:16] - coef_pool.IV_p[2:16] 
coef.dev.IV7.IV_p <- coef.dev.IV7.IV_p %>% mutate(quantile = seq(0.1, 0.9, 0.1))

coef.dev.IV8.IV_p <- I8.coef.IV_p[2:16] - coef_pool.IV_p[2:16] 
coef.dev.IV8.IV_p <- coef.dev.IV8.IV_p %>% mutate(quantile = seq(0.1, 0.9, 0.1))

coef.dev.IV9.IV_p <- I9.coef.IV_p[2:16] - coef_pool.IV_p[2:16] 
coef.dev.IV9.IV_p <- coef.dev.IV9.IV_p %>% mutate(quantile = seq(0.1, 0.9, 0.1))

coef.dev.IV10.IV_p <- I10.coef.IV_p[2:16] - coef_pool.IV_p[2:16] 
coef.dev.IV10.IV_p <- coef.dev.IV10.IV_p %>% mutate(quantile = seq(0.1, 0.9, 0.1))

coef.dev.IV11.IV_p <- I11.coef.IV_p[2:16] - coef_pool.IV_p[2:16] 
coef.dev.IV11.IV_p <- coef.dev.IV11.IV_p %>% mutate(quantile = seq(0.1, 0.9, 0.1))

coef.dev.IV12.IV_p <- I12.coef.IV_p[2:16] - coef_pool.IV_p[2:16] 
coef.dev.IV12.IV_p <- coef.dev.IV12.IV_p %>% mutate(quantile = seq(0.1, 0.9, 0.1))

coef.dev.IV13.IV_p <- I13.coef.IV_p[2:16] - coef_pool.IV_p[2:16] 
coef.dev.IV13.IV_p <- coef.dev.IV13.IV_p %>% mutate(quantile = seq(0.1, 0.9, 0.1))

coef.dev.IV14.IV_p <- I14.coef.IV_p[2:16] - coef_pool.IV_p[2:16] 
coef.dev.IV14.IV_p <- coef.dev.IV14.IV_p %>% mutate(quantile = seq(0.1, 0.9, 0.1))

coef.dev.IV15.IV_p <- I15.coef.IV_p[2:16] - coef_pool.IV_p[2:16] 
coef.dev.IV15.IV_p <- coef.dev.IV15.IV_p %>% mutate(quantile = seq(0.1 ,0.9, 0.1))

coef.dev.IV16.IV_p <- I16.coef.IV_p[2:16] - coef_pool.IV_p[2:16] 
coef.dev.IV16.IV_p <- coef.dev.IV16.IV_p %>% mutate(quantile = seq(0.1, 0.9, 0.1))

coef.dev.IV17.IV_p <- I17.coef.IV_p[2:16] - coef_pool.IV_p[2:16] 
coef.dev.IV17.IV_p <- coef.dev.IV17.IV_p %>% mutate(quantile = seq(0.1, 0.9, 0.1))

coef.dev.IV18.IV_p <- I18.coef.IV_p[2:16] - coef_pool.IV_p[2:16] 
coef.dev.IV18.IV_p <- coef.dev.IV18.IV_p %>% mutate(quantile = seq(0.1, 0.9, 0.1))

coef.dev.IV19.IV_p <- I19.coef.IV_p[2:16] - coef_pool.IV_p[2:16] 
coef.dev.IV19.IV_p <- coef.dev.IV19.IV_p %>% mutate(quantile = seq(0.1, 0.9, 0.1))

coef.dev.IV20.IV_p <- I20.coef.IV_p[2:16] - coef_pool.IV_p[2:16] 
coef.dev.IV20.IV_p <- coef.dev.IV20.IV_p %>% mutate(quantile = seq(0.1, 0.9, 0.1))

coef.dev.IV21.IV_p <- I21.coef.IV_p[2:16] - coef_pool.IV_p[2:16] 
coef.dev.IV21.IV_p <- coef.dev.IV21.IV_p %>% mutate(quantile = seq(0.1, 0.9, 0.1))

coef.dev.IV22.IV_p <- I22.coef.IV_p[2:16] - coef_pool.IV_p[2:16] 
coef.dev.IV22.IV_p <- coef.dev.IV22.IV_p %>% mutate(quantile = seq(0.1, 0.9, 0.1))

coef.dev.IV23.IV_p <- I23.coef.IV_p[2:16] - coef_pool.IV_p[2:16] 
coef.dev.IV23.IV_p <- coef.dev.IV23.IV_p %>% mutate(quantile = seq(0.1, 0.9, 0.1))

coef.dev.IV24.IV_p <- I24.coef.IV_p[2:16] - coef_pool.IV_p[2:16] 
coef.dev.IV24.IV_p <- coef.dev.IV24.IV_p %>% mutate(quantile = seq(0.1, 0.9, 0.1))

coef.dev.IV25.IV_p <- I25.coef.IV_p[2:16] - coef_pool.IV_p[2:16] 
coef.dev.IV25.IV_p <- coef.dev.IV25.IV_p %>% mutate(quantile = seq(0.1 ,0.9, 0.1))

#combine all coef dev matrices into one giant thing
all.coef.dev.IV_p <- coef.dev.IV1.IV_p %>% full_join(coef.dev.IV2.IV_p)  %>% full_join(coef.dev.IV3.IV_p) %>% full_join(coef.dev.IV4.IV_p) %>% full_join(coef.dev.IV5.IV_p) %>% full_join(coef.dev.IV6.IV_p) %>% full_join(coef.dev.IV7.IV_p) %>% full_join(coef.dev.IV8.IV_p) %>% full_join(coef.dev.IV9.IV_p) %>% full_join(coef.dev.IV10.IV_p) %>% full_join(coef.dev.IV11.IV_p) %>% full_join(coef.dev.IV12.IV_p) %>% full_join(coef.dev.IV13.IV_p) %>% full_join(coef.dev.IV14.IV_p) %>% full_join(coef.dev.IV15.IV_p) %>% full_join(coef.dev.IV16.IV_p) %>% full_join(coef.dev.IV17.IV_p) %>% full_join(coef.dev.IV18.IV_p) %>% full_join(coef.dev.IV19.IV_p) %>% full_join(coef.dev.IV20.IV_p) %>% full_join(coef.dev.IV21.IV_p) %>% full_join(coef.dev.IV22.IV_p) %>% full_join(coef.dev.IV23.IV_p) %>% full_join(coef.dev.IV24.IV_p) %>% full_join(coef.dev.IV25.IV_p) 

#separate VCOV and ceof.dev by quantile because I am a bad coder
all.coef.dev.q1.IV_p <- all.coef.dev.IV_p %>% filter(quantile == 0.1) %>% select (-quantile) %>% as.matrix()
all.coef.dev.q2.IV_p <- all.coef.dev.IV_p %>% filter(quantile == 0.2) %>% select (-quantile) %>% as.matrix()
all.coef.dev.q3.IV_p <- all.coef.dev.IV_p %>% filter(quantile == "0.3")  %>% select (-quantile) %>% as.matrix() #not sure why it won't give anything with just the numbers
all.coef.dev.q4.IV_p <- all.coef.dev.IV_p %>% filter(quantile == 0.4) %>% select (-quantile) %>% as.matrix()
all.coef.dev.q5.IV_p <- all.coef.dev.IV_p %>% filter(quantile == 0.5) %>% select (-quantile) %>% as.matrix()
all.coef.dev.q6.IV_p <- all.coef.dev.IV_p %>% filter(quantile == 0.6)%>% select (-quantile) %>% as.matrix()
all.coef.dev.q7.IV_p <- all.coef.dev.IV_p %>% filter(quantile == "0.7")%>% select (-quantile) %>% as.matrix()
all.coef.dev.q8.IV_p <- all.coef.dev.IV_p %>% filter(quantile == 0.8) %>% select (-quantile) %>% as.matrix()
all.coef.dev.q9.IV_p <- all.coef.dev.IV_p %>% filter(quantile == 0.9)%>% select (-quantile) %>% as.matrix()

all.cov.q1.IV_p <- cov_pool.IV_p %>% filter(quantile == 0.1) %>% select(-c("rowname", "quantile")) %>% as.matrix()
all.cov.q2.IV_p <- cov_pool.IV_p %>% filter(quantile == 0.2) %>% select(-c("rowname", "quantile")) %>% as.matrix()
all.cov.q3.IV_p <- cov_pool.IV_p %>% filter(quantile == 0.3) %>% select(-c("rowname", "quantile")) %>% as.matrix()
all.cov.q4.IV_p <- cov_pool.IV_p %>% filter(quantile == 0.4) %>% select(-c("rowname", "quantile")) %>% as.matrix()
all.cov.q5.IV_p <- cov_pool.IV_p %>% filter(quantile == 0.5) %>% select(-c("rowname", "quantile")) %>% as.matrix()
all.cov.q6.IV_p <- cov_pool.IV_p %>% filter(quantile == 0.6) %>% select(-c("rowname", "quantile")) %>% as.matrix()
all.cov.q7.IV_p <- cov_pool.IV_p %>% filter(quantile == 0.7) %>% select(-c("rowname", "quantile")) %>% as.matrix()
all.cov.q8.IV_p <- cov_pool.IV_p %>% filter(quantile == 0.8) %>% select(-c("rowname", "quantile")) %>% as.matrix()
all.cov.q9.IV_p <- cov_pool.IV_p %>% filter(quantile == 0.9) %>% select(-c("rowname", "quantile")) %>% as.matrix()

#multiply matrix by its inverse, do this separately per quantile
coef.dev2.q1.IV_p <- t(all.coef.dev.q1.IV_p) %*% all.coef.dev.q1.IV_p
coef.dev2.q2.IV_p <- t(all.coef.dev.q2.IV_p) %*% all.coef.dev.q2.IV_p
coef.dev2.q3.IV_p <- t(all.coef.dev.q3.IV_p) %*% all.coef.dev.q3.IV_p
coef.dev2.q4.IV_p <- t(all.coef.dev.q4.IV_p) %*% all.coef.dev.q4.IV_p
coef.dev2.q5.IV_p <- t(all.coef.dev.q5.IV_p) %*% all.coef.dev.q5.IV_p
coef.dev2.q6.IV_p <- t(all.coef.dev.q6.IV_p) %*% all.coef.dev.q6.IV_p
coef.dev2.q7.IV_p <- t(all.coef.dev.q7.IV_p) %*% all.coef.dev.q7.IV_p
coef.dev2.q8.IV_p <- t(all.coef.dev.q8.IV_p) %*% all.coef.dev.q8.IV_p
coef.dev2.q9.IV_p <- t(all.coef.dev.q9.IV_p) %*% all.coef.dev.q9.IV_p

#divide the sum of the above by #of imputations - 1 (24). to get missing data number of imputations error
mdni.error.q1.IV_p <- coef.dev2.q1.IV_p / 24
mdni.error.q2.IV_p <- coef.dev2.q2.IV_p / 24
mdni.error.q3.IV_p <- coef.dev2.q3.IV_p / 24
mdni.error.q4.IV_p <- coef.dev2.q4.IV_p / 24
mdni.error.q5.IV_p <- coef.dev2.q5.IV_p / 24
mdni.error.q6.IV_p <- coef.dev2.q6.IV_p / 24
mdni.error.q7.IV_p <- coef.dev2.q7.IV_p / 24
mdni.error.q8.IV_p <- coef.dev2.q8.IV_p / 24
mdni.error.q9.IV_p <- coef.dev2.q9.IV_p / 24

#divide the mdni  by total number of imputations to get error due to finite sampling
mdfs.error.q1.IV_p <- mdni.error.q1.IV_p / 25
mdfs.error.q2.IV_p <- mdni.error.q2.IV_p / 25
mdfs.error.q3.IV_p <- mdni.error.q3.IV_p / 25
mdfs.error.q4.IV_p <- mdni.error.q4.IV_p / 25
mdfs.error.q5.IV_p <- mdni.error.q5.IV_p / 25
mdfs.error.q6.IV_p <- mdni.error.q6.IV_p / 25
mdfs.error.q7.IV_p <- mdni.error.q7.IV_p / 25
mdfs.error.q8.IV_p <- mdni.error.q8.IV_p / 25
mdfs.error.q9.IV_p <- mdni.error.q9.IV_p / 25

#add the three error components to get total error
total.error.q1.IV_p <- all.cov.q1.IV_p + mdni.error.q1.IV_p + mdfs.error.q1.IV_p
total.error.q2.IV_p <- all.cov.q2.IV_p + mdni.error.q2.IV_p + mdfs.error.q2.IV_p
total.error.q3.IV_p <- all.cov.q3.IV_p + mdni.error.q3.IV_p + mdfs.error.q3.IV_p
total.error.q4.IV_p <- all.cov.q4.IV_p + mdni.error.q4.IV_p + mdfs.error.q4.IV_p
total.error.q5.IV_p <- all.cov.q5.IV_p + mdni.error.q5.IV_p + mdfs.error.q5.IV_p
total.error.q6.IV_p <- all.cov.q6.IV_p + mdni.error.q6.IV_p + mdfs.error.q6.IV_p
total.error.q7.IV_p <- all.cov.q7.IV_p + mdni.error.q7.IV_p + mdfs.error.q7.IV_p
total.error.q8.IV_p <- all.cov.q8.IV_p + mdni.error.q8.IV_p + mdfs.error.q8.IV_p
total.error.q9.IV_p <- all.cov.q9.IV_p + mdni.error.q9.IV_p + mdfs.error.q9.IV_p

fmi.q1.IV_p <- (diag(mdni.error.q1.IV_p) + diag(mdfs.error.q1.IV_p)) / diag(total.error.q1.IV_p)
fmi.q2.IV_p <- (diag(mdni.error.q2.IV_p) + diag(mdfs.error.q2.IV_p)) / diag(total.error.q2.IV_p)
fmi.q3.IV_p <- (diag(mdni.error.q3.IV_p) + diag(mdfs.error.q3.IV_p)) / diag(total.error.q3.IV_p)
fmi.q4.IV_p <- (diag(mdni.error.q4.IV_p) + diag(mdfs.error.q4.IV_p)) / diag(total.error.q4.IV_p)
fmi.q5.IV_p <- (diag(mdni.error.q5.IV_p) + diag(mdfs.error.q5.IV_p)) / diag(total.error.q5.IV_p)
fmi.q6.IV_p <- (diag(mdni.error.q6.IV_p) + diag(mdfs.error.q6.IV_p)) / diag(total.error.q6.IV_p)
fmi.q7.IV_p <- (diag(mdni.error.q7.IV_p) + diag(mdfs.error.q7.IV_p)) / diag(total.error.q7.IV_p)
fmi.q8.IV_p <- (diag(mdni.error.q8.IV_p) + diag(mdfs.error.q8.IV_p)) / diag(total.error.q8.IV_p)
fmi.q9.IV_p <- (diag(mdni.error.q9.IV_p) + diag(mdfs.error.q9.IV_p)) / diag(total.error.q9.IV_p)

#then calculate the appropriate degrees of freedom
#dfimputation=1/1m−1FMI2+1(1−FMI)(dfcomplete+1dfcomplete+3)dfcomplete

df_com.IV_p <- sum.IV1.IV_p$rdf %>% as.numeric() #same for all quantiles

v.q1.IV_p <- (24) / fmi.q1.IV_p^2
vhat.q1.IV_p <- (1 - fmi.q1.IV_p) * ((df_com.IV_p + 1) / (df_com.IV_p+ 3)) * df_com.IV_p
df_imp.q1.IV_p <- (v.q1.IV_p^(-1) + vhat.q1.IV_p^(-1))^(-1)

v.q2.IV_p <- (24) / fmi.q2.IV_p^2
vhat.q2.IV_p <- (1 - fmi.q2.IV_p) * ((df_com.IV_p + 1) / (df_com.IV_p + 3)) * df_com.IV_p
df_imp.q2.IV_p <- (v.q2.IV_p^(-1) + vhat.q2.IV_p^(-1))^(-1)

v.q3.IV_p <- (24) / fmi.q3.IV_p^2
vhat.q3.IV_p <- (1 - fmi.q3.IV_p) * ((df_com.IV_p + 1) / (df_com.IV_p + 3)) * df_com.IV_p
df_imp.q3.IV_p <- (v.q3.IV_p^(-1) + vhat.q3.IV_p^(-1))^(-1)

v.q4.IV_p <- (24) / fmi.q4.IV_p^2
vhat.q4.IV_p <- (1 - fmi.q4.IV_p) * ((df_com.IV_p + 1) / (df_com.IV_p + 3)) * df_com.IV_p
df_imp.q4.IV_p <- (v.q4.IV_p^(-1) + vhat.q4.IV_p^(-1))^(-1)

v.q5.IV_p <- (24) / fmi.q5.IV_p^2
vhat.q5.IV_p <- (1 - fmi.q5.IV_p) * ((df_com.IV_p + 1) / (df_com.IV_p + 3)) * df_com.IV_p
df_imp.q5.IV_p <- (v.q5.IV_p^(-1) + vhat.q5.IV_p^(-1))^(-1)

v.q6.IV_p <- (24) / fmi.q6.IV_p^2
vhat.q6.IV_p <- (1 - fmi.q6.IV_p) * ((df_com.IV_p + 1) / (df_com.IV_p + 3)) * df_com.IV_p
df_imp.q6.IV_p <- (v.q6.IV_p^(-1) + vhat.q6.IV_p^(-1))^(-1)

v.q7.IV_p <- (24) / fmi.q7.IV_p^2
vhat.q7.IV_p <- (1 - fmi.q7.IV_p) * ((df_com.IV_p + 1) / (df_com.IV_p + 3)) * df_com.IV_p
df_imp.q7.IV_p <- (v.q7.IV_p^(-1) + vhat.q4.IV_p^(-1))^(-1)

v.q8.IV_p <- (24) / fmi.q8.IV_p^2
vhat.q8.IV_p <- (1 - fmi.q8.IV_p) * ((df_com.IV_p + 1) / (df_com.IV_p + 3)) * df_com.IV_p
df_imp.q8.IV_p <- (v.q8.IV_p^(-1) + vhat.q8.IV_p^(-1))^(-1)

v.q9.IV_p <- (24) / fmi.q9.IV_p^2
vhat.q9.IV_p <- (1 - fmi.q9.IV_p) * ((df_com.IV_p + 1) / (df_com.IV_p + 3)) * df_com.IV_p
df_imp.q9.IV_p <- (v.q9.IV_p^(-1) + vhat.q9.IV_p^(-1))^(-1)

#now get the SE, t, and p values
se.pool.q1.IV_p <- sqrt(diag(total.error.q1.IV_p))
t.pool.q1.IV_p <- coef_pool.IV_p[1,2:16] / se.pool.q1.IV_p 
t.pool.q1.IV.abs_p <- abs(t.pool.q1.IV_p) %>% as.matrix()
(p.pool.q1.IV_p <- 2 * pt(q = t.pool.q1.IV.abs_p, df = df_imp.q1.IV_p, lower.tail = F))

se.pool.q2.IV_p <- sqrt(diag(total.error.q2.IV_p))
t.pool.q2.IV_p <- coef_pool.IV_p[2,2:16] / se.pool.q2.IV_p 
t.pool.q2.IV.abs_p <- abs(t.pool.q2.IV_p) %>% as.matrix()
(p.pool.q2.IV_p <- 2 * pt(q = t.pool.q2.IV.abs_p, df = df_imp.q2.IV_p, lower.tail = F))

se.pool.q3.IV_p <- sqrt(diag(total.error.q3.IV_p))
t.pool.q3.IV_p <- coef_pool.IV_p[3,2:16] / se.pool.q3.IV_p 
t.pool.q3.IV.abs_p <- abs(t.pool.q3.IV_p) %>% as.matrix()
(p.pool.q3.IV_p <- 2 * pt(q = t.pool.q3.IV.abs_p, df = df_imp.q3.IV_p, lower.tail = F))

se.pool.q4.IV_p <- sqrt(diag(total.error.q4.IV_p))
t.pool.q4.IV_p <- coef_pool.IV_p[4,2:16] / se.pool.q4.IV_p 
t.pool.q4.IV.abs_p <- abs(t.pool.q4.IV_p) %>% as.matrix()
(p.pool.q4.IV_p <- 2 * pt(q = t.pool.q4.IV.abs_p, df = df_imp.q4.IV_p, lower.tail = F))

se.pool.q5.IV_p <- sqrt(diag(total.error.q5.IV_p))
t.pool.q5.IV_p <- coef_pool.IV_p[5,2:16] / se.pool.q5.IV_p
t.pool.q5.IV.abs_p <- abs(t.pool.q5.IV_p) %>% as.matrix()
(p.pool.q5.IV_p <- 2 * pt(q = t.pool.q5.IV.abs_p, df = df_imp.q5.IV_p, lower.tail = F))

se.pool.q6.IV_p <- sqrt(diag(total.error.q6.IV_p))
t.pool.q6.IV_p <- coef_pool.IV_p[6,2:16] / se.pool.q6.IV_p 
t.pool.q6.IV.abs_p <- abs(t.pool.q6.IV_p) %>% as.matrix()
(p.pool.q6.IV_p <- 2 * pt(q = t.pool.q6.IV.abs_p, df = df_imp.q6.IV_p, lower.tail = F))

se.pool.q7.IV_p <- sqrt(diag(total.error.q7.IV_p))
t.pool.q7.IV_p <- coef_pool.IV_p[7,2:16] / se.pool.q7.IV_p 
t.pool.q7.IV.abs_p <- abs(t.pool.q7.IV_p) %>% as.matrix()
(p.pool.q7.IV_p <- 2 * pt(q = t.pool.q7.IV.abs_p, df = df_imp.q7.IV_p, lower.tail = F))

se.pool.q8.IV_p <- sqrt(diag(total.error.q8.IV_p))
t.pool.q8.IV_p <- coef_pool.IV_p[8,2:16] / se.pool.q8.IV_p
t.pool.q8.IV.abs_p <- abs(t.pool.q8.IV_p) %>% as.matrix()
(p.pool.q8.IV_p <- 2 * pt(q = t.pool.q8.IV.abs_p, df = df_imp.q8.IV_p, lower.tail = F))

se.pool.q9.IV_p <- sqrt(diag(total.error.q9.IV_p))
t.pool.q9.IV_p <- coef_pool.IV_p[9,2:16] / se.pool.q9.IV_p
t.pool.q9.IV.abs_p <- abs(t.pool.q9.IV_p) %>% as.matrix()
(p.pool.q9.IV_p <- 2 * pt(q = t.pool.q9.IV.abs_p, df = df_imp.q9.IV_p, lower.tail = F))

crit.pool.q1.IV_p <- qt(p = 1 - (.05 / 2), df = df_imp.q1.IV_p, lower.tail = T)
hw.pool.q1.IV_p <- se.pool.q1.IV_p * crit.pool.q1.IV_p
ci_pool.q1.IV_p <- cbind("lwr" = coef_pool.IV_p[1,2:16] - hw.pool.q1.IV_p, "upr" = coef_pool.IV_p[1,2:16] + hw.pool.q1.IV_p) %>% mutate(quantile = "0.1")

crit.pool.q2.IV_p <- qt(p = 1 - (.05 / 2), df = df_imp.q2.IV_p, lower.tail = T)
hw.pool.q2.IV_p <- se.pool.q2.IV_p * crit.pool.q2.IV_p
ci_pool.q2.IV_p <- cbind("lwr" = coef_pool.IV_p[2,2:16] - hw.pool.q2.IV_p, "upr" = coef_pool.IV_p[2,2:16] + hw.pool.q2.IV_p) %>% mutate(quantile = "0.2")

crit.pool.q3.IV_p <- qt(p = 1 - (.05 / 2), df = df_imp.q3.IV_p, lower.tail = T)
hw.pool.q3.IV_p <- se.pool.q3.IV_p * crit.pool.q3.IV_p
ci_pool.q3.IV_p <- cbind("lwr" = coef_pool.IV_p[3,2:16] - hw.pool.q3.IV_p, "upr" = coef_pool.IV_p[3,2:16] + hw.pool.q3.IV_p) %>% mutate(quantile = "0.3")

crit.pool.q4.IV_p <- qt(p = 1 - (.05 / 2), df = df_imp.q4.IV_p, lower.tail = T)
hw.pool.q4.IV_p <- se.pool.q4.IV_p * crit.pool.q4.IV_p
ci_pool.q4.IV_p <- cbind("lwr" = coef_pool.IV_p[4,2:16] - hw.pool.q4.IV_p, "upr" = coef_pool.IV_p[4,2:16] + hw.pool.q4.IV_p) %>% mutate(quantile = "0.4")

crit.pool.q5.IV_p <- qt(p = 1 - (.05 / 2), df = df_imp.q5.IV_p, lower.tail = T)
hw.pool.q5.IV_p <- se.pool.q5.IV_p * crit.pool.q5.IV_p
ci_pool.q5.IV_p <- cbind("lwr" = coef_pool.IV_p[5,2:16] - hw.pool.q5.IV_p, "upr" = coef_pool.IV_p[5,2:16] + hw.pool.q5.IV_p) %>% mutate(quantile = "0.5")

crit.pool.q6.IV_p <- qt(p = 1 - (.05 / 2), df = df_imp.q6.IV_p, lower.tail = T)
hw.pool.q6.IV_p <- se.pool.q6.IV_p * crit.pool.q6.IV_p
ci_pool.q6.IV_p <- cbind("lwr" = coef_pool.IV_p[6,2:16] - hw.pool.q6.IV_p, "upr" = coef_pool.IV_p[6,2:16] + hw.pool.q6.IV_p) %>% mutate(quantile = "0.6")

crit.pool.q7.IV_p <- qt(p = 1 - (.05 / 2), df = df_imp.q7.IV_p, lower.tail = T)
hw.pool.q7.IV_p <- se.pool.q7.IV * crit.pool.q7.IV_p
ci_pool.q7.IV_p <- cbind("lwr" = coef_pool.IV_p[7,2:16] - hw.pool.q7.IV_p, "upr" = coef_pool.IV_p[7,2:16] + hw.pool.q7.IV_p) %>% mutate(quantile = "0.7")

crit.pool.q8.IV_p <- qt(p = 1 - (.05 / 2), df = df_imp.q8.IV_p, lower.tail = T)
hw.pool.q8.IV_p <- se.pool.q8.IV_p * crit.pool.q8.IV_p
ci_pool.q8.IV_p <- cbind("lwr" = coef_pool.IV_p[8,2:16] - hw.pool.q8.IV_p, "upr" = coef_pool.IV_p[8,2:16] + hw.pool.q8.IV_p) %>% mutate(quantile = "0.8")

crit.pool.q9.IV_p <- qt(p = 1 - (.05 / 2), df = df_imp.q9.IV_p, lower.tail = T)
hw.pool.q9.IV_p <- se.pool.q9.IV_p * crit.pool.q9.IV_p
ci_pool.q9.IV_p <- cbind("lwr" = coef_pool.IV_p[9,2:16] - hw.pool.q9.IV_p, "upr" = coef_pool.IV_p[9,2:16] + hw.pool.q9.IV_p) %>% mutate(quantile = "0.9")

all_ci_IV_p <- ci_pool.q1.IV_p %>% rbind(ci_pool.q2.IV_p) %>% rbind(ci_pool.q3.IV_p) %>% rbind(ci_pool.q4.IV_p) %>% rbind(ci_pool.q5.IV_p) %>% rbind(ci_pool.q6.IV_p) %>% rbind(ci_pool.q7.IV_p) %>% rbind(ci_pool.q8.IV_p) %>% rbind(ci_pool.q9.IV_p)

##### BH adjustments
p_values_RQ2_LW_p <- p.pool.q1.IV_p %>% rbind(p.pool.q2.IV_p) %>% rbind(p.pool.q3.IV_p) %>% rbind(p.pool.q4.IV_p) %>% rbind(p.pool.q5.IV_p) %>% rbind(p.pool.q6.IV_p) %>% rbind(p.pool.q7.IV_p) %>% rbind(p.pool.q8.IV_p) %>% rbind(p.pool.q9.IV_p)   
fdrs_RQ2_LW_p <- p.adjust(p_values_RQ2_LW_p, method="BH") 
p_values_adj_RQ2_LW_p <- matrix(unlist(fdrs_RQ2_LW_p), nrow=9) %>% round(3) %>% as.data.frame()  %>% mutate(quant=c(1, 2, 3, 4, 5, 6, 7, 8, 9))
colnames(p_values_adj_RQ2_LW_p) <- c("(Intercept)_adj", "treatment_adj", "LW_Factor_1_adj", "PV_Factor_1_adj", "P201_adj",  "P501_adj", "P601_adj", "P901_adj","P301_adj", "treatment:LW_Factor_1_adj", "treatment:PV_Factor_1_adj", "treatment:P201_adj", "treatment:P501_adj", "treatment:P601_adj", "treatment:P901_adj", "Quantile")
p_values_RQ2_LW_wquant_p <- p_values_RQ2_LW_p %>% as.data.frame() %>% mutate(quant=c(1, 2, 3, 4, 5, 6, 7, 8, 9))

(p_values_RQ2_LW_all_p <- p_values_RQ2_LW_wquant_p %>% cbind(p_values_adj_RQ2_LW_p) %>% dplyr::select(1, 17, 2, 18, 3, 19, 4, 20, 5, 21, 6, 22, 7, 23, 8, 24, 9, 25, 10, 26, 11, 27, 12, 28, 13, 29, 14, 30, 15, 31, 16, 32)  %>% round(3))

(all_new_VI_p <- all_ci_IV_p %>% full_join(coef_pool.IV_p, join_by(quantile == rowname)))

## get the statistically significant effects of interest from previous line (I'm doing this by hand)
#treatment = not statistically significant
#LW pre test skills = statistically significant for all quartiles
#PV pre test skills = NOT statistically significant 
#interaction of treatment with LW pre = NOT statistically significant at all quartiles
#interaction of treatment with PV pre = NOT statistically significant at all quartiles
```