科技主题基金数据分析

科技基金数据分析

  1. 基金的收益分布分析

  2. 建立风险测度模型

一、数据预处理

#设置工作路径
setwd("D:/LengPY")
#导入数据
library(readxl)
lqydata<-read_excel("lqydata.xlsx")
head(lqydata)
A tibble: 6 × 25
基金代码基金名称投资类型类别成立时间基金经理累计从业时间基金净值基金规模阿尔法系数贝塔系数...近一周近一月近三月近六月今年来近一年近两年近三年晨星风险系数风险评级
<chr><chr><chr><chr><dbl><dbl><chr><dbl><dbl><dbl>...<dbl><dbl><dbl><dbl><dbl><dbl><dbl><dbl><dbl><chr>
000209信诚新兴产业混合混合型新能源 201362.9870 4.1116.041.15... 8.3013.23-12.0242.51 3.25107.57143.24117.2416.51
000336农银研究精选混合混合型新能源 201343.535630.4327.361.16... 4.61 6.57-13.8924.72 -3.66119.62203.30183.6014.44
000409鹏华环保产业股票股票型新能源 201433.888013.8720.730.98... 5.80 9.92-15.9721.01 -6.74 87.01134.78141.1912.56中高
000432中银优秀企业混合混合型人工智能201432.1320 0.23 4.330.79... 3.29 5.70 -6.20-2.60 -5.29 11.22 31.85 44.3510.35中高
000432中银优秀企业混合混合型国产软件201432.1320 0.23 4.330.79... 3.29 5.70 -6.20-2.60 -5.29 11.22 31.85 44.3510.35中高
000535长盛航天海工装备混合型无人机 201451.3400 3.16 4.980.97...-0.52-0.96-20.94 1.43-18.38 39.71 40.87 43.6911.05
summary(lqydata)
   基金代码           基金名称           投资类型             类别          
 Length:102         Length:102         Length:102         Length:102        
 Class :character   Class :character   Class :character   Class :character  
 Mode  :character   Mode  :character   Mode  :character   Mode  :character  
                                                                            
                                                                            
                                                                            
    成立时间    基金经理累计从业时间   基金净值            基金规模      
 Min.   :2003   Min.   : 1.000       Length:102         Min.   :  0.090  
 1st Qu.:2014   1st Qu.: 3.000       Class :character   1st Qu.:  2.505  
 Median :2015   Median : 5.000       Mode  :character   Median :  5.035  
 Mean   :2014   Mean   : 4.941                          Mean   : 19.211  
 3rd Qu.:2016   3rd Qu.: 6.000                          3rd Qu.: 12.797  
 Max.   :2018   Max.   :11.000                          Max.   :271.100  
   阿尔法系数         贝塔系数          R平方           标准差     
 Min.   :-10.100   Min.   :0.7100   Min.   : 4.79   Min.   :18.22  
 1st Qu.:  1.627   1st Qu.:0.8525   1st Qu.:59.33   1st Qu.:25.27  
 Median :  5.385   Median :0.9700   Median :75.25   Median :27.25  
 Mean   :  6.947   Mean   :1.0204   Mean   :73.52   Mean   :27.20  
 3rd Qu.: 12.002   3rd Qu.:1.1300   3rd Qu.:92.53   3rd Qu.:29.52  
 Max.   : 27.360   Max.   :2.8200   Max.   :99.20   Max.   :36.01  
    夏普比率          回报率          日增长率           近一周      
 Min.   :-0.110   Min.   : -9.42   Min.   :-1.1900   Min.   :-1.190  
 1st Qu.: 0.320   1st Qu.: 15.38   1st Qu.:-0.2775   1st Qu.: 2.170  
 Median : 0.450   Median : 29.96   Median : 0.2800   Median : 3.370  
 Mean   : 0.515   Mean   : 37.73   Mean   : 0.3211   Mean   : 3.601  
 3rd Qu.: 0.715   3rd Qu.: 57.66   3rd Qu.: 0.9500   3rd Qu.: 5.420  
 Max.   : 1.200   Max.   :117.37   Max.   : 1.8400   Max.   : 8.960  
     近一月           近三月            近六月            今年来       
 Min.   :-2.250   Min.   :-25.090   Min.   :-13.900   Min.   :-22.130  
 1st Qu.: 4.940   1st Qu.:-15.625   1st Qu.: -1.275   1st Qu.: -6.885  
 Median : 6.755   Median :-11.435   Median :  4.265   Median : -4.125  
 Mean   : 6.610   Mean   :-12.630   Mean   :  7.434   Mean   : -5.142  
 3rd Qu.: 8.910   3rd Qu.: -9.592   3rd Qu.: 16.517   3rd Qu.: -1.090  
 Max.   :15.560   Max.   : -5.060   Max.   : 42.510   Max.   :  7.340  
     近一年           近两年           近三年        晨星风险系数  
 Min.   :-11.74   Min.   : -1.31   Min.   : -4.18   Min.   :10.35  
 1st Qu.: 15.41   1st Qu.: 41.02   1st Qu.: 30.46   1st Qu.:13.78  
 Median : 29.32   Median : 53.62   Median : 47.91   Median :15.21  
 Mean   : 36.85   Mean   : 64.63   Mean   : 61.63   Mean   :15.55  
 3rd Qu.: 54.02   3rd Qu.: 87.98   3rd Qu.: 86.92   3rd Qu.:16.20  
 Max.   :119.62   Max.   :203.30   Max.   :183.60   Max.   :31.01  
   风险评级        
 Length:102        
 Class :character  
 Mode  :character  
