# Load packages
library(tidyverse)
library(readxl)
library(MASS)
library(lmtest)
library(car)

# Import the dataset
data <- read.csv("GOVSEMPTIMESERIES.GS00EMP01-2026-09-22T170141.csv")

# Check the dataset
dim(data)
## [1] 1976   20
head(data)
##   Year..time. Geographic.Area.Name..NAME.
## 1        2025               United States
## 2        2025               United States
## 3        2025               United States
## 4        2025               United States
## 5        2025               United States
## 6        2025               United States
##   Meaning.of.Aggregate.Description..AGG_DESC_LABEL.
## 1       Total - All Government Employment Functions
## 2                          Financial Administration
## 3                   Other Government Administration
## 4                                Judicial and Legal
## 5                           Police Protection Total
## 6  Police Protection - Persons with Power of Arrest
##   Meaning.of.Type.of.Government..GOVTYPE_LABEL. Full.Time.Employment..FT_EMP.
## 1                               State and Local                    15,790,344
## 2                               State and Local                       429,784
## 3                               State and Local                       270,830
## 4                               State and Local                       424,793
## 5                               State and Local                       936,007
## 6                               State and Local                       709,292
##   Full.Time.Payroll..FT_PAY. Part.Time.Employment..PT_EMP.
## 1            106,743,344,645                     4,524,442
## 2              2,992,697,403                        53,004
## 3              1,888,513,707                       180,646
## 4              3,161,227,083                        37,611
## 5              7,972,096,680                        87,506
## 6              6,477,500,863                        33,677
##   Part.Time.Payroll..PT_PAY. Part.Time.Hours..PT_HRS.
## 1              8,367,667,849                        N
## 2                 88,775,081                        N
## 3                157,914,189                        N
## 4                 87,901,725                        N
## 5                146,523,037                        N
## 6                 59,852,258                        N
##   Full.Time.Equivalent.Employment..FTE.
## 1                            17,527,046
## 2                               448,891
## 3                               301,610
## 4                               440,423
## 5                               970,667
## 6                               723,157
##   Total.Full.Time.and.Part.Time.Employment..TOT_EMP.
## 1                                         20,314,786
## 2                                            482,788
## 3                                            451,476
## 4                                            462,404
## 5                                          1,023,513
## 6                                            742,969
##   Total.Full.Time.and.Part.Time.Payroll..TOT_PAY.
## 1                                 115,111,012,494
## 2                                   3,081,472,484
## 3                                   2,046,427,896
## 4                                   3,249,128,808
## 5                                   8,118,619,717
## 6                                   6,537,353,121
##   Full.Time.Employment.Coefficient.of.Variation..FT_EMP_CV.
## 1                                                      0.38
## 2                                                      1.66
## 3                                                      2.55
## 4                                                      0.65
## 5                                                      1.93
## 6                                                      2.35
##   Full.Time.Payroll.Coefficient.of.Variation..FT_PAY_CV.
## 1                                                   0.34
## 2                                                   1.35
## 3                                                   3.29
## 4                                                   0.96
## 5                                                   1.71
## 6                                                   1.98
##   Part.Time.Employment.Coefficient.of.Variation..PT_EMP_CV.
## 1                                                      0.42
## 2                                                      9.59
## 3                                                      2.50
## 4                                                      1.38
## 5                                                      1.87
## 6                                                      4.18
##   Part.Time.Payroll.Coefficient.of.Variation..PT_PAY_CV.
## 1                                                   1.42
## 2                                                   6.14
## 3                                                   8.68
## 4                                                   1.77
## 5                                                   4.41
## 6                                                  10.59
##   Part.Time.Hours.Coefficient.of.Variation..PT_HRS_CV.
## 1                                                    N
## 2                                                    N
## 3                                                    N
## 4                                                    N
## 5                                                    N
## 6                                                    N
##   Full.Time.Equivalent.Employment.Coefficient.of.Variation..FTE_CV.
## 1                                                              0.36
## 2                                                              1.95
## 3                                                              2.73
## 4                                                              0.64
## 5                                                              1.87
## 6                                                              2.32
##   Total.Full.Time.and.Part.Time.Employment.Coefficient.of.Variation..TOT_EMP_CV.
## 1                                                                           0.31
## 2                                                                           1.81
## 3                                                                           1.83
## 4                                                                           0.60
## 5                                                                           1.78
## 6                                                                           2.26
##   Total.Full.Time.and.Part.Time.Payroll.Coefficient.of.Variation..TOT_PAY_CV.
## 1                                                                        0.33
## 2                                                                        1.32
## 3                                                                        3.11
## 4                                                                        0.94
## 5                                                                        1.69
## 6                                                                        1.96
names(data)
##  [1] "Year..time."                                                                   
##  [2] "Geographic.Area.Name..NAME."                                                   
##  [3] "Meaning.of.Aggregate.Description..AGG_DESC_LABEL."                             
##  [4] "Meaning.of.Type.of.Government..GOVTYPE_LABEL."                                 
##  [5] "Full.Time.Employment..FT_EMP."                                                 
##  [6] "Full.Time.Payroll..FT_PAY."                                                    
##  [7] "Part.Time.Employment..PT_EMP."                                                 
##  [8] "Part.Time.Payroll..PT_PAY."                                                    
##  [9] "Part.Time.Hours..PT_HRS."                                                      
## [10] "Full.Time.Equivalent.Employment..FTE."                                         
## [11] "Total.Full.Time.and.Part.Time.Employment..TOT_EMP."                            
## [12] "Total.Full.Time.and.Part.Time.Payroll..TOT_PAY."                               
## [13] "Full.Time.Employment.Coefficient.of.Variation..FT_EMP_CV."                     
## [14] "Full.Time.Payroll.Coefficient.of.Variation..FT_PAY_CV."                        
## [15] "Part.Time.Employment.Coefficient.of.Variation..PT_EMP_CV."                     
## [16] "Part.Time.Payroll.Coefficient.of.Variation..PT_PAY_CV."                        
## [17] "Part.Time.Hours.Coefficient.of.Variation..PT_HRS_CV."                          
## [18] "Full.Time.Equivalent.Employment.Coefficient.of.Variation..FTE_CV."             
## [19] "Total.Full.Time.and.Part.Time.Employment.Coefficient.of.Variation..TOT_EMP_CV."
## [20] "Total.Full.Time.and.Part.Time.Payroll.Coefficient.of.Variation..TOT_PAY_CV."
# Display the variable names
names(data)
##  [1] "Year..time."                                                                   
##  [2] "Geographic.Area.Name..NAME."                                                   
##  [3] "Meaning.of.Aggregate.Description..AGG_DESC_LABEL."                             
##  [4] "Meaning.of.Type.of.Government..GOVTYPE_LABEL."                                 
##  [5] "Full.Time.Employment..FT_EMP."                                                 
##  [6] "Full.Time.Payroll..FT_PAY."                                                    
##  [7] "Part.Time.Employment..PT_EMP."                                                 
##  [8] "Part.Time.Payroll..PT_PAY."                                                    
##  [9] "Part.Time.Hours..PT_HRS."                                                      
## [10] "Full.Time.Equivalent.Employment..FTE."                                         
## [11] "Total.Full.Time.and.Part.Time.Employment..TOT_EMP."                            
## [12] "Total.Full.Time.and.Part.Time.Payroll..TOT_PAY."                               
## [13] "Full.Time.Employment.Coefficient.of.Variation..FT_EMP_CV."                     
## [14] "Full.Time.Payroll.Coefficient.of.Variation..FT_PAY_CV."                        
## [15] "Part.Time.Employment.Coefficient.of.Variation..PT_EMP_CV."                     
## [16] "Part.Time.Payroll.Coefficient.of.Variation..PT_PAY_CV."                        
## [17] "Part.Time.Hours.Coefficient.of.Variation..PT_HRS_CV."                          
## [18] "Full.Time.Equivalent.Employment.Coefficient.of.Variation..FTE_CV."             
## [19] "Total.Full.Time.and.Part.Time.Employment.Coefficient.of.Variation..TOT_EMP_CV."
## [20] "Total.Full.Time.and.Part.Time.Payroll.Coefficient.of.Variation..TOT_PAY_CV."
# Display summary statistics
summary(data)
##   Year..time.   Geographic.Area.Name..NAME.
##  Min.   :2025   Length   :1976             
##  1st Qu.:2025   N.unique :  52             
##  Median :2025   N.blank  :   0             
##  Mean   :2025   Min.nchar:   4             
##  3rd Qu.:2025   Max.nchar:  20             
##  Max.   :2025                              
##  Meaning.of.Aggregate.Description..AGG_DESC_LABEL.
##  Length   :1976                                   
##  N.unique :  38                                   
##  N.blank  :   0                                   
##  Min.nchar:   6                                   
##  Max.nchar:  50                                   
##                                                   
##  Meaning.of.Type.of.Government..GOVTYPE_LABEL. Full.Time.Employment..FT_EMP.
##  Length   :1976                                Length   :1976               
##  N.unique :   1                                N.unique :1766               
##  N.blank  :   0                                N.blank  :   0               
##  Min.nchar:  15                                Min.nchar:   1               
##  Max.nchar:  15                                Max.nchar:  10               
##                                                                             
##  Full.Time.Payroll..FT_PAY. Part.Time.Employment..PT_EMP.
##  Length   :1976             Length   :1976               
##  N.unique :1902             N.unique :1294               
##  N.blank  :   0             N.blank  :   0               
##  Min.nchar:   1             Min.nchar:   1               
##  Max.nchar:  15             Max.nchar:   9               
##                                                          
##  Part.Time.Payroll..PT_PAY. Part.Time.Hours..PT_HRS.
##  Length   :1976             Length   :1976          
##  N.unique :1862             N.unique :   1          
##  N.blank  :   0             N.blank  :   0          
##  Min.nchar:   1             Min.nchar:   1          
##  Max.nchar:  13             Max.nchar:   1          
##                                                     
##  Full.Time.Equivalent.Employment..FTE.
##  Length   :1976                       
##  N.unique :1765                       
##  N.blank  :   0                       
##  Min.nchar:   1                       
##  Max.nchar:  10                       
##                                       
##  Total.Full.Time.and.Part.Time.Employment..TOT_EMP.
##  Length   :1976                                    
##  N.unique :1773                                    
##  N.blank  :   0                                    
##  Min.nchar:   1                                    
##  Max.nchar:  10                                    
##                                                    
##  Total.Full.Time.and.Part.Time.Payroll..TOT_PAY.
##  Length   :1976                                 
##  N.unique :1903                                 
##  N.blank  :   0                                 
##  Min.nchar:   1                                 
##  Max.nchar:  15                                 
##                                                 
##  Full.Time.Employment.Coefficient.of.Variation..FT_EMP_CV.
##  Min.   : 0.000                                           
##  1st Qu.: 0.000                                           
##  Median : 1.060                                           
##  Mean   : 3.433                                           
##  3rd Qu.: 2.922                                           
##  Max.   :49.730                                           
##  Full.Time.Payroll.Coefficient.of.Variation..FT_PAY_CV.
##  Min.   : 0.000                                        
##  1st Qu.: 0.000                                        
##  Median : 1.040                                        
##  Mean   : 3.623                                        
##  3rd Qu.: 3.340                                        
##  Max.   :77.910                                        
##  Part.Time.Employment.Coefficient.of.Variation..PT_EMP_CV.
##  Min.   : 0.00                                            
##  1st Qu.: 0.00                                            
##  Median : 1.69                                            
##  Mean   :10.41                                            
##  3rd Qu.: 5.44                                            
##  Max.   :99.30                                            
##  Part.Time.Payroll.Coefficient.of.Variation..PT_PAY_CV.
##  Min.   : 0.000                                        
##  1st Qu.: 0.000                                        
##  Median : 1.850                                        
##  Mean   : 9.024                                        
##  3rd Qu.: 6.385                                        
##  Max.   :98.230                                        
##  Part.Time.Hours.Coefficient.of.Variation..PT_HRS_CV.
##  Length   :1976                                      
##  N.unique :   1                                      
##  N.blank  :   0                                      
##  Min.nchar:   1                                      
##  Max.nchar:   1                                      
##                                                      
##  Full.Time.Equivalent.Employment.Coefficient.of.Variation..FTE_CV.
##  Min.   :  0.000                                                  
##  1st Qu.:  0.000                                                  
##  Median :  1.310                                                  
##  Mean   :  5.072                                                  
##  3rd Qu.:  5.253                                                  
##  Max.   :143.640                                                  
##  Total.Full.Time.and.Part.Time.Employment.Coefficient.of.Variation..TOT_EMP_CV.
##  Length   :1976                                                                
##  N.unique : 780                                                                
##  N.blank  :   0                                                                
##  Min.nchar:   1                                                                
##  Max.nchar:   5                                                                
##                                                                                
##  Total.Full.Time.and.Part.Time.Payroll.Coefficient.of.Variation..TOT_PAY_CV.
##  Length   :1976                                                             
##  N.unique : 742                                                             
##  N.blank  :   0                                                             
##  Min.nchar:   1                                                             
##  Max.nchar:   5                                                             
## 
# Rename variables for easier analysis
research_data <- data.frame(
  PT_PAY = data$Part.Time.Payroll..PT_PAY.,
  PT_EMP = data$Part.Time.Employment..PT_EMP.,
  FT_EMP = data$Full.Time.Employment..FT_EMP.
)

