#
# plot data
pacman::p_load(tidyverse, reshape, lm.beta, arm, coefplot, multiplot, data.table)
## Installing package into 'C:/Users/user/Documents/R/win-library/3.4'
## (as 'lib' is unspecified)
## Warning: package 'multiplot' is not available (for R version 3.4.3)
## Bioconductor version 3.6 (BiocInstaller 1.28.0), ?biocLite for help
## A new version of Bioconductor is available after installing the most
##   recent version of R; see http://bioconductor.org/install
## Warning in p_install(package, character.only = TRUE, ...):
## Warning in library(package, lib.loc = lib.loc, character.only = TRUE,
## logical.return = TRUE, : there is no package called 'multiplot'
## Warning in pacman::p_load(tidyverse, reshape, lm.beta, arm, coefplot, multiplot, : Failed to install/load:
## multiplot
dta <- read.csv("年度與學歷薪資.csv", header = TRUE)

#
mdta<-melt(dta,id.vars="學歷")
names(mdta)[2:3] <-c("年度", "薪資")
mdta <- mdta %>% mutate(薪資 = as.numeric(薪資),
                        年度 = as.numeric(年度))

#年度 學歷薪資 vs GDP
ggplot(mdta, aes(x = as.numeric(年度) , y = 薪資, col = 學歷))+
  geom_point(alpha = 0.5)+
  geom_line(size = 0.5)+
  geom_text(aes(label =薪資, vjust = -.4), size=4)+
  scale_x_continuous(breaks=seq(1,6, by = 1),labels=c('2000','2006','2010','2014','2015','2016'))+
  scale_y_continuous(limits = c(19000,63000))+
  labs(x = "年", y = "薪資")+
  theme_bw()

#
# model data
dta <- read.csv("data0505.csv", header = T)
options(digits = 3)
dta <- dta %>% mutate( Gender = relevel(Gender, ref = "女"),
                       Sector = relevel(Sector, ref = "私立"),
                       Field = relevel(Field, ref = "大眾傳播學群"),
                       EduLv = factor(EduLv, levels=c("博士","碩士","普通大學","科技大學",
                                                      "技術學院","五專","三專",
                                                      "二專","高中","高職","國中")),
                       EduLv = relevel(EduLv, ref = "技術學院"),
                       Region = factor(Region, levels =c("宜花東離島","北北基","桃竹苗",
                                                         "中彰投","雲嘉南","高屏澎")),
                       Age = as.numeric(Age), 
                       J_year = as.numeric(J_year), 
                       JobZone = as.numeric(JobZone),
                       EduZone = as.numeric(EduZone),
                       JobZone_D = as.numeric(EduZone-JobZone),
                       Salary = as.numeric(Salary),
                       SubEduOver = relevel(SubEduOver, ref="符合工作要求"),
                       Core = recode_factor(as.factor(JobCor), "1" = "無關聯",
                                            "2" = "部分關聯",
                                            "3" = "核心關聯"),
                       SubEduOver = factor(SubEduOver,levels =c("符合工作要求","高於工作要求","低於工作要求")))

