#Load in libraries
library(tidyverse)
## Warning: package 'stringr' was built under R version 4.2.3
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ dplyr     1.1.3     ✔ readr     2.1.4
## ✔ forcats   1.0.0     ✔ stringr   1.5.1
## ✔ ggplot2   3.4.4     ✔ tibble    3.2.1
## ✔ lubridate 1.9.3     ✔ tidyr     1.3.0
## ✔ purrr     1.0.2     
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag()    masks stats::lag()
## ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
library(survMisc)
## Loading required package: survival
## 
## Attaching package: 'survMisc'
## 
## The following object is masked from 'package:ggplot2':
## 
##     autoplot
library(haven)
library(lubridate)
library(mfp)
library(knitr)
library(kableExtra)
## 
## Attaching package: 'kableExtra'
## 
## The following object is masked from 'package:dplyr':
## 
##     group_rows
library(sjPlot)
## Learn more about sjPlot with 'browseVignettes("sjPlot")'.
library(ggplot2)
library(broom)

#Load in dataset
absence <- read_dta("/Users/usc-shannabadhesha/Desktop/PM592/PM511B_Project5_data.dta")

#Table 1: means, percentages, SDs, Ns
absence %>%
  skimr::skim()
Data summary
Name Piped data
Number of rows 395
Number of columns 22
_______________________
Column type frequency:
character 10
numeric 12
________________________
Group variables None

Variable type: character

skim_variable n_missing complete_rate min max empty n_unique whitespace
school 0 1 2 2 0 2 0
sex 0 1 1 1 0 2 0
address 0 1 1 1 0 2 0
famsize 0 1 3 3 0 2 0
pstatus 0 1 1 1 0 2 0
paid 0 1 2 3 0 2 0
activities 0 1 2 3 0 2 0
higher 0 1 2 3 0 2 0
internet 0 1 2 3 0 2 0
romantic 0 1 2 3 0 2 0

Variable type: numeric

skim_variable n_missing complete_rate mean sd p0 p25 p50 p75 p100 hist
age 0 1 16.70 1.28 15 16 17 18 22 ▇▅▅▁▁
medu 0 1 2.75 1.09 0 2 3 4 4 ▁▃▆▆▇
fedu 0 1 2.52 1.09 0 2 2 3 4 ▁▆▇▇▇
traveltime 0 1 1.45 0.70 1 1 1 2 4 ▇▃▁▁▁
studytime 0 1 2.04 0.84 1 1 2 2 4 ▅▇▁▂▁
famrel 0 1 3.94 0.90 1 4 4 5 5 ▁▁▃▇▅
freetime 0 1 3.24 1.00 1 3 3 4 5 ▁▃▇▆▂
goout 0 1 3.11 1.11 1 2 3 4 5 ▂▆▇▅▃
dalc 0 1 1.48 0.89 1 1 1 2 5 ▇▂▁▁▁
walc 0 1 2.29 1.29 1 1 2 3 5 ▇▅▅▃▂
health 0 1 3.55 1.39 1 3 4 5 5 ▂▂▅▃▇
absences 0 1 5.71 8.00 0 0 4 8 75 ▇▁▁▁▁
#viewing the unique values for each column
lapply(absence, function(col) unique(col))
## $school
## [1] "GP" "MS"
## 
## $sex
## [1] "F" "M"
## 
## $age
## [1] 18 17 15 16 19 22 20 21
## 
## $address
## [1] "U" "R"
## 
## $famsize
## [1] "GT3" "LE3"
## 
## $pstatus
## [1] "A" "T"
## 
## $medu
## [1] 4 1 3 2 0
## 
## $fedu
## [1] 4 1 2 3 0
## 
## $traveltime
## [1] 2 1 3 4
## 
## $studytime
## [1] 2 3 1 4
## 
## $paid
## [1] "no"  "yes"
## 
## $activities
## [1] "no"  "yes"
## 
## $higher
## [1] "yes" "no" 
## 
## $internet
## [1] "no"  "yes"
## 
## $romantic
## [1] "no"  "yes"
## 
## $famrel
## [1] 4 5 3 1 2
## 
## $freetime
## [1] 3 2 4 1 5
## 
## $goout
## [1] 4 3 2 1 5
## 
## $dalc
## [1] 1 2 5 3 4
## 
## $walc
## [1] 1 3 2 4 5
## 
## $health
## [1] 3 5 1 2 4
## 
## $absences
##  [1]  6  4 10  2  0 16 14  7  8 25 12 54 18 26 20 56 24 28  5 13 15 22  3 21  1
## [26] 75 30 19  9 11 38 40 23 17
absence %>%
  select_if(is.character) %>%
  summarise_all(~table(.))
## Warning: Returning more (or less) than 1 row per `summarise()` group was deprecated in
## dplyr 1.1.0.
## ℹ Please use `reframe()` instead.
## ℹ When switching from `summarise()` to `reframe()`, remember that `reframe()`
##   always returns an ungrouped data frame and adjust accordingly.
## ℹ The deprecated feature was likely used in the dplyr package.
##   Please report the issue at <https://github.com/tidyverse/dplyr/issues>.
## This warning is displayed once every 8 hours.
## Call `lifecycle::last_lifecycle_warnings()` to see where this warning was
## generated.
## # A tibble: 2 × 10
##   school sex   address famsize pstatus paid  activities higher internet romantic
##   <tabl> <tab> <table> <table> <table> <tab> <table[1d> <tabl> <table[> <table[>
## 1 349    208    88     281      41     214   194         20     66      263     
## 2  46    187   307     114     354     181   201        375    329      132
absence %>%
  summarise_all(~sum(is.na(.)) / n())