research_data$PT_PAY <- as.numeric(gsub(",", "", research_data$PT_PAY))
research_data$PT_EMP <- as.numeric(gsub(",", "", research_data$PT_EMP))
research_data$FT_EMP <- as.numeric(gsub(",", "", research_data$FT_EMP))

# Check the prepared dataset
head(research_data)
##       PT_PAY  PT_EMP   FT_EMP
## 1 8367667849 4524442 15790344
## 2   88775081   53004   429784
## 3  157914189  180646   270830
## 4   87901725   37611   424793
## 5  146523037   87506   936007
## 6   59852258   33677   709292
# Check for missing values
colSums(is.na(research_data))
## PT_PAY PT_EMP FT_EMP 
##      0      0      0
# Descriptive statistics
summary(research_data)
##      PT_PAY              PT_EMP              FT_EMP         
##  Min.   :0.000e+00   Min.   :      0.0   Min.   :0.000e+00  
##  1st Qu.:1.419e+05   1st Qu.:     99.0   1st Qu.:9.642e+02  
##  Median :8.601e+05   Median :    539.5   Median :3.676e+03  
##  Mean   :2.884e+07   Mean   :  15757.7   Mean   :4.996e+04  
##  3rd Qu.:5.330e+06   3rd Qu.:   3488.5   3rd Qu.:1.451e+04  
##  Max.   :8.368e+09   Max.   :4524442.0   Max.   :1.579e+07
# Check the data types
str(research_data)
## 'data.frame':    1976 obs. of  3 variables:
##  $ PT_PAY: num  8.37e+09 8.88e+07 1.58e+08 8.79e+07 1.47e+08 ...
##  $ PT_EMP: num  4524442 53004 180646 37611 87506 ...
##  $ FT_EMP: num  15790344 429784 270830 424793 936007 ...
# Check the class of each variable
sapply(research_data, class)
##    PT_PAY    PT_EMP    FT_EMP 
## "numeric" "numeric" "numeric"
# Look for missing values
colSums(is.na(research_data))
## PT_PAY PT_EMP FT_EMP 
##      0      0      0
# Run the original OLS regression
ols_model <- lm(PT_PAY ~ PT_EMP + FT_EMP, data = research_data)

