├── Lecture1 ├── movie_star.txt └── movie_star_1.R ├── Lecture2 └── Lecture2_ggplot2.R ├── Lecture3 ├── .Rhistory ├── stop.txt ├── yitian.R ├── yitian_split.bin ├── yitian_split.txt ├── 主角名单.txt └── 倚天屠龙记.Txt └── README.md /Lecture1/movie_star.txt: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/BearAcademy/R-Tutorial/ce00889917d3839ba977d0e39cb1c76ce43f9a9f/Lecture1/movie_star.txt -------------------------------------------------------------------------------- /Lecture1/movie_star_1.R: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/BearAcademy/R-Tutorial/ce00889917d3839ba977d0e39cb1c76ce43f9a9f/Lecture1/movie_star_1.R -------------------------------------------------------------------------------- /Lecture2/Lecture2_ggplot2.R: -------------------------------------------------------------------------------- 1 | 2 | 3 | #### download cran logs 4 | 5 | library(devtools) 6 | install_github("metacran/cranlogs") 7 | library(cranlogs) 8 | cran_top_downloads(when = "last-week") 9 | 10 | cran_ggplot2 = cran_downloads(package = "ggplot2", from = "2012-10-01", to = "2016-05-31") 11 | head(cran_ggplot2) 12 | sum(cran_ggplot2$count) 13 | 14 | #### ggplot2 histograms 15 | 16 | library(ggplot2) 17 | 18 | ggplot(cran_ggplot2, aes(count)) + 19 | geom_histogram(aes(y=..density..), bins = 30, color = "white", fill = "grey") 20 | 21 | library(showtext) 22 | X11() 23 | ggplot(cran_ggplot2, aes(count)) + 24 | geom_histogram(aes(y=..density..), bins = 30, color = "white", fill = "grey") + 25 | geom_density(aes(y = ..density..), color = 'red',fill='red', alpha=0.2) + 26 | theme(plot.margin = unit(c(0,1,1,0), "cm"), 27 | axis.text=element_text(size=17, family = "wqy-microhei"), 28 | axis.title.x = element_text(vjust=-2), 29 | axis.title=element_text(size=20,face="bold")) 30 | 31 | showtext.auto(enable = TRUE) 32 | 33 | cran2 = cran_downloads(package = c("ggplot2", "plyr"), from = "2012-10-01", to = "2016-05-31") 34 | cran2$package = factor(cran2$package) 35 | ggplot(cran2, aes(count, fill = package, colour = package)) + 36 | geom_density(aes(y = ..density..), alpha=0.2, size = 1) + 37 | theme(plot.margin = unit(c(0,1,1,0), "cm"), 38 | axis.text=element_text(size=17, family = "wqy-microhei"), 39 | axis.title.x = element_text(vjust=-2), 40 | axis.title=element_text(size=20,face="bold")) 41 | 42 | #### barplot of super stars 43 | 44 | super_star = read.table("movie_star.txt", col.names = c("super", "star")) 45 | head(super_star) 46 | Num = sort(table(super_star$star), decreasing = T) 47 | df = data.frame(star = names(Num), num_super = Num) 48 | df$star = factor(df$star, levels = df$star) 49 | 50 | 51 | showtext.auto(enable = TRUE) 52 | X11() 53 | ggplot(df, aes(x = star, y = num_super, fill = num_super)) + 54 | geom_bar(stat = "identity", width = 0.75) + 55 | scale_fill_gradient(limits=c(0, 4), low="white",high="red")+ 56 | xlab("Super Star") + 57 | ylab("Number of TV Series") + 58 | theme(axis.text=element_text(size=17, family = "wqy-microhei"), 59 | axis.title=element_text(size=20,face="bold"), 60 | axis.title.x = element_text(vjust=-2), 61 | legend.position="none") 62 | 63 | -------------------------------------------------------------------------------- /Lecture3/.Rhistory: -------------------------------------------------------------------------------- 1 | polygon(c(xdate, rev(xdate)), c(down[ind,], rev(up[ind,])), col = 'grey80', border = NA) 2 | lines(xdate, coef[ind,], lwd = 2, type = "l") 3 | lines(xdate, down[ind,], col="black", lty = 'dashed') 4 | lines(xdate, up[ind,], col="black",lty = 'dashed') 5 | #### plot the momentum effect 6 | ind = 3 7 | par(mai = c(1.2, 1.3, 1.2, 1.2), mar = c(6, 7, 4, 2) + 0.1, mgp = c(5,2,0)) 8 | #par(mfrow = c(1,1)) 9 | plot(xdate, 10 | coef[ind,], xlab = "Time", ylab = "Momentum", 11 | type = "l" , lwd = 2, ylim = c(min(down[ind,])-0.05, max(up[ind,])+0.01), col = "black", 12 | cex.lab = 1.5, cex.axis = 1.5) 13 | polygon(c(xdate, rev(xdate)), c(down[ind,], rev(up[ind,])), col = 'grey80', border = NA) 14 | lines(xdate, coef[ind,], lwd = 2, type = "l") 15 | lines(xdate, down[ind,], col="black", lty = 'dashed') 16 | lines(xdate, up[ind,], col="black",lty = 'dashed') 17 | coef 18 | win 19 | library(reshape2) 20 | library(lubridate) 21 | library(quantreg) 22 | setwd("F:/netQuantile/data/") 23 | source("F:/OneDrive/quantileReg/code/realData/realDataFunc.R") 24 | source("F:/OneDrive/quantileReg/code/estimator.R") 25 | #### Stock price information 26 | load("rda/stockDateP.rda") ### daily price 27 | load("rda/stockALs.rda") ### stock network 28 | load( "rda/stockwL.rda") ### weekly price 29 | load( "rda/weekDatesL.rda") ### the first date in a week (for plot) 30 | load( "rda/stockWeekAll.rda") 31 | load( "rda/weekdate.rda") 32 | #### covariate information 33 | load( "rda/stockYearCap.rda") ### market value 34 | load( "rda/stockYearBook.rda") ### equity 35 | load( "rda/stockYearAsset.rda") ### Asset 36 | load( "rda/stockYearLev.rda") ### leverage 37 | load( "rda/stockYearAssetR.rda") ### increasing asset ratio 38 | load( "rda/stockYearIncome.rda") ### profit 39 | load( "rda/stockYearcashflow.rda") ### cash flow 40 | specify_decimal = function(x, k) gsub('\\s+','',format(round(x, k), nsmall=k)) ### format function for keeping 2 digits after 41 | ############################################################################# 42 | ##### analyze the dataset 43 | win = 48 #### set the window size (48 weeks) 44 | yys = 1990:2015 45 | years = rep(2007:2015, sapply(stockwL[(2007:2015)-1990+1], ncol)); length(years) 46 | coef = matrix(0, nrow = 9, ncol = ncol(stockWeekAll)-win) 47 | SE = matrix(0, nrow = 9, ncol = ncol(stockWeekAll)-win) 48 | tau = c(0.05) 49 | for (i in 1:(ncol(stockWeekAll)-win)) 50 | { 51 | cat("i: ", i, "\r") 52 | yy = years[i] 53 | ##### stock time series 54 | stockWeek = stockWeekAll[,i:(i+win)] 55 | stockNa = rowSums(is.na(stockWeek)) 56 | #free_week = risk_free_week[(i+1):(i+win)] 57 | ##### fanancial fundamental index 58 | cap_sub = stockYearCap[,which(yys==yy)] 59 | equity_sub = stockYearBook[, which(yys==yy)] 60 | profit_sub = stockYearIncome[, which(yys==yy)] 61 | assetR_sub = stockYearAssetR[, which(yys==yy)] 62 | cash_sub = stockYearcashflow[, which(yys==yy)] 63 | lev_sub = stockYearLev[, which(yys==yy)] 64 | stockAL = stockALs[[yy-1990+1]] 65 | Amat = stockAL$Amat 66 | ##### keep the stock which has no NA during the period 67 | ids1 = Reduce(intersect, list(stockDateP$SECUCODE[which(stockNa==0)], 68 | stockAL$IDs[[1]], 69 | stockYearCap$SECUCODE[!is.na(cap_sub)], 70 | stockYearBook$SECUCODE[!is.na(equity_sub)], 71 | stockYearIncome$SECUCODE[!is.na(profit_sub)], 72 | stockYearAssetR$SECUCODE[!is.na(assetR_sub)], 73 | stockYearcashflow$SECUCODE[!is.na(cash_sub)], 74 | stockYearLev$SECUCODE[!is.na(lev_sub)])) 75 | ##### keep the covariates 76 | stockWeek1 = stockWeek[match(ids1, stockDateP$SECUCODE),] 77 | Amat1 = Amat[match(ids1,stockAL$IDs[[1]]), match(ids1,stockAL$IDs[[1]])] 78 | cap_sub1 = cap_sub[match(ids1, stockYearCap$SECUCODE)] 79 | equity_sub1 = equity_sub[match(ids1, stockYearBook$SECUCODE)] 80 | profit_sub1 = profit_sub[match(ids1, stockYearIncome$SECUCODE)] 81 | assetR_sub1 = assetR_sub[match(ids1, stockYearAssetR$SECUCODE)] 82 | cash_sub1 = cash_sub[match(ids1, stockYearcashflow$SECUCODE)] 83 | lev_sub1 = lev_sub[match(ids1, stockYearLev$SECUCODE)] 84 | bm_ratio = equity_sub1/cap_sub1 85 | profit_ratio = profit_sub1/equity_sub1 86 | ##### calculate the returns 87 | logP = log(stockWeek1) 88 | logPDiff = logP[,-1]-logP[,-ncol(logP)] 89 | logPDiff = (stockWeek1[,-1] - stockWeek1[,-ncol(logP)])/stockWeek1[,-ncol(logP)] 90 | #logPDiff = t(apply(logPDiff, 1, "-", free_week)) 91 | W = Amat1/rowSums(Amat1) 92 | W[is.na(W)] = 0 93 | ##### shift the covariates to be positive 94 | Z = cbind(cap = log(cap_sub1/10^8) +1 , bm_ratio = bm_ratio*1000 +1.5, 95 | profit_ratio = profit_ratio/10 + 7, asset_ratio = assetR_sub1/10+0.1, 96 | cash = cash_sub1/10^8+0.15, lev = lev_sub1/10 +0.02) 97 | ##### fit the NQAR model 98 | ThetaEstQuant = netQuantile(Ymat = (logPDiff)+1, 99 | W, Z = Z, 100 | tau = tau) 101 | coef[,i] = ThetaEstQuant$coefficients 102 | SE[,i] = ThetaEstQuant$se 103 | } 104 | #### the up and down 95 CI 105 | up = coef + 1.96*SE 106 | down = coef - 1.96*SE 107 | xdate = weekdate[1:(ncol(stockWeekAll)-win)] 108 | #### plot the network effect 109 | ind = 2 110 | par(mai = c(1.2, 1.3, 1.2, 1.2), mar = c(6, 7, 4, 2) + 0.1, mgp = c(5,2,0)) 111 | par(mfrow = c(1,2)) 112 | plot(xdate, 113 | coef[ind,], xlab = "Time", ylab = "Network", 114 | type = "l" , lwd = 2, ylim = c(min(down[ind,])-0.01, max(up[ind,])+0.01), col = "black", 115 | cex.lab = 1.5, cex.axis = 2) 116 | polygon(c(xdate, rev(xdate)), c(down[ind,], rev(up[ind,])), col = 'grey80', border = NA) 117 | lines(xdate, coef[ind,], lwd = 2, type = "l") 118 | lines(xdate, down[ind,], col="black", lty = 'dashed') 119 | lines(xdate, up[ind,], col="black",lty = 'dashed') 120 | #### plot the momentum effect 121 | ind = 3 122 | par(mai = c(1.2, 1.3, 1.2, 1.2), mar = c(6, 7, 4, 2) + 0.1, mgp = c(5,2,0)) 123 | #par(mfrow = c(1,1)) 124 | plot(xdate, 125 | coef[ind,], xlab = "Time", ylab = "Momentum", 126 | type = "l" , lwd = 2, ylim = c(min(down[ind,])-0.05, max(up[ind,])+0.01), col = "black", 127 | cex.lab = 1.5, cex.axis = 1.5) 128 | polygon(c(xdate, rev(xdate)), c(down[ind,], rev(up[ind,])), col = 'grey80', border = NA) 129 | lines(xdate, coef[ind,], lwd = 2, type = "l") 130 | lines(xdate, down[ind,], col="black", lty = 'dashed') 131 | lines(xdate, up[ind,], col="black",lty = 'dashed') 132 | win = 48 #### set the window size (48 weeks) 133 | yys = 1990:2015 134 | years = rep(2007:2015, sapply(stockwL[(2007:2015)-1990+1], ncol)); length(years) 135 | coef = matrix(0, nrow = 9, ncol = ncol(stockWeekAll)-win) 136 | SE = matrix(0, nrow = 9, ncol = ncol(stockWeekAll)-win) 137 | tau = c(0.05) 138 | for (i in 1:(ncol(stockWeekAll)-win)) 139 | { 140 | cat("i: ", i, "\r") 141 | yy = years[i] 142 | ##### stock time series 143 | stockWeek = stockWeekAll[,i:(i+win)] 144 | stockNa = rowSums(is.na(stockWeek)) 145 | #free_week = risk_free_week[(i+1):(i+win)] 146 | ##### fanancial fundamental index 147 | cap_sub = stockYearCap[,which(yys==yy)] 148 | equity_sub = stockYearBook[, which(yys==yy)] 149 | profit_sub = stockYearIncome[, which(yys==yy)] 150 | assetR_sub = stockYearAssetR[, which(yys==yy)] 151 | cash_sub = stockYearcashflow[, which(yys==yy)] 152 | lev_sub = stockYearLev[, which(yys==yy)] 153 | stockAL = stockALs[[yy-1990+1]] 154 | Amat = stockAL$Amat 155 | ##### keep the stock which has no NA during the period 156 | ids1 = Reduce(intersect, list(stockDateP$SECUCODE[which(stockNa==0)], 157 | stockAL$IDs[[1]], 158 | stockYearCap$SECUCODE[!is.na(cap_sub)], 159 | stockYearBook$SECUCODE[!is.na(equity_sub)], 160 | stockYearIncome$SECUCODE[!is.na(profit_sub)], 161 | stockYearAssetR$SECUCODE[!is.na(assetR_sub)], 162 | stockYearcashflow$SECUCODE[!is.na(cash_sub)], 163 | stockYearLev$SECUCODE[!is.na(lev_sub)])) 164 | ##### keep the covariates 165 | stockWeek1 = stockWeek[match(ids1, stockDateP$SECUCODE),] 166 | Amat1 = Amat[match(ids1,stockAL$IDs[[1]]), match(ids1,stockAL$IDs[[1]])] 167 | cap_sub1 = cap_sub[match(ids1, stockYearCap$SECUCODE)] 168 | equity_sub1 = equity_sub[match(ids1, stockYearBook$SECUCODE)] 169 | profit_sub1 = profit_sub[match(ids1, stockYearIncome$SECUCODE)] 170 | assetR_sub1 = assetR_sub[match(ids1, stockYearAssetR$SECUCODE)] 171 | cash_sub1 = cash_sub[match(ids1, stockYearcashflow$SECUCODE)] 172 | lev_sub1 = lev_sub[match(ids1, stockYearLev$SECUCODE)] 173 | bm_ratio = equity_sub1/cap_sub1 174 | profit_ratio = profit_sub1/equity_sub1 175 | ##### calculate the returns 176 | logP = log(stockWeek1) 177 | logPDiff = logP[,-1]-logP[,-ncol(logP)] 178 | logPDiff = (stockWeek1[,-1] - stockWeek1[,-ncol(logP)])/stockWeek1[,-ncol(logP)] 179 | #logPDiff = t(apply(logPDiff, 1, "-", free_week)) 180 | W = Amat1/rowSums(Amat1) 181 | W[is.na(W)] = 0 182 | ##### shift the covariates to be positive 183 | Z = cbind(cap = log(cap_sub1/10^8) +1 , bm_ratio = bm_ratio*1000 +1.5, 184 | profit_ratio = profit_ratio/10 + 7, asset_ratio = assetR_sub1/10+0.1, 185 | cash = cash_sub1/10^8+0.15, lev = lev_sub1/10 +0.02) 186 | ##### fit the NQAR model 187 | ThetaEstQuant = netQuantile(Ymat = (logPDiff)+0.8, 188 | W, Z = Z, 189 | tau = tau) 190 | coef[,i] = ThetaEstQuant$coefficients 191 | SE[,i] = ThetaEstQuant$se 192 | } 193 | #### t 194 | tau 195 | win = 48 #### set the window size (48 weeks) 196 | yys = 1990:2015 197 | years = rep(2007:2015, sapply(stockwL[(2007:2015)-1990+1], ncol)); length(years) 198 | coef = matrix(0, nrow = 9, ncol = ncol(stockWeekAll)-win) 199 | SE = matrix(0, nrow = 9, ncol = ncol(stockWeekAll)-win) 200 | tau = c(0.95) 201 | for (i in 1:(ncol(stockWeekAll)-win)) 202 | { 203 | cat("i: ", i, "\r") 204 | yy = years[i] 205 | ##### stock time series 206 | stockWeek = stockWeekAll[,i:(i+win)] 207 | stockNa = rowSums(is.na(stockWeek)) 208 | #free_week = risk_free_week[(i+1):(i+win)] 209 | ##### fanancial fundamental index 210 | cap_sub = stockYearCap[,which(yys==yy)] 211 | equity_sub = stockYearBook[, which(yys==yy)] 212 | profit_sub = stockYearIncome[, which(yys==yy)] 213 | assetR_sub = stockYearAssetR[, which(yys==yy)] 214 | cash_sub = stockYearcashflow[, which(yys==yy)] 215 | lev_sub = stockYearLev[, which(yys==yy)] 216 | stockAL = stockALs[[yy-1990+1]] 217 | Amat = stockAL$Amat 218 | ##### keep the stock which has no NA during the period 219 | ids1 = Reduce(intersect, list(stockDateP$SECUCODE[which(stockNa==0)], 220 | stockAL$IDs[[1]], 221 | stockYearCap$SECUCODE[!is.na(cap_sub)], 222 | stockYearBook$SECUCODE[!is.na(equity_sub)], 223 | stockYearIncome$SECUCODE[!is.na(profit_sub)], 224 | stockYearAssetR$SECUCODE[!is.na(assetR_sub)], 225 | stockYearcashflow$SECUCODE[!is.na(cash_sub)], 226 | stockYearLev$SECUCODE[!is.na(lev_sub)])) 227 | ##### keep the covariates 228 | stockWeek1 = stockWeek[match(ids1, stockDateP$SECUCODE),] 229 | Amat1 = Amat[match(ids1,stockAL$IDs[[1]]), match(ids1,stockAL$IDs[[1]])] 230 | cap_sub1 = cap_sub[match(ids1, stockYearCap$SECUCODE)] 231 | equity_sub1 = equity_sub[match(ids1, stockYearBook$SECUCODE)] 232 | profit_sub1 = profit_sub[match(ids1, stockYearIncome$SECUCODE)] 233 | assetR_sub1 = assetR_sub[match(ids1, stockYearAssetR$SECUCODE)] 234 | cash_sub1 = cash_sub[match(ids1, stockYearcashflow$SECUCODE)] 235 | lev_sub1 = lev_sub[match(ids1, stockYearLev$SECUCODE)] 236 | bm_ratio = equity_sub1/cap_sub1 237 | profit_ratio = profit_sub1/equity_sub1 238 | ##### calculate the returns 239 | logP = log(stockWeek1) 240 | logPDiff = logP[,-1]-logP[,-ncol(logP)] 241 | logPDiff = (stockWeek1[,-1] - stockWeek1[,-ncol(logP)])/stockWeek1[,-ncol(logP)] 242 | #logPDiff = t(apply(logPDiff, 1, "-", free_week)) 243 | W = Amat1/rowSums(Amat1) 244 | W[is.na(W)] = 0 245 | ##### shift the covariates to be positive 246 | Z = cbind(cap = log(cap_sub1/10^8) +1 , bm_ratio = bm_ratio*1000 +1.5, 247 | profit_ratio = profit_ratio/10 + 7, asset_ratio = assetR_sub1/10+0.1, 248 | cash = cash_sub1/10^8+0.15, lev = lev_sub1/10 +0.02) 249 | ##### fit the NQAR model 250 | ThetaEstQuant = netQuantile(Ymat = abs(logPDiff), 251 | W, Z = Z, 252 | tau = tau) 253 | coef[,i] = ThetaEstQuant$coefficients 254 | SE[,i] = ThetaEstQuant$se 255 | } 256 | tau = 0.95 257 | specify_decimal = function(x, k) gsub('\\s+','',format(round(x, k), nsmall=k)) ### format function for keeping 2 digits after 258 | yys = 1990:2015 259 | years = 2007:2015 260 | coef = matrix(0, nrow = 9, ncol = length(years)) 261 | SE = matrix(0, nrow = 9, ncol = length(years)) 262 | P_val = matrix(0, nrow = 9, ncol = length(years)) 263 | id_list = list() 264 | for (yy in years) 265 | { 266 | show(yy) 267 | stockWeek = stockwL[[yy-1990+1]] #### weekly stock price for every year 268 | weekNaR = apply(stockWeek, 2, function(x) mean(is.na(x))) #### na ratios 269 | stockWeek = stockWeek[,weekNaR<=0.5] #### delete the weeks with >50% percent NAs 270 | stockNa = rowSums(is.na(stockWeek)) 271 | #### the Covariates 272 | cap_sub = stockYearCap[,which(yys==yy)] 273 | equity_sub = stockYearBook[, which(yys==yy)] 274 | profit_sub = stockYearIncome[, which(yys==yy)] 275 | assetR_sub = stockYearAssetR[, which(yys==yy)] 276 | cash_sub = stockYearcashflow[, which(yys==yy)] 277 | lev_sub = stockYearLev[, which(yys==yy)] 278 | #### stock network 279 | stockAL = stockALs[[yy-1990+1]] 280 | Amat = stockAL$Amat 281 | #Amat = matrix(1, length(stockAL$IDs[[1]]), length(stockAL$IDs[[1]])); diag(Amat) = 0 282 | #### intersect ids (delelte stocks with NA obs) 283 | ids1 = Reduce(intersect, list(stockDateP$SECUCODE[which(stockNa==0)], 284 | stockAL$IDs[[1]], 285 | stockYearCap$SECUCODE[!is.na(cap_sub)], 286 | stockYearBook$SECUCODE[!is.na(equity_sub)], 287 | stockYearIncome$SECUCODE[!is.na(profit_sub)], 288 | stockYearAssetR$SECUCODE[!is.na(assetR_sub)], 289 | stockYearcashflow$SECUCODE[!is.na(cash_sub)], 290 | stockYearLev$SECUCODE[!is.na(lev_sub)])) 291 | id_list[[yy-2007+1]] = ids1 292 | #### extract the dataset according to the ids 293 | stockWeek1 = stockWeek[match(ids1, stockDateP$SECUCODE),] 294 | Amat1 = Amat[match(ids1,stockAL$IDs[[1]]), match(ids1,stockAL$IDs[[1]])] 295 | cap_sub1 = cap_sub[match(ids1, stockYearCap$SECUCODE)] 296 | equity_sub1 = equity_sub[match(ids1, stockYearBook$SECUCODE)] 297 | profit_sub1 = profit_sub[match(ids1, stockYearIncome$SECUCODE)] 298 | assetR_sub1 = assetR_sub[match(ids1, stockYearAssetR$SECUCODE)] 299 | cash_sub1 = cash_sub[match(ids1, stockYearcashflow$SECUCODE)] 300 | lev_sub1 = lev_sub[match(ids1, stockYearLev$SECUCODE)] 301 | bm_ratio = equity_sub1/cap_sub1 #### book to market ratio 302 | profit_ratio = profit_sub1/equity_sub1 303 | #### calculate the stock returns 304 | logP = log(stockWeek1) 305 | logPDiff = logP[,-1]-logP[,-ncol(logP)] 306 | logPDiff = (stockWeek1[,-1] - stockWeek1[,-ncol(logP)])/stockWeek1[,-ncol(logP)] 307 | W = Amat1/rowSums(Amat1) 308 | W[is.na(W)] = 0 309 | #### shift to positive values 310 | Z = cbind(cap = log(cap_sub1/10^8) +0.8 , bm_ratio = bm_ratio*1000 +1.5, 311 | profit_ratio = profit_ratio/10 + 7, asset_ratio = assetR_sub1/10+0.1, 312 | cash = cash_sub1/10^8+0.15, lev = lev_sub1/10 +0.02) 313 | #### conduct NQAR estimation 314 | ThetaEstQuant = netQuantile(Ymat = abs(logPDiff), 315 | W, Z = Z, 316 | tau = tau) 317 | #### save the estimation results 318 | ind = yy-2007+1 319 | coef[,ind] = ThetaEstQuant$coefficients 320 | SE[,ind] = ThetaEstQuant$se 321 | P_val[,ind] = ThetaEstQuant$p_val 322 | } 323 | coef 324 | win = 48 #### set the window size (48 weeks) 325 | yys = 1990:2015 326 | years = rep(2007:2015, sapply(stockwL[(2007:2015)-1990+1], ncol)); length(years) 327 | coef = matrix(0, nrow = 9, ncol = ncol(stockWeekAll)-win) 328 | SE = matrix(0, nrow = 9, ncol = ncol(stockWeekAll)-win) 329 | tau = c(0.95) 330 | for (i in 1:(ncol(stockWeekAll)-win)) 331 | { 332 | cat("i: ", i, "\r") 333 | yy = years[i] 334 | ##### stock time series 335 | stockWeek = stockWeekAll[,i:(i+win)] 336 | stockNa = rowSums(is.na(stockWeek)) 337 | #free_week = risk_free_week[(i+1):(i+win)] 338 | ##### fanancial fundamental index 339 | cap_sub = stockYearCap[,which(yys==yy)] 340 | equity_sub = stockYearBook[, which(yys==yy)] 341 | profit_sub = stockYearIncome[, which(yys==yy)] 342 | assetR_sub = stockYearAssetR[, which(yys==yy)] 343 | cash_sub = stockYearcashflow[, which(yys==yy)] 344 | lev_sub = stockYearLev[, which(yys==yy)] 345 | stockAL = stockALs[[yy-1990+1]] 346 | Amat = stockAL$Amat 347 | ##### keep the stock which has no NA during the period 348 | ids1 = Reduce(intersect, list(stockDateP$SECUCODE[which(stockNa==0)], 349 | stockAL$IDs[[1]], 350 | stockYearCap$SECUCODE[!is.na(cap_sub)], 351 | stockYearBook$SECUCODE[!is.na(equity_sub)], 352 | stockYearIncome$SECUCODE[!is.na(profit_sub)], 353 | stockYearAssetR$SECUCODE[!is.na(assetR_sub)], 354 | stockYearcashflow$SECUCODE[!is.na(cash_sub)], 355 | stockYearLev$SECUCODE[!is.na(lev_sub)])) 356 | ##### keep the covariates 357 | stockWeek1 = stockWeek[match(ids1, stockDateP$SECUCODE),] 358 | Amat1 = Amat[match(ids1,stockAL$IDs[[1]]), match(ids1,stockAL$IDs[[1]])] 359 | cap_sub1 = cap_sub[match(ids1, stockYearCap$SECUCODE)] 360 | equity_sub1 = equity_sub[match(ids1, stockYearBook$SECUCODE)] 361 | profit_sub1 = profit_sub[match(ids1, stockYearIncome$SECUCODE)] 362 | assetR_sub1 = assetR_sub[match(ids1, stockYearAssetR$SECUCODE)] 363 | cash_sub1 = cash_sub[match(ids1, stockYearcashflow$SECUCODE)] 364 | lev_sub1 = lev_sub[match(ids1, stockYearLev$SECUCODE)] 365 | bm_ratio = equity_sub1/cap_sub1 366 | profit_ratio = profit_sub1/equity_sub1 367 | ##### calculate the returns 368 | logP = log(stockWeek1) 369 | logPDiff = logP[,-1]-logP[,-ncol(logP)] 370 | logPDiff = (stockWeek1[,-1] - stockWeek1[,-ncol(logP)])/stockWeek1[,-ncol(logP)] 371 | #logPDiff = t(apply(logPDiff, 1, "-", free_week)) 372 | W = Amat1/rowSums(Amat1) 373 | W[is.na(W)] = 0 374 | ##### shift the covariates to be positive 375 | Z = cbind(cap = log(cap_sub1/10^8) +1 , bm_ratio = bm_ratio*1000 +1.5, 376 | profit_ratio = profit_ratio/10 + 7, asset_ratio = assetR_sub1/10+0.1, 377 | cash = cash_sub1/10^8+0.15, lev = lev_sub1/10 +0.02) 378 | ##### fit the NQAR model 379 | ThetaEstQuant = netQuantile(Ymat = abs(logPDiff), 380 | W, Z = Z, 381 | tau = tau) 382 | coef[,i] = ThetaEstQuant$coefficients 383 | SE[,i] = ThetaEstQuant$se 384 | } 385 | #### the up and down 95 CI 386 | up = coef + 1.96*SE 387 | down = coef - 1.96*SE 388 | xdate = weekdate[1:(ncol(stockWeekAll)-win)] 389 | #### the up and down 95 CI 390 | up = coef + 1.96*SE 391 | down = coef - 1.96*SE 392 | xdate = weekdate[1:(ncol(stockWeekAll)-win)] 393 | #### plot the network effect 394 | ind = 2 395 | par(mai = c(1.2, 1.3, 1.2, 1.2), mar = c(6, 7, 4, 2) + 0.1, mgp = c(5,2,0)) 396 | par(mfrow = c(1,2)) 397 | plot(xdate, 398 | coef[ind,], xlab = "Time", ylab = "Network", 399 | type = "l" , lwd = 2, ylim = c(min(down[ind,])-0.01, max(up[ind,])+0.01), col = "black", 400 | cex.lab = 1.5, cex.axis = 2) 401 | polygon(c(xdate, rev(xdate)), c(down[ind,], rev(up[ind,])), col = 'grey80', border = NA) 402 | lines(xdate, coef[ind,], lwd = 2, type = "l") 403 | lines(xdate, down[ind,], col="black", lty = 'dashed') 404 | lines(xdate, up[ind,], col="black",lty = 'dashed') 405 | #### plot the momentum effect 406 | ind = 3 407 | par(mai = c(1.2, 1.3, 1.2, 1.2), mar = c(6, 7, 4, 2) + 0.1, mgp = c(5,2,0)) 408 | #par(mfrow = c(1,1)) 409 | plot(xdate, 410 | coef[ind,], xlab = "Time", ylab = "Momentum", 411 | type = "l" , lwd = 2, ylim = c(min(down[ind,])-0.05, max(up[ind,])+0.01), col = "black", 412 | cex.lab = 1.5, cex.axis = 1.5) 413 | polygon(c(xdate, rev(xdate)), c(down[ind,], rev(up[ind,])), col = 'grey80', border = NA) 414 | lines(xdate, coef[ind,], lwd = 2, type = "l") 415 | lines(xdate, down[ind,], col="black", lty = 'dashed') 416 | lines(xdate, up[ind,], col="black",lty = 'dashed') 417 | mean(coef[2,]) 418 | mean(coef[3,]) 419 | paste0("rda/coef", specify_decimal(tau*100, 0), ".rda") 420 | save(coef, file = paste0("rda/CIcoef", specify_decimal(tau*100, 0), ".rda")) 421 | save(SE, file = paste0("rda/CISE", specify_decimal(tau*100, 0), ".rda")) 422 | win = 48 #### set the window size (48 weeks) 423 | yys = 1990:2015 424 | years = rep(2007:2015, sapply(stockwL[(2007:2015)-1990+1], ncol)); length(years) 425 | coef = matrix(0, nrow = 9, ncol = ncol(stockWeekAll)-win) 426 | SE = matrix(0, nrow = 9, ncol = ncol(stockWeekAll)-win) 427 | tau = c(0.05) 428 | for (i in 1:(ncol(stockWeekAll)-win)) 429 | { 430 | cat("i: ", i, "\r") 431 | yy = years[i] 432 | ##### stock time series 433 | stockWeek = stockWeekAll[,i:(i+win)] 434 | stockNa = rowSums(is.na(stockWeek)) 435 | #free_week = risk_free_week[(i+1):(i+win)] 436 | ##### fanancial fundamental index 437 | cap_sub = stockYearCap[,which(yys==yy)] 438 | equity_sub = stockYearBook[, which(yys==yy)] 439 | profit_sub = stockYearIncome[, which(yys==yy)] 440 | assetR_sub = stockYearAssetR[, which(yys==yy)] 441 | cash_sub = stockYearcashflow[, which(yys==yy)] 442 | lev_sub = stockYearLev[, which(yys==yy)] 443 | stockAL = stockALs[[yy-1990+1]] 444 | Amat = stockAL$Amat 445 | ##### keep the stock which has no NA during the period 446 | ids1 = Reduce(intersect, list(stockDateP$SECUCODE[which(stockNa==0)], 447 | stockAL$IDs[[1]], 448 | stockYearCap$SECUCODE[!is.na(cap_sub)], 449 | stockYearBook$SECUCODE[!is.na(equity_sub)], 450 | stockYearIncome$SECUCODE[!is.na(profit_sub)], 451 | stockYearAssetR$SECUCODE[!is.na(assetR_sub)], 452 | stockYearcashflow$SECUCODE[!is.na(cash_sub)], 453 | stockYearLev$SECUCODE[!is.na(lev_sub)])) 454 | ##### keep the covariates 455 | stockWeek1 = stockWeek[match(ids1, stockDateP$SECUCODE),] 456 | Amat1 = Amat[match(ids1,stockAL$IDs[[1]]), match(ids1,stockAL$IDs[[1]])] 457 | cap_sub1 = cap_sub[match(ids1, stockYearCap$SECUCODE)] 458 | equity_sub1 = equity_sub[match(ids1, stockYearBook$SECUCODE)] 459 | profit_sub1 = profit_sub[match(ids1, stockYearIncome$SECUCODE)] 460 | assetR_sub1 = assetR_sub[match(ids1, stockYearAssetR$SECUCODE)] 461 | cash_sub1 = cash_sub[match(ids1, stockYearcashflow$SECUCODE)] 462 | lev_sub1 = lev_sub[match(ids1, stockYearLev$SECUCODE)] 463 | bm_ratio = equity_sub1/cap_sub1 464 | profit_ratio = profit_sub1/equity_sub1 465 | ##### calculate the returns 466 | logP = log(stockWeek1) 467 | logPDiff = logP[,-1]-logP[,-ncol(logP)] 468 | logPDiff = (stockWeek1[,-1] - stockWeek1[,-ncol(logP)])/stockWeek1[,-ncol(logP)] 469 | #logPDiff = t(apply(logPDiff, 1, "-", free_week)) 470 | W = Amat1/rowSums(Amat1) 471 | W[is.na(W)] = 0 472 | ##### shift the covariates to be positive 473 | Z = cbind(cap = log(cap_sub1/10^8) +1 , bm_ratio = bm_ratio*1000 +1.5, 474 | profit_ratio = profit_ratio/10 + 7, asset_ratio = assetR_sub1/10+0.1, 475 | cash = cash_sub1/10^8+0.15, lev = lev_sub1/10 +0.02) 476 | ##### fit the NQAR model 477 | ThetaEstQuant = netQuantile(Ymat = abs(logPDiff), 478 | W, Z = Z, 479 | tau = tau) 480 | coef[,i] = ThetaEstQuant$coefficients 481 | SE[,i] = ThetaEstQuant$se 482 | } 483 | save(coef, file = paste0("rda/CIcoef", specify_decimal(tau*100, 0), ".rda")) 484 | save(SE, file = paste0("rda/CISE", specify_decimal(tau*100, 0), ".rda")) 485 | library(showtext) 486 | library(ggplot2) 487 | ### read data 488 | yitian = readLines("倚天屠龙记.Txt") 489 | yitian[1:10] 490 | setwd("F:/OneDrive/熊学院R课程/R语千寻/Lecture3") 491 | library(showtext) 492 | library(ggplot2) 493 | ### read data 494 | yitian = readLines("倚天屠龙记.Txt") 495 | yitian[1:10] 496 | #### cut paragraph 497 | para_head = grep("\\s+", yitian) 498 | cut_para1 = cbind(para_head[1:(length(para_head)-1)], para_head[-1]-1) 499 | yitian_para = sapply(1:nrow(cut_para1), function(i) paste(yitian[cut_para1[i,1]:cut_para1[i,2]], collapse = "")) 500 | yitian_para[1:4] 501 | #### read in main roles 502 | roles = readLines("主角名单.txt") 503 | roles[1:5] 504 | roles = readLines("主角名单.txt") 505 | roles[1:5] 506 | #### cut paragraph 507 | para_head = grep("\\s+", yitian) 508 | cut_para1 = cbind(para_head[1:(length(para_head)-1)], para_head[-1]-1) 509 | yitian_para = sapply(1:nrow(cut_para1), function(i) paste(yitian[cut_para1[i,1]:cut_para1[i,2]], collapse = "")) 510 | yitian_para[1:4] 511 | tau 512 | tau = 0.95 513 | -------------------------------------------------------------------------------- /Lecture3/stop.txt: -------------------------------------------------------------------------------- 1 | 的 2 | 了 3 | 他 4 | 她 5 | 啊 6 | 呀 7 | -------------------------------------------------------------------------------- /Lecture3/yitian.R: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/BearAcademy/R-Tutorial/ce00889917d3839ba977d0e39cb1c76ce43f9a9f/Lecture3/yitian.R -------------------------------------------------------------------------------- /Lecture3/yitian_split.bin: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/BearAcademy/R-Tutorial/ce00889917d3839ba977d0e39cb1c76ce43f9a9f/Lecture3/yitian_split.bin -------------------------------------------------------------------------------- /Lecture3/yitian_split.txt: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/BearAcademy/R-Tutorial/ce00889917d3839ba977d0e39cb1c76ce43f9a9f/Lecture3/yitian_split.txt -------------------------------------------------------------------------------- /Lecture3/主角名单.txt: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/BearAcademy/R-Tutorial/ce00889917d3839ba977d0e39cb1c76ce43f9a9f/Lecture3/主角名单.txt -------------------------------------------------------------------------------- /Lecture3/倚天屠龙记.Txt: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/BearAcademy/R-Tutorial/ce00889917d3839ba977d0e39cb1c76ce43f9a9f/Lecture3/倚天屠龙记.Txt -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # R-Tutorial 2 | Codes for channel "R语千寻" in the BearAcademy. 3 | - Lecture 1: 用R做柱状图:2016,哪些明星最忙? 4 | - Lecture 2: 也谈 ggplot2 5 | - Lecture 3: 张无忌究竟爱谁? 6 | --------------------------------------------------------------------------------