## # A tibble: 1 × 22
##   school   sex   age address famsize pstatus  medu  fedu traveltime studytime
##    <dbl> <dbl> <dbl>   <dbl>   <dbl>   <dbl> <dbl> <dbl>      <dbl>     <dbl>
## 1      0     0     0       0       0       0     0     0          0         0
## # ℹ 12 more variables: paid <dbl>, activities <dbl>, higher <dbl>,
## #   internet <dbl>, romantic <dbl>, famrel <dbl>, freetime <dbl>, goout <dbl>,
## #   dalc <dbl>, walc <dbl>, health <dbl>, absences <dbl>
absence %>%
  summarise_all(~sum(!is.na(.)))
## # A tibble: 1 × 22
##   school   sex   age address famsize pstatus  medu  fedu traveltime studytime
##    <int> <int> <int>   <int>   <int>   <int> <int> <int>      <int>     <int>
## 1    395   395   395     395     395     395   395   395        395       395
## # ℹ 12 more variables: paid <int>, activities <int>, higher <int>,
## #   internet <int>, romantic <int>, famrel <int>, freetime <int>, goout <int>,
## #   dalc <int>, walc <int>, health <int>, absences <int>
absence %>%
  Hmisc::describe()
## . 
## 
##  22  Variables      395  Observations
## --------------------------------------------------------------------------------
## school  Format:%9s 
##        n  missing distinct 
##      395        0        2 
##                       
## Value         GP    MS
## Frequency    349    46
## Proportion 0.884 0.116
## --------------------------------------------------------------------------------
## sex  Format:%9s 
##        n  missing distinct 
##      395        0        2 
##                       
## Value          F     M
## Frequency    208   187
## Proportion 0.527 0.473
## --------------------------------------------------------------------------------
## age  Format:%8.0g 
##        n  missing distinct     Info     Mean      Gmd 
##      395        0        8    0.948     16.7    1.411 
##                                                           
## Value         15    16    17    18    19    20    21    22
## Frequency     82   104    98    82    24     3     1     1
## Proportion 0.208 0.263 0.248 0.208 0.061 0.008 0.003 0.003
## 
## For the frequency table, variable is rounded to the nearest 0
## --------------------------------------------------------------------------------
## address  Format:%9s 
##        n  missing distinct 
##      395        0        2 
##                       
## Value          R     U
## Frequency     88   307
## Proportion 0.223 0.777
## --------------------------------------------------------------------------------
## famsize  Format:%9s 
##        n  missing distinct 
##      395        0        2 
##                       
## Value        GT3   LE3
## Frequency    281   114
## Proportion 0.711 0.289
## --------------------------------------------------------------------------------
## pstatus : Pstatus  Format:%9s 
##        n  missing distinct 
##      395        0        2 
##                       
## Value          A     T
## Frequency     41   354
## Proportion 0.104 0.896
## --------------------------------------------------------------------------------
## medu : Medu  Format:%8.0g 
##        n  missing distinct     Info     Mean      Gmd 
##      395        0        5    0.927    2.749    1.213 
##                                         
## Value          0     1     2     3     4
## Frequency      3    59   103    99   131
## Proportion 0.008 0.149 0.261 0.251 0.332
## 
## For the frequency table, variable is rounded to the nearest 0
## --------------------------------------------------------------------------------
## fedu : Fedu  Format:%8.0g 
##        n  missing distinct     Info     Mean      Gmd 
##      395        0        5    0.936    2.522    1.216 
##                                         
## Value          0     1     2     3     4
## Frequency      2    82   115   100    96
## Proportion 0.005 0.208 0.291 0.253 0.243
## 
## For the frequency table, variable is rounded to the nearest 0
## --------------------------------------------------------------------------------
## traveltime  Format:%8.0g 
##        n  missing distinct     Info     Mean      Gmd 
##      395        0        4    0.704    1.448   0.6406 
##                                   
## Value          1     2     3     4
## Frequency    257   107    23     8
## Proportion 0.651 0.271 0.058 0.020
## 
## For the frequency table, variable is rounded to the nearest 0
## --------------------------------------------------------------------------------
## studytime  Format:%8.0g 
##        n  missing distinct     Info     Mean      Gmd 
##      395        0        4     0.85    2.035   0.8772 
##                                   
## Value          1     2     3     4
## Frequency    105   198    65    27
## Proportion 0.266 0.501 0.165 0.068
## 
## For the frequency table, variable is rounded to the nearest 0
## --------------------------------------------------------------------------------
## paid  Format:%9s 
##        n  missing distinct 
##      395        0        2 
##                       
## Value         no   yes
## Frequency    214   181
## Proportion 0.542 0.458
## --------------------------------------------------------------------------------
## activities  Format:%9s 
##        n  missing distinct 
##      395        0        2 
##                       
## Value         no   yes
## Frequency    194   201
## Proportion 0.491 0.509
## --------------------------------------------------------------------------------
## higher  Format:%9s 
##        n  missing distinct 
##      395        0        2 
##                       
## Value         no   yes
## Frequency     20   375
## Proportion 0.051 0.949
## --------------------------------------------------------------------------------
## internet  Format:%9s 
##        n  missing distinct 
##      395        0        2 
##                       
## Value         no   yes
## Frequency     66   329
## Proportion 0.167 0.833
## --------------------------------------------------------------------------------
## romantic  Format:%9s 
##        n  missing distinct 
##      395        0        2 
##                       
## Value         no   yes
## Frequency    263   132
## Proportion 0.666 0.334
## --------------------------------------------------------------------------------
## famrel  Format:%8.0g 
##        n  missing distinct     Info     Mean      Gmd 
##      395        0        5    0.855    3.944   0.9204 
##                                         
## Value          1     2     3     4     5
## Frequency      8    18    68   195   106
## Proportion 0.020 0.046 0.172 0.494 0.268
## 
## For the frequency table, variable is rounded to the nearest 0
## --------------------------------------------------------------------------------
## freetime  Format:%8.0g 
##        n  missing distinct     Info     Mean      Gmd 
##      395        0        5    0.907    3.235    1.085 
##                                         
## Value          1     2     3     4     5
## Frequency     19    64   157   115    40
## Proportion 0.048 0.162 0.397 0.291 0.101
## 
## For the frequency table, variable is rounded to the nearest 0
## --------------------------------------------------------------------------------
## goout  Format:%8.0g 
##        n  missing distinct     Info     Mean      Gmd 
##      395        0        5    0.934    3.109    1.236 
##                                         
## Value          1     2     3     4     5
## Frequency     23   103   130    86    53
## Proportion 0.058 0.261 0.329 0.218 0.134
## 
## For the frequency table, variable is rounded to the nearest 0
## --------------------------------------------------------------------------------
## dalc : Dalc  Format:%8.0g 
##        n  missing distinct     Info     Mean      Gmd 
##      395        0        5    0.652    1.481   0.7524 
##                                         
## Value          1     2     3     4     5
## Frequency    276    75    26     9     9
## Proportion 0.699 0.190 0.066 0.023 0.023
## 
## For the frequency table, variable is rounded to the nearest 0
## --------------------------------------------------------------------------------
## walc : Walc  Format:%8.0g 
##        n  missing distinct     Info     Mean      Gmd 
##      395        0        5    0.923    2.291    1.409 
##                                         
## Value          1     2     3     4     5
## Frequency    151    85    80    51    28
## Proportion 0.382 0.215 0.203 0.129 0.071
## 
## For the frequency table, variable is rounded to the nearest 0
## --------------------------------------------------------------------------------
## health  Format:%8.0g 
##        n  missing distinct     Info     Mean      Gmd 
##      395        0        5    0.929    3.554    1.534 
##                                         
## Value          1     2     3     4     5
## Frequency     47    45    91    66   146
## Proportion 0.119 0.114 0.230 0.167 0.370
## 
## For the frequency table, variable is rounded to the nearest 0
## --------------------------------------------------------------------------------
## absences  Format:%8.0g 
##        n  missing distinct     Info     Mean      Gmd      .05      .10 
##      395        0       34    0.968    5.709    7.026      0.0      0.0 
##      .25      .50      .75      .90      .95 
##      0.0      4.0      8.0     14.0     18.3 
## 
## lowest :  0  1  2  3  4, highest: 38 40 54 56 75
## --------------------------------------------------------------------------------
absence$school.f <- as.numeric(factor(absence$school, levels = c("GP", "MS")))
absence$sex.f <- as.numeric(factor(absence$sex, levels = c("F", "M")))
absence$address.f <- as.numeric(factor(absence$address, levels = c("U", "R")))
absence$famsize.f <- as.numeric(factor(absence$famsize, levels = c("GT3", "LE3")))
absence$pstatus.f <- as.numeric(factor(absence$pstatus, levels = c("A", "T")))
absence$paid.f <- as.numeric(factor(absence$paid, levels = c("no", "yes")))
absence$activities.f <- as.numeric(factor(absence$activities, levels = c("no", "yes")))
absence$higher.f <- as.numeric(factor(absence$higher, levels = c("no", "yes")))
absence$internet.f <- as.numeric(factor(absence$internet, levels = c("no", "yes")))
absence$romantic.f <- as.numeric(factor(absence$romantic, levels = c("no", "yes")))

