knitr::opts_chunk$set(echo = TRUE)

read libraries

library(dplyr)
library(tidyr)
library(CQR)
library(quantreg)

read dataset

data <- read.csv(file = "C:/Users/A13375/Documents/GitHub/CQR/EXAMPLE/data/extramartial_affair.csv")

quantile regression

qr_result <- rq(data = data,
                tau = seq(0.5, 0.95, 0.05),
                method = "fn",
                formula = affairs ~ rate_marriage + age + 
                  yrs_married + children + religious + 
                  educ + occupation + occupation_husb)

plot(qr_result)

1st step

taus <- seq(0.5, 0.95, 0.05)
q1 <- 0.1
cut_value <- 0
q2 <- 0.09

YV <- "affairs"
XV <- c("rate_marriage", "age",
        "yrs_married", "children", "religious",
        "educ, occupation", "occupation_husb")

cqr_data <- data %>% select(affairs, rate_marriage, age,
                            yrs_married, children, religious, 
                            educ, occupation, occupation_husb)



sq_extra <- cqr_data %>% select_(paste("-",YV, sep = "")) %>% mutate_at(vars(-matches(YV)), pt, n = 2)
colnames(sq_extra) <- paste("sq_", colnames(sq_extra), sep = "")

cub_extra <- cqr_data %>% select_(paste("-",YV, sep = "")) %>% mutate_at(vars(-matches(YV)), pt, n = 3)
colnames(cub_extra) <- paste("cub_", colnames(cub_extra), sep = "")

inter <- inter_party(cqr_data %>% select_(paste("-",YV, sep = "")))
lr_extra <- cbind(cqr_data, sq_extra, cub_extra, inter) %>%
  mutate(binom_var = ifelse(affairs > 0, 1, 0))

first_logit <- glm(lr_extra, formula = binom_var ~ . - affairs, family = binomial())
step_logit <- step(first_logit, trace = 0)

2nd and 3rd step

three_step_result <- two_three_step(first_step_model = step_logit, 
                                    taus = taus, 
                                    q1 = q1, 
                                    q2 = q2, 
                                    YV = YV, 
                                    XV = XV, 
                                    cqr_data = cqr_data)

result plot

sum_coef <- lapply(three_step_result[[1]],"[[", 3)
taus <- sapply(three_step_result[[1]],"[[", 6)
coefficients <- lapply(sum_coef, function(x){x[,1] %>% t() %>% as.data.frame()}) %>% bind_rows
stder <- lapply(sum_coef, function(x){x[,2] %>% t() %>% as.data.frame()}) %>% bind_rows

coef_df <- data.frame(coefficients, tau = taus) %>% gather(vid, value, -tau)
stder_df <- data.frame(stder, tau = taus) %>% gather(vid, stder, -tau)
plot_df <- coef_df %>% inner_join(stder_df, by = c("vid", "tau"))

plot_df %>%
  ggplot(aes(y = value, x = tau)) +
  facet_wrap(~vid, scales = "free") +
  geom_point() + 
  geom_line() +
  geom_errorbar(plot_df, 
                mapping = aes(x = tau, ymin = value - (stder*2), ymax = value + (stder*2)),
                width = 0.0005, size = 1, color="blue")


yasui-salmon/CQR documentation built on May 20, 2019, 12:32 p.m.