#检查是否有缺失值
## 可视化查看数据是否有缺失值
library(VIM)
aggr(lqydata)

在这里插入图片描述

## complete.cases()输出样例是否包含缺失值
## 输出包含缺失值的样例
mynadata <- lqydata[!complete.cases(lqydata),]
dim(mynadata)
  1. 0
  2. 25
## matrixplot()可视化缺失值的详细情况
## 红色代表缺失数据的情况
matrixplot(mynadata)  

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-Xc340Xnr-1620132051399)(output_9_0.png)]

分析可知,是由于新成立基金公司缺失值验证,可直接删除对应样本可得:

在这里插入图片描述

head(lqydata)
A tibble: 6 × 25
基金代码基金名称投资类型类别成立时间基金经理累计从业时间基金净值基金规模阿尔法系数贝塔系数...近一周近一月近三月近六月今年来近一年近两年近三年晨星风险系数风险评级
<chr><chr><chr><chr><dbl><dbl><chr><dbl><dbl><dbl>...<dbl><dbl><dbl><dbl><dbl><dbl><dbl><dbl><dbl><chr>
000209信诚新兴产业混合混合型新能源 201362.9870 4.1116.041.15... 8.3013.23-12.0242.51 3.25107.57143.24117.2416.51
000336农银研究精选混合混合型新能源 201343.535630.4327.361.16... 4.61 6.57-13.8924.72 -3.66119.62203.30183.6014.44
000409鹏华环保产业股票股票型新能源 201433.888013.8720.730.98... 5.80 9.92-15.9721.01 -6.74 87.01134.78141.1912.56中高
000432中银优秀企业混合混合型人工智能201432.1320 0.23 4.330.79... 3.29 5.70 -6.20-2.60 -5.29 11.22 31.85 44.3510.35中高
000432中银优秀企业混合混合型国产软件201432.1320 0.23 4.330.79... 3.29 5.70 -6.20-2.60 -5.29 11.22 31.85 44.3510.35中高
000535长盛航天海工装备混合型无人机 201451.3400 3.16 4.980.97...-0.52-0.96-20.94 1.43-18.38 39.71 40.87 43.6911.05

二、基础分布描述性分析

2.1 R平方与风险系数散点分布图

#(a) 二维散点与统计直方图
library(ggplot2)
library(RColorBrewer)
library(ggpubr)
ggscatterhist(
  lqydata, x ='R平方', y = '晨星风险系数', shape=21,fill="#00AFBB",color = "black",size = 3, alpha = 1,
  #palette = c("#00AFBB", "#E7B800", "#FC4E07"),
  margin.params = list( fill="#00AFBB",color = "black", size = 0.2,alpha=1),
  margin.plot =  "histogram",
  legend = c(0.8,0.8),
  ggtheme = theme_minimal())

在这里插入图片描述

table(lqydata$类别)
  半导体 国产软件 国产芯片 人工智能 生物识别   无人机   新能源 智能穿戴 
       6       24        7       13        7       14       24        7 
data1<-as.data.frame(table(lqydata$类别))
colnames(data1)<-c('type','num')
head(data1)
A data.frame: 6 × 2
typenum
<fct><int>
1半导体 6
2国产软件24
3国产芯片 7
4人工智能13
5生物识别 7
6无人机 14
#"半导体","国产软件","国产芯片","人工智能","生物识别","无人机"
#16,37,16,17,16,22
table(lqydata$成立时间)

2003 2004 2006 2007 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 
   2    1    1    1    2    5    7    1    2   20   32    8   17    3 
table(lqydata$投资类型)
  股票型 股票指数   混合型 联接基金 
      22       33       30       17 
lqydata$基金净值<-as.numeric( lqydata$基金净值) 

3.2 探索相关性

colnames(lqydata)
  1. '基金代码'
  2. '基金名称'
  3. '投资类型'
  4. '类别'
  5. '成立时间'
  6. '基金经理累计从业时间'
  7. '基金净值'
  8. '基金规模'
  9. '阿尔法系数'
  10. '贝塔系数'
  11. 'R平方'
  12. '标准差'
  13. '夏普比率'
  14. '回报率'
  15. '日增长率'
  16. '近一周'
  17. '近一月'
  18. '近三月'
  19. '近六月'
  20. '今年来'
  21. '近一年'
  22. '近两年'
  23. '近三年'
  24. '晨星风险系数'
  25. '风险评级'
mcor<-cor(lqydata[,c(6:24)])
library(corrplot) # 载入corrplot包
corrplot(mcor)

在这里插入图片描述

corrplot(mcor, method = "shade",shade.col = NA, tl.col ="black", tl.srt = 45, order = "AOE") # 绘制相关系数矩阵图

在这里插入图片描述

三、探索基金收益分布之间规律及其分布情况

3.1 不同主题基金规模分布对比

#转换为数值型
lqydata$基金规模<-as.numeric(lqydata$基金规模)
#不同主题基金规模之间对比
library(beanplot)
par(mai=c(0.5,0.5,0.25,1.2))
beanplot(基金规模~类别, lqydata,col = list("#FF6B5E", "#00C3C2"),
         side = "both",xlab ="city",ylab ="value")
Warning message:
"package 'beanplot' was built under R version 4.0.5"
log="y" selected

在这里插入图片描述

3.2 不同主题基金回报率分布

