forked from tjsipin/MacDonald-REU-Summer-22
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Neural.network.practice.Rmd
159 lines (128 loc) · 4.21 KB
/
Neural.network.practice.Rmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
---
title: "Deep Neural Network Regression"
author: "Lyndsey Umsted"
date: '2022-06-24'
output:
html_document:
toc: true
number_sections: false
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
```{r}
suppressMessages(library(readr))
suppressMessages(library(keras))
suppressMessages(library(DT))
```
<style type = "text/css">
h1 {
color: #1a2451;
}
h2 {
color: #ffbd4a;
}
h3 {
color: #1a2451;
}
</style>
## Introduction
Introducing a densely connected deep neural network with two hidden layers and an output layer
## Data
```{r}
aad <- read.csv("Annual_Amazon_Data.csv")
view(aad)
cutaneous <- aad$Cutaneous.Leishmaniasis
mucosal <- aad$Mucosal.Leishmaniasis
visceral <- aad$Visceral.Leishmaniasis
new_data <- subset(aad, !is.na(cutaneous))
#View(new_data)
library(tidyverse)
library(dplyr)
#names(new_data)
## splitting the data into a before 2014 set and an after 2014 set
early_data <- new_data %>%
filter(Year < 2014)%>%
dplyr::select(-c(29:69))
later_data <- new_data %>%
filter(Year > 2013) %>%
dplyr::select(-c(29:69))
#names(early_data)
## removing unnecessary variables
early_data <- early_data %>%
dplyr::select(-c("AvgRad"))
later_data <- later_data %>%
dplyr::select(-c("StableLights")) %>%
mutate(later_data$OptTemp_Obs <- as.numeric(later_data$OptTemp_Obs)) %>%
mutate(later_data$Year <- as.numeric(later_data$Year)) %>%
mutate(later_data$Population <- as.numeric(later_data$Population))
```
```{r, fig.width = 10, fig.height = 10}
later_data_small <- later_data %>%
dplyr::select(c("Year", "Population", "Cutaneous.Leishmaniasis", "Dengue_Alb_OptTemp", "Dengue_Aeg_OptTemp", "Chik_Alb_OptTemp", "Chik_Aeg_OptTemp", "Zika_OptTemp", "Malaria_OptTemp", "LST_Day", "LST_Night", "OptTemp_Obs", "NDVI", "EVI", "Precip", "AvgRad", "SWOccurrence")) %>%
na.omit(later_data_small)
later_data_t <- later_data_small %>%
filter(Cutaneous.Leishmaniasis > 0)%>%
mutate(Cutaneous.Leishmaniasis = (Cutaneous.Leishmaniasis)^(-0.025))
# pairs(later_data_small, labels = c("Year", "Population", "Cutaneous.Leishmaniasis","LST_Day", "LST_Night", "OptTemp_Obs", "NDVI", "EVI", "Precip", "AvgRad", "SWOccurrence"))
# library(ggplot2)
# library(GGally)
# ggpairs(data.frame(Year = later_data_small[,1], Population = later_data_small[,2], Cutaneous.Leishmaniasis = later_data_small[,3], LST_Day = later_data_small[,4], LST_Night = later_data_small[,5], OptTemp_Obs = later_data_small[,6], NDVI = later_data_small[,7], EVI = later_data_small[,8], Precip = later_data_small[,9], StableLights = later_data_small[,10], SWOccurrence = later_data_small[,11]), lower = list(continuous = wrap('points'), alpha = 0.3, size = 0.2))
```
```{r, fig.width = 10, fig.height = 10}
plot(later_data_t)
```
```{r}
# splitting data into training and test data
set.seed(2)
library(caTools)
split <- sample.split(later_data_t, SplitRatio = 0.7)
split
train <- subset(later_data_t, split = "TRUE")
test <- subset(later_data_t, split = "FALSE")
train
test
```
```{r}
# create the model
model <- lm(later_data_t$Cutaneous.Leishmaniasis ~ .,data = train)
summary(model)
```
```{r}
library(MASS)
boxcox(model, plotit = T, lambda = seq(0,2,len = 100))
```
```{r}
plot(model)
```
```{r}
#prediction
pred <- predict(model, test)
pred
```
```{r}
#install.packages("ggpubr")
library(ggpubr)
ggdensity(pred-later_data_t$Cutaneous.Leishmaniasis) + stat_overlay_normal_density(color = "red", linetype = "dashed")
```
```{r}
# comparing predicted vs actual values
par(mfrow = c(1,2), mgp = c(2,1,0), mar = c(3,3,3,1) + 0.1)
plot(test$Cutaneous.Leishmaniasis, type ="l", lty = 1.8, col = "red")
lines(pred, type ="l", col = "blue")
plot(pred, type ="l", lty = 1.8, col = "blue")
```
```{r}
# Finding Accuracy
rmse <- sqrt(mean(pred-later_data_t$Cutaneous.Leishmaniasis)^2)
rmse
```
```{r}
par(mfrow = c(1,3), mgp = c(2,1,0), mar = c(3,3,3,1) + 0.1)
plot(fitted(model), residuals(model), xlab = "fitted values", ylab = "residuals")
abline(h = 0)
title("Residuals vs Fitted")
qqnorm(residuals(model), ylab = "residuals", main = "QQ Plot of Residuals")
qqline(residuals(model))
plot(model, 4)
```