研究背景:随着时代的发展,时间管理大师越来越多,离婚率也越来越高,如何准确识别时间管理大师有助于降低离婚率。
研究目的:利用神经网络来建立时间管理大师的预测模型
首先介绍一下今天使用的数据集 Affairs数据集,是R的AER包里面的数据集,其中包含的变量是Affairs(出轨次数),gender(性别), age(年龄), yearsmarried(结婚年数) children (是否有娃),religiousness (信仰),education(教育程度),rating(对自己婚姻的评价)
##加载数据集
library(AER)
library(ggplot2)
data("Affairs")
head(Affairs)
##做一点数据预处理,把文本表示变量和结局变量变成二分类变量
Affairs$gender <- ifelse(Affairs$gender=="female",2,1)
Affairs$children <- ifelse(Affairs$children=="no",0,1)
Affairs$ynaffair[Affairs$affairs>3]<-1
Affairs$ynaffair[Affairs$affairs<=3]<-0
table(Affairs$ynaffair)
##
## 0 1
## 521 80
#出轨3次以上的为“时间管理大师”,否则为“非时间管理大师”
#一共有80个“时间管理大师”和521个“非时间管理大师”
#看一下数据一共有多少行,建立训练集和验证集
nrow(Affairs)
## [1] 601
# 建立训练集和验证集
set.seed(123)
train_sample <- sample(601,200)
Affairs_train <- Affairs[train_sample,]
Affairs_test <- Affairs[-train_sample,]
开始建模
library(neuralnet)
# simple ANN with only a single hidden neuron单个隐藏层
RNGversion("3.5.2")# use an older random number generator to match the book
set.seed(12345) # to guarantee repeatable results
Affairs_trainmodel <- neuralnet(formula =ynaffair~ gender + age +
yearsmarried+ children + religiousness +
education+ occupation + rating,
data = Affairs_train)
#绘图
plot(Affairs_trainmodel)
单个隐藏层还是挺好解读的
模型的验证和具体的参数解读我们留到下期,谢谢大家的支持~