#Question 1- demographics

#age
mfp(absences~fp(age), data= absence, family= poisson)
## Call:
## mfp(formula = absences ~ fp(age), data = absence, family = poisson)
## 
## 
## Deviance table:
##           Resid. Dev
## Null model    3223.267
## Linear model  3092.319
## Final model   3084.598
## 
## Fractional polynomials:
##     df.initial select alpha df.final power1 power2
## age          4      1  0.05        2     -2      .
## 
## 
## Transformations of covariates:
##            formula
## age I((age/10)^-2)
## 
## Coefficients:
## Intercept      age.1  
##     3.399     -4.626  
## 
## Degrees of Freedom: 394 Total (i.e. Null);  393 Residual
## Null Deviance:       3223 
## Residual Deviance: 3085  AIC: 4098
absence <-
  absence %>%
  mutate(age100 = ((age/100)^2))

glm_age <- glm(absences~fp(age100), data =  absence, family = poisson)

#mother's education
mfp(absences~fp(medu), data= absence, family= poisson)
## Call:
## mfp(formula = absences ~ fp(medu), data = absence, family = poisson)
## 
## 
## Deviance table:
##           Resid. Dev
## Null model    3223.267
## Linear model  3178.044
## Final model   3105.226
## 
## Fractional polynomials:
##      df.initial select alpha df.final power1 power2
## medu          4      1  0.05        4      3      3
## 
## 
## Transformations of covariates:
##                                                       formula
## medu I(((medu+1)/10)^3)+I(((medu+1)/10)^3*log(((medu+1)/10)))
## 
## Coefficients:
## Intercept     medu.1     medu.2  
##    0.7959   -26.5891   -49.4971  
## 
## Degrees of Freedom: 394 Total (i.e. Null);  392 Residual
## Null Deviance:       3223 
## Residual Deviance: 3105  AIC: 4121
absence <-
  absence %>%
  mutate(medu1 = ((medu+1)/10)^3)

absence <-
  absence %>%
  mutate(medu2 = ((medu+1)/10)^3*log(((medu+1)/10)))


glm_medu <- glm(absences~fp(medu1) + fp(medu2), data =  absence, family = poisson)