# data construction
glimpse(dta)
## Observations: 1,571
## Variables: 26
## $ SID         <fctr> A1, A10, A100, A102, A103, A104, A105, A106, A107...
## $ Gender      <fctr> 女, 女, 女, 女, 男, 女, 女, 女, 女, 女, 女, 男, 男, 男, 女, 男, 男...
## $ Sector      <fctr> 國立(公立), 國立(公立), 私立, 國立(公立), 國立(公立), 國立(公立), 國立(公立...
## $ EduLv       <fctr> 碩士, 碩士, 普通大學, 普通大學, 高職, 普通大學, 普通大學, 普通大學, 普通大學, 普...
## $ SubEduOver  <fctr> 符合工作要求, 高於工作要求, 符合工作要求, 符合工作要求, 符合工作要求, 符合工作要求, 符...
## $ Require     <fctr> 碩士, 高中/高職, 普通大學, 普通大學, 高中/高職, 普通大學, 普通大學, 普通大學, 普...
## $ Field       <fctr> 教育學群, 資訊學群, 外語學群, 教育學群, 工程學群, 文史哲學群, 文史哲學群, 大眾傳播學...
## $ City        <fctr> 臺南市, 高雄市, 苗栗縣, 新北市, 高雄市, 南投縣, 嘉義市, 臺北市, 臺北市, 南投縣,...
## $ Category    <fctr> 受雇於公營機關, 受雇於公營機關, 受雇於公營機關, 受雇於公營機關, 受雇者於私營企業, 受雇於...
## $ Staff       <fctr> 10-29人, 50-99人, 50-99人, 10-29人, 2-9人, 100-199人, 1...
## $ Hours       <int> 48, 40, 70, 50, 57, 51, 64, 50, 50, 47, 50, 60, 45...
## $ J_year      <dbl> 2, 8, 4, 1, 21, 1, 6, 0, 1, 1, 17, 7, 3, 23, 1, 2,...
## $ J_total     <dbl> 2, 8, 4, 1, 30, 1, 6, 0, 2, 2, 28, 7, 30, 26, 1, 1...
## $ income      <fctr> 2萬以下, 2-3萬以下, 3-4萬以下, 4-5萬以下, 3-4萬以下, 4-5萬以下, 2萬以...
## $ SubMismatch <int> 4, 2, 3, 4, 5, 4, 5, 4, 3, 3, 4, 4, 4, 4, 5, 5, 3,...
## $ JobSat      <int> 6, 4, 3, 5, 5, 6, 7, 5, 3, 6, 3, 5, 4, 7, 3, 4, 4,...
## $ EduZone     <dbl> 5, 5, 4, 4, 2, 4, 4, 4, 4, 4, 5, 5, 3, 5, 4, 4, 4,...
## $ Region      <fctr> 雲嘉南, 高屏澎, 桃竹苗, 北北基, 高屏澎, 中彰投, 雲嘉南, 北北基, 北北基, 中彰投,...
## $ Salary      <dbl> 20000, 25000, 35000, 45000, 35000, 45000, 20000, 3...
## $ Age         <dbl> 26, 34, 30, 25, 62, 25, 21, 24, 25, 26, 57, 35, 54...
## $ JobZone     <dbl> 4, 3, 4, 4, 3, 4, 4, 4, 4, 4, 4, 4, 4, 5, 4, 4, 3,...
## $ JobCor      <int> 3, 1, 2, 3, 1, 2, 1, 3, 1, 1, 1, 3, 1, 1, 3, 1, 3,...
## $ Core        <fctr> 核心關聯, 無關聯, 部分關聯, 核心關聯, 無關聯, 部分關聯, 無關聯, 核心關聯, 無關聯,...
## $ ObjOver     <fctr> over, over, adequate, adequate, under, adequate, ...
## $ X           <int> NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA...
## $ JobZone_D   <dbl> 1, 2, 0, 0, -1, 0, 0, 0, 0, 0, 1, 1, -1, 0, 0, 0, ...
# check and pick out
lapply(dta[,c("Sector", "Field", "City", "Region","EduLv", "SubEduOver", "ObjOver")], levels)
## $Sector
## [1] "私立"         "國外學校"     "國立(公立)"
## 
## $Field
##  [1] "大眾傳播學群"   "工程學群"       "文史哲學群"     "外語學群"      
##  [5] "生命科學學群"   "生物資源學群"   "地球與環境學群" "法政學群"      
##  [9] "社會與心理學群" "建築與設計學群" "財經學群"       "教育學群"      
## [13] "資訊學群"       "遊憩與運動學群" "管理學群"       "數理化學群"    
## [17] "醫藥衛生學群"   "藝術學群"      
## 
## $City
##  [1] "宜蘭縣" "花蓮縣" "金門縣" "南投縣" "屏東縣" "苗栗縣" "桃園市"
##  [8] "高雄市" "基隆市" "雲林縣" "新北市" "新竹市" "新竹縣" "嘉義市"
## [15] "嘉義縣" "彰化縣" "臺中市" "臺北市" "臺東縣" "臺南市" "澎湖縣"
## 
## $Region
## [1] "宜花東離島" "北北基"     "桃竹苗"     "中彰投"     "雲嘉南"    
## [6] "高屏澎"    
## 
## $EduLv
##  [1] "技術學院" "博士"     "碩士"     "普通大學" "科技大學" "五專"    
##  [7] "三專"     "二專"     "高中"     "高職"     "國中"    
## 
## $SubEduOver
## [1] "符合工作要求" "高於工作要求" "低於工作要求"
## 
## $ObjOver
## [1] "adequate" "over"     "under"
# pick out
names(dta)
##  [1] "SID"         "Gender"      "Sector"      "EduLv"       "SubEduOver" 
##  [6] "Require"     "Field"       "City"        "Category"    "Staff"      
## [11] "Hours"       "J_year"      "J_total"     "income"      "SubMismatch"
## [16] "JobSat"      "EduZone"     "Region"      "Salary"      "Age"        
## [21] "JobZone"     "JobCor"      "Core"        "ObjOver"     "X"          
## [26] "JobZone_D"
po <- dplyr::select(dta, -City, -income, -JobSat, -X)%>%
  filter(Age <= 65 & Age >= 20 & Category != "自營者" )