# Display regression results
summary(ols_model)
## 
## Call:
## lm(formula = PT_PAY ~ PT_EMP + FT_EMP, data = research_data)
## 
## Residuals:
##        Min         1Q     Median         3Q        Max 
## -472561885    -603608     206099     332698  536793227 
## 
## Coefficients:
##               Estimate Std. Error t value Pr(>|t|)    
## (Intercept) -3.327e+05  6.167e+05  -0.539     0.59    
## PT_EMP       1.720e+03  1.488e+01 115.584   <2e-16 ***
## FT_EMP       4.152e+01  4.654e+00   8.920   <2e-16 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 27240000 on 1973 degrees of freedom
## Multiple R-squared:  0.9893, Adjusted R-squared:  0.9893 
## F-statistic: 9.119e+04 on 2 and 1973 DF,  p-value: < 2.2e-16
# Correlation between the independent variables
cor(research_data$PT_EMP, research_data$FT_EMP)
## [1] 0.9568142
# Variance Inflation Factor (VIF)
car::vif(ols_model)
##  PT_EMP  FT_EMP 
## 11.8334 11.8334
# Durbin-Watson test
lmtest::dwtest(ols_model)
## 
##  Durbin-Watson test
## 
## data:  ols_model
## DW = 1.9958, p-value = 0.455
## alternative hypothesis: true autocorrelation is greater than 0
# Shapiro-Wilk test for residual normality
shapiro.test(residuals(ols_model))
## 
##  Shapiro-Wilk normality test
## 
## data:  residuals(ols_model)
## W = 0.19564, p-value < 2.2e-16
# Keep observations with positive values for all three variables
log_data <- research_data %>%
  filter(PT_PAY > 0, PT_EMP > 0, FT_EMP > 0)