#回报率(一年)
#不同主题之间对比
library(beanplot)
par(mai=c(0.5,0.5,0.25,1.2))
beanplot(lqydata$回报率~lqydata$类别, lqydata,col = list("#FF6B5E", "#00C3C2"),
         side = "both",xlab ="city",ylab ="value")

在这里插入图片描述

3.3不同主题基金净值之间对比

#不同主题之间对比
library(beanplot)
par(mai=c(0.5,0.5,0.25,1.2))
beanplot(lqydata$基金净值~lqydata$类别, lqydata,col = list("#FF6B5E", "#00C3C2"),
         side = "both",xlab ="city",ylab ="value")
log="y" selected

在这里插入图片描述

colnames(lqydata)
  1. '基金代码'
  2. '基金名称'
  3. '投资类型'
  4. '类别'
  5. '成立时间'
  6. '基金经理累计从业时间'
  7. '基金净值'
  8. '基金规模'
  9. '阿尔法系数(%)'
  10. '贝塔系数'
  11. 'R平方'
  12. '标准差(%)'
  13. '夏普比率'
  14. '回报率'
  15. '日增长率'
  16. '近一周'
  17. '近一月'
  18. '近三月'
  19. '近六月'
  20. '今年来'
  21. '近一年'
  22. '近两年'
  23. '近三年'
  24. '晨星风险系数'
  25. '风险评级'

3.4 不同主题基金回报率分布

#一年回报率
library(reshape2)
library(ggplot2)
library(ggridges)
library(RColorBrewer)
colormap <- colorRampPalette(rev(brewer.pal(11,'Spectral')))(32)
dt<-lqydata[,c(4,14)]
colnames(dt)<-c("主题","收益率")
splitdata<-split(dt,dt$主题)
xmax<-max(dt$`收益率`)*1.1
xmin<-min(dt$`收益率`)*1.1


N<-length(splitdata)
labels_y<-names(splitdata)

mydata<-data.frame(x=numeric(),y=numeric(),variable=numeric()) #?????յ?Data.Frame

for (i in 1:N){
  tempy<-density(splitdata[[i]][2]$`收益率`,bw = 3.37,from=xmin, to=xmax)
  newdata<-data.frame(x=tempy$x,y=tempy$y)
  newdata$variable<-i
  mydata<-rbind(mydata,newdata)
}

Step<-max(mydata$y)*0.6
mydata$offest<--as.numeric(mydata$variable)*Step
mydata$V1_density_offest<-mydata$y+mydata$offest

p<-ggplot()
for (i in 1:N){
  p<-p+ geom_linerange(data=mydata[mydata$variable==i,],aes(x=x,ymin=offest,ymax=V1_density_offest,group=variable,color=y),size =1, alpha =1) +
        geom_line(data=mydata[mydata$variable==i,],aes(x=x, y=V1_density_offest),color="black",size=0.5)
}

p+scale_color_gradientn(colours=colormap,name="Density")+
  scale_y_continuous(breaks=seq(-Step,-Step*N,-Step),labels=labels_y)+
  xlab("回报率")+
  ylab("主题")+
  theme_classic()+
  theme(
    panel.background=element_rect(fill="white",colour=NA),
    panel.grid.major.x = element_line(colour = "grey80",size=.25),
    panel.grid.major.y = element_line(colour = "grey60",size=.25),
    axis.line = element_blank(),
    text=element_text(size=15,colour = "black"),
    plot.title=element_text(size=15,hjust=.5),
    legend.position="right"
  )

在这里插入图片描述

3.5 近一年增长率分布情况

#近一年收益率分布情况
library(reshape2)
library(ggplot2)
library(ggridges)
library(RColorBrewer)
colormap <- colorRampPalette(rev(brewer.pal(11,'Spectral')))(32)
dt<-lqydata[,c(4,20)]
colnames(dt)<-c("主题","收益率")
splitdata<-split(dt,dt$主题)
xmax<-max(dt$`收益率`)*1.1
xmin<-min(dt$`收益率`)*1.1


N<-length(splitdata)
labels_y<-names(splitdata)

mydata<-data.frame(x=numeric(),y=numeric(),variable=numeric()) #?????յ?Data.Frame

for (i in 1:N){
  tempy<-density(splitdata[[i]][2]$`收益率`,bw = 3.37,from=xmin, to=xmax)
  newdata<-data.frame(x=tempy$x,y=tempy$y)
  newdata$variable<-i
  mydata<-rbind(mydata,newdata)
}

Step<-max(mydata$y)*0.6
mydata$offest<--as.numeric(mydata$variable)*Step
mydata$V1_density_offest<-mydata$y+mydata$offest

p<-ggplot()
for (i in 1:N){
  p<-p+ geom_linerange(data=mydata[mydata$variable==i,],aes(x=x,ymin=offest,ymax=V1_density_offest,group=variable,color=y),size =1, alpha =1) +
        geom_line(data=mydata[mydata$variable==i,],aes(x=x, y=V1_density_offest),color="black",size=0.5)
}

p+scale_color_gradientn(colours=colormap,name="Density")+
  scale_y_continuous(breaks=seq(-Step,-Step*N,-Step),labels=labels_y)+
  xlab("近一年收益率")+
  ylab("主题")+
  theme_classic()+
  theme(
    panel.background=element_rect(fill="white",colour=NA),
    panel.grid.major.x = element_line(colour = "grey80",size=.25),
    panel.grid.major.y = element_line(colour = "grey60",size=.25),
    axis.line = element_blank(),
    text=element_text(size=15,colour = "black"),
    plot.title=element_text(size=15,hjust=.5),
    legend.position="right"
  )

