---
title: "Reliability Study 2020"
author: "Devan Antczak"
date: "6/11/2020"
output:
  word_document: default
  html_document: default
---

#Packages
```{r}
library(tidyverse)
library(tidylog)
library(data.table)
library(ggplot2)
library(magrittr)
library(qwraps2)
library(tidyr)
library(nlme)
library(multilevel)
library(psych)
library(irr)
library(CTT)
library(dplyr)
library(psychometric)
library(ggpubr)
library(rstatix)
library(qqplotr)
```


##MVPA
```{r message=FALSE, warning=FALSE}
mvparesults <- rbind(1:8*0,1:8*0)
colnames(mvparesults) <- c("Crit", "Day","SingleDayICC","lowci","upci","0.7","0.8","0.9")
mvparesults <- mvparesults[-(1:2),]

for(i in 1:24){
  for(j in 2:7){
    x <- mvpadat
    x <- x %>% mutate_at(.vars = 2,~ ifelse(x[,10]<i,NA,.))
    x <- x %>% mutate_at(.vars = 3,~ ifelse(x[,11]<i,NA,.))
    x <- x %>% mutate_at(.vars = 4,~ ifelse(x[,12]<i,NA,.))
    x <- x %>% mutate_at(.vars = 5,~ ifelse(x[,13]<i,NA,.))
    x <- x %>% mutate_at(.vars = 6,~ ifelse(x[,14]<i,NA,.))
    x <- x %>% mutate_at(.vars = 7,~ ifelse(x[,15]<i,NA,.))
    x <- x %>% mutate_at(.vars = 8,~ ifelse(x[,16]<i,NA,.))
    x <- x %>% mutate_at(.vars = 9,~ ifelse(x[,17]<i,NA,.))
    
    x$includedays <- 8 - rowSums(is.na(x[,2:9]))
    xdays <- subset(x, includedays >= j)
    
    xdays <- xdays %>% 
      dplyr::select(id,timepoint, mvpa2:mvpa9)
    
    temp <- xdays %>%
      melt(id.vars=c("id","timepoint"))
    
    temp <- arrange(temp, id, timepoint)
    temp <- temp[!is.na(temp$value),]
    
    sample1 <- temp %>% group_by(id, timepoint) %>% sample_n(j)
    sample2 <- temp %>% group_by(id, timepoint) %>% sample_n(j)
    sample3 <- temp %>% group_by(id, timepoint) %>% sample_n(j)
    sample4 <- temp %>% group_by(id, timepoint) %>% sample_n(j)
    sample5 <- temp %>% group_by(id, timepoint) %>% sample_n(j)
    
    sample1$variable <- rep(c(1:j), length.out=nrow(sample1))
    sample2$variable <- rep(c(1:j), length.out=nrow(sample2))
    sample3$variable <- rep(c(1:j), length.out=nrow(sample3))
    sample4$variable <- rep(c(1:j), length.out=nrow(sample4))
    sample5$variable <- rep(c(1:j), length.out=nrow(sample5))
    
    sample1 <- reshape2::dcast(sample1, id + timepoint ~ variable, value.var="value")
    sample2 <- reshape2::dcast(sample2, id + timepoint ~ variable, value.var="value")
    sample3 <- reshape2::dcast(sample3, id + timepoint ~ variable, value.var="value")
    sample4 <- reshape2::dcast(sample4, id + timepoint ~ variable, value.var="value")
    sample5 <- reshape2::dcast(sample5, id + timepoint ~ variable, value.var="value")
      
    x1 <- sample1 %>% dplyr::select(3:(2+j))
    x2 <- sample2 %>% dplyr::select(3:(2+j))
    x3 <- sample3 %>% dplyr::select(3:(2+j))
    x4 <- sample4 %>% dplyr::select(3:(2+j))
    x5 <- sample5 %>% dplyr::select(3:(2+j))
    
    x1 <- as.matrix(x1)
    x2 <- as.matrix(x2)
    x3 <- as.matrix(x3)
    x4 <- as.matrix(x4)
    x5 <- as.matrix(x5)
    
    IC1 <- ICC(x1)
    IC2 <- ICC(x2)
    IC3 <- ICC(x3)
    IC4 <- ICC(x4)
    IC5 <- ICC(x5)
    
    sum1 <- IC1[[1]]
    sum2 <- IC2[[1]]
    sum3 <- IC3[[1]]
    sum4 <- IC4[[1]]
    sum5 <- IC5[[1]]
    
    est1 <- sum1[3,2]
    est2 <- sum2[3,2]
    est3 <- sum3[3,2]
    est4 <- sum4[3,2]
    est5 <- sum5[3,2]
    
    upci1 <- sum1[3,8]
     upci2 <- sum2[3,8]
      upci3 <- sum3[3,8] 
       upci4 <- sum4[3,8]
        upci5 <- sum5[3,8]
        
    lowci1 <- sum1[3,7]
    lowci2 <- sum2[3,7]
    lowci3 <- sum3[3,7]
    lowci4 <- sum4[3,7]
    lowci5 <- sum5[3,7]
      
    average.est <- mean(c(est1,est2,est3,est4,est5))
    average.upci <- mean(c(upci1,upci2,upci3,upci4,upci5))
    average.lowci <- mean(c(lowci1,lowci2,lowci3,lowci4,lowci5))
    
    sb7 <- spearman.brown(average.est, .7, "r")
    sb7[[1]]
    sb8 <- spearman.brown(average.est, .8, "r")
    sb8[[1]]
    sb9 <- spearman.brown(average.est, .9, "r")
    sb9[[1]]
    
    mvparesults <- rbind(mvparesults, c(i,j,average.est, average.lowci,average.upci,sb7[[1]],sb8[[1]],sb9[[1]]))
  }
}
```


