jiedian <- read.csv("E:\\e-program\\在用数据\\edata综合版-去掉金融业(least).csv")
eco <- read.csv("E:\\e-program\\在用数据\\经济数据-2.csv")
jiedian$年月 <- seq(as.Date("2016-01-01"),length = 83,by = "1 month" )
eco$年月 <- seq(as.Date("2016-01-01"),length = 83,by = "1 month" )
library(dplyr)
## Warning: 程辑包'dplyr'是用R版本4.2.1 来建造的
##
## 载入程辑包:'dplyr'
## The following objects are masked from 'package:stats':
##
## filter, lag
## The following objects are masked from 'package:base':
##
## intersect, setdiff, setequal, union
library(ggplot2)
library(forecast)
## Registered S3 method overwritten by 'quantmod':
## method from
## as.zoo.data.frame zoo
library(tidyverse)
## Warning: 程辑包'tidyverse'是用R版本4.2.2 来建造的
## -- Attaching packages --------------------------------------- tidyverse 1.3.2 --
## v tibble 3.1.6 v purrr 0.3.5
## v tidyr 1.2.1 v stringr 1.4.1
## v readr 2.1.3 v forcats 0.5.2
## Warning: 程辑包'tidyr'是用R版本4.2.2 来建造的
## Warning: 程辑包'readr'是用R版本4.2.2 来建造的
## Warning: 程辑包'purrr'是用R版本4.2.2 来建造的
## Warning: 程辑包'stringr'是用R版本4.2.1 来建造的
## Warning: 程辑包'forcats'是用R版本4.2.2 来建造的
## -- Conflicts ------------------------------------------ tidyverse_conflicts() --
## x dplyr::filter() masks stats::filter()
## x dplyr::lag() masks stats::lag()
library(lubridate)
## Warning: 程辑包'lubridate'是用R版本4.2.2 来建造的
## 载入需要的程辑包:timechange
## Warning: 程辑包'timechange'是用R版本4.2.2 来建造的
##
## 载入程辑包:'lubridate'
##
## The following objects are masked from 'package:base':
##
## date, intersect, setdiff, union
data_classify <- function(data,i){
a <- data %>%
filter(`index1`==i) %>%
select(`年月`,`业扩类型`,`接电容量`,`申请容量`,`一级行业`)
return(a)
}
Eco_classify <- function(data,i){
a <- data %>%
filter(`index2`==i)%>%
select(`年月`,`经济数值`,`经济指标`)
return(a)
}
moving_cor <- function(start_date,data,window_length){
data %>%
filter(between(`年月`,start_date-months(window_length),start_date+months(window_length))) %>%
select(`接电容量`,`申请容量`,`业扩类型`,`经济数值`) %>%
group_by(`业扩类型`) %>%
summarise(
`年月` = start_date,
`业扩类型` = first(`业扩类型`),
`接电相关系数`=cor(`接电容量`,`经济数值`),
`申请相关系数`=cor(`申请容量`,`经济数值`))
}
plot_pearson1 <- function(data,window_length){
start_data <- seq(as.Date("2016-06-01"),length = 78,by = "1 month" )
b <- map_dfr(start_data,~(moving_cor(.x,data,window_length)))
i = data$一级行业[1]
j = data$经济指标[1]
m = 2*window_length
k = paste(i,"-",j,"窗口数为",m)
p <- b %>%
ggplot()+
geom_line(aes(`年月`,`接电相关系数`))+
geom_hline(yintercept = 0.5, linetype = "dashed")+
geom_hline(yintercept = 0.8, linetype = "dashed")+
geom_hline(yintercept = -0.5, linetype = "dashed")+
geom_hline(yintercept = -0.8, linetype = "dashed")+
facet_wrap(~`业扩类型`)+
ylim(-1,1)+
labs(x = "日期")+
labs(title = bquote(''*.(k)))
return(p)
}
plot_pearson2 <- function(data,window_length){
start_data <- seq(as.Date("2016-06-01"),length = 78,by = "1 month" )
b <- map_dfr(start_data,~(moving_cor(.x,data,window_length)))
i = data$一级行业[1]
j = data$经济指标[1]
m = 2*window_length
k = paste(i,"-",j,"窗口数为",m)
p <- b %>%
ggplot()+
geom_line(aes(`年月`,`申请相关系数`))+
geom_hline(yintercept = 0.5, linetype = "dashed")+
geom_hline(yintercept = 0.8, linetype = "dashed")+
geom_hline(yintercept = -0.5, linetype = "dashed")+
geom_hline(yintercept = -0.8, linetype = "dashed")+
facet_wrap(~`业扩类型`)+
ylim(-1,1)+
labs(x = "日期")+
labs(title = bquote(''*.(k)))
return(p)
}
# q <- Eco_classify(eco,1)
# # for (i in c(0:10)) {
# p <- data_classify(jiedian,1)
# pq <- merge(p,q,by="年月",all=T)
# plot_pearson1(pq,5)
# # plot_pearson2(pq,5)
# # }
for (j in c(1:7)) {
q <- Eco_classify(eco,j)
for (i in c(0:10)) {
p <- data_classify(jiedian,i)
# head(p)
pq <- merge(p,q,by="年月",all=T)
a <- plot_pearson1(pq,5)
print(a)
b <- plot_pearson2(pq,5)
print(b)
}
}


























































































































































# p <- data_classify(jiedian,2)
# pq <- merge(p,q,by="年月",all=T)
# plot_pearson1(pq,5)