p<- filter(po, Age <= 40)
p <- p[p$EduLv %in% c("博士","碩士", "普通大學", "科技大學", "技術學院"),]
p <- p %>% mutate(   EduLv = factor(EduLv, levels=c("博士","碩士","普通大學","科技大學",
                                                    "技術學院")),
                     EduLv = relevel(EduLv, ref = "技術學院"),
                     Category = factor(Category,levels=c("受雇者於私營企業","受雇於公營機關")))

# Fit the null model
null <- lm(Salary ~1, data= p)

# 最終模型設定

# 薪資對性別 / 公私立.教育程度.學群
a2 <- update(null,~.+Gender+Sector+ EduLv+ Field)
summary(a2)
## 
## Call:
## lm(formula = Salary ~ Gender + Sector + EduLv + Field, data = p)
## 
## Residuals:
##    Min     1Q Median     3Q    Max 
## -43136 -10413  -2259   5813 237764 
## 
## Coefficients:
##                     Estimate Std. Error t value Pr(>|t|)    
## (Intercept)          30928.0     4558.8    6.78  1.8e-11 ***
## Gender男              6492.6     1276.1    5.09  4.1e-07 ***
## Sector國外學校        9498.6     4767.1    1.99  0.04652 *  
## Sector國立(公立)    3592.6     1306.3    2.75  0.00604 ** 
## EduLv博士            23012.9     9514.4    2.42  0.01571 *  
## EduLv碩士            14522.3     3592.3    4.04  5.6e-05 ***
## EduLv普通大學         2015.2     3500.7    0.58  0.56494    
## EduLv科技大學        -3962.4     3585.3   -1.11  0.26928    
## Field工程學群        12600.3     3457.1    3.64  0.00028 ***
## Field文史哲學群      -1491.4     3789.0   -0.39  0.69393    
## Field外語學群         3414.7     3865.7    0.88  0.37722    
## Field生命科學學群      240.0     4615.3    0.05  0.95854    
## Field生物資源學群     -547.3     5240.0   -0.10  0.91683    
## Field地球與環境學群    740.7     5320.5    0.14  0.88930    
## Field法政學群         1532.0     3968.8    0.39  0.69955    
## Field社會與心理學群    -81.9     3499.3   -0.02  0.98134    
## Field建築與設計學群   6700.7     4205.9    1.59  0.11136    
## Field財經學群         4779.5     3614.7    1.32  0.18631    
## Field教育學群         2957.0     3675.1    0.80  0.42119    
## Field資訊學群         7586.6     3626.7    2.09  0.03664 *  
## Field遊憩與運動學群   1678.8     4883.0    0.34  0.73105    
## Field管理學群        -1853.8     3507.0   -0.53  0.59717    
## Field數理化學群       5671.7     4038.7    1.40  0.16045    
## Field醫藥衛生學群    10851.3     3665.8    2.96  0.00313 ** 
## Field藝術學群        -5917.2     4716.1   -1.25  0.20981    
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 19800 on 1318 degrees of freedom
## Multiple R-squared:  0.231,  Adjusted R-squared:  0.217 
## F-statistic: 16.5 on 24 and 1318 DF,  p-value: <2e-16
# 薪資對性別 / 公私立.教育程度.學群 / 總年資.工時
a3 <- update(null,~.+Gender+Sector+ EduLv+ Field +J_total+ Hours)
summary(a3)
## 
## Call:
## lm(formula = Salary ~ Gender + Sector + EduLv + Field + J_total + 
##     Hours, data = p)
## 
## Residuals:
##    Min     1Q Median     3Q    Max 
## -42197 -10051  -1733   6178 228906 
## 
## Coefficients:
##                     Estimate Std. Error t value Pr(>|t|)    
## (Intercept)          13930.9     5178.4    2.69  0.00723 ** 
## Gender男              6189.8     1241.0    4.99  6.9e-07 ***
## Sector國外學校        9839.6     4635.0    2.12  0.03395 *  
## Sector國立(公立)    4422.2     1275.0    3.47  0.00054 ***
## EduLv博士            22094.8     9252.7    2.39  0.01708 *  
## EduLv碩士            15183.0     3498.4    4.34  1.5e-05 ***
## EduLv普通大學         4311.1     3418.2    1.26  0.20745    
## EduLv科技大學        -2976.4     3488.7   -0.85  0.39373    
## Field工程學群        12072.2     3365.5    3.59  0.00035 ***
## Field文史哲學群      -3706.2     3691.8   -1.00  0.31560    
## Field外語學群         2264.1     3759.9    0.60  0.54716    
## Field生命科學學群      447.9     4487.1    0.10  0.92050    
## Field生物資源學群      568.0     5095.1    0.11  0.91124    
## Field地球與環境學群  -1570.0     5181.1   -0.30  0.76193    
## Field法政學群          686.2     3859.4    0.18  0.85890    
## Field社會與心理學群   -650.7     3402.2   -0.19  0.84836    
## Field建築與設計學群   5219.0     4094.3    1.27  0.20264    
## Field財經學群         3454.0     3516.8    0.98  0.32621    
## Field教育學群          686.2     3581.5    0.19  0.84809    
## Field資訊學群         7473.2     3525.4    2.12  0.03421 *  
## Field遊憩與運動學群   2073.3     4747.1    0.44  0.66237    
## Field管理學群        -2286.0     3409.9   -0.67  0.50272    
## Field數理化學群       4491.3     3928.1    1.14  0.25310    
## Field醫藥衛生學群    10270.2     3567.4    2.88  0.00406 ** 
## Field藝術學群        -6912.2     4585.8   -1.51  0.13197    
## J_total               1062.3      130.8    8.12  1.1e-15 ***
## Hours                  221.8       56.3    3.94  8.5e-05 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 19200 on 1316 degrees of freedom
## Multiple R-squared:  0.274,  Adjusted R-squared:  0.26 
## F-statistic: 19.1 on 26 and 1316 DF,  p-value: <2e-16
# 薪資對性別 / 公私立.教育程度.學群 / 年資.工時 / 自評過量.自評關聯
a42 <- update(null,~.+Gender+Sector+ EduLv+ Field +J_total+ Hours+ SubEduOver+SubMismatch)
summary(a42)
## 
## Call:
## lm(formula = Salary ~ Gender + Sector + EduLv + Field + J_total + 
##     Hours + SubEduOver + SubMismatch, data = p)
## 
## Residuals:
##    Min     1Q Median     3Q    Max 
## -44066 -10027  -1662   6144 222306 
## 
## Coefficients:
##                        Estimate Std. Error t value Pr(>|t|)    
## (Intercept)             11099.0     5257.2    2.11  0.03495 *  
## Gender男                 6977.7     1212.7    5.75  1.1e-08 ***
## Sector國外學校          13865.8     4536.8    3.06  0.00229 ** 
## Sector國立(公立)       4088.7     1247.0    3.28  0.00107 ** 
## EduLv博士               18499.9     9037.9    2.05  0.04086 *  
## EduLv碩士               14258.0     3427.5    4.16  3.4e-05 ***
## EduLv普通大學            3816.2     3328.0    1.15  0.25171    
## EduLv科技大學           -3435.1     3398.3   -1.01  0.31228    
## Field工程學群           11763.3     3287.5    3.58  0.00036 ***
## Field文史哲學群         -3596.7     3600.7   -1.00  0.31803    
## Field外語學群            2339.5     3663.0    0.64  0.52314    
## Field生命科學學群        2897.6     4383.5    0.66  0.50871    
## Field生物資源學群        1380.9     4975.4    0.28  0.78140    
## Field地球與環境學群      -906.5     5061.2   -0.18  0.85788    
## Field法政學群            1462.0     3763.6    0.39  0.69775    
## Field社會與心理學群     -2051.2     3318.5   -0.62  0.53662    
## Field建築與設計學群      5661.2     3989.3    1.42  0.15611    
## Field財經學群            2479.6     3425.6    0.72  0.46930    
## Field教育學群           -1177.9     3504.3   -0.34  0.73683    
## Field資訊學群            7131.0     3444.9    2.07  0.03864 *  
## Field遊憩與運動學群      1131.5     4633.5    0.24  0.80712    
## Field管理學群           -1378.1     3327.6   -0.41  0.67883    
## Field數理化學群          4267.0     3830.9    1.11  0.26555    
## Field醫藥衛生學群        9049.4     3486.8    2.60  0.00956 ** 
## Field藝術學群           -6521.4     4469.6   -1.46  0.14479    
## J_total                  1066.8      128.0    8.34  < 2e-16 ***
## Hours                     243.3       54.9    4.43  1.0e-05 ***
## SubEduOver高於工作要求  -7781.9     1372.4   -5.67  1.8e-08 ***
## SubEduOver低於工作要求  -7421.3     1761.1   -4.21  2.7e-05 ***
## SubMismatch              1582.4      466.0    3.40  0.00070 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 18700 on 1313 degrees of freedom
## Multiple R-squared:  0.314,  Adjusted R-squared:  0.299 
## F-statistic: 20.7 on 29 and 1313 DF,  p-value: <2e-16
# 薪資對性別 / 公私立.教育程度.學群 / 年資.工時 / 客評過量.客評關聯
a52 <- update(null,~.+Gender+Sector+ EduLv+ Field +J_total+ Hours+ ObjOver+ Core)
summary(a52)
## 
## Call:
## lm(formula = Salary ~ Gender + Sector + EduLv + Field + J_total + 
##     Hours + ObjOver + Core, data = p)
## 
## Residuals:
##    Min     1Q Median     3Q    Max 
## -46227  -9947  -1726   6485 231551 
## 
## Coefficients:
##                     Estimate Std. Error t value Pr(>|t|)    
## (Intercept)          15922.6     5197.2    3.06  0.00223 ** 
## Gender男              5963.6     1229.2    4.85  1.4e-06 ***
## Sector國外學校        9159.8     4580.2    2.00  0.04572 *  
## Sector國立(公立)    4324.9     1262.7    3.43  0.00063 ***
## EduLv博士            20827.1     9145.2    2.28  0.02292 *  
## EduLv碩士            15521.3     3476.1    4.47  8.7e-06 ***
## EduLv普通大學         3157.1     3386.4    0.93  0.35136    
## EduLv科技大學        -4071.9     3449.3   -1.18  0.23801    
## Field工程學群        12327.2     3345.0    3.69  0.00024 ***
## Field文史哲學群      -4704.0     3676.6   -1.28  0.20096    
## Field外語學群         2000.1     3753.8    0.53  0.59425    
## Field生命科學學群     1448.5     4453.1    0.33  0.74502    
## Field生物資源學群     1674.1     5054.3    0.33  0.74053    
## Field地球與環境學群  -2724.3     5150.7   -0.53  0.59694    
## Field法政學群         1103.1     3861.9    0.29  0.77520    
## Field社會與心理學群  -2596.0     3423.9   -0.76  0.44847    
## Field建築與設計學群   4417.8     4051.8    1.09  0.27577    
## Field財經學群         3699.1     3492.5    1.06  0.28972    
## Field教育學群         -491.2     3563.6   -0.14  0.89039    
## Field資訊學群         6545.6     3500.0    1.87  0.06168 .  
## Field遊憩與運動學群   3199.1     4702.1    0.68  0.49640    
## Field管理學群        -1921.0     3406.0   -0.56  0.57285    
## Field數理化學群       4413.0     3886.2    1.14  0.25635    
## Field醫藥衛生學群     8534.6     3613.7    2.36  0.01834 *  
## Field藝術學群        -7535.1     4533.0   -1.66  0.09669 .  
## J_total               1076.0      130.0    8.28  3.0e-16 ***
## Hours                  214.3       55.6    3.85  0.00012 ***
## ObjOverover          -3459.5     1320.6   -2.62  0.00890 ** 
## ObjOverunder          7064.9     2872.9    2.46  0.01406 *  
## Core部分關聯          5600.9     1636.7    3.42  0.00064 ***
## Core核心關聯          2761.5     1356.3    2.04  0.04194 *  
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 19000 on 1312 degrees of freedom
## Multiple R-squared:  0.295,  Adjusted R-squared:  0.279 
## F-statistic: 18.3 on 30 and 1312 DF,  p-value: <2e-16
# 薪資對性別 / 公私立.教育程度.學群 / 年資.工時 / 自評過量.客評關聯
a6 <- update(null,~.+Gender+Sector+ EduLv+ Field +J_total+ Hours+ SubEduOver+ Core)
summary(a6)
## 
## Call:
## lm(formula = Salary ~ Gender + Sector + EduLv + Field + J_total + 
##     Hours + SubEduOver + Core, data = p)
## 
## Residuals:
##    Min     1Q Median     3Q    Max 
## -48434  -9862  -1803   6214 226660 
## 
## Coefficients:
##                        Estimate Std. Error t value Pr(>|t|)    
## (Intercept)             15293.4     5063.9    3.02  0.00258 ** 
## Gender男                 6782.4     1211.9    5.60  2.7e-08 ***
## Sector國外學校          13093.9     4537.1    2.89  0.00397 ** 
## Sector國立(公立)       4107.8     1247.4    3.29  0.00102 ** 
## EduLv博士               19420.7     9024.0    2.15  0.03157 *  
## EduLv碩士               14803.1     3415.7    4.33  1.6e-05 ***
## EduLv普通大學            3830.1     3329.2    1.15  0.25017    
## EduLv科技大學           -3293.0     3397.6   -0.97  0.33262    
## Field工程學群           11734.5     3290.9    3.57  0.00038 ***
## Field文史哲學群         -4293.0     3627.1   -1.18  0.23679    
## Field外語學群            1785.4     3691.1    0.48  0.62868    
## Field生命科學學群        2523.1     4383.0    0.58  0.56495    
## Field生物資源學群         331.4     4963.9    0.07  0.94678    
## Field地球與環境學群     -1736.8     5092.9   -0.34  0.73315    
## Field法政學群            1489.5     3764.4    0.40  0.69240    
## Field社會與心理學群     -1924.2     3317.6   -0.58  0.56202    
## Field建築與設計學群      5091.3     3996.9    1.27  0.20296    
## Field財經學群            2390.1     3429.5    0.70  0.48598    
## Field教育學群           -1491.9     3517.5   -0.42  0.67154    
## Field資訊學群            6308.1     3456.3    1.83  0.06821 .  
## Field遊憩與運動學群      1696.2     4638.1    0.37  0.71463    
## Field管理學群           -1798.0     3343.5   -0.54  0.59083    
## Field數理化學群          4700.5     3832.7    1.23  0.22026    
## Field醫藥衛生學群        8746.1     3499.7    2.50  0.01257 *  
## Field藝術學群           -7074.2     4473.6   -1.58  0.11405    
## J_total                  1057.4      128.2    8.25  3.8e-16 ***
## Hours                     238.4       54.9    4.34  1.5e-05 ***
## SubEduOver高於工作要求  -8439.2     1324.8   -6.37  2.6e-10 ***
## SubEduOver低於工作要求  -8310.6     1731.7   -4.80  1.8e-06 ***
## Core部分關聯             4832.8     1616.3    2.99  0.00284 ** 
## Core核心關聯             3087.6     1293.4    2.39  0.01712 *  
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 18700 on 1312 degrees of freedom
## Multiple R-squared:  0.314,  Adjusted R-squared:  0.298 
## F-statistic:   20 on 30 and 1312 DF,  p-value: <2e-16
# coefplot
# model 1
# coef
dt <- data.table::setDT(as.data.frame(summary(a2)$coefficients[, 1:2]),T)
rn <- dt$rn  # save the levels from rownames
dt <- dt %>% mutate( rn = factor(rn, levels = rn)) # appoint the levels to data