#father's education
mfp(absences~fp(fedu), data= absence, family= poisson)
## Call:
## mfp(formula = absences ~ fp(fedu), data = absence, family = poisson)
## 
## 
## Deviance table:
##           Resid. Dev
## Null model    3223.267
## Linear model  3220.619
## Final model   3207.043
## 
## Fractional polynomials:
##      df.initial select alpha df.final power1 power2
## fedu          4      1  0.05        4      3      3
## 
## 
## Transformations of covariates:
##                                                       formula
## fedu I(((fedu+1)/10)^3)+I(((fedu+1)/10)^3*log(((fedu+1)/10)))
## 
## Coefficients:
## Intercept     fedu.1     fedu.2  
##     1.459    -10.877    -18.514  
## 
## Degrees of Freedom: 394 Total (i.e. Null);  392 Residual
## Null Deviance:       3223 
## Residual Deviance: 3207  AIC: 4222
absence <-
  absence %>%
  mutate(fedu1 = ((fedu+1)/10)^3)

absence <-
  absence %>%
  mutate(fedu2 = ((fedu+1)/10)^3*log(((fedu+1)/10)))

glm_fedu <- glm(absences~fp(fedu1) + fp(fedu2), data =  absence, family = poisson)

#sex

absence %>%
  ggplot(aes(group = sex.f, y = absences)) +
  geom_boxplot()

lm_sex <- lm(absences~sex.f, data= absence)

#school
absence %>%
  ggplot(aes(group = school.f, y = absences)) +
  geom_boxplot()

lm_school <- lm(absences~school.f, data= absence)

#address
absence %>%
  ggplot(aes(group = address.f, y = absences)) +
  geom_boxplot()

lm_address <- lm(absences~address.f, data= absence)

#family size
absence %>%
  ggplot(aes(group = famsize.f, y = absences)) +
  geom_boxplot()

lm_famsize <- lm(absences~famsize.f, data= absence)

#parental status
absence %>%
  ggplot(aes(group = pstatus.f, y = absences)) +
  geom_boxplot()

lm_pstatus <- lm(absences~pstatus.f, data= absence)


lm_age <- lm(absences~age, data= absence)
lm_medu <- lm(absences~medu, data= absence)
lm_fedu <- lm(absences~fedu, data= absence)

#table for all the models
tab_model(lm_sex, lm_school, lm_pstatus, lm_famsize, lm_address, lm_age, lm_fedu, lm_medu)
  absences absences absences absences absences absences absences absences
Predictors Estimates CI p Estimates CI p Estimates CI p Estimates CI p Estimates CI p Estimates CI p Estimates CI p Estimates CI p
(Intercept) 7.29 4.82 – 9.75 <0.001 8.17 5.31 – 11.03 <0.001 12.41 7.47 – 17.36 <0.001 4.90 2.51 – 7.28 <0.001 5.05 2.59 – 7.51 <0.001 -12.64 -22.89 – -2.39 0.016 5.26 3.25 – 7.26 <0.001 3.69 1.56 – 5.83 0.001
sex.f -1.07 -2.66 – 0.51 0.184
school.f -2.20 -4.67 – 0.26 0.079
pstatus.f -3.54 -6.11 – -0.96 0.007
famsize.f 0.63 -1.12 – 2.38 0.478
address.f 0.54 -1.37 – 2.44 0.581
age 1.10 0.49 – 1.71 <0.001
Fedu 0.18 -0.55 – 0.91 0.628
Medu 0.73 0.01 – 1.45 0.046
Observations 395 395 395 395 395 395 395 395
R2 / R2 adjusted 0.004 / 0.002 0.008 / 0.005 0.018 / 0.016 0.001 / -0.001 0.001 / -0.002 0.031 / 0.028 0.001 / -0.002 0.010 / 0.008
tab_model(lm_sex, lm_school, lm_pstatus, lm_famsize, lm_address, glm_age, glm_fedu, glm_medu)
  absences absences absences absences absences absences absences absences
Predictors Estimates CI p Estimates CI p Estimates CI p Estimates CI p Estimates CI p Incidence Rate Ratios CI p Incidence Rate Ratios CI p Incidence Rate Ratios CI p
(Intercept) 7.29 4.82 – 9.75 <0.001 8.17 5.31 – 11.03 <0.001 12.41 7.47 – 17.36 <0.001 4.90 2.51 – 7.28 <0.001 5.05 2.59 – 7.51 <0.001 1.30 1.00 – 1.68 0.049 4.30 3.70 – 4.99 <0.001 2.22 1.83 – 2.67 <0.001
sex.f -1.07 -2.66 – 0.51 0.184
school.f -2.20 -4.67 – 0.26 0.079
pstatus.f -3.54 -6.11 – -0.96 0.007
famsize.f 0.63 -1.12 – 2.38 0.478
address.f 0.54 -1.37 – 2.44 0.581
fp(age100) 35460375951276140134400.00 5224340780938489856.00 – 220584104974688272185819136.00 <0.001
fp(fedu1) 0.00 0.00 – 0.01 <0.001
fp(fedu2) 0.00 0.00 – 0.00 <0.001
fp(medu1) 0.00 0.00 – 0.00 <0.001
fp(medu2) 0.00 0.00 – 0.00 <0.001
Observations 395 395 395 395 395 395 395 395
R2 / R2 adjusted 0.004 / 0.002 0.008 / 0.005 0.018 / 0.016 0.001 / -0.001 0.001 / -0.002 0.275 0.040 0.258
absence$sex_interaction <- absence$sex.f * absence$absences
absence$school_interaction <- absence$school.f * absence$absences
absence$famsize_interaction <- absence$famsize.f * absence$absences
absence$medu_interaction <- absence$medu * absence$absences
absence$fedu_interaction <- absence$fedu * absence$absences