在这里插入图片描述

3.8 不同投资类型的基金规模情况

library(beanplot)
par(mai=c(0.5,0.5,0.25,1.2))
beanplot(基金规模~投资类型, lqydata,col = list("#FF6B5E", "#00C3C2"),
         side = "both",xlab ="city",ylab ="value")
log="y" selected

在这里插入图片描述

3.9 基金规模分布情况

hist(lqydata$基金规模, prob = TRUE, col = "lightblue",main="Normal Distribution") 

在这里插入图片描述

3.12 收益矩阵分布图

#基金收益矩阵图
lqy<-lqydata[,c(7,8,14:23)]
library(GGally) 
ggpairs(lqy,columns=c(1:6),aes(color='green'),alpha=0.8)+ 
 theme_bw(base_family="STKaiti",base_size=10)+ 
 theme(plot.title=element_text(hjust=0.5))+ 
 ggtitle("散点矩阵图")

在这里插入图片描述

四、基金风险分布可视化

4.1 不同主题晨星风险系数之间对比

#不同主题之间对比
library(beanplot)
par(mai=c(0.5,0.5,0.25,1.2))
beanplot(晨星风险系数~类别, lqydata,col = list("#FF6B5E", "#00C3C2"),
         side = "both",xlab ="city",ylab ="value")

在这里插入图片描述

4.2 风险分布矩阵图,验证风险评级是否能区分开数据

#风险分布矩阵图
lqy<-lqydata[,c(9:13,24,25)]
library(GGally) 
ggpairs(lqy,columns=c(1:6),aes(color=风险评级),alpha=0.8)+ 
 theme_bw(base_family="STKaiti",base_size=10)+ 
 theme(plot.title=element_text(hjust=0.5))+ 
 ggtitle("散点矩阵图")

在这里插入图片描述

数据中原给定的风险评级,并未能将各风险指标区分开,故该风险评级存在一定的局限性,设法改进。

聚类结果较不理想,考虑更换风险度量指标后再进行聚类。

4.3 基金风险聚类评估

colnames(lqydata)
  1. '基金代码'
  2. '基金名称'
  3. '投资类型'
  4. '类别'
  5. '成立时间'
  6. '基金经理累计从业时间'
  7. '基金净值'
  8. '基金规模'
  9. '阿尔法系数(%)'
  10. '贝塔系数'
  11. 'R平方'
  12. '标准差(%)'
  13. '夏普比率'
  14. '回报率'
  15. '日增长率'
  16. '近一周'
  17. '近一月'
  18. '近三月'
  19. '近六月'
  20. '今年来'
  21. '近一年'
  22. '近两年'
  23. '近三年'
  24. '晨星风险系数'
  25. '风险评级'
cludata<-lqydata[,c(1,2,9:13,24)]
head(cludata)
A tibble: 6 × 8
基金代码基金名称阿尔法系数(%)贝塔系数R平方标准差(%)夏普比率晨星风险系数
<chr><chr><dbl><dbl><dbl><dbl><dbl><dbl>
000209信诚新兴产业混合16.041.1552.2030.490.8116.51
000336农银研究精选混合27.361.1655.0829.871.2014.44
000409鹏华环保产业股票20.730.9854.3025.371.1012.56
000432中银优秀企业混合 4.330.7959.3319.620.5210.35
000432中银优秀企业混合 4.330.7959.3319.620.5210.35
000535长盛航天海工装备 4.980.9749.2626.320.4611.05
## 计算组内平方和  组间平方和
tot_withinss <- vector()
betweenss <- vector()
for(ii in 1:5){
  k1 <- kmeans(cludata[,c(3:7)],ii)
  tot_withinss[ii] <- k1$tot.withinss
  betweenss[ii] <- k1$betweenss
}

kmeanvalue <- data.frame(kk = 1:5,
                         tot_withinss = tot_withinss,
                         betweenss = betweenss)

library(ggplot2)
library(gridExtra)
library(ggdendro)
library(cluster)
library(ggfortify)
p1 <- ggplot(kmeanvalue,aes(x = kk,y = tot_withinss))+
  theme_bw()+
  geom_point() + geom_line() +labs(y = "value") +
  ggtitle("Total within-cluster sum of squares")+
  theme(plot.title = element_text(hjust = 0.5))+
  scale_x_continuous("kmean 聚类个数",kmeanvalue$kk)

p2 <- ggplot(kmeanvalue,aes(x = kk,y = betweenss))+
  theme_bw()+
  geom_point() +geom_line() +labs(y = "value") +
  ggtitle("The between-cluster sum of squares") +
  theme(plot.title = element_text(hjust = 0.5))+
  scale_x_continuous("kmean 聚类个数",kmeanvalue$kk)

grid.arrange(p1,p2,nrow=2)
Warning message:
"package 'ggplot2' was built under R version 4.0.4"
Warning message:
"package 'gridExtra' was built under R version 4.0.4"
Warning message:
"package 'ggdendro' was built under R version 4.0.4"
Warning message:
"package 'ggfortify' was built under R version 4.0.4"

在这里插入图片描述

set.seed(245)
k3 <- kmeans(cludata[,c(3:7)],2)
summary(k3)
             Length Class  Mode   