# plot
ggplot(dt, aes(rn, Estimate)) +
  geom_hline(yintercept=0, lty=2, lwd=1, colour="grey50") +
  geom_errorbar(aes(ymin=Estimate-1.96*`Std. Error`, 
                    ymax=Estimate+1.96*`Std. Error`),
                lwd=1, colour="#99CCCC", width=0) +
  geom_errorbar(aes(ymin=Estimate-`Std. Error`,
                    ymax=Estimate+`Std. Error`), 
                lwd=1.5, colour="steelblue", width=0) +
  geom_point(size=3, pch=16) +
  # set digtal and vertical adjust the text
  geom_text(aes(label = round(dt$Estimate, 2)),size=3.5, vjust = -.5)+
  labs(x = "係數", y = "變項")+
  coord_flip()+
  theme_bw()

#
# model 2
# coef
dt <- data.table::setDT(as.data.frame(summary(a3)$coefficients[, 1:2]), keep.rownames = T)
rn <- dt$rn  # save the levels from rownames
dt <- dt %>% mutate( rn = factor(rn, levels = rn)) # appoint the levels to data

# plot
ggplot(dt, aes(rn, Estimate)) +
  geom_hline(yintercept=0, lty=2, lwd=1, colour="grey50") +
  geom_errorbar(aes(ymin=Estimate-1.96*`Std. Error`, 
                    ymax=Estimate+1.96*`Std. Error`),
                lwd=1, colour="#99CCCC", width=0) +
  geom_errorbar(aes(ymin=Estimate-`Std. Error`,
                    ymax=Estimate+`Std. Error`), 
                lwd=1.5, colour="steelblue", width=0) +
  geom_point(size=3, pch=16) +
  # set digtal and vertical adjust the text
  geom_text(aes(label = round(dt$Estimate, 2)),size=3.5, vjust = -.5)+
  labs(x = "係數", y = "變項")+
  coord_flip()+
  theme_bw()

