Regression PASCHPOL MATHS

# Linear regression analysis of Exam result compared with parental involvement
# Before any linear regression analysis, it is important to make a visualization with plot()
setwd("C:/Users/antho/Downloads/r final project")
library(flexdashboard)
## Warning: package 'flexdashboard' was built under R version 4.3.1
library(shiny)
pisair <- readRDS("PISA_2018_IRL.rds")

# Regression Analysis for PV1MATH
plot(pisair$PASCHPOL, pisair$PV1MATH, 
     main = "Regression Analysis of Exam Result vs Parental Involvement (PV1MATH)",
     xlab = "Parental Involvement",
     ylab = "PV1MATH")

lm_math <- lm(pisair$PV1MATH ~ pisair$PASCHPOL)
summary(lm_math)
## 
## Call:
## lm(formula = pisair$PV1MATH ~ pisair$PASCHPOL)
## 
## Residuals:
##      Min       1Q   Median       3Q      Max 
## -307.460  -51.715    2.008   54.663  237.424 
## 
## Coefficients:
##                 Estimate Std. Error t value Pr(>|t|)    
## (Intercept)      503.516      1.114 451.850   <2e-16 ***
## pisair$PASCHPOL   -2.818      1.153  -2.445   0.0145 *  
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 77.14 on 4926 degrees of freedom
##   (649 observations deleted due to missingness)
## Multiple R-squared:  0.001212,   Adjusted R-squared:  0.001009 
## F-statistic: 5.977 on 1 and 4926 DF,  p-value: 0.01453
boxplot(pisair$PV1MATH, main = "PV1MATH Results")

# Regression Analysis for PV1SCIE
plot(pisair$PASCHPOL, pisair$PV1SCIE, 
     main = "Regression Analysis of Exam Result vs Parental Involvement (PV1SCIE)",
     xlab = "Parental Involvement",
     ylab = "PV1SCIE")

lm_scie <- lm(pisair$PV1SCIE ~ pisair$PASCHPOL)
summary(lm_scie)
## 
## Call:
## lm(formula = pisair$PV1SCIE ~ pisair$PASCHPOL)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -324.28  -60.07    1.16   61.28  280.21 
## 
## Coefficients:
##                 Estimate Std. Error t value Pr(>|t|)    
## (Intercept)      499.779      1.271 393.119  < 2e-16 ***
## pisair$PASCHPOL   -3.548      1.315  -2.697  0.00701 ** 
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 88.01 on 4926 degrees of freedom
##   (649 observations deleted due to missingness)
## Multiple R-squared:  0.001475,   Adjusted R-squared:  0.001272 
## F-statistic: 7.276 on 1 and 4926 DF,  p-value: 0.007013
boxplot(pisair$PV1SCIE, main = "PV1SCIE Results")

# Regression Analysis for PV1READ
plot(pisair$PASCHPOL, pisair$PV1READ, 
     main = "Regression Analysis of Exam Result vs Parental Involvement (PV1READ)",
     xlab = "Parental Involvement",
     ylab = "PV1READ")

lm_read <- lm(pisair$PV1READ ~ pisair$PASCHPOL)
summary(lm_read)
## 
## Call:
## lm(formula = pisair$PV1READ ~ pisair$PASCHPOL)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -317.18  -59.33    2.46   63.96  279.67 
## 
## Coefficients:
##                 Estimate Std. Error t value Pr(>|t|)    
## (Intercept)      523.193      1.290 405.654  < 2e-16 ***
## pisair$PASCHPOL   -5.334      1.334  -3.998 6.49e-05 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 89.28 on 4926 degrees of freedom
##   (649 observations deleted due to missingness)
## Multiple R-squared:  0.003234,   Adjusted R-squared:  0.003032 
## F-statistic: 15.98 on 1 and 4926 DF,  p-value: 6.489e-05
boxplot(pisair$PV1READ, main = "PV1READ Results")