final_model_q1 <- glm(
  absences ~ age100 + sex.f + school.f + address.f + famsize.f + pstatus.f + sex_interaction + school_interaction + medu1 + medu2 + fedu1 + fedu2 + medu_interaction + fedu_interaction,
  data = absence,
  family = poisson
)
summary(final_model_q1)
## 
## Call:
## glm(formula = absences ~ age100 + sex.f + school.f + address.f + 
##     famsize.f + pstatus.f + sex_interaction + school_interaction + 
##     medu1 + medu2 + fedu1 + fedu2 + medu_interaction + fedu_interaction, 
##     family = poisson, data = absence)
## 
## Deviance Residuals: 
##     Min       1Q   Median       3Q      Max  
## -4.5383  -1.9369  -0.0925   0.8585   4.5441  
## 
## Coefficients:
##                      Estimate Std. Error z value Pr(>|z|)    
## (Intercept)          1.301516   0.276016   4.715 2.41e-06 ***
## age100               7.425159   5.313004   1.398 0.162250    
## sex.f               -0.415551   0.063584  -6.535 6.34e-11 ***
## school.f            -0.441161   0.095674  -4.611 4.01e-06 ***
## address.f           -0.110229   0.059130  -1.864 0.062296 .  
## famsize.f            0.131610   0.047854   2.750 0.005955 ** 
## pstatus.f            0.035413   0.072295   0.490 0.624253    
## sex_interaction      0.053145   0.004148  12.811  < 2e-16 ***
## school_interaction   0.063780   0.006553   9.734  < 2e-16 ***
## medu1               -6.469019   3.380710  -1.914 0.055683 .  
## medu2              -21.895641   5.875938  -3.726 0.000194 ***
## fedu1                5.269271   3.245725   1.623 0.104493    
## fedu2               12.508681   5.560167   2.250 0.024468 *  
## medu_interaction    -0.027103   0.002391 -11.337  < 2e-16 ***
## fedu_interaction     0.005906   0.001909   3.093 0.001979 ** 
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## (Dispersion parameter for poisson family taken to be 1)
## 
##     Null deviance: 3223.3  on 394  degrees of freedom
## Residual deviance: 1038.2  on 380  degrees of freedom
## AIC: 2077.6
## 
## Number of Fisher Scoring iterations: 5
final_model_q1_tidy <- tidy(final_model_q1)

print(final_model_q1_tidy)
## # A tibble: 15 × 5
##    term                estimate std.error statistic  p.value
##    <chr>                  <dbl>     <dbl>     <dbl>    <dbl>
##  1 (Intercept)          1.30      0.276       4.72  2.41e- 6
##  2 age100               7.43      5.31        1.40  1.62e- 1
##  3 sex.f               -0.416     0.0636     -6.54  6.34e-11
##  4 school.f            -0.441     0.0957     -4.61  4.01e- 6
##  5 address.f           -0.110     0.0591     -1.86  6.23e- 2
##  6 famsize.f            0.132     0.0479      2.75  5.95e- 3
##  7 pstatus.f            0.0354    0.0723      0.490 6.24e- 1
##  8 sex_interaction      0.0531    0.00415    12.8   1.42e-37
##  9 school_interaction   0.0638    0.00655     9.73  2.17e-22
## 10 medu1               -6.47      3.38       -1.91  5.57e- 2
## 11 medu2              -21.9       5.88       -3.73  1.94e- 4
## 12 fedu1                5.27      3.25        1.62  1.04e- 1
## 13 fedu2               12.5       5.56        2.25  2.45e- 2
## 14 medu_interaction    -0.0271    0.00239   -11.3   8.60e-30
## 15 fedu_interaction     0.00591   0.00191     3.09  1.98e- 3
mfp(absences ~ age100 + sex.f + school.f + address.f + famsize.f + pstatus.f + sex_interaction + school_interaction + medu1 + medu2 + fedu1 + fedu2 + medu_interaction + fedu_interaction,
    data = absence,
    family = poisson
)
## Call:
## mfp(formula = absences ~ age100 + sex.f + school.f + address.f + 
##     famsize.f + pstatus.f + sex_interaction + school_interaction + 
##     medu1 + medu2 + fedu1 + fedu2 + medu_interaction + fedu_interaction, 
##     data = absence, family = poisson)
## 
## 
## Deviance table:
##           Resid. Dev
## Null model    3223.267
## Linear model  1038.21
## Final model   1038.21
## 
## Fractional polynomials:
##                    df.initial select alpha df.final power1 power2
## sex_interaction             1      1  0.05        1      1      .
## medu_interaction            1      1  0.05        1      1      .
## school_interaction          1      1  0.05        1      1      .
## sex.f                       1      1  0.05        1      1      .
## school.f                    1      1  0.05        1      1      .
## medu2                       1      1  0.05        1      1      .
## fedu_interaction            1      1  0.05        1      1      .
## famsize.f                   1      1  0.05        1      1      .
## fedu2                       1      1  0.05        1      1      .
## medu1                       1      1  0.05        1      1      .
## address.f                   1      1  0.05        1      1      .
## fedu1                       1      1  0.05        1      1      .
## age100                      1      1  0.05        1      1      .
## pstatus.f                   1      1  0.05        1      1      .
## 
## 
## Transformations of covariates:
##                               formula
## age100                         age100
## sex.f                           sex.f
## school.f                     school.f
## address.f                   address.f
## famsize.f                   famsize.f
## pstatus.f                   pstatus.f
## sex_interaction       sex_interaction
## school_interaction school_interaction
## medu1                           medu1
## medu2                           medu2
## fedu1                           fedu1
## fedu2                           fedu2
## medu_interaction     medu_interaction
## fedu_interaction     fedu_interaction
## 
## Coefficients:
##            Intercept     sex_interaction.1    medu_interaction.1  
##             1.301516              0.053145             -0.027103  
## school_interaction.1               sex.f.1            school.f.1  
##             0.063780             -0.415551             -0.441161  
##              medu2.1    fedu_interaction.1           famsize.f.1  
##           -21.895641              0.005906              0.131610  
##              fedu2.1               medu1.1           address.f.1  
##            12.508681             -6.469019             -0.110229  
##              fedu1.1              age100.1           pstatus.f.1  
##             5.269271              7.425159              0.035413  
## 
## Degrees of Freedom: 394 Total (i.e. Null);  380 Residual
## Null Deviance:       3223 
## Residual Deviance: 1038  AIC: 2078
qqnorm(resid(final_model_q1))
qqline(resid(final_model_q1))