cluster      102    -none- numeric
centers       10    -none- numeric
totss          1    -none- numeric
withinss       2    -none- numeric
tot.withinss   1    -none- numeric
betweenss      1    -none- numeric
size           2    -none- numeric
iter           1    -none- numeric
ifault         1    -none- numeric
#将标签写入
cludata$cluster<-k3$cluster
head(cludata)
cludata$cluster<-as.factor(cludata$cluster)
A tibble: 6 × 9
基金代码基金名称阿尔法系数(%)贝塔系数R平方标准差(%)夏普比率晨星风险系数cluster
<chr><chr><dbl><dbl><dbl><dbl><dbl><dbl><int>
000209信诚新兴产业混合16.041.1552.2030.490.8116.512
000336农银研究精选混合27.361.1655.0829.871.2014.442
000409鹏华环保产业股票20.730.9854.3025.371.1012.562
000432中银优秀企业混合 4.330.7959.3319.620.5210.352
000432中银优秀企业混合 4.330.7959.3319.620.5210.352
000535长盛航天海工装备 4.980.9749.2626.320.4611.052
#基金收益矩阵图
library(GGally) 
ggpairs(cludata[,3:9],columns=c(1:6),aes(color=cluster),alpha=0.8)+ 
 theme_bw(base_family="STKaiti",base_size=10)+ 
 theme(plot.title=element_text(hjust=0.5))+ 
 ggtitle("散点矩阵图")

在这里插入图片描述

聚类为2类时,数据区分度较高,模型合理

## 对聚类结果可视化
clusplot(cludata[,c(3:7)],k3$cluster,main = "kmean cluster number=2")

在这里插入图片描述

#如果分为三类
set.seed(245)
k3 <- kmeans(cludata[,c(3:7)],3)
summary(k3)
             Length Class  Mode   
cluster      102    -none- numeric
centers       15    -none- numeric
totss          1    -none- numeric
withinss       3    -none- numeric
tot.withinss   1    -none- numeric
betweenss      1    -none- numeric
size           3    -none- numeric
iter           1    -none- numeric
ifault         1    -none- numeric
#将标签写入
cludata$cluster<-k3$cluster
head(cludata)
cludata$cluster<-as.factor(cludata$cluster)
A tibble: 6 × 9
基金代码基金名称阿尔法系数(%)贝塔系数R平方标准差(%)夏普比率晨星风险系数cluster
<chr><chr><dbl><dbl><dbl><dbl><dbl><dbl><int>
000209信诚新兴产业混合16.041.1552.2030.490.8116.512
000336农银研究精选混合27.361.1655.0829.871.2014.442
000409鹏华环保产业股票20.730.9854.3025.371.1012.562
000432中银优秀企业混合 4.330.7959.3319.620.5210.352
000432中银优秀企业混合 4.330.7959.3319.620.5210.352
000535长盛航天海工装备 4.980.9749.2626.320.4611.052
## 对聚类结果可视化
clusplot(cludata[,c(3:7)],k3$cluster,main = "kmean cluster number=3")

在这里插入图片描述

此时模型得到合适的分类,比较合理

可知,分为2-3类也较合理

五、建立风险评估模型

5.1 选取建模指标

colnames(lqydata)
  1. '基金代码'
  2. '基金名称'
  3. '投资类型'
  4. '类别'
  5. '成立时间'
  6. '基金经理累计从业时间'
  7. '基金净值'
  8. '基金规模'
  9. '阿尔法系数'
  10. '贝塔系数'
  11. 'R平方'
  12. '标准差'
  13. '夏普比率'
  14. '回报率'
  15. '日增长率'
  16. '近一周'
  17. '近一月'
  18. '近三月'
  19. '近六月'
  20. '今年来'
  21. '近一年'
  22. '近两年'
  23. '近三年'
  24. '晨星风险系数'
  25. '风险评级'
#由于风险评级并不能有效地区分开样本,故可暂时进行剔除
scoredata<-lqydata[,c(-1,-2,-25)]
head(scoredata)
A tibble: 6 × 22
投资类型类别成立时间基金经理累计从业时间基金净值基金规模阿尔法系数贝塔系数R平方标准差...日增长率近一周近一月近三月近六月今年来近一年近两年近三年晨星风险系数
<chr><chr><dbl><dbl><dbl><dbl><dbl><dbl><dbl><dbl>...<dbl><dbl><dbl><dbl><dbl><dbl><dbl><dbl><dbl><dbl>
混合型新能源 201362.9870 4.1116.041.1552.2030.49... 1.60 8.3013.23-12.0242.51 3.25107.57143.24117.2416.51
混合型新能源 201343.535630.4327.361.1655.0829.87... 0.95 4.61 6.57-13.8924.72 -3.66119.62203.30183.6014.44
股票型新能源 201433.888013.8720.730.9854.3025.37... 0.96 5.80 9.92-15.9721.01 -6.74 87.01134.78141.1912.56
混合型人工智能201432.1320 0.23 4.330.7959.3319.62... 0.61 3.29 5.70 -6.20-2.60 -5.29 11.22 31.85 44.3510.35
混合型国产软件201432.1320 0.23 4.330.7959.3319.62... 0.61 3.29 5.70 -6.20-2.60 -5.29 11.22 31.85 44.3510.35
混合型无人机 201451.3400 3.16 4.980.9749.2626.32...-0.22-0.52-0.96-20.94 1.43-18.38 39.71 40.87 43.6911.05

5.2 建立支持向量机模型

## 支持向量机回归模型
library(e1071)
library(caret)
library(Metrics)
library(readr)

system.time(
  svmreg <- svm(晨星风险系数~.,data =scoredata,kernel = "radial")
)
   user  system elapsed 
   0.02    0.00    0.02 