#
# model 3a
# coef
dt <- data.table::setDT(as.data.frame(summary(a42)$coefficients[, 1:2]), keep.rownames = T)
rn <- dt$rn  # save the levels from rownames
dt <- dt %>% mutate( rn = c("截距", 
                            "男",
                            "國外學校",
                            "國立(公立)",
                            "博士",
                            "碩士",
                            "普通大學",
                            "科技大學",
                            "工程學群",
                            "文史哲學群",
                            "外語學群",
                            "生命科學學群",
                            "生物資源學群",
                            "地球與環境學群",
                            "法政學群",
                            "社會與心理學群",
                            "建築與設計學群",
                            "財經學群",
                            "教育學群",
                            "資訊學群",
                            "遊憩與運動學群",
                            "管理學群",
                            "數理化學群",
                            "醫藥衛生學群",
                            "藝術學群",
                            "總年資",
                            "每週工時",
                            "高於工作要求",
                            "低於工作要求",
                            "學非所用程度")) # appoint the levels to data

# plot
ggplot(dt, aes(reorder(rn, Estimate), Estimate)) +
  geom_hline(yintercept=0, lty=2, lwd=1, colour="grey50") +
  geom_errorbar(aes(ymin=Estimate-1.96*`Std. Error`, 
                    ymax=Estimate+1.96*`Std. Error`),
                lwd=1, colour="#99CCCC", width=0) +
  geom_errorbar(aes(ymin=Estimate-`Std. Error`,
                    ymax=Estimate+`Std. Error`), 
                lwd=1.5, colour="steelblue", width=0) +
  geom_point(size=3, pch=16) +
  # set digtal and vertical adjust the text
  geom_text(aes(label = round(dt$Estimate, 2)),size=3.5, vjust = -.5)+
  labs(x = "係數", y = "變項")+
  coord_flip()+
  theme_bw()