# Gender Subset Analysis for PV1READ
subset_male <- subset(pisair, ST004D01T == '2')
subset_female <- subset(pisair, ST004D01T == '1')

lm(subset_male$PV1READ ~ subset_male$PASCHPOL)
## 
## Call:
## lm(formula = subset_male$PV1READ ~ subset_male$PASCHPOL)
## 
## Coefficients:
##          (Intercept)  subset_male$PASCHPOL  
##               512.13                 -3.47
lm(subset_female$PV1READ ~ subset_female$PASCHPOL)
## 
## Call:
## lm(formula = subset_female$PV1READ ~ subset_female$PASCHPOL)
## 
## Coefficients:
##            (Intercept)  subset_female$PASCHPOL  
##                 533.57                   -6.15
# ... (continue with other analyses)

# Fit linear regression model with PASCHPOL, PV1READ, PV1SCIE, PV1MATH, and ESCS
model_ESCS <- lm(pisair$PASCHPOL ~ pisair$PV1MATH + pisair$ESCS)

# Summary of the model
summary(model_ESCS)
## 
## Call:
## lm(formula = pisair$PASCHPOL ~ pisair$PV1MATH + pisair$ESCS)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -3.3398 -0.6515 -0.0280  0.5693  2.4169 
## 
## Coefficients:
##                  Estimate Std. Error t value Pr(>|t|)   
## (Intercept)     0.2830126  0.0938771   3.015  0.00259 **
## pisair$PV1MATH -0.0002282  0.0001861  -1.226  0.22020   
## pisair$ESCS    -0.0543856  0.0169881  -3.201  0.00138 **
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 0.9521 on 4883 degrees of freedom
##   (691 observations deleted due to missingness)
## Multiple R-squared:  0.003238,   Adjusted R-squared:  0.002829 
## F-statistic:  7.93 on 2 and 4883 DF,  p-value: 0.0003643
# ... (continue with other analyses)

# ... (previous code)

# Filter PASCHPOL by socio-economic status (e.g., high and low ESCS)
high_ESCS_data <- subset(pisair, ESCS >= 0.5693)
low_ESCS_data <- subset(pisair, ESCS <= -0.6515)

# Fit separate models for high and low socio-economic status
high_ESCS_model <- lm(high_ESCS_data$PV1MATH ~ high_ESCS_data$PASCHPOL)
low_ESCS_model <- lm(low_ESCS_data$PV1MATH ~ low_ESCS_data$PASCHPOL)

# Summary of the models for high and low socio-economic status
summary(high_ESCS_model)
## 
## Call:
## lm(formula = high_ESCS_data$PV1MATH ~ high_ESCS_data$PASCHPOL)
## 
## Residuals:
##      Min       1Q   Median       3Q      Max 
## -269.217  -47.782    2.473   50.649  209.354 
## 
## Coefficients:
##                         Estimate Std. Error t value Pr(>|t|)    
## (Intercept)              530.945      1.760  301.63   <2e-16 ***
## high_ESCS_data$PASCHPOL   -1.365      1.869   -0.73    0.465    
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 72.72 on 1735 degrees of freedom
##   (189 observations deleted due to missingness)
## Multiple R-squared:  0.0003074,  Adjusted R-squared:  -0.0002688 
## F-statistic: 0.5334 on 1 and 1735 DF,  p-value: 0.4653
summary(low_ESCS_model)
## 
## Call:
## lm(formula = low_ESCS_data$PV1MATH ~ low_ESCS_data$PASCHPOL)
## 
## Residuals:
##      Min       1Q   Median       3Q      Max 
## -239.021  -53.950   -0.363   51.825  224.521 
## 
## Coefficients:
##                        Estimate Std. Error t value Pr(>|t|)    
## (Intercept)             468.757      2.624 178.628   <2e-16 ***
## low_ESCS_data$PASCHPOL   -3.181      2.561  -1.242    0.214    
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 76.89 on 917 degrees of freedom
##   (179 observations deleted due to missingness)
## Multiple R-squared:  0.00168,    Adjusted R-squared:  0.0005917 
## F-statistic: 1.543 on 1 and 917 DF,  p-value: 0.2144
# Fit linear regression model with PASCHPOL, PV1SCIE, and ESCS
modelScience_ESCS <- lm(pisair$PASCHPOL ~ pisair$PV1SCIE + pisair$ESCS)

