give me code sections of preprocessing,eda and preidctiv emodelling Load required…
- User
give me code sections of preprocessing,eda and preidctiv emodelling
Load required libraries
library(dplyr) library(lubridate) library(tidyr) library(ggplot2) library(sf) library(leaflet) library(htmlwidgets) library(prophet) library(RColorBrewer) library(rworldmap)
Assume that ‘CV_LatLon_21Jan_12Mar’ is the pre-loaded dataframe containing the COVID-19 data
Clean data
data <- CV_LatLon_21Jan_12Mar %>% mutate( state = replace_na(state, “Unknown”), country = replace_na(country, “Unknown”), # Added country column handling date = as.Date(date, format = “%m/%d/%y”), # Specify the date format lat = as.numeric(lat), lon = as.numeric(lon) ) %>% filter( lat >= -90 & lat <= 90 & lon >= -180 & lon <= 180 & !(confirmed == 0 & recovered == 0 & death == 0) )
Aggregate data by country
agg_data <- data %>% group_by(country) %>% summarise( total_confirmed = sum(confirmed), total_recovered = sum(recovered), total_deaths = sum(death) ) %>% ungroup()
Visualizations
Top countries by confirmed cases
top_countries_confirmed <- agg_data %>% arrange(desc(total_confirmed)) %>% head(10) ggplot(top_countries_confirmed, aes(x=reorder(country, total_confirmed), y=total_confirmed)) + geom_bar(stat=“identity”, fill=“steelblue”) + coord_flip() + labs(title=“Top 10 Countries by Confirmed Cases”, x=“Country”, y=“Total Confirmed Cases”)
Top countries by deaths
top_countries_deaths <- agg_data %>% arrange(desc(total_deaths)) %>% head(10) ggplot(top_countries_deaths, aes(x=reorder(country, total_deaths), y=total_deaths)) + geom_bar(stat=“identity”, fill=“red”) + coord_flip() + labs(title=“Top 10 Countries by Deaths”, x=“Country”, y=“Total Deaths”)
Top countries by recovered cases
top_countries_recovered <- agg_data %>% arrange(desc(total_recovered)) %>% head(10) ggplot(top_countries_recovered, aes(x=reorder(country, total_recovered), y=total_recovered)) + geom_bar(stat=“identity”, fill=“green”) + coord_flip() + labs(title=“Top 10 Countries by Recovered Cases”, x=“Country”, y=“Total Recovered Cases”)
Geospatial Analysis Using Leaflet
Convert to spatial object
data_sf <- st_as_sf(data, coords = c(“lon”, “lat”), crs = 4326)
Interactive Map of Confirmed Cases
m_confirmed <- leaflet(data_sf) %>% addTiles() %>% addCircleMarkers( radius = ~log1p(confirmed + 1), color = “blue”, popup = ~paste0(“Country: ”, country, “”, “Confirmed Cases: ”, confirmed, “”, “Recovered: ”, recovered, “”, “Deaths: ”, death) ) %>% addLegend(“bottomright”, colors = c(“blue”), labels = c(“Confirmed Cases”), opacity = 0.5, title = “COVID-19 Cases”) saveWidget(m_confirmed, “Confirmed_Cases_Map.html”)
Time Series Forecasting with Prophet
Aggregate data by date
time_series_data <- data %>% group_by(date) %>% summarise(total_confirmed = sum(confirmed)) %>% ungroup()
- Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.