---
title: "Teaching behaviours teaching practices"
author: "Matteo"
date: "19/01/2021"
output: html_document
---

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

library(stats)
library(readr)
library(tidyverse)
library(MASS)
library(sjPlot)
library(DCluster)
library(pscl)

#dataset
setwd("C:/Users/Utente/Documents/Matteo PhD/study3")
data<-read_csv("C:/Users/Utente/Documents/Matteo PhD/study3/TeachingFINALdata.csv")
data<-data%>%
  mutate(group = replace(group, group %in% c(1), 4))
data$group <- as.factor(data$group)

#to check variance
data1<-data%>%
  filter(group %in% c(4))
data2<-data%>%
  filter(group %in% c(2))
data3<-data%>%
  filter(group %in% c(3))

data$group<-as.factor(data$group)
data$group
data$teacher<-as.factor(data$teacher)
data$teacher

```




```{r cars, message=FALSE, warning=FALSE, paged.print=FALSE, include=FALSE}

#Supervises Poisson / Negative Binomial
mean(data1$Supervises)
var(data1$Supervises)
mean(data2$Supervises)
var(data2$Supervises)
mean(data3$Supervises)
var(data3$Supervises)

Supervises1<-glm(Supervises~ group+offset(log(Countofnumber2)), family= "poisson",  data= data)
Supervises2<-glm.nb(Supervises ~ group +offset(log(Countofnumber2)), data= data)

summary(Supervises1)
summary(Supervises2)

#likelyhood ratio test and Dean's test
test.nb.pois(Supervises2, Supervises1)
DeanB(Supervises1, alternative="greater")
DeanB2(Supervises1, alternative="greater")

#exponentialised coefficients
x<-coef(Supervises2)
exp(x)

tab_model(Supervises1,show.ci = 0.95,string.ci=TRUE, show.se = TRUE)
tab_model(Supervises2,show.ci = 0.95,string.ci=TRUE, show.se = TRUE)




#Instructssinglechild Poisson / Negative Binomial
mean(data1$Instructssinglechild)
var(data1$Instructssinglechild)
mean(data2$Instructssinglechild)
var(data2$Instructssinglechild)
mean(data3$Instructssinglechild)
var(data3$Instructssinglechild)

Instructssinglechild1<-glm(Instructssinglechild~ group+offset(log(Countofnumber2)), family= "poisson",  data= data)
Instructssinglechild2<-glm.nb(Instructssinglechild ~ group +offset(log(Countofnumber2)), data= data)

summary(Instructssinglechild1)
summary(Instructssinglechild2)

#likelyhood ratio test and Dean's test
test.nb.pois(Instructssinglechild2, Instructssinglechild1)
DeanB(Instructssinglechild1, alternative="greater")
DeanB2(Instructssinglechild1, alternative="greater")

#exponentialised coefficients
x<-coef(Instructssinglechild2)
exp(x)

tab_model(Instructssinglechild1,show.ci = 0.95,string.ci=TRUE, show.se = TRUE)
tab_model(Instructssinglechild2,show.ci = 0.95,string.ci=TRUE, show.se = TRUE)





#Instructsgroup Poisson / Negative Binomial
mean(data1$Instructsgroup)
var(data1$Instructsgroup)
mean(data2$Instructsgroup)
var(data2$Instructsgroup)
mean(data3$Instructsgroup)
var(data3$Instructsgroup)

Instructsgroup1<-glm(Instructsgroup~ group+offset(log(Countofnumber2)), family= "poisson",  data= data)
Instructsgroup2<-glm.nb(Instructsgroup ~ group +offset(log(Countofnumber2)), data= data)

summary(Instructsgroup1)
summary(Instructsgroup2)

#likelyhood ratio test and Dean's test
test.nb.pois(Instructsgroup2, Instructsgroup1)
DeanB(Instructsgroup1, alternative="greater")
DeanB2(Instructsgroup1, alternative="greater")

#exponentialised coefficients
x<-coef(Instructsgroup2)
exp(x)

tab_model(Instructsgroup1,show.ci = 0.95,string.ci=TRUE, show.se = TRUE)
tab_model(Instructsgroup2,show.ci = 0.95,string.ci=TRUE, show.se = TRUE)



#Instructsclass Poisson / Negative Binomial
mean(data1$Instructsclass)
var(data1$Instructsclass)
mean(data2$Instructsclass)
var(data2$Instructsclass)
mean(data3$Instructsclass)
var(data3$Instructsclass)

Instructsclass1<-glm(Instructsclass~ group+offset(log(Countofnumber2)), family= "poisson",  data= data)
Instructsclass2<-glm.nb(Instructsclass ~ group +offset(log(Countofnumber2)), data= data)

summary(Instructsclass1)
summary(Instructsclass2)

#likelyhood ratio test and Dean's test
test.nb.pois(Instructsclass2, Instructsclass1)
DeanB(Instructsclass1, alternative="greater")
DeanB2(Instructsclass1, alternative="greater")

#exponentialised coefficients
x<-coef(Instructsclass2)
exp(x)

tab_model(Instructsclass1,show.ci = 0.95,string.ci=TRUE, show.se = TRUE)
tab_model(Instructsclass2,show.ci = 0.95,string.ci=TRUE, show.se = TRUE)




#OffTask Poisson / Negative Binomial
mean(data1$OffTask)
var(data1$OffTask)
mean(data2$OffTask)
var(data2$OffTask)
mean(data3$OffTask)
var(data3$OffTask)

OffTask1<-glm(OffTask~ group+offset(log(Countofnumber2)), family= "poisson",  data= data)
OffTask2<-glm.nb(OffTask ~ group +offset(log(Countofnumber2)), data= data)

summary(OffTask1)
summary(OffTask2)

#likelyhood ratio test and Dean's test
test.nb.pois(OffTask2, OffTask1)
DeanB(OffTask1, alternative="greater")
DeanB2(OffTask1, alternative="greater")

#exponentialised coefficients
x<-coef(OffTask2)
exp(x)

tab_model(OffTask1,show.ci = 0.95,string.ci=TRUE, show.se = TRUE)
tab_model(OffTask2,show.ci = 0.95,string.ci=TRUE, show.se = TRUE)




```

## Results


```{r pressure, echo=FALSE}


summary(Supervises2)
tab_model(Supervises2,show.ci = 0.95,string.ci=TRUE, show.se = TRUE)


summary(Instructssinglechild2)
tab_model(Instructssinglechild2,show.ci = 0.95,string.ci=TRUE, show.se = TRUE)


summary(Instructsgroup2)
tab_model(Instructsgroup2,show.ci = 0.95,string.ci=TRUE, show.se = TRUE)


summary(Instructsclass2)
tab_model(Instructsclass2,show.ci = 0.95,string.ci=TRUE, show.se = TRUE)


summary(OffTask2)
tab_model(OffTask2,show.ci = 0.95,string.ci=TRUE, show.se = TRUE)









```


