---
title: "Activity management 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)
library(rcompanion)

#dataset
setwd("C:/Users/Utente/Documents/Matteo PhD/study3")
data<-read_csv("C:/Users/Utente/Documents/Matteo PhD/study3/TeachingFINALdata.csv")
#to check variance
data1<-data%>%
  filter(group %in% c(1))
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}


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

Signaling1<-glm(Signaling~ group+offset(log(Countofnumber2)), family= "poisson",  data= data)
Signaling2<-glm.nb(Signaling ~ group +offset(log(Countofnumber2)), data= data)

summary(Signaling1)
summary(Signaling2)

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

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

tab_model(Signaling1,show.ci = 0.95,string.ci=TRUE, show.se = TRUE)
tab_model(Signaling2,show.ci = 0.95,string.ci=TRUE, show.se = TRUE)




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

RetrievingequipmentO1<-glm(RetrievingequipmentO~ group+offset(log(Countofnumber2)), family= "poisson",  data= data)
RetrievingequipmentO2<-glm.nb(RetrievingequipmentO ~ group +offset(log(Countofnumber2)), data= data)

summary(RetrievingequipmentO1)
summary(RetrievingequipmentO2)

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

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

tab_model(RetrievingequipmentO1,show.ci = 0.95,string.ci=TRUE, show.se = TRUE)
tab_model(RetrievingequipmentO2,show.ci = 0.95,string.ci=TRUE, show.se = TRUE)




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

InterruptionPublic1<-glm(InterruptionPublic~ group+offset(log(Countofnumber2)), family= "poisson",  data= data)
InterruptionPublic2<-glm.nb(InterruptionPublic ~ group +offset(log(Countofnumber2)), data= data)

summary(InterruptionPublic1)
summary(InterruptionPublic2)

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

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

tab_model(InterruptionPublic1,show.ci = 0.95,string.ci=TRUE, show.se = TRUE)
tab_model(InterruptionPublic2,show.ci = 0.95,string.ci=TRUE, show.se = TRUE)



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

InterruptionPrivate1<-glm(InterruptionPrivate~ group+offset(log(Countofnumber2)), family= "poisson",  data= data)
InterruptionPrivate2<-glm.nb(InterruptionPrivate ~ group +offset(log(Countofnumber2)), data= data)

summary(InterruptionPrivate1)
summary(InterruptionPrivate2)

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

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

tab_model(InterruptionPrivate1,show.ci = 0.95,string.ci=TRUE, show.se = TRUE)
tab_model(InterruptionPrivate2,show.ci = 0.95,string.ci=TRUE, show.se = TRUE)




```

## Results


```{r pressure, echo=FALSE}



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


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


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


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

```