library(car)
## Loading required package: carData
## 
## Attaching package: 'car'
## 
## The following object is masked from 'package:dplyr':
## 
##     recode
## 
## The following object is masked from 'package:purrr':
## 
##     some
vif(final_model_q1)
##             age100              sex.f           school.f          address.f 
##           1.338063           2.230127           1.462080           1.434078 
##          famsize.f          pstatus.f    sex_interaction school_interaction 
##           1.107120           1.595396          20.168717          38.564748 
##              medu1              medu2              fedu1              fedu2 
##          47.083335          49.195059          43.093469          47.732918 
##   medu_interaction   fedu_interaction 
##          49.569523          23.350415
cooks_distance <- cooks.distance(final_model_q1)
plot(cooks_distance, pch = 19, main = "Cook's Distance")

#question 2
mfp(absences~fp(traveltime), data= absence, family= poisson)
## Call:
## mfp(formula = absences ~ fp(traveltime), data = absence, family = poisson)
## 
## 
## Deviance table:
##           Resid. Dev
## Null model    3223.267
## Linear model  3222.519
## Final model   3222.519
## 
## Fractional polynomials:
##            df.initial select alpha df.final power1 power2
## traveltime          4      1  0.05        1      1      .
## 
## 
## Transformations of covariates:
##                    formula
## traveltime I(traveltime^1)
## 
## Coefficients:
##    Intercept  traveltime.1  
##       1.7801       -0.0264  
## 
## Degrees of Freedom: 394 Total (i.e. Null);  393 Residual
## Null Deviance:       3223 
## Residual Deviance: 3223  AIC: 4236
lm_travel <- lm(absences~traveltime, data= absence)

mfp(absences~fp(studytime), data= absence, family= poisson)
## Call:
## mfp(formula = absences ~ fp(studytime), data = absence, family = poisson)
## 
## 
## Deviance table:
##           Resid. Dev
## Null model    3223.267
## Linear model  3205.555
## Final model   3183.331
## 
## Fractional polynomials:
##           df.initial select alpha df.final power1 power2
## studytime          4      1  0.05        4     -2     -2
## 
## 
## Transformations of covariates:
##                                                  formula
## studytime I(studytime^-2)+I(studytime^-2*log(studytime))
## 
## Coefficients:
##   Intercept  studytime.1  studytime.2  
##      1.0052       0.7464       3.7083  
## 
## Degrees of Freedom: 394 Total (i.e. Null);  392 Residual
## Null Deviance:       3223 
## Residual Deviance: 3183  AIC: 4199
absence <-
  absence %>%
  mutate(study1 = (studytime^-2))

absence <-
  absence %>%
  mutate(study2 = (studytime^-2*log(studytime)))

glm_study <- glm(absences~fp(study1) + fp(study2), data =  absence, family = poisson)

mfp(absences~fp(famrel), data= absence, family= poisson)
## Call:
## mfp(formula = absences ~ fp(famrel), data = absence, family = poisson)
## 
## 
## Deviance table:
##           Resid. Dev
## Null model    3223.267
## Linear model  3214.737
## Final model   3214.737
## 
## Fractional polynomials:
##        df.initial select alpha df.final power1 power2
## famrel          4      1  0.05        1      1      .
## 
## 
## Transformations of covariates:
##                 formula
## famrel I((famrel/10)^1)
## 
## Coefficients:
## Intercept   famrel.1  
##    2.0059    -0.6737  
## 
## Degrees of Freedom: 394 Total (i.e. Null);  393 Residual
## Null Deviance:       3223 
## Residual Deviance: 3215  AIC: 4228
lm_famrel <- lm(absences~famrel, data= absence)

mfp(absences~fp(freetime), data= absence, family= poisson)
## Call:
## mfp(formula = absences ~ fp(freetime), data = absence, family = poisson)
## 
## 
## Deviance table:
##           Resid. Dev
## Null model    3223.267
## Linear model  3208.42
## Final model   3127.012
## 
## Fractional polynomials:
##          df.initial select alpha df.final power1 power2
## freetime          4      1  0.05        4    0.5      1
## 
## 
## Transformations of covariates:
##                                          formula
## freetime I((freetime/10)^0.5)+I((freetime/10)^1)
## 
## Coefficients:
##  Intercept  freetime.1  freetime.2  
##      5.762     -14.814      13.209  
## 
## Degrees of Freedom: 394 Total (i.e. Null);  392 Residual
## Null Deviance:       3223 
## Residual Deviance: 3127  AIC: 4142
absence <-
  absence %>%
  mutate(free1 = ((freetime/10)^0.5))

absence <-
  absence %>%
  mutate(free2 = ((freetime/10)^1))

glm_free <- glm(absences~fp(free1) + fp(free2), data =  absence, family = poisson)


mfp(absences~fp(goout), data= absence, family= poisson)
## Call:
## mfp(formula = absences ~ fp(goout), data = absence, family = poisson)
## 
## 
## Deviance table:
##           Resid. Dev
## Null model    3223.267
## Linear model  3214.61
## Final model   3195.216
## 
## Fractional polynomials:
##       df.initial select alpha df.final power1 power2
## goout          4      1  0.05        4     -2     -1
## 
## 
## Transformations of covariates:
##                       formula
## goout I(goout^-2)+I(goout^-1)
## 
## Coefficients:
## Intercept    goout.1    goout.2  
##     2.293      2.098     -2.457  
## 
## Degrees of Freedom: 394 Total (i.e. Null);  392 Residual
## Null Deviance:       3223 
## Residual Deviance: 3195  AIC: 4211
absence <-
  absence %>%
  mutate(go1 = (goout^-2))

