rm(list = ls())

# install.packages("haven")

library(haven) 

setwd("D:/FTU/SGK/Kinh tế lượng/Econ335")


# the following brings a STATA data set into R, create a dataset called
mt2dat <- read_dta("econ 335 2024 mt 2.dta")

# this command helps identify the variables in the dataset "mt2dat"

head(mt2dat)
## # A tibble: 6 × 3
##   termGPA priGPA atndrte
##     <dbl>  <dbl>   <dbl>
## 1    3.19   2.64    84.4
## 2    2.73   3.52    68.8
## 3    3      2.46    93.8
## 4    2.04   2.61    96.9
## 5    3.68   3.32   100  
## 6    3.23   2.93    90.6
# this command will reduce the data set to the variable we're interested in, lm is linear regression, phai dung dau <-

model_1 <- lm(atndrte~ termGPA + priGPA, data = mt2dat)

# this command prints out the results of the regression, phai dung ten model 
summary(model_1)
## 
## Call:
## lm(formula = atndrte ~ termGPA + priGPA, data = mt2dat)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -63.955  -6.844   2.362   9.422  30.521 
## 
## Coefficients:
##             Estimate Std. Error t value Pr(>|t|)    
## (Intercept)  43.5432     2.6398  16.495   <2e-16 ***
## termGPA      11.3320     0.9687  11.698   <2e-16 ***
## priGPA        3.3601     1.3099   2.565   0.0105 *  
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 14.08 on 677 degrees of freedom
## Multiple R-squared:   0.32,  Adjusted R-squared:  0.3179 
## F-statistic: 159.3 on 2 and 677 DF,  p-value: < 2.2e-16
model_2 <- lm(atndrte~ termGPA, data = mt2dat)

# this command prints out the results of the regression, phai dung ten model 
summary(model_2)
## 
## Call:
## lm(formula = atndrte ~ termGPA, data = mt2dat)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -65.585  -6.649   2.184   9.357  30.611 
## 
## Coefficients:
##             Estimate Std. Error t value Pr(>|t|)    
## (Intercept)  48.0136     1.9909   24.12   <2e-16 ***
## termGPA      12.9550     0.7365   17.59   <2e-16 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 14.14 on 678 degrees of freedom
## Multiple R-squared:  0.3133, Adjusted R-squared:  0.3123 
## F-statistic: 309.4 on 1 and 678 DF,  p-value: < 2.2e-16
# tim mean cua termGPA 
mean_value_termGPA <- mean(mt2dat$termGPA)

print(mean_value_termGPA)
## [1] 2.601
# tim mean cua priGPA
mean_value_priGPA <- mean(mt2dat$priGPA)

print(mean_value_priGPA)
## [1] 2.586775