##Sleep Duration
```{r message=FALSE, warning=FALSE}
slstart <- Sys.time()

nocresults <- rbind(1:8*0,1:8*0)
colnames(nocresults) <- c("Crit", "Day","SingleDayICC","lowci","upci","0.7","0.8","0.9")
nocresults <- nocresults[-(1:2),]

for(i in 5:10){
  for(j in 2:7){
    x <- nocdat
    x <- x %>% mutate_at(.vars = 2,~ ifelse(x[,10]<i,NA,.))
    x <- x %>% mutate_at(.vars = 3,~ ifelse(x[,11]<i,NA,.))
    x <- x %>% mutate_at(.vars = 4,~ ifelse(x[,12]<i,NA,.))
    x <- x %>% mutate_at(.vars = 5,~ ifelse(x[,13]<i,NA,.))
    x <- x %>% mutate_at(.vars = 6,~ ifelse(x[,14]<i,NA,.))
    x <- x %>% mutate_at(.vars = 7,~ ifelse(x[,15]<i,NA,.))
    x <- x %>% mutate_at(.vars = 8,~ ifelse(x[,16]<i,NA,.))
    x <- x %>% mutate_at(.vars = 9,~ ifelse(x[,17]<i,NA,.))
    
    x$includedays <- 8 - rowSums(is.na(x[,2:9]))
    xdays <- subset(x, includedays >= j)
    
    xdays <- xdays %>% 
      dplyr::select(id,timepoint, noc2:noc9)
    
    temp <- xdays %>%
      melt(id.vars=c("id","timepoint"))
    
    temp <- arrange(temp, id, timepoint)
    temp <- temp[!is.na(temp$value),]
    
    sample1 <- temp %>% group_by(id, timepoint) %>% sample_n(j)
    sample2 <- temp %>% group_by(id, timepoint) %>% sample_n(j)
    sample3 <- temp %>% group_by(id, timepoint) %>% sample_n(j)
    sample4 <- temp %>% group_by(id, timepoint) %>% sample_n(j)
    sample5 <- temp %>% group_by(id, timepoint) %>% sample_n(j)
    
    sample1$variable <- rep(c(1:j), length.out=nrow(sample1))
    sample2$variable <- rep(c(1:j), length.out=nrow(sample2))
    sample3$variable <- rep(c(1:j), length.out=nrow(sample3))
    sample4$variable <- rep(c(1:j), length.out=nrow(sample4))
    sample5$variable <- rep(c(1:j), length.out=nrow(sample5))
    
    sample1 <- reshape2::dcast(sample1, id + timepoint ~ variable, value.var="value")
    sample2 <- reshape2::dcast(sample2, id + timepoint ~ variable, value.var="value")
    sample3 <- reshape2::dcast(sample3, id + timepoint ~ variable, value.var="value")
    sample4 <- reshape2::dcast(sample4, id + timepoint ~ variable, value.var="value")
    sample5 <- reshape2::dcast(sample5, id + timepoint ~ variable, value.var="value")
      
    x1 <- sample1 %>% dplyr::select(3:(2+j))
    x2 <- sample2 %>% dplyr::select(3:(2+j))
    x3 <- sample3 %>% dplyr::select(3:(2+j))
    x4 <- sample4 %>% dplyr::select(3:(2+j))
    x5 <- sample5 %>% dplyr::select(3:(2+j))
    
    x1 <- as.matrix(x1)
    x2 <- as.matrix(x2)
    x3 <- as.matrix(x3)
    x4 <- as.matrix(x4)
    x5 <- as.matrix(x5)
    
    IC1 <- ICC(x1)
    IC2 <- ICC(x2)
    IC3 <- ICC(x3)
    IC4 <- ICC(x4)
    IC5 <- ICC(x5)
    
    sum1 <- IC1[[1]]
    sum2 <- IC2[[1]]
    sum3 <- IC3[[1]]
    sum4 <- IC4[[1]]
    sum5 <- IC5[[1]]
    
    est1 <- sum1[3,2]
    est2 <- sum2[3,2]
    est3 <- sum3[3,2]
    est4 <- sum4[3,2]
    est5 <- sum5[3,2]
    
    upci1 <- sum1[3,8]
     upci2 <- sum2[3,8]
      upci3 <- sum3[3,8] 
       upci4 <- sum4[3,8]
        upci5 <- sum5[3,8]
        
    lowci1 <- sum1[3,7]
    lowci2 <- sum2[3,7]
    lowci3 <- sum3[3,7]
    lowci4 <- sum4[3,7]
    lowci5 <- sum5[3,7]
      
    average.est <- mean(c(est1,est2,est3,est4,est5))
    average.upci <- mean(c(upci1,upci2,upci3,upci4,upci5))
    average.lowci <- mean(c(lowci1,lowci2,lowci3,lowci4,lowci5))
    
    sb7 <- spearman.brown(average.est, .7, "r")
    sb7[[1]]
    sb8 <- spearman.brown(average.est, .8, "r")
    sb8[[1]]
    sb9 <- spearman.brown(average.est, .9, "r")
    sb9[[1]]
    
    nocresults <- rbind(nocresults, c(i*10,j,average.est, average.lowci,average.upci,sb7[[1]],sb8[[1]],sb9[[1]]))
  }
}
slend <- Sys.time()
slend-slstart

#nocresults
```