absence <-
  absence %>%
  mutate(go2 = (goout^-1))

glm_go <- glm(absences~fp(go1) + fp(go2), data =  absence, family = poisson)

mfp(absences~fp(walc), data= absence, family= poisson)
## Call:
## mfp(formula = absences ~ fp(walc), data = absence, family = poisson)
## 
## 
## Deviance table:
##           Resid. Dev
## Null model    3223.267
## Linear model  3143.908
## Final model   3127.509
## 
## Fractional polynomials:
##      df.initial select alpha df.final power1 power2
## walc          4      1  0.05        4     -2     -2
## 
## 
## Transformations of covariates:
##                              formula
## walc I(walc^-2)+I(walc^-2*log(walc))
## 
## Coefficients:
## Intercept     walc.1     walc.2  
##    2.3766    -0.7982    -3.6050  
## 
## Degrees of Freedom: 394 Total (i.e. Null);  392 Residual
## Null Deviance:       3223 
## Residual Deviance: 3128  AIC: 4143
absence <-
  absence %>%
  mutate(walc1 = walc^-2)

absence <-
  absence %>%
  mutate(walc2 = (walc^-2*log(walc)))

glm_walc <- glm(absences~fp(walc1) + fp(walc2), data =  absence, family = poisson)

mfp(absences~fp(dalc), data= absence, family= poisson)
## Call:
## mfp(formula = absences ~ fp(dalc), data = absence, family = poisson)
## 
## 
## Deviance table:
##           Resid. Dev
## Null model    3223.267
## Linear model  3173.305
## Final model   3173.305
## 
## Fractional polynomials:
##      df.initial select alpha df.final power1 power2
## dalc          4      1  0.05        1      1      .
## 
## 
## Transformations of covariates:
##        formula
## dalc I(dalc^1)
## 
## Coefficients:
## Intercept     dalc.1  
##    1.5075     0.1516  
## 
## Degrees of Freedom: 394 Total (i.e. Null);  393 Residual
## Null Deviance:       3223 
## Residual Deviance: 3173  AIC: 4187
lm_dalc <- lm(absences~dalc, data= absence)

lm_paid <- lm(absences~paid.f, data= absence)
lm_activites <- lm(absences~activities.f, data= absence)
lm_higher <- lm(absences~higher.f, data= absence)
lm_internet <- lm(absences~internet.f, data= absence)
lm_romantic <- lm(absences~romantic.f, data= absence)

tab_model(lm_travel, glm_study, lm_famrel, glm_free, glm_go, glm_walc, lm_dalc, lm_paid, lm_activites, lm_higher, lm_internet, lm_romantic
          )
  absences absences absences absences absences absences absences absences absences absences absences absences
Predictors Estimates CI p Incidence Rate Ratios CI p Estimates CI p Incidence Rate Ratios CI p Incidence Rate Ratios CI p Incidence Rate Ratios CI p Estimates CI p Estimates CI p Estimates CI p Estimates CI p Estimates CI p Estimates CI p
(Intercept) 5.92 4.10 – 7.75 <0.001 2.73 2.13 – 3.47 <0.001 7.27 3.69 – 10.85 <0.001 318.13 144.04 – 690.56 <0.001 9.91 7.98 – 12.29 <0.001 10.77 9.31 – 12.44 <0.001 4.22 2.69 – 5.75 <0.001 5.53 3.08 – 7.99 <0.001 6.04 3.52 – 8.56 <0.001 9.69 2.61 – 16.78 0.007 1.71 -2.24 – 5.67 0.394 2.24 -0.11 – 4.59 0.062
traveltime -0.15 -1.29 – 0.99 0.798
fp(study1) 2.11 1.64 – 2.72 <0.001
fp(study2) 40.79 12.33 – 137.90 <0.001
famrel -0.40 -1.28 – 0.49 0.379
fp(free1) 0.00 0.00 – 0.00 <0.001
fp(free2) 545202.62 27549.19 – 10347954.03 <0.001
fp(go1) 8.15 3.74 – 17.72 <0.001
fp(go2) 0.09 0.03 – 0.22 <0.001
fp(walc1) 0.45 0.38 – 0.53 <0.001
fp(walc2) 0.03 0.01 – 0.08 <0.001
Dalc 1.01 0.12 – 1.89 0.026
paid.f 0.12 -1.47 – 1.71 0.883
activities.f -0.22 -1.80 – 1.37 0.787
higher.f -2.04 -5.65 – 1.57 0.266
internet.f 2.18 0.07 – 4.29 0.043
romantic.f 2.60 0.94 – 4.26 0.002
Observations 395 395 395 395 395 395 395 395 395 395 395 395
R2 / R2 adjusted 0.000 / -0.002 0.096 0.002 / -0.001 0.216 0.069 0.215 0.013 / 0.010 0.000 / -0.002 0.000 / -0.002 0.003 / 0.001 0.010 / 0.008 0.024 / 0.021
final_model_q2 <- glm(
  absences ~ traveltime + famrel + dalc + paid + activities + higher + internet + romantic + study1 + study2+ free1+ free2 + go1 + go2 + walc1 + walc2,
  data = absence,
  family = poisson
)
summary(final_model_q2)
## 
## Call:
## glm(formula = absences ~ traveltime + famrel + dalc + paid + 
##     activities + higher + internet + romantic + study1 + study2 + 
##     free1 + free2 + go1 + go2 + walc1 + walc2, family = poisson, 
##     data = absence)
## 
## Deviance Residuals: 
##     Min       1Q   Median       3Q      Max  
## -5.5928  -2.6953  -0.7913   0.9670  11.5749  
## 
## Coefficients:
##                 Estimate Std. Error z value Pr(>|z|)    
## (Intercept)     5.714256   0.462417  12.357  < 2e-16 ***
## traveltime     -0.054570   0.030891  -1.767   0.0773 .  
## famrel         -0.008359   0.024007  -0.348   0.7277    
## dalc            0.041736   0.027147   1.537   0.1242    
## paidyes        -0.053291   0.045499  -1.171   0.2415    
## activitiesyes   0.009085   0.043823   0.207   0.8358    
## higheryes      -0.047087   0.091384  -0.515   0.6064    
## internetyes     0.501264   0.069171   7.247 4.27e-13 ***
## romanticyes     0.412945   0.043889   9.409  < 2e-16 ***
## study1          0.658332   0.135084   4.874 1.10e-06 ***
## study2          3.441989   0.626355   5.495 3.90e-08 ***
## free1         -15.236687   1.625175  -9.375  < 2e-16 ***
## free2          12.913085   1.560605   8.274  < 2e-16 ***
## go1             0.865444   0.432581   2.001   0.0454 *  
## go2            -0.909351   0.522616  -1.740   0.0819 .  
## walc1          -0.731962   0.112952  -6.480 9.16e-11 ***
## walc2          -3.324800   0.632377  -5.258 1.46e-07 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## (Dispersion parameter for poisson family taken to be 1)
## 
##     Null deviance: 3223.3  on 394  degrees of freedom
## Residual deviance: 2809.6  on 378  degrees of freedom
## AIC: 3853
## 
## Number of Fisher Scoring iterations: 6
qqnorm(resid(final_model_q2))
qqline(resid(final_model_q2))

