Day 3 Linear Regression

Việc 1: Đọc dữ liệu Professorial Salaries

library(readxl)
salary = read_excel("C:\\Thach\\VN trips\\VN trip 5 (Apr 2023)\\Datasets\\Professorial Salaries.xlsx")

Việc 2: Đánh giá sơ bộ mối liên quan giữa các biến

library(GGally)
## Loading required package: ggplot2
## Registered S3 method overwritten by 'GGally':
##   method from   
##   +.gg   ggplot2
vars = salary[, c("Rank", "Discipline", "Yrs.since.phd", "Yrs.service", "Sex", "Salary")]
ggpairs(data = vars, mapping = aes(color = Sex))
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.

Việc 3: Mô tả phân bố của lương GS

ggplot(data = salary, aes(x = Salary)) + geom_histogram(fill = "blue", col = "white") + labs(title = "Distribution of Professiorial salary", x = "Salary (dollars)", y = "Number of participants")
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.

Việc 4: Đánh giá mối liên quan giữa giới tính và tiền lương

4.1 Vẽ biểu đồ hộp

ggplot(salary, aes(group = Sex, x = Sex, y = Salary, fill = Sex, color = Sex)) + geom_boxplot(colour = "black") + geom_jitter(aes(color = Sex))+theme(legend.position = "none")+ labs(x = "Sex", y = "Salary (dollars)")

4.2 Đánh giá mối liên quan giữa giới tính và tiền lương

m.43 = lm(Salary ~ Sex, data = salary)
summary(m.43)
## 
## Call:
## lm(formula = Salary ~ Sex, data = salary)
## 
## Residuals:
##    Min     1Q Median     3Q    Max 
## -57290 -23502  -6828  19710 116455 
## 
## Coefficients:
##             Estimate Std. Error t value Pr(>|t|)    
## (Intercept)   101002       4809  21.001  < 2e-16 ***
## SexMale        14088       5065   2.782  0.00567 ** 
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 30030 on 395 degrees of freedom
## Multiple R-squared:  0.01921,    Adjusted R-squared:  0.01673 
## F-statistic: 7.738 on 1 and 395 DF,  p-value: 0.005667

4.3 Kiểm tra giả định của mô hình

par(mfrow = c(2, 2))
plot(m.43)

4.4 Nhận xét kết quả

Việc 5: Đánh giá mối liên quan giữa thời gian sau tiến sĩ và tiền lương

5.1 Vẽ biểu đồ tán xạ

ggplot(data = salary, aes(x = Yrs.since.phd, y = Salary)) + geom_point() + geom_smooth(method="loess") + 
  labs(x = "Time since PhD (years)", y = "Salary (dollars)")
## `geom_smooth()` using formula = 'y ~ x'

5.2 Thực hiện mô hình tuyến tính

m.52 = lm(Salary ~ Yrs.since.phd, data = salary)
summary(m.52)
## 
## Call:
## lm(formula = Salary ~ Yrs.since.phd, data = salary)
## 
## Residuals:
##    Min     1Q Median     3Q    Max 
## -84171 -19432  -2858  16086 102383 
## 
## Coefficients:
##               Estimate Std. Error t value Pr(>|t|)    
## (Intercept)    91718.7     2765.8  33.162   <2e-16 ***
## Yrs.since.phd    985.3      107.4   9.177   <2e-16 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 27530 on 395 degrees of freedom
## Multiple R-squared:  0.1758, Adjusted R-squared:  0.1737 
## F-statistic: 84.23 on 1 and 395 DF,  p-value: < 2.2e-16

5.3 Kiểm tra giả định của mô hình

plot(m.52)

5.4 Nhận xét kết quả

Việc 6: Đánh giá mối liên quan giữa giới tính, thời gian sau tiến sĩ với tiền lương

6.1 Tại sao cần thực hiện mô hình này

6.2 Thực hiện mô hình

m.62 = lm(Salary ~ Sex + Yrs.since.phd, data = salary)
summary(m.62)
## 
## Call:
## lm(formula = Salary ~ Sex + Yrs.since.phd, data = salary)
## 
## Residuals:
##    Min     1Q Median     3Q    Max 
## -84167 -19735  -2551  15427 102033 
## 
## Coefficients:
##               Estimate Std. Error t value Pr(>|t|)    
## (Intercept)    85181.8     4748.3  17.939   <2e-16 ***
## SexMale         7923.6     4684.1   1.692   0.0915 .  
## Yrs.since.phd    958.1      108.3   8.845   <2e-16 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 27470 on 394 degrees of freedom
## Multiple R-squared:  0.1817, Adjusted R-squared:  0.1775 
## F-statistic: 43.74 on 2 and 394 DF,  p-value: < 2.2e-16

6.3 Kiểm tra giả định mô hình

plot(m.62)

6.4 Nhận xét kết quả

6.5 So giá hệ số beta với mô hình 4.2 và 5.1

Việc 7: Ghi lại các lệnh và chia sẻ trên tài khoản RPubs của bạn