#About data

###Dataset descrpiton
[1] "This dataset has 43 attributes and 55367 instance. It has fire name,fire size,fire magnitude,latitude,longitude,state,temperature,weather,humidity etc.., "
[1] "The dataset presented here is a sub-sample of data presented here :1.88 Million US Fires. I took this dataset and downselected to a random sampling of 50,000 fire samples and combined this dataset with historical weather data at a specific lat/long, historical vegetation data. A metric is representing the measure of the remoteness of a fire using city lat/long database."
###Problem Statement
[1] "Forest fire destroys plants,animals species,ecosystm,living organsisms.It cause big disaster in living things."

view dataset

# A tibble: 55,367 × 43
    ...1 `Unnamed: 0` fire_name fire_size fire_size_class stat_cause_descr 
   <dbl>        <dbl> <chr>         <dbl> <chr>           <chr>            
 1     0            0 <NA>           10   C               Missing/Undefined
 2     1            1 <NA>            3   B               Arson            
 3     2            2 <NA>           60   C               Arson            
 4     3            3 WNA  1          1   B               Debris Burning   
 5     4            4 <NA>            2   B               Miscellaneous    
 6     5            5 <NA>            1   B               Debris Burning   
 7     6            6 <NA>            5.2 B               Debris Burning   
 8     7            7 <NA>            1   B               Campfire         
 9     8            8 <NA>            1   B               Arson            