summary(svmreg)
Call:
svm(formula = 晨星风险系数 ~ ., data = scoredata, kernel = "radial")


Parameters:
   SVM-Type:  eps-regression 
 SVM-Kernel:  radial 
       cost:  1 
      gamma:  0.03333333 
    epsilon:  0.1 


Number of Support Vectors:  73
set.seed(123)
index <- sample(nrow(scoredata),round(0.7*nrow(scoredata)))
train_data <- scoredata[index,]
test_data <-scoredata[-index,]
dim(train_data)
  1. 69
  2. 22
## 在训练集上的误差
train_pre <- predict(svmreg,train_data)
train_mae <- mae(train_data$晨星风险系数,train_pre)
sprintf("训练集上的绝对值误差: %f",train_mae)

‘训练集上的绝对值误差: 0.346671’

total_pre <- predict(svmreg,scoredata[,-22])
total_mae <- mse(scoredata$晨星风险系数,total_pre)
sprintf("全部上的绝对值误差: %f",total_mae)

‘全部上的绝对值误差: 0.251737’

data5<-data.frame(total_pre,scoredata$晨星风险系数)
colnames(data5)<-c('预测','实际')
head(data5)
A data.frame: 6 × 2
预测实际
<dbl><dbl>
115.3220716.51
214.2350014.44
312.8545312.56
411.2757510.35
511.2605810.35
613.3359911.05
df<-data.frame(x=c(1:99),y=data5$实际)
pre_y=data5$预测
head(df)
#训练集可视化
plot1 <- ggplot(data = df, aes(x = x)) + geom_line(aes(y = y, linetype = "实际值", colour = "实际值"), size = 0.8) ### 画实际值得曲线
plot2 <- plot1 + geom_line(aes(y = pre_y, linetype = "预测值", colour = "预测值"), size = 0.8) ### 画预测值曲线

plot2 + scale_linetype_manual(name = "", values = c("实际值" = "solid", "预测值" = "twodash")) +
  scale_colour_manual(name = "", values = c("实际值" = "red", "预测值" = "blue"))  ### 设置图例
A data.frame: 6 × 2
xy
<int><dbl>
1116.51
2214.44
3312.56
4410.35
5510.35
6611.05

5.3 建立随机森林模型

library(readr)
library(VIM)
library(rpart)
library(rpart.plot)
library(Metrics)
library(ROCR)
library(tidyr)
library(randomForest)
library(ggRandomForests)
set.seed(123)
index <- sample(nrow(scoredata),round(0.7*nrow(scoredata)))
train_data <-scoredata[index,]
test_data <-scoredata[-index,]
rfreg <- randomForest(晨星风险系数~.,data = train_data,ntree=150)
summary(rfreg)
                Length Class  Mode     
call              4    -none- call     
type              1    -none- character
predicted        69    -none- numeric  
mse             150    -none- numeric  
rsq             150    -none- numeric  
oob.times        69    -none- numeric  
importance       21    -none- numeric  
importanceSD      0    -none- NULL     
localImportance   0    -none- NULL     
proximity         0    -none- NULL     
ntree             1    -none- numeric  
mtry              1    -none- numeric  
forest           11    -none- list     
coefs             0    -none- NULL     
y                69    -none- numeric  
test              0    -none- NULL     
inbag             0    -none- NULL     
terms             3    terms  call     
## 可视化模型随着树的增加误差OOB的变化
par(family = "STKaiti")
plot(rfreg,type = "l",col = "red",main = "随机森林回归")

在这里插入图片描述

## 使用ggrandomforest包可视化误差
plot(gg_error(rfreg))+labs(title = "随机森林回归")

在这里插入图片描述

## 可视化变量的重要性
importance(rfreg)
A matrix: 21 × 1 of type dbl
IncNodePurity
投资类型 1.873738
类别 5.235171
成立时间 2.823165
基金经理累计从业时间 1.664659
基金净值 9.702394
基金规模11.184948
阿尔法系数 2.492178
贝塔系数12.371313
R平方29.013155
标准差86.714834
夏普比率 7.370843
回报率14.513756
日增长率 8.427672
近一周 5.300910
近一月 9.149165
近三月 8.074077
近六月12.011001
今年来 3.402688
近一年 6.970955
近两年 6.801926
近三年 2.011527
varImpPlot(rfreg,pch = 20, main = "Importance of Variables")

在这里插入图片描述

## 对测试集进行预测,并计算 Mean Squared Error
rfpre <- predict(rfreg,test_data)
sprintf("均方根误差为: %f",mse(test_data$晨星风险系数,rfpre))

‘均方根误差为: 1.154161’

## 参数搜索,寻找合适的 mtry参数,训练更好的模型
## Tune randomForest for the optimal mtry parameter
set.seed(1234)
rftune <- tuneRF(x = test_data[,1:21],y = test_data$晨星风险系数,
                 stepFactor=1.5,ntreeTry = 150)
mtry = 7  OOB error = 2.296108 
Searching left ...
mtry = 5     OOB error = 2.187399 
0.04734515 0.05 
Searching right ...
mtry = 10     OOB error = 2.057613 
0.1038693 0.05 
mtry = 15     OOB error = 1.913855 
0.06986641 0.05 
mtry = 21     OOB error = 1.90867 
0.002709463 0.05 

在这里插入图片描述

print(rftune)
   mtry OOBError
5     5 2.187399
7     7 2.296108
10   10 2.057613
15   15 1.913855
21   21 1.908670
## OOBError误差最小的mtry参数为6