# Summary of the model
summary(modelScience_ESCS)
## 
## Call:
## lm(formula = pisair$PASCHPOL ~ pisair$PV1SCIE + pisair$ESCS)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -3.3569 -0.6503 -0.0313  0.5765  2.4152 
## 
## Coefficients:
##                  Estimate Std. Error t value Pr(>|t|)    
## (Intercept)     0.2866259  0.0817200   3.507 0.000457 ***
## pisair$PV1SCIE -0.0002374  0.0001628  -1.458 0.144780    
## pisair$ESCS    -0.0534523  0.0169178  -3.160 0.001590 ** 
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 0.9521 on 4883 degrees of freedom
##   (691 observations deleted due to missingness)
## Multiple R-squared:  0.003365,   Adjusted R-squared:  0.002957 
## F-statistic: 8.243 on 2 and 4883 DF,  p-value: 0.0002667
# Filter PASCHPOL by socio-economic status for science
high_ESCS_dataScience <- subset(pisair, ESCS >= 0.5765)
low_ESCS_dataScience <- subset(pisair, ESCS <= -0.6503)

# Fit separate models for high and low socio-economic status for science
high_ESCS_modelScience <- lm(high_ESCS_dataScience$PV1SCIE ~ high_ESCS_dataScience$PASCHPOL)
low_ESCS_modelScience <- lm(low_ESCS_dataScience$PV1SCIE ~ low_ESCS_dataScience$PASCHPOL)

# Summary of the models for science
summary(high_ESCS_modelScience)
## 
## Call:
## lm(formula = high_ESCS_dataScience$PV1SCIE ~ high_ESCS_dataScience$PASCHPOL)
## 
## Residuals:
##      Min       1Q   Median       3Q      Max 
## -270.923  -54.507    5.691   60.044  250.174 
## 
## Coefficients:
##                                Estimate Std. Error t value Pr(>|t|)    
## (Intercept)                     530.037      2.067 256.381   <2e-16 ***
## high_ESCS_dataScience$PASCHPOL   -3.006      2.199  -1.367    0.172    
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 85.08 on 1720 degrees of freedom
##   (186 observations deleted due to missingness)
## Multiple R-squared:  0.001086,   Adjusted R-squared:  0.0005049 
## F-statistic: 1.869 on 1 and 1720 DF,  p-value: 0.1717
summary(low_ESCS_modelScience)
## 
## Call:
## lm(formula = low_ESCS_dataScience$PV1SCIE ~ low_ESCS_dataScience$PASCHPOL)
## 
## Residuals:
##      Min       1Q   Median       3Q      Max 
## -286.070  -62.200   -0.061   57.947  225.951 
## 
## Coefficients:
##                               Estimate Std. Error t value Pr(>|t|)    
## (Intercept)                    461.623      2.962 155.862   <2e-16 ***
## low_ESCS_dataScience$PASCHPOL   -3.803      2.883  -1.319    0.187    
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 86.99 on 919 degrees of freedom
##   (179 observations deleted due to missingness)
## Multiple R-squared:  0.00189,    Adjusted R-squared:  0.000804 
## F-statistic:  1.74 on 1 and 919 DF,  p-value: 0.1874
# Fit linear regression model with PASCHPOL, PV1READ, and ESCS
model_ESCSReading <- lm(pisair$PASCHPOL ~ pisair$PV1READ + pisair$ESCS)