10     9            9 CRAZY           1   B               Miscellaneous    
# ℹ 55,357 more rows
# ℹ 37 more variables: latitude <dbl>, longitude <dbl>, state <chr>,
#   disc_clean_date <chr>, cont_clean_date <chr>, discovery_month <chr>,
#   disc_date_final <chr>, cont_date_final <chr>, putout_time <chr>,
#   disc_date_pre <chr>, disc_pre_year <dbl>, disc_pre_month <chr>,
#   wstation_usaf <chr>, dstation_m <dbl>, wstation_wban <dbl>,
#   wstation_byear <dbl>, wstation_eyear <dbl>, Vegetation <dbl>, …
      ...1         Unnamed: 0     fire_name           fire_size       
 Min.   :    0   Min.   :    0   Length:55367       Min.   :     0.5  
 1st Qu.:13842   1st Qu.:13842   Class :character   1st Qu.:     1.2  
 Median :27683   Median :27683   Mode  :character   Median :     4.0  
 Mean   :27683   Mean   :27683                      Mean   :  2104.6  
 3rd Qu.:41524   3rd Qu.:41524                      3rd Qu.:    20.0  
 Max.   :55366   Max.   :55366                      Max.   :606945.0  
 fire_size_class    stat_cause_descr      latitude       longitude      
 Length:55367       Length:55367       Min.   :17.96   Min.   :-165.94  
 Class :character   Class :character   1st Qu.:32.27   1st Qu.:-102.54  
 Mode  :character   Mode  :character   Median :34.60   Median : -91.21  
                                       Mean   :36.17   Mean   : -94.76  
                                       3rd Qu.:38.98   3rd Qu.: -82.85  
                                       Max.   :69.85   Max.   : -65.29  
    state           disc_clean_date    cont_clean_date    discovery_month   
 Length:55367       Length:55367       Length:55367       Length:55367      
 Class :character   Class :character   Class :character   Class :character  
 Mode  :character   Mode  :character   Mode  :character   Mode  :character  
                                                                            
                                                                            
                                                                            
 disc_date_final    cont_date_final    putout_time        disc_date_pre     
 Length:55367       Length:55367       Length:55367       Length:55367      
 Class :character   Class :character   Class :character   Class :character  
 Mode  :character   Mode  :character   Mode  :character   Mode  :character  
                                                                            
                                                                            
                                                                            
 disc_pre_year  disc_pre_month     wstation_usaf        dstation_m       
 Min.   :1991   Length:55367       Length:55367       Min.   :     6.17  
 1st Qu.:1999   Class :character   Class :character   1st Qu.: 21373.36  
 Median :2005   Mode  :character   Mode  :character   Median : 35621.33  
 Mean   :2004                                         Mean   : 40256.47  
 3rd Qu.:2009                                         3rd Qu.: 53985.90  
 Max.   :2015                                         Max.   :224153.66  
 wstation_wban   wstation_byear wstation_eyear   Vegetation   
 Min.   :  100   Min.   :1931   Min.   :1993   Min.   : 0.00  
 1st Qu.:13927   1st Qu.:1973   1st Qu.:2010   1st Qu.: 9.00  
 Median :73803   Median :1978   Median :2020   Median :12.00  
 Mean   :61030   Mean   :1979   Mean   :2015   Mean   :11.07  
 3rd Qu.:99999   3rd Qu.:2004   3rd Qu.:2020   3rd Qu.:15.00  
 Max.   :99999   Max.   :2014   Max.   :2020   Max.   :16.00  
    fire_mag       weather_file        Temp_pre_30       Temp_pre_15    
 Min.   :  0.100   Length:55367       Min.   :-49.211   Min.   :-37.00  
 1st Qu.:  0.100   Class :character   1st Qu.: -1.000   1st Qu.: -1.00  
 Median :  0.100   Mode  :character   Median :  9.946   Median : 10.12  
 Mean   :  9.414                      Mean   : 10.689   Mean   : 10.80  
 3rd Qu.:  1.000                      3rd Qu.: 20.016   3rd Qu.: 20.26  
 Max.   :100.000                      Max.   : 46.600   Max.   : 51.57  
   Temp_pre_7       Temp_cont        Wind_pre_30      Wind_pre_15    
 Min.   :-20.85   Min.   :-22.750   Min.   :-1.000   Min.   :-1.000  
 1st Qu.: -1.00   1st Qu.: -1.000   1st Qu.:-1.000   1st Qu.:-1.000  
 Median : 10.24   Median :  0.000   Median : 2.423   Median : 2.296  
 Mean   : 10.91   Mean   :  7.838   Mean   : 1.926   Mean   : 1.844  
 3rd Qu.: 20.50   3rd Qu.: 17.012   3rd Qu.: 3.470   3rd Qu.: 3.435  
 Max.   : 55.00   Max.   : 55.000   Max.   :29.800   Max.   :29.800  
   Wind_pre_7       Wind_cont        Hum_pre_30      Hum_pre_15   
 Min.   :-1.000   Min.   :-1.000   Min.   :-1.00   Min.   :-1.00  
 1st Qu.:-1.000   1st Qu.:-1.000   1st Qu.:-1.00   1st Qu.:-1.00  
 Median : 2.175   Median : 0.000   Median :55.66   Median :51.75  
 Mean   : 1.791   Mean   : 1.132   Mean   :40.78   Mean   :38.45  
 3rd Qu.: 3.392   3rd Qu.: 2.849   3rd Qu.:67.38   3rd Qu.:65.91  
 Max.   :25.700   Max.   :24.200   Max.   :96.00   Max.   :94.00  
   Hum_pre_7        Hum_cont      Prec_pre_30        Prec_pre_15     
 Min.   :-1.00   Min.   :-1.00   Min.   :   -1.00   Min.   :  -1.00  
 1st Qu.:-1.00   1st Qu.:-1.00   1st Qu.:   -1.00   1st Qu.:  -1.00  
 Median :48.23   Median : 0.00   Median :    0.00   Median :   0.00  
 Mean   :37.00   Mean   :25.06   Mean   :   26.28   Mean   :  11.65  
 3rd Qu.:64.65   3rd Qu.:60.19   3rd Qu.:   18.90   3rd Qu.:   3.60  
 Max.   :96.00   Max.   :94.00   Max.   :13560.80   Max.   :2527.00  
   Prec_pre_7        Prec_cont         remoteness    
 Min.   :  -1.00   Min.   :  -1.00   Min.   :0.0000  
 1st Qu.:  -1.00   1st Qu.:  -1.00   1st Qu.:0.1378  
 Median :   0.00   Median :   0.00   Median :0.2021  
 Mean   :   4.69   Mean   :  15.59   Mean   :0.2368  
 3rd Qu.:   0.00   3rd Qu.:   0.00   3rd Qu.:0.2848  
 Max.   :1638.00   Max.   :2126.00   Max.   :1.0000  