#Table Data
##Functions
```{r}
AggRes <- function(x){
  frame <- as.data.frame(x)
  frame <- frame %>% 
  group_by(Crit) %>% summarise(SingleDayICC = mean(SingleDayICC))
}

lowerci <- function(x){
  frame <- as.data.frame(x)
  frame <- frame %>% 
  group_by(Crit) %>% summarise(lowci = mean(lowci))
}

upperci <- function(x){
  frame <- as.data.frame(x)
  frame <- frame %>% 
  group_by(Crit) %>% summarise(upci = mean(upci))
}

seven <- function(x){
  spearman.brown(x,.7,"r")
}

eight <- function(x){
  spearman.brown(x,.8,"r")
}

nine <- function(x){
  spearman.brown(x,.9,"r")
}

getICC <- function(x){
res <- AggRes(x)

low <- lowerci(x)
low <- round(low[,2],2)

up <- upperci(x)
up <- round(up[,2],2)

sev <- sapply(res[,2], FUN=seven)
sev <- round(sev[[1]],1)

eig <- sapply(res[,2], FUN=eight)
eig <- round(eig[[1]],1)

nin <- sapply(res[,2], FUN=nine)
nin <- round(nin[[1]],1)

cbind(round(res,2), low,up,sev,eig,nin)
}
```

##ICC Table
```{r message=FALSE, warning=FALSE}
e <- getICC(mvparesults)
y <- getICC(nocresults)
```


##Meeting Criteria
###mvpa
```{r message=FALSE, warning=FALSE}

nmvparesults <- rbind(1:2*0,1:2*0)
colnames(nmvparesults) <- c("mvpN7","%7")
nmvparesults <- nmvparesults[-(1:2),]

for(i in e$Crit){
  
  b <- i/10
  c <- e$sev[e$Crit==i]
    x <- mvpadat
    x <- x %>% mutate_at(.vars = 2,~ ifelse(x[,10]<b,NA,.))
    x <- x %>% mutate_at(.vars = 3,~ ifelse(x[,11]<b,NA,.))
    x <- x %>% mutate_at(.vars = 4,~ ifelse(x[,12]<b,NA,.))
    x <- x %>% mutate_at(.vars = 5,~ ifelse(x[,13]<b,NA,.))
    x <- x %>% mutate_at(.vars = 6,~ ifelse(x[,14]<b,NA,.))
    x <- x %>% mutate_at(.vars = 7,~ ifelse(x[,15]<b,NA,.))
    x <- x %>% mutate_at(.vars = 8,~ ifelse(x[,16]<b,NA,.))
    x <- x %>% mutate_at(.vars = 9,~ ifelse(x[,17]<b,NA,.))
    
    x$includedays <- 8 - rowSums(is.na(x[,2:9]))
    as.numeric(c)
    x <- subset(x, includedays >= c)
    
    total <- nrow(mvpadat)
    N <- nrow(x)
    percent <- N/total
    
    nmvparesults <- rbind(nmvparesults, c(N,round(percent*100,1)))
  }
e <- cbind(e, nmvparesults)

nmvparesults <- rbind(1:2*0,1:2*0)
colnames(nmvparesults) <- c("mvpN8","%8")
nmvparesults <- nmvparesults[-(1:2),]

for(i in e$Crit){
  
  b <- i/10
  c <- e$eig[e$Crit==i]
    x <- mvpadat
    x <- x %>% mutate_at(.vars = 2,~ ifelse(x[,10]<b,NA,.))
    x <- x %>% mutate_at(.vars = 3,~ ifelse(x[,11]<b,NA,.))
    x <- x %>% mutate_at(.vars = 4,~ ifelse(x[,12]<b,NA,.))
    x <- x %>% mutate_at(.vars = 5,~ ifelse(x[,13]<b,NA,.))
    x <- x %>% mutate_at(.vars = 6,~ ifelse(x[,14]<b,NA,.))
    x <- x %>% mutate_at(.vars = 7,~ ifelse(x[,15]<b,NA,.))
    x <- x %>% mutate_at(.vars = 8,~ ifelse(x[,16]<b,NA,.))
    x <- x %>% mutate_at(.vars = 9,~ ifelse(x[,17]<b,NA,.))
    
    x$includedays <- 8 - rowSums(is.na(x[,2:9]))
    as.numeric(c)
    x <- subset(x, includedays >= c)
    
    total <- nrow(mvpadat)
    N <- nrow(x)
    percent <- N/total
    
    nmvparesults <- rbind(nmvparesults, c(N,round(percent*100,1)))
  }
e <- cbind(e, nmvparesults)
```