vif(final_model_q2)
## traveltime     famrel       dalc       paid activities     higher   internet 
##   1.078022   1.060699   1.771377   1.160828   1.082632   1.191025   1.089506 
##   romantic     study1     study2      free1      free2        go1        go2 
##   1.067822   5.145188   4.831498  69.111874  69.078138  23.432098  24.617806 
##      walc1      walc2 
##   4.951055   3.674186
cooks_distance <- cooks.distance(final_model_q2)
plot(cooks_distance, pch = 19, main = "Cook's Distance")

#question 3
final_model_q3 <- glm(
  absences ~ traveltime + famrel + dalc + paid + activities + higher + internet + romantic + study1 + study2+ free1+ free2 + go1 + go2 + walc1 + walc2 + age100 + sex.f + school.f + address.f + famsize.f + pstatus.f + sex_interaction + school_interaction + medu1 + medu2 + fedu1 + fedu2 + medu_interaction + fedu_interaction,
  data = absence,
  family = poisson
)
summary(final_model_q3)
## 
## Call:
## glm(formula = absences ~ traveltime + famrel + dalc + paid + 
##     activities + higher + internet + romantic + study1 + study2 + 
##     free1 + free2 + go1 + go2 + walc1 + walc2 + age100 + sex.f + 
##     school.f + address.f + famsize.f + pstatus.f + sex_interaction + 
##     school_interaction + medu1 + medu2 + fedu1 + fedu2 + medu_interaction + 
##     fedu_interaction, family = poisson, data = absence)
## 
## Deviance Residuals: 
##     Min       1Q   Median       3Q      Max  
## -3.5332  -1.7889  -0.2119   0.8048   3.9995  
## 
## Coefficients:
##                      Estimate Std. Error z value Pr(>|z|)    
## (Intercept)          1.023068   0.627332   1.631 0.102928    
## traveltime          -0.031680   0.037993  -0.834 0.404366    
## famrel              -0.111650   0.025225  -4.426 9.59e-06 ***
## dalc                 0.025601   0.030742   0.833 0.404968    
## paidyes             -0.055444   0.049487  -1.120 0.262555    
## activitiesyes       -0.055748   0.048683  -1.145 0.252160    
## higheryes            0.525584   0.141820   3.706 0.000211 ***
## internetyes         -0.039333   0.073217  -0.537 0.591121    
## romanticyes          0.008646   0.051424   0.168 0.866476    
## study1               0.113170   0.151298   0.748 0.454463    
## study2               0.458886   0.693294   0.662 0.508041    
## free1                1.025759   2.049467   0.501 0.616723    
## free2               -0.664259   1.941660  -0.342 0.732269    
## go1                 -0.562414   0.523680  -1.074 0.282839    
## go2                  0.375072   0.612415   0.612 0.540241    
## walc1               -0.242141   0.125535  -1.929 0.053747 .  
## walc2                0.268095   0.715676   0.375 0.707955    
## age100              19.580531   6.121745   3.199 0.001381 ** 
## sex.f               -0.412144   0.073072  -5.640 1.70e-08 ***
## school.f            -0.719403   0.111798  -6.435 1.24e-10 ***
## address.f           -0.012479   0.064937  -0.192 0.847604    
## famsize.f            0.022008   0.051448   0.428 0.668822    
## pstatus.f           -0.021927   0.076916  -0.285 0.775585    
## sex_interaction      0.048394   0.004847   9.984  < 2e-16 ***
## school_interaction   0.071813   0.007989   8.989  < 2e-16 ***
## medu1               -4.174815   3.454829  -1.208 0.226893    
## medu2              -15.959878   6.014007  -2.654 0.007959 ** 
## fedu1                3.988930   3.341781   1.194 0.232613    
## fedu2                8.849493   5.765588   1.535 0.124813    
## medu_interaction    -0.021656   0.002782  -7.784 7.02e-15 ***
## fedu_interaction     0.000163   0.002329   0.070 0.944184    
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## (Dispersion parameter for poisson family taken to be 1)
## 
##     Null deviance: 3223.27  on 394  degrees of freedom
## Residual deviance:  935.17  on 364  degrees of freedom
## AIC: 2006.6
## 
## Number of Fisher Scoring iterations: 5