spc_tbl_ [55,367 × 43] (S3: spec_tbl_df/tbl_df/tbl/data.frame)
 $ ...1            : num [1:55367] 0 1 2 3 4 5 6 7 8 9 ...
 $ Unnamed: 0      : num [1:55367] 0 1 2 3 4 5 6 7 8 9 ...
 $ fire_name       : chr [1:55367] NA NA NA "WNA  1" ...
 $ fire_size       : num [1:55367] 10 3 60 1 2 1 5.2 1 1 1 ...
 $ fire_size_class : chr [1:55367] "C" "B" "C" "B" ...
 $ stat_cause_descr: chr [1:55367] "Missing/Undefined" "Arson" "Arson" "Debris Burning" ...
 $ latitude        : num [1:55367] 18.1 35 34.9 39.6 30.7 ...
 $ longitude       : num [1:55367] -66.8 -87.6 -88.7 -119.3 -90.6 ...
 $ state           : chr [1:55367] "PR" "TN" "MS" "NV" ...
 $ disc_clean_date : chr [1:55367] "2/11/2007" "12/11/2006" "2/29/2004" "6/6/2005" ...
 $ cont_clean_date : chr [1:55367] NA NA NA "6/6/2005" ...
 $ discovery_month : chr [1:55367] "Feb" "Dec" "Feb" "Jun" ...
 $ disc_date_final : chr [1:55367] NA NA NA "6/6/2005 2:14" ...
 $ cont_date_final : chr [1:55367] NA NA NA "6/6/2005 18:00" ...
 $ putout_time     : chr [1:55367] NA NA NA "0 days 00:00:00.000000000" ...
 $ disc_date_pre   : chr [1:55367] "1/12/2007" "11/11/2006" "1/30/2004" "5/7/2005" ...
 $ disc_pre_year   : num [1:55367] 2007 2006 2004 2005 1999 ...
 $ disc_pre_month  : chr [1:55367] "Jan" "Nov" "Jan" "May" ...
 $ wstation_usaf   : chr [1:55367] "785140" "723235" "723235" "724880" ...
 $ dstation_m      : num [1:55367] 58918 32665 105122 43433 25941 ...
 $ wstation_wban   : num [1:55367] 11603 13896 13896 23185 3908 ...
 $ wstation_byear  : num [1:55367] 1945 1978 1978 1942 1987 ...
 $ wstation_eyear  : num [1:55367] 2018 2020 2020 2020 2016 ...
 $ Vegetation      : num [1:55367] 12 15 16 0 12 12 12 12 15 15 ...
 $ fire_mag        : num [1:55367] 1 0.1 1 0.1 0.1 0.1 0.1 0.1 0.1 0.1 ...
 $ weather_file    : chr [1:55367] "785140-11603-2007.gz" "723235-13896-2006.gz" "723235-13896-2004.gz" "724880-23185-2005.gz" ...
 $ Temp_pre_30     : num [1:55367] 24.48 7.55 4.97 16.28 -1 ...
 $ Temp_pre_15     : num [1:55367] 24.72 7.01 5.78 19 -1 ...
 $ Temp_pre_7      : num [1:55367] 24.903 0.344 5.559 18.143 -1 ...
 $ Temp_cont       : num [1:55367] 24.5 10.4 13.7 0 -1 ...
 $ Wind_pre_30     : num [1:55367] 4.34 2.71 3.36 4.05 -1 ...
 $ Wind_pre_15     : num [1:55367] 3.49 2.88 2.92 3.4 -1 ...
 $ Wind_pre_7      : num [1:55367] 3.26 1.98 2.7 3.67 -1 ...
 $ Wind_cont       : num [1:55367] 3.25 2.12 3.37 0 -1 ...
 $ Hum_pre_30      : num [1:55367] 78.2 70.8 75.5 44.8 -1 ...
 $ Hum_pre_15      : num [1:55367] 76.8 65.9 75.9 37.1 -1 ...
 $ Hum_pre_7       : num [1:55367] 76.4 55.5 76.8 35.4 -1 ...
 $ Hum_cont        : num [1:55367] 78.7 81.7 65.1 0 -1 ...
 $ Prec_pre_30     : num [1:55367] 0 59.8 168.8 10.4 -1 ...
 $ Prec_pre_15     : num [1:55367] 0 8.4 42.2 7.2 -1 -1 0 27.5 3.3 1.8 ...
 $ Prec_pre_7      : num [1:55367] 0 0 18.1 0 -1 -1 0 1.2 0 0 ...
 $ Prec_cont       : num [1:55367] 0 86.8 124.5 0 -1 ...
 $ remoteness      : num [1:55367] 0.0179 0.1844 0.1945 0.4874 0.2146 ...
 - attr(*, "spec")=
  .. cols(
  ..   ...1 = col_double(),
  ..   `Unnamed: 0` = col_double(),
  ..   fire_name = col_character(),
  ..   fire_size = col_double(),
  ..   fire_size_class = col_character(),
  ..   stat_cause_descr = col_character(),
  ..   latitude = col_double(),
  ..   longitude = col_double(),
  ..   state = col_character(),
  ..   disc_clean_date = col_character(),
  ..   cont_clean_date = col_character(),
  ..   discovery_month = col_character(),
  ..   disc_date_final = col_character(),
  ..   cont_date_final = col_character(),
  ..   putout_time = col_character(),
  ..   disc_date_pre = col_character(),
  ..   disc_pre_year = col_double(),
  ..   disc_pre_month = col_character(),
  ..   wstation_usaf = col_character(),
  ..   dstation_m = col_double(),
  ..   wstation_wban = col_double(),
  ..   wstation_byear = col_double(),
  ..   wstation_eyear = col_double(),
  ..   Vegetation = col_double(),
  ..   fire_mag = col_double(),
  ..   weather_file = col_character(),
  ..   Temp_pre_30 = col_double(),
  ..   Temp_pre_15 = col_double(),
  ..   Temp_pre_7 = col_double(),
  ..   Temp_cont = col_double(),
  ..   Wind_pre_30 = col_double(),
  ..   Wind_pre_15 = col_double(),
  ..   Wind_pre_7 = col_double(),
  ..   Wind_cont = col_double(),
  ..   Hum_pre_30 = col_double(),
  ..   Hum_pre_15 = col_double(),
  ..   Hum_pre_7 = col_double(),
  ..   Hum_cont = col_double(),
  ..   Prec_pre_30 = col_double(),
  ..   Prec_pre_15 = col_double(),
  ..   Prec_pre_7 = col_double(),
  ..   Prec_cont = col_double(),
  ..   remoteness = col_double()
  .. )
 - attr(*, "problems")=<externalptr> 