# Summary of the model
summary(model_ESCSReading)
## 
## Call:
## lm(formula = pisair$PASCHPOL ~ pisair$PV1READ + pisair$ESCS)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -3.4044 -0.6559 -0.0373  0.5679  2.4279 
## 
## Coefficients:
##                  Estimate Std. Error t value Pr(>|t|)    
## (Intercept)     0.4053733  0.0840988   4.820 1.48e-06 ***
## pisair$PV1READ -0.0004559  0.0001601  -2.847  0.00442 ** 
## pisair$ESCS    -0.0465304  0.0168781  -2.757  0.00586 ** 
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 0.9515 on 4883 degrees of freedom
##   (691 observations deleted due to missingness)
## Multiple R-squared:  0.004584,   Adjusted R-squared:  0.004176 
## F-statistic: 11.24 on 2 and 4883 DF,  p-value: 1.345e-05
# Filter PASCHPOL by socio-economic status for reading
high_ESCS_dataReading <- subset(pisair, ESCS >= 0.5679)
low_ESCS_dataReading <- subset(pisair, ESCS <= -0.6559)

# Fit separate models for high and low socio-economic status for reading
high_ESCS_modelReading <- lm(high_ESCS_dataReading$PV1READ ~ high_ESCS_dataReading$PASCHPOL)
low_ESCS_modelReading <- lm(low_ESCS_dataReading$PV1READ ~ low_ESCS_dataReading$PASCHPOL)

# Summary of the models for reading
summary(high_ESCS_modelReading)
## 
## Call:
## lm(formula = high_ESCS_dataReading$PV1READ ~ high_ESCS_dataReading$PASCHPOL)
## 
## Residuals:
##      Min       1Q   Median       3Q      Max 
## -310.647  -55.068    3.861   61.095  224.312 
## 
## Coefficients:
##                                Estimate Std. Error t value Pr(>|t|)    
## (Intercept)                     552.924      2.073 266.704   <2e-16 ***
## high_ESCS_dataReading$PASCHPOL   -4.246      2.202  -1.928    0.054 .  
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 85.7 on 1737 degrees of freedom
##   (189 observations deleted due to missingness)
## Multiple R-squared:  0.002137,   Adjusted R-squared:  0.001562 
## F-statistic: 3.719 on 1 and 1737 DF,  p-value: 0.05396
summary(low_ESCS_modelReading)
## 
## Call:
## lm(formula = low_ESCS_dataReading$PV1READ ~ low_ESCS_dataReading$PASCHPOL)
## 
## Residuals:
##      Min       1Q   Median       3Q      Max 
## -262.114  -63.109    1.368   61.386  253.648 
## 
## Coefficients:
##                               Estimate Std. Error t value Pr(>|t|)    
## (Intercept)                    484.974      3.009 161.186   <2e-16 ***
## low_ESCS_dataReading$PASCHPOL   -6.335      2.933  -2.159   0.0311 *  
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 87.97 on 912 degrees of freedom
##   (179 observations deleted due to missingness)
## Multiple R-squared:  0.005087,   Adjusted R-squared:  0.003996 
## F-statistic: 4.663 on 1 and 912 DF,  p-value: 0.03108
# ... (continue with other analyses)

# Construction of new index 1 (ENGAGE), measuring parents' initiative to engage in schools
# $PA008Q01TA: Discussed my child’s behavior with a teacher on my initiative.
# $PA008Q03TA: Discussed my child’s progress with a teacher on my initiative. 
# $PA008Q09NA: Talked about how to support learning at home and homework with my child’s teachers.
# $PA008Q10NA: Exchanged ideas on parenting, family support, or the child’s development with my child’s teachers.

# Performance of linear regressions

pisair$ENGAGE <- pisair$PA008Q01TA + pisair$PA008Q03TA + pisair$PA008Q09NA + pisair$PA008Q10NA