## 建立优化后的随机森林回归模型
rfregbest <- randomForest(晨星风险系数~.,data = train_data,ntree=150,mtry = 15)

## 可视化两种模型随着树的增加误差OOB的变化
rfregerr <- as.data.frame(plot(rfreg))

在这里插入图片描述

colnames(rfregerr) <- "rfregerr"
rfregbesterr <- as.data.frame(plot(rfregbest))

在这里插入图片描述

#优化前后进行对比,可知优化后更好
colnames(rfregbesterr) <- "rfregbesterr"
plotrfdata <- cbind.data.frame(rfregerr,rfregbesterr)
plotrfdata$ntree <- 1:nrow(plotrfdata)
plotrfdata <- gather(plotrfdata,key = "Type",value = "Error",1:2)
ggplot(plotrfdata,aes(x = ntree,y = Error))+
  geom_line(aes(linetype = Type,colour = Type),size = 0.9)+
  theme(legend.position = "top")+
  ggtitle("随机森林回归模型")+
  theme(plot.title = element_text(hjust = 0.5))

在这里插入图片描述

优化前后进行对比,可知优化后更好

## 使用优化后的随机森林回归模型,对测试集进行预测,并计算 Mean Squared Error
rfprebest <- predict(rfregbest,test_data[,1:21])
sprintf("优化后均方根误差为: %f",mse(test_data$晨星风险系数,rfprebest))

‘优化后均方根误差为: 1.046931’

## 使用优化后的随机森林回归模型,对测试集进行预测,并计算 Mean Squared Error
#全部数据
total <- predict(rfregbest,scoredata[,1:21])
sprintf("优化后均方根误差为: %f",mse(scoredata$晨星风险系数,total))

‘优化后均方根误差为: 0.460142’