EDA

Column

  fire_size latitude  longitude fire_mag Temp_pre_30 Temp_pre_15 Temp_pre_7
1        10 18.10507  -66.75304      1.0   24.480974   24.716923 24.9025974
2         3 35.03833  -87.61000      0.1    7.553433    7.010000  0.3435294
3        60 34.94780  -88.72250      1.0    4.971930    5.782766  5.5587500
4         1 39.64140 -119.30830      0.1   16.275967   18.996181 18.1425641
5         2 30.70060  -90.59140      0.1   -1.000000   -1.000000 -1.0000000
  Temp_cont Wind_pre_30 Wind_pre_15 Wind_pre_7 Wind_cont Hum_pre_30 Hum_pre_15
1  24.52796    4.341807    3.492857   3.262092  3.250413   78.21659   76.79375
2  10.44830    2.709764    2.881707   1.976471  2.122320   70.84000   65.85891
3  13.69660    3.364499    2.923830   2.695833  3.369050   75.53163   75.86861
4   0.00000    4.054982    3.398329   3.671282  0.000000   44.77843   37.14081
5  -1.00000   -1.000000   -1.000000  -1.000000 -1.000000   -1.00000   -1.00000
  Hum_pre_7 Hum_cont Prec_pre_30 Prec_pre_15 Prec_pre_7 Prec_cont remoteness
1  76.38158 78.72437         0.0         0.0        0.0       0.0 0.01792339
2  55.50588 81.68268        59.8         8.4        0.0      86.8 0.18435495
3  76.81283 65.06380       168.8        42.2       18.1     124.5 0.19454351
4  35.35385  0.00000        10.4         7.2        0.0       0.0 0.48744745
5  -1.00000 -1.00000        -1.0        -1.0       -1.0      -1.0 0.21463294

# Kmeans

Model

###decision tree
n= 55367 

node), split, n, deviance, yval
      * denotes terminal node

1) root 55367 1.208971e+13  2104.645  
  2) remoteness< 0.5367243 53910 6.795798e+12  1501.234 *
  3) remoteness>=0.5367243 1457 4.548005e+12 24431.260  
    6) remoteness>=0.7348854 551 3.411093e+11  8761.659 *
    7) remoteness< 0.7348854 906 3.989325e+12 33961.010 *

Predicted fire size: 8761.659

Evaluation

---
title: "Forest Fire Prediction"
output: 
  flexdashboard::flex_dashboard:
    orientation: columns
    vertical_layout: scroll
    theme: cosmo
    social: menu
    source_code: embed
---

```{r setup, include=FALSE}
library(flexdashboard)
library(readr)
library(ggplot2)
library(dplyr)
library(lattice)
library(stats)
library(corrplot)
library(rpart.plot)
library(caret)
library(rpart)


```


#About data
-----------------------------------------------------------------------
###Dataset descrpiton
```{r}
print("This dataset has 43 attributes and 55367 instance. It has fire name,fire size,fire magnitude,latitude,longitude,state,temperature,weather,humidity etc.., ")
print("The dataset presented here is a sub-sample of data presented here :1.88 Million US Fires. I took this dataset and downselected to a random sampling of 50,000 fire samples and combined this dataset with historical weather data at a specific lat/long, historical vegetation data. A metric is representing the measure of the remoteness of a fire using city lat/long database.")

```

###Problem Statement
```{r}
print("Forest fire destroys plants,animals species,ecosystm,living organsisms.It cause big disaster in living things.")
```
###  view dataset