###Sleep Duration
```{r message=FALSE, warning=FALSE}

nnocresults <- rbind(1:2*0,1:2*0)
colnames(nnocresults) <- c("nocN7","%7")
nnocresults <- nnocresults[-(1:2),]

for(i in y$Crit){
  
  b <- i/10
  c <- y$sev[y$Crit==i]
    x <- nocdat
    x <- x %>% mutate_at(.vars = 2,~ ifelse(x[,10]<b,NA,.))
    x <- x %>% mutate_at(.vars = 3,~ ifelse(x[,11]<b,NA,.))
    x <- x %>% mutate_at(.vars = 4,~ ifelse(x[,12]<b,NA,.))
    x <- x %>% mutate_at(.vars = 5,~ ifelse(x[,13]<b,NA,.))
    x <- x %>% mutate_at(.vars = 6,~ ifelse(x[,14]<b,NA,.))
    x <- x %>% mutate_at(.vars = 7,~ ifelse(x[,15]<b,NA,.))
    x <- x %>% mutate_at(.vars = 8,~ ifelse(x[,16]<b,NA,.))
    x <- x %>% mutate_at(.vars = 9,~ ifelse(x[,17]<b,NA,.))
    
    x$includedays <- 8 - rowSums(is.na(x[,2:9]))
    as.numeric(c)
    x <- subset(x, includedays >= c)
    
    total <- nrow(nocdat)
    N <- nrow(x)
    percent <- N/total
    
    nnocresults <- rbind(nnocresults, c(N,round(percent*100,1)))
  }
y <- cbind(y, nnocresults)


nnocresults <- rbind(1:2*0,1:2*0)
colnames(nnocresults) <- c("nocN8","%8")
nnocresults <- nnocresults[-(1:2),]

for(i in y$Crit){
  
  b <- i/10
  c <- y$eig[y$Crit==i]
    x <- nocdat
    x <- x %>% mutate_at(.vars = 2,~ ifelse(x[,10]<b,NA,.))
    x <- x %>% mutate_at(.vars = 3,~ ifelse(x[,11]<b,NA,.))
    x <- x %>% mutate_at(.vars = 4,~ ifelse(x[,12]<b,NA,.))
    x <- x %>% mutate_at(.vars = 5,~ ifelse(x[,13]<b,NA,.))
    x <- x %>% mutate_at(.vars = 6,~ ifelse(x[,14]<b,NA,.))
    x <- x %>% mutate_at(.vars = 7,~ ifelse(x[,15]<b,NA,.))
    x <- x %>% mutate_at(.vars = 8,~ ifelse(x[,16]<b,NA,.))
    x <- x %>% mutate_at(.vars = 9,~ ifelse(x[,17]<b,NA,.))
    
    x$includedays <- 8 - rowSums(is.na(x[,2:9]))
    as.numeric(c)
    x <- subset(x, includedays >= c)
    
    total <- nrow(nocdat)
    N <- nrow(x)
    percent <- N/total
    
    nnocresults <- rbind(nnocresults, c(N,round(percent*100,1)))
  }
y <- cbind(y, nnocresults)
```

#### Show Tables
```{r}
e
y
```