#预测结果
totalpre<-data.frame(scoredata$晨星风险系数,total)
colnames(totalpre)<-c('实际','预测')
scoredata$预测<-total 
head(scoredata)
A tibble: 6 × 23
投资类型类别成立时间基金经理累计从业时间基金净值基金规模阿尔法系数贝塔系数R平方标准差...近一周近一月近三月近六月今年来近一年近两年近三年晨星风险系数预测
<chr><chr><dbl><dbl><dbl><dbl><dbl><dbl><dbl><dbl>...<dbl><dbl><dbl><dbl><dbl><dbl><dbl><dbl><dbl><dbl>
混合型新能源 0.66666670.50.42776630.01483340100.69781100.208530810.50217140.6897133...0.934975370.869174620.65252121.00000000.86121480.90826740.70646600.646607716.5116.41049
混合型新能源 0.66666670.30.53082850.11195158851.00000000.213270140.53267660.6548623...0.571428570.495227400.55916130.68463040.62673911.00000001.00000001.000000014.4416.32278
股票型新能源 0.73333330.20.59703170.05084683220.82301120.127962090.52441480.4019112...0.688669950.683323980.45531700.61886190.52222600.75175090.66511900.774150612.5613.20960
混合型人工智能0.73333330.20.26714260.00051658610.38521090.037914690.57769300.0786959...0.441379310.446378440.94308540.20031910.57142860.17478680.16206440.258440710.3510.97816
混合型国产软件0.73333330.20.26714260.00051658610.38521090.037914690.57769300.0786959...0.441379310.446378440.94308540.20031910.57142860.17478680.16206440.258440710.3510.97816
混合型无人机 0.73333330.40.11835430.01132799530.40256270.123222750.47103060.4553120...0.066009850.072431220.20718920.27176030.12724800.39167170.20614830.254926011.0512.38051
#预测结果
head(scoredata,25)
A tibble: 25 × 23
投资类型类别成立时间基金经理累计从业时间基金净值基金规模阿尔法系数贝塔系数R平方标准差...近一周近一月近三月近六月今年来近一年近两年近三年晨星风险系数预测
<chr><chr><dbl><dbl><dbl><dbl><dbl><dbl><dbl><dbl>...<dbl><dbl><dbl><dbl><dbl><dbl><dbl><dbl><dbl><dbl>
混合型 新能源 0.66666670.50.427766300.01483340100.69781100.208530810.50217140.6897133...0.934975370.869174620.65252121.00000000.86121480.90826740.70646600.646607716.5116.41049
混合型 新能源 0.66666670.30.530828480.11195158851.00000000.213270140.53267660.6548623...0.571428570.495227400.55916130.68463040.62673911.00000001.00000001.000000014.4416.32278
股票型 新能源 0.73333330.20.597031750.05084683220.82301120.127962090.52441480.4019112...0.688669950.683323980.45531700.61886190.52222600.75175090.66511900.774150612.5613.20960
混合型 人工智能0.73333330.20.267142590.00051658610.38521090.037914690.57769300.0786959...0.441379310.446378440.94308540.20031910.57142860.17478680.16206440.258440710.3510.97816
混合型 国产软件0.73333330.20.267142590.00051658610.38521090.037914690.57769300.0786959...0.441379310.446378440.94308540.20031910.57142860.17478680.16206440.258440710.3510.97816
混合型 无人机 0.73333330.40.118354310.01132799530.40256270.123222750.47103060.4553120...0.066009850.072431220.20718920.27176030.12724800.39167170.20614830.254926011.0512.38051
股票指数无人机 0.73333330.30.195002820.04438950590.34730380.203791470.61116410.5306352...0.077832510.081976420.15626560.22832830.12147950.32635510.20111430.182341014.5014.54577
股票型 新能源 0.73333330.00.267330450.11195158850.48131340.127962090.61211740.3035413...0.761576350.691184730.54018970.65059390.59178830.69031670.50955480.393385913.7913.43880
股票型 国产软件0.73333330.10.247604730.16575034130.39135080.080568720.87300070.6273187...0.661083740.650196520.64503250.41287010.67560230.39433620.42363520.280913817.6116.79782
股票型 新能源 0.73333330.20.267142590.01136489430.78777360.241706160.57144370.6902754...0.998029561.000000000.48227660.88406310.56430270.67722290.64781780.797742015.2816.51623
股票型 国产软件0.80000000.50.209656210.01881849380.51922050.194312800.75055610.3569421...0.609852220.683885460.97603590.52632510.86834070.44914740.42094720.447758013.6613.77112
股票型 新能源 0.80000000.40.136577120.08195269550.74719700.165876780.71327190.3209668...0.652216750.537338570.66949580.88601310.87580590.75997260.52529200.676163611.8412.46147
股票型 新能源 0.80000000.90.106518880.04007232210.53150030.104265400.59167460.2647555...0.714285710.417742840.71243140.68002130.70376650.68772840.43956800.416231811.5412.18295
股票型 新能源 0.80000001.00.170768360.04645585030.80352380.170616110.55894500.4952220...0.913300490.616507580.24563160.68658040.42857140.77976550.68642780.750239613.6413.58897
股票型 国产软件0.80000000.40.089329330.01627246230.51601710.018957350.86315010.3962901...0.541871920.494665920.41587620.26626480.48998980.26096220.32867410.370327014.8114.81494
联接基金智能穿戴0.80000000.60.000000000.01217667240.26721840.066350710.99311510.4665542...0.432512320.470522180.74687970.23896470.40719380.20127890.14403010.158909415.9115.89441
联接基金人工智能0.80000000.60.000000000.01217667240.26721840.066350710.99311510.4665542...0.432512320.470522180.74687970.23896470.68069220.20127890.14403010.158909415.9115.84949
联接基金生物识别0.80000000.60.000000000.01217667240.26721840.066350710.99311510.4665542...0.432512320.470522180.74687970.23896470.68069220.20127890.14403010.158909415.9115.84943
股票型 国产芯片0.80000000.50.081908700.02058964610.43272820.033175360.80118630.4890388...0.436453200.547445260.46679980.22194650.52935190.29636110.26846200.307807016.8714.90964
股票型 半导体 0.80000000.50.081908700.02058964610.43272820.033175360.80118630.4890388...0.436453200.547445260.46679980.22194650.52935190.29636110.26846200.307807016.8715.03520
混合型 无人机 0.80000000.00.115160620.41168222570.47010140.236966820.60374960.6312535...0.000000000.234699610.33250120.29888320.19714960.50746040.34665950.320534713.7814.17263
股票型 新能源 0.80000000.50.166071760.04361462680.72851040.227488150.61116410.5879708...0.685714290.624929810.53170240.60468000.71733970.86997560.72924100.714612815.5714.91463
混合型 国产软件0.86666670.80.299830920.15523412420.64442070.071090050.88963030.5705453...0.653201970.686131390.44183720.27140580.53206650.25205540.40301060.528650515.2915.32525
联接基金国产芯片0.80000000.50.141818520.01276705660.45301660.118483410.97669740.6891512...0.527093600.591240880.70244630.32086510.68781810.32148290.33180200.376823917.9117.50908
联接基金人工智能0.80000000.30.033045280.01405852180.29551520.042654030.91325070.4345138...0.340886700.427288040.78332500.17071440.62945370.12431490.10238990.141335614.6714.95419
df<-data.frame(x=c(1:99),y=totalpre$实际)
pre_y=totalpre$预测
head(df)
#训练集可视化
plot1 <- ggplot(data = df, aes(x = x)) + geom_line(aes(y = y, linetype = "实际值", colour = "实际值"), size = 0.8) ### 画实际值得曲线
plot2 <- plot1 + geom_line(aes(y = pre_y, linetype = "预测值", colour = "预测值"), size = 0.8) ### 画预测值曲线

plot2 + scale_linetype_manual(name = "", values = c("实际值" = "solid", "预测值" = "twodash")) +
  scale_colour_manual(name = "", values = c("实际值" = "red", "预测值" = "blue"))  ### 设置图例
A data.frame: 6 × 2
xy
<int><dbl>
1116.51
2214.44
3312.56
4410.35
5510.35
6611.05

在这里插入图片描述

由于个别极端值,训练集中缺少这一部分较高的样本,故导致测试结果不尽人意,剔除这部分样本后,模型良好。

## 数据准备
index <- order(test_data$晨星风险系数)
X <- sort(index)
Y1 <- test_data$晨星风险系数[index]
rfpre2 <- rfpre[index]
rfprebest2 <- rfprebest[index]

plotdata <- data.frame(X = X,Y1 = Y1,rfpre =rfpre2,rfprebest = rfprebest2)
plotdata <- gather(plotdata,key="model",value="value",c(-X))

## 可视化模型的预测误差
ggplot(plotdata,aes(x = X,y = value))+
  geom_line(aes(linetype = model,colour = model),size = 0.8)+
  theme(legend.position = c(0.1,0.8),
        plot.title = element_text(hjust = 0.5))+
  ggtitle("随机森林回归模型")

### 5.4 MLP神经网络

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值