# Check the number of observations
nrow(log_data)
## [1] 1868
# Run the log-transformed regression
log_model <- lm(log(PT_PAY) ~ log(PT_EMP) + log(FT_EMP),
                data = log_data)

# Display regression results
summary(log_model)
## 
## Call:
## lm(formula = log(PT_PAY) ~ log(PT_EMP) + log(FT_EMP), data = log_data)
## 
## Residuals:
##      Min       1Q   Median       3Q      Max 
## -2.39070 -0.28576 -0.00249  0.30819  1.81590 
## 
## Coefficients:
##             Estimate Std. Error t value Pr(>|t|)    
## (Intercept) 6.550988   0.054271  120.71   <2e-16 ***
## log(PT_EMP) 0.852762   0.008744   97.52   <2e-16 ***
## log(FT_EMP) 0.209633   0.010953   19.14   <2e-16 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 0.5097 on 1865 degrees of freedom
## Multiple R-squared:  0.9618, Adjusted R-squared:  0.9618 
## F-statistic: 2.35e+04 on 2 and 1865 DF,  p-value: < 2.2e-16
# Check multicollinearity
car::vif(log_model)
## log(PT_EMP) log(FT_EMP) 
##    3.601679    3.601679
# Durbin-Watson test
lmtest::dwtest(log_model)
## 
##  Durbin-Watson test
## 
## data:  log_model
## DW = 1.4159, p-value < 2.2e-16
## alternative hypothesis: true autocorrelation is greater than 0
# Shapiro-Wilk normality test
shapiro.test(residuals(log_model))
## 
##  Shapiro-Wilk normality test
## 
## data:  residuals(log_model)
## W = 0.98599, p-value = 1.541e-12