#
# model 3b
# coef
dt <- data.table::setDT(as.data.frame(summary(a52)$coefficients[, 1:2]), keep.rownames = T)
rn <- dt$rn  # save the levels from rownames
dt <- dt %>% mutate( rn = c("截距", 
                            "男",
                            "國外學校",
                            "國立(公立)",
                            "博士",
                            "碩士",
                            "普通大學",
                            "科技大學",
                            "工程學群",
                            "文史哲學群",
                            "外語學群",
                            "生命科學學群",
                            "生物資源學群",
                            "地球與環境學群",
                            "法政學群",
                            "社會與心理學群",
                            "建築與設計學群",
                            "財經學群",
                            "教育學群",
                            "資訊學群",
                            "遊憩與運動學群",
                            "管理學群",
                            "數理化學群",
                            "醫藥衛生學群",
                            "藝術學群",
                            "總年資",
                            "每週工時",
                            "高於工作要求",
                            "低於工作要求",
                            "學用部份關聯",
                            "學用核心關聯")) # appoint the levels to data


# plot
ggplot(dt, aes(reorder(rn, Estimate), Estimate)) +
  geom_hline(yintercept=0, lty=2, lwd=1, colour="grey50") +
  geom_errorbar(aes(ymin=Estimate-1.96*`Std. Error`, 
                    ymax=Estimate+1.96*`Std. Error`),
                lwd=1, colour="#99CCCC", width=0) +
  geom_errorbar(aes(ymin=Estimate-`Std. Error`,
                    ymax=Estimate+`Std. Error`), 
                lwd=1.5, colour="steelblue", width=0) +
  geom_point(size=3, pch=16) +
  # set digtal and vertical adjust the text
  geom_text(aes(label = round(dt$Estimate, 2)),size=3.5, vjust = -.5)+
  labs(x = "係數", y = "變項")+
  coord_flip()+
  theme_bw()