################################################################################################# ##--------------------------------------------------------------------------------------------- # Functions for Classes by Prof. Jin Man Lee # DePaul University # # To use any functions here, you need to run the following code to read into your program # source("http://bigblue.depaul.edu/jlee141/econdata/R/func_tslib.R") # ################################################################################################# ##---------------------------------------------------------------------------------------------## adf_fulltest <- function(indata,maxlag){ if (!require("fUnitRoots")) install.packages("fUnitRoots") maxlag <- maxlag A <- matrix(nrow=maxlag+1,ncol=7) for (k in 0:maxlag) { k1 <- k + 1 adf0 <- adfTest(indata, lags = k, type = "nc") A[k1,2] <- round(adf0@test$statistic[1],digit=2) ; A[k1,3] <- adf0@test$p.value ; A[k1,1] <- k adf0 <- adfTest(indata, lags = k, type = "c") A[k1,4] <- round(adf0@test$statistic[1],digit=2) ; A[k1,5] <- adf0@test$p.value adf0 <- adfTest(indata, lags = k, type = "ct") A[k1,6] <- round(adf0@test$statistic[1],digit=2) ; A[k1,7] <- round(adf0@test$p.value,digit=2) } colnames(A) <- c("LAG","ADF","P(ADF)","ADF_C","P(ADF_C)","ADF_CT","P(ADF_CT)") return(A) } ##---------------------------------------------------------------------------------------------## ################################################################################################# # Local Projection Model Estimation ##---------------------------------------------------------------------------------------------## localproj <- function(endog_data,exog_data,lagend,lagexo,maxlag){ results_lin <- lp_lin(endog_data , lags_endog_lin = lagend, # Number of lags for endogenous data exog_data=exog_data , lags_exog = lagexo , trend = 0, # 0 = no trend, 1 = trend, 2 = trend & trend^2 shock_type = 1, # 0 = standard deviation shock, 1 = unit shock confint = 1.96, # Width of confidence bands: # 1 = 68%, 1.67 = 90%, 1.96 = 95% hor = maxlag) # Number of cores to use. When NULL, the number of cores # is chosen automatically g1 <- summary(results_lin) linear_plots <- plot_lin(results_lin) linear_plots[[1]] g2 <- linear_plots[[1]] lin_plots_all <- sapply(linear_plots, ggplotGrob) g3 <- marrangeGrob(lin_plots_all, nrow = ncol(endog_data), ncol = ncol(endog_data), top = NULL) gout <- list(g1,g2,g3) return(gout) } ################################################################################################ # Automatic Unit Root Tests using tseries ##--------------------------------------------------------------------------------------------## unitroot_tests <- function(series) { if (!require("tseries")) install.packages("tseries") adf_test <- adf.test(series, alternative = "stationary") kpss_test <- kpss.test(series, null = "Level", lshort = TRUE) pp_test <- pp.test(series) data.frame( Test = c("ADF Test", "PP Test", "KPSS Test"), Lag_order = c(adf_test$parameter, pp_test$parameter, kpss_test$parameter), Statistic = c(adf_test$statistic, pp_test$statistic, kpss_test$statistic), Stationary_P_Value = c(adf_test$p.value, pp_test$p.value, kpss_test$p.value)) } #-----------------------------------------------------------------------------# # Function for the # calculate_price_index(coeff_df, "200801","coeff","yearmm") # Make sure to have the rest options should have the quotations calculate_price_index <- function(data, base_ym, var_input, yearmm_column) { # Ensure column names are provided correctly if (!yearmm_column %in% names(data) || !var_input %in% names(data)) { stop("Column names provided do not exist in the dataframe.") } # Filter to get the base year-month row base_val <- data %>% filter(get(yearmm_column) == base_ym) %>% pull(var_input) %>% unlist() # Check if base_val is empty or NA if (length(base_val) == 0 || is.na(base_val)) { stop("Base value for specified year-month not found or NA.") } # Calculate the price index using the base value index_column <- 100 * data[[var_input]] / base_val[1] # Return the updated dataframe with the price index return(index_column) } ###########################################################################