lm(pisair$PV1MATH ~ pisair$ENGAGE)
## 
## Call:
## lm(formula = pisair$PV1MATH ~ pisair$ENGAGE)
## 
## Coefficients:
##   (Intercept)  pisair$ENGAGE  
##       452.700          7.848
summary(lm(pisair$PV1MATH ~ pisair$ENGAGE))
## 
## Call:
## lm(formula = pisair$PV1MATH ~ pisair$ENGAGE)
## 
## Residuals:
##      Min       1Q   Median       3Q      Max 
## -307.912  -51.444    1.891   53.784  224.216 
## 
## Coefficients:
##               Estimate Std. Error t value Pr(>|t|)    
## (Intercept)    452.700      4.936   91.71   <2e-16 ***
## pisair$ENGAGE    7.848      0.739   10.62   <2e-16 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 76.26 on 4815 degrees of freedom
##   (760 observations deleted due to missingness)
## Multiple R-squared:  0.02288,    Adjusted R-squared:  0.02268 
## F-statistic: 112.8 on 1 and 4815 DF,  p-value: < 2.2e-16
plot(pisair$ENGAGE, pisair$PV1MATH,
     main = "Linear Regression of PV1MATH vs Parental Engagement",
     xlab = "Parental Engagement",
     ylab = "PV1MATH")
abline(lm(pisair$PV1MATH ~ pisair$ENGAGE), col = "red")

lm(pisair$PV1READ ~ pisair$ENGAGE)
## 
## Call:
## lm(formula = pisair$PV1READ ~ pisair$ENGAGE)
## 
## Coefficients:
##   (Intercept)  pisair$ENGAGE  
##       465.610          8.848
lm(pisair$PV1SCIE ~ pisair$ENGAGE)
## 
## Call:
## lm(formula = pisair$PV1SCIE ~ pisair$ENGAGE)
## 
## Coefficients:
##   (Intercept)  pisair$ENGAGE  
##       449.449          7.765
# ... (continue with other analyses)

# $PA009Q06NA: I felt unwelcome at my child’s school.
# $PA009Q07NA: I feel generally awkward in a school.
# $PA009Q09NA: I think participation is not relevant for my child’s development.
# $PA009Q10NA: I do not know how I could participate in school activities. 
# $PA009Q11NA: My child does not want me to participate. 

# Performance of linear regressions

pisair$HINDER <- pisair$PA009Q06NA + pisair$PA009Q07NA + pisair$PA009Q09NA + pisair$PA009Q10NA + pisair$PA009Q11NA
lm(pisair$PV1MATH ~ pisair$HINDER)
## 
## Call:
## lm(formula = pisair$PV1MATH ~ pisair$HINDER)
## 
## Coefficients:
##   (Intercept)  pisair$HINDER  
##       428.013          7.873
summary(lm(pisair$PV1MATH ~ pisair$HINDER))
## 
## Call:
## lm(formula = pisair$PV1MATH ~ pisair$HINDER)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -314.87  -52.62    2.40   53.65  232.95 
## 
## Coefficients:
##               Estimate Std. Error t value Pr(>|t|)    
## (Intercept)    428.013     14.171  30.204  < 2e-16 ***
## pisair$HINDER    7.873      1.469   5.359 8.78e-08 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 77 on 4807 degrees of freedom
##   (768 observations deleted due to missingness)
## Multiple R-squared:  0.005938,   Adjusted R-squared:  0.005731 
## F-statistic: 28.71 on 1 and 4807 DF,  p-value: 8.779e-08
plot(pisair$HINDER, pisair$PV1MATH,
     main = "Linear Regression of PV1MATH vs Parental Hindrance",
     xlab = "Parental Hindrance",
     ylab = "PV1MATH")
abline(lm(pisair$PV1MATH ~ pisair$HINDER), col = "red")

lm(pisair$PV1READ ~ pisair$HINDER)
## 
## Call:
## lm(formula = pisair$PV1READ ~ pisair$HINDER)
## 
## Coefficients:
##   (Intercept)  pisair$HINDER  
##       441.812          8.488
lm(pisair$PV1SCIE ~ pisair$HINDER)
## 
## Call:
## lm(formula = pisair$PV1SCIE ~ pisair$HINDER)
## 
## Coefficients:
##   (Intercept)  pisair$HINDER  
##       424.590          7.862
# ... (continue with other analyses)