-
Notifications
You must be signed in to change notification settings - Fork 0
/
ForestFire_analysis.R
183 lines (140 loc) · 5.18 KB
/
ForestFire_analysis.R
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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
# Setting up
library(tidyverse)
# Import data
forestfires <- read_csv("D:/BENA/Data Analytics/Dataquest/Project3_ForestFires/forestfires.csv")
head(forestfires)
glimpse(forestfires)
skimr::skim_without_charts(forestfires)
View(forestfires)
colnames(forestfires)
# The column names stand for....
# Each row represents a fire incident, its location & the weather variables associated with it. How each variable is related to the fire itself?
# Data processing
# Rearrange the month & day columns to their inherent orders
forestfires %>%
pull(month) %>%
unique
forestfires %>%
pull(day) %>%
unique
# Converting day & month into categorical variables, to make sure they're ordered correctly
forestfires <- forestfires %>%
mutate(
month_cat = factor(month, levels = c("jan","feb","mar","apr","may","jun","jul","aug","sep","oct","nov","dec"))
)
forestfires <- forestfires %>%
mutate(
day_cat = factor(day, levels = c("sun","mon","tue","wed","thu","fri","sat"))
)
# Alternatively
month_order <- c("jan","feb","mar","apr","may","jun","jul","aug","sep","oct","nov","dec")
day_order <- c("sun","mon","tue","wed","thu","fri","sat")
forestfires <- forestfires %>%
mutate(
month = factor(month, levels = month_order),
day = factor(day, levels = day_order)
)
# When do most fires happen? summary by month & day
fires_per_month <- forestfires %>%
group_by(month_cat) %>%
summarise(total_fires = n())
forestfires %>%
ggplot(aes(x = month_cat)) +
geom_bar() + #viz1. aug has the highest number of fire incidents followed by sep
labs(
title = "Number of forest fires per month",
y = "fire_incidents_count"
)
# Alternatively
fires_per_month %>%
ggplot(aes(x = month_cat, y = total_fires)) +
geom_col() +
labs(
title = "Number of forest fires per month",
y = "fire_incidents_count"
)
fires_per_day <- forestfires %>%
group_by(day_cat) %>%
summarise(total_fires = n())
forestfires %>%
ggplot(aes(x = day_cat)) +
geom_bar() +
labs(title = "Forest fires per day") +
theme(panel.background = element_rect(fill = "lightblue"))
# Alternatively
fires_per_day %>% # sun had the highest number of fire incidents followed by fri. day with lowest number of incidents was wed
ggplot(aes(x=day_cat, y=total_fires)) +
geom_col() +
labs(
title = "Forest fires per day",
y = "fire_incidents_count"
)
# Plotting Other Variables Against Time
# Focus is on months since the variations are also brought about by seasons
forestfires_long <- forestfires %>% #transforms the data from wide to long format
pivot_longer(
cols = c(FFMC,DMC,DC,ISI,temp,RH,wind,rain),
names_to = "weather_variable",
values_to = "quantity"
)
forestfires_long %>%
ggplot(aes(x = month_cat, y = quantity)) +
geom_boxplot() +
facet_wrap(vars(weather_variable), scales = "free_y") +
labs(
title = "weather variable changes per month",
y = "quantity",
x = "month"
)
# DC, DMC, FFMC, RH, temp & wind have high values in Aug & Sep
# Examining Forest Fire Severity
# the area column will be used as a proxy, based on the assumption that the more severe a fire is the larger the area that will be burnt
forestfires_long %>%
ggplot(aes(x=quantity, y=area)) +
geom_point() +
facet_wrap(vars(weather_variable), scales = "free_x") +
labs(
title = "Relationship between area & weather_variables",
x = "quantity of variable",
y = "burnt area (in hectares)"
)
# When the wind speed, temperatures, DC, FFMC are high, the area burnt tends to also be larger
# When RH, & rain are low the fires seem to be more severe & have a wide coverage
# Outlier Problems
forestfires_long_filtered <- forestfires_long %>%
filter(area != 0 & area < 746)
# Check the max value
summary <- forestfires_long_filtered %>%
summarise(
maximum = max(area),
minimum = min(area)
)
forestfires_long_filtered1 <- forestfires_long_filtered %>%
filter(area > 0.09 & area < 80) #constitute 94% of the population after filtering in code above
forestfires_long_filtered %>%
filter(area > 0.09 & area < 80) %>%
ggplot(aes(x=quantity, y=area)) +
geom_point() +
facet_wrap(vars(weather_variable), scales = "free_x") +
labs(
title = "Relationship between area & weather_variables",
x = "quantity of variable",
y = "burnt area (in hectares)"
)
forestfires_long_filtered2 <- forestfires_long_filtered %>%
filter(area > 80)
forestfires_long_filtered %>%
filter(area > 80) %>%
ggplot(aes(x=quantity, y=area)) +
geom_point() +
facet_wrap(vars(weather_variable), scales = "free_x") +
labs(
title = "Relationship between area & weather_variables",
x = "quantity of variable",
y = "burnt area (in hectares)"
)
# It's hard to describe relationship for most of the weather variables. However based on 94% of the population after filtering, the area increased the longer it took the place to receive rain. There also seems to be a positive relationship between FFMC & area
# Find out relationship between area & the other weather variables***
# Investigate the zeros. Are there forest fires that don't burn any land?
# Insight gathered from the analysis...
#Testing for emergency-fix github