() ## train an neural network for health risk prediction |
() trainCNN <- function(trainset,risks) |
() require(mxnet); |
() # Define the input data |
() data <- mx.symbol.Variable(‘data’); |
() # 1st convolutional layer |
() conv_1 <- mx.symbol.Convolution(data = data, kernel = c(5,5), num_filter = 20); |
() act_1 <- mx.symbol.Activation(data = conv_1, act_type = “relu”) |
() pool_1 <- mx.symbol.Pooling(data = act_1, pool_type = “max”, kernel = c(2,2), |
stride = c(2,2)) |
() # 2nd convolutional layer |
() conv_2 <- mx.symbol.Convolution(data = pool_1, kernel = c(5,5), num_filter = 50) |
() act_2 <- mx.symbol.Activation(data = conv_2, act_type = “relu”) |
() pool_2 <- mx.symbol.Pooling(data=act_2, pool_type = “max”, c(2,2), stride = c(2,2)) |
() # 1st fully connected layer |
() flatten <- mx.symbol.Flatten(data = pool_2) |
() fc_1 <- mx.symbol.FullyConnected(data = flatten, num_hidden = 100) |
() act_3 <- mx.symbol.Activation(data = fc_1, act_type = “relu”) |
() # 2nd fully connected layer |
() fc_2 <- mx.symbol.FullyConnected(data = act_3, num_hidden = 40) |
() # Set up the symbolic model |
() NN_model <- mx.symbol.SoftmaxOutput(data = fc_2) |
() # Set seed for reproducibility |
() mx.set.seed() |
() # Train the model |
() model <- mx.model.FeedForward.create(NN_model, X=trainset, y=risks, ctx=mx.cpu(), |
num.round = 50, array.batch.size = 40, learning.rate = 0.01, momentum = 0.9) |
() return(model); |
() |
() ## use an neural network to predict health risk |
() predictRisk <- function(nn,dataset,risks) |
() require(neuralnet); |
() nn.results <- compute(nn, dataset); |
() pr = creditnet.results$net.result; |
() colnames(pr) <- risks; |
() return(pr); |
() |
() ## use ARMA to predict health risk and plot |
() plotARMA <- function(dataset,start=c(2011,1)) |
() require(forecast); |
() datat<-ts(dataset,2,start=start,frequency=12); |
() fit <- Arima(datat, order=c(2,0,1)); |
() plot(forecast(fit)); |
() |