```{r}
data  <- read_csv("forest data.csv")
data
summary(data)
str(data)

```



# EDA
Column {data-width=350, .tabset}
-----------------------------------------------------------------------

```{r}
df <- data.frame(
  fire_size = data$fire_size,
  latitude = data$latitude,
  longitude = data$longitude,
  fire_mag = data$fire_mag,
  Temp_pre_30 = data$Temp_pre_30,
  Temp_pre_15 = data$Temp_pre_15,
  Temp_pre_7 = data$Temp_pre_7,
  Temp_cont = data$Temp_cont,
  Wind_pre_30 = data$Wind_pre_30,
  Wind_pre_15 = data$Wind_pre_15,
  Wind_pre_7 = data$Wind_pre_7,
  Wind_cont = data$Wind_cont,
  Hum_pre_30 = data$Hum_pre_30,
  Hum_pre_15 = data$Hum_pre_15,
  Hum_pre_7 = data$Hum_pre_7,
  Hum_cont = data$Hum_cont,
  Prec_pre_30 = data$Prec_pre_30,
  Prec_pre_15 = data$Prec_pre_15,
  Prec_pre_7 = data$Prec_pre_7,
  Prec_cont = data$Prec_cont,
  remoteness = data$remoteness
)

head(df,5)

histogram(df$Temp_pre_30,breaks = 50, xlab = "firesize",ylab = "counts",
          main = "distribution of temprature")

histogram(df$Wind_pre_30,breaks = 50,
          xlab = "Wind_pre_30",ylab = "counts",
          main = "distribution of wind")

histogram(df$Hum_pre_30,breaks = 50,
          xlab = "hum_pre_30",ylab = "counts",
          main = "distribution of humidity")

histogram(df$remoteness,breaks = 50,
          xlab = "remoteness",ylab = "counts",
          main = "distribution of remoteness")

histogram(df$latitude,breaks = 50,
          xlab = "latitude",ylab = "counts",
          main = "distribution of latitude")


histogram(df$longitude,breaks = 50,
          xlab = "longitude",ylab = "counts",
          main = "distribution of longitude")



xyplot(remoteness~fire_size,data  = df,
       main = "firesize based on remoteness",xlab = "firesize",
       ylab ="remoteness")

xyplot(Hum_pre_30~fire_size,data  = df,
       main = "fire size based on humidity",xlab = "firesize",
       ylab ="humidity per 30")

xyplot(Wind_pre_30~fire_size,data  = df,
       main = "fire size based on wind",xlab = "firesize",
       ylab ="wind_per_30")

xyplot(Temp_pre_30~fire_size,data  = df,
       main = "fire size based on temp",xlab = "firesize",
       ylab ="temp_per_30")   



correlation_matrix <- cor(df)
heatmap(correlation_matrix, 
        col = heat.colors(10), 
        margins = c(10, 10))

```

-----------------------------------------------------------------------
# Kmeans
```{r}
df_scaled <- scale(df)

kmeans_results <- kmeans(df_scaled, centers = 3)

df$cluster <- kmeans_results$cluster

ggplot(df, aes(x = Temp_pre_30, y =fire_size , color = cluster)) +
  geom_point() +
  labs(title = "K-means Clustering Results",
       x = "temp pre 30", y = "firesize")

```
-----------------------------------------------------------------------

# Model 

###decision tree
```{r}

features <- c("Temp_pre_30", "Wind_pre_30", "Hum_pre_30", "remoteness")

tree_formula <- as.formula(paste("fire_size ~", paste(features, collapse = " + "), sep=""))

ctrl <- rpart.control(cp = 0.01, minbucket = 20)  

fire_size_tree <- rpart(tree_formula, data = df, control = ctrl)

print(fire_size_tree)
plot(fire_size_tree)
text(fire_size_tree, simplify = TRUE)  

new_data <- data.frame(
  Temp_pre_30 = 25,
  Wind_pre_30 = 10,
  Hum_pre_30 = 60,
  remoteness = 50
)

predicted_fire_size <- predict(fire_size_tree, newdata = new_data)



cat("Predicted fire size:", predicted_fire_size)

```


-----------------------------------------------------------------------


# Evaluation

```{r}

set.seed(123)  
train_index <- createDataPartition(df$fire_size, p = 0.7, list = FALSE)
train_data <- df[train_index, ]
test_data <- df[-train_index, ]

fire_formula <- as.formula("fire_size ~ Temp_pre_30 + Wind_pre_30 + Hum_pre_30 + remoteness")

fire_tree <- rpart(formula = fire_formula, data = train_data)

rpart.plot(fire_tree)

```
-----------------------------------------------------------------------