# Section 3.1 script

source("prep.R")

# set working directory to the data repositor
### setwd("YOUR WD")
ffa <- read_csv("Data/public_data.csv")
c(nrow(ffa),sum(ffa$cl=="H")) # two sample sizes
qtrs<-data.frame(month=rep(c(1:12),times=8),year=rep(c(2017:2024),each=12),qtr=rep(c(1:32),each=3))

# interest rates for plot
fra <- read.csv("Data/personal_loan_finance_rate.csv") %>%
  mutate(date=as.Date(observation_date)) %>% # https://fred.stlouisfed.org/graph/?id=TERMCBPER24NS,
  mutate(month=month(date),year=year(date)) %>% inner_join(qtrs) %>%
  group_by(qtr) %>% summarize(intRate=mean(TERMCBPER24NS,na.rm=T)) %>% mutate(rsRate=intRate*5)

fcln<-function(cll,lb) {
  a<-ffa %>% filter(cl%in%cll) %>%
    group_by(qtr,year) %>% summarize(loan=mean(loan*100),tpo=mean(tpo*100),cash=mean(cash*100)) 
  colnames(a)<-c("qtr","year",paste0(c("loan","tpo","cash"),lb))
  return(a)
} 
fia<-fcln(c("H","M","L"),"") %>% inner_join(fcln("H","H")) %>% arrange(qtr) 
fta<-data.frame(fnc=rep(c("loan","tpo","cash"),each=nrow(fia)),qtr=rep(fia$qtr,times=3),
                full=c(fia$loan,fia$tpo,fia$cash),high=c(fia$loanH,fia$tpoH,fia$cashH)) %>%
  mutate(lo=ifelse(full<high,full,high),hi=ifelse(full>high,full,high))
# combined figure
ggsave("Figures/figure3.jpg",
       plot=base(fta)+coord_cartesian(ylim=c(0,70))+
         geom_ribbon(aes(x=qtr,ymin=lo,ymax=hi,fill=fnc),alpha=0.6)+
         geom_line(aes(qtr,full,color=fnc))+geom_line(aes(qtr,high,color=fnc),linetype='dashed')+
         geom_line(data=fra,aes(qtr,rsRate),color='firebrick4',linetype='dotted')+
         theme(axis.title.x=element_blank())+ylab("Market\nShare\n(%)")+
         scale_fill_manual(values=c("darkcyan","navy","tan1"),guide="none")+
         scale_color_manual(values=c("darkcyan","navy","tan1"),guide="none")+
         ant("Loans",17,67,5,'navy')+ant("TPO",5,57,5,'tan1')+
         ant("Customer",28,3.5,5,'darkcyan')+ant("financed",28,-0.5,5,'darkcyan')+
         ant("10.1%",2/2,47,3.5,'firebrick4')+ant("12.3%",31.5,58,3.5,'firebrick4')+
         ant("Personal loan",30,69,3,'firebrick4')+ant("finance rate",30,66,3,'firebrick4')+
         scale_x_continuous(breaks=c(5,13,21,29),labels=c(2018,2020,2022,2024))+
         geom_segment(x=1,xend=4,y=70,yend=70,color='gray50')+ant("Full sample",7,70,4,'gray50')+
         geom_segment(x=1,xend=4,y=65,yend=65,color='gray50',linetype="dashed")+ant("High confidence",8.1,65,4,'gray50')+
         scale_y_continuous(breaks=c(0,35,70)),width=6,height=4)

# breakdowns by filer types
lfa<-filter(ffa,loan==1)
c(mean(lfa$mjr),mean(lfa$mjr[lfa$cl=="H"])) # mjr = PV loan specialist
c(mean(lfa$bank),mean(lfa$bank[lfa$cl=="H"])) 
c(mean(lfa$cu),mean(lfa$cu[lfa$cl=="H"])) # cu = credit union
aggregate(any~year,data=filter(lfa,cl=="H"),mean) # any = any of PV loan specialist, bank, or credit union

# 
lya<-lfa %>% group_by(year) %>% summarize(mjr=mean(mjr*100),bank=mean(bank*100),cu=mean(cu*100)) %>%
  inner_join(filter(lfa,cl=="H") %>% group_by(year) %>% summarize(mjrH=mean(mjr*100),bankH=mean(bank*100),cuH=mean(cu*100)))
lffa<-data.frame(year=rep(lya$year,times=3),typ=rep(c("aMjr","bBank","cCu"),each=nrow(lya)),
                full=c(lya$mjr,lya$bank,lya$cu),high=c(lya$mjrH,lya$bankH,lya$cuH)) %>%
  mutate(lo=ifelse(full<high,full,high),hi=ifelse(full>high,full,high))
lbz<-data.frame(lb=c("PV loan specialists","Banks","Credit","unions"),typ=c("aMjr","bBank","cCu","cCu"),
                x=c(2021,2023.1,2020.8,2020.8),y=c(50,20.5,21,17))
ggsave("Figures/figure4.jpg",
       plot=base(lffa)+coord_cartesian(ylim=c(0,75))+
         geom_ribbon(aes(year,ymin=lo,ymax=hi,fill=typ),alpha=0.6)+
         geom_line(aes(year,full,color=typ))+geom_line(aes(year,high,color=typ),linetype='dashed')+
         geom_text(data=lbz,aes(x,y,label=lb,color=typ),size=5)+
         scale_fill_manual(values=c("slateblue","purple","darkorange"),guide="none")+
         scale_color_manual(values=c("slateblue","purple","darkorange4"),guide="none")+
         ylab("PV Loan\nMarket\nShare\n(%)")+scale_y_continuous(breaks=c(0,25,50,75))+
         geom_segment(x=2020,xend=2021,y=77,yend=77,color='gray50')+ant("Full sample",2022.25,77,4,'gray50')+
         geom_segment(x=2020,xend=2021,y=74,yend=74,color='gray50',linetype="dashed")+ant("High confidence",2022.7,74,4,'gray50')+
         theme(axis.title.x=element_blank()),width=4,height=4)

