library(dplyr) library(lubridate) library(tidyr) library(ggplot2) library(sf)…
- User
library(dplyr) library(lubridate) library(tidyr) library(ggplot2) library(sf) library(leaflet) library(htmlwidgets) library(prophet)
Clean data
data <- data %>% mutate( state = replace_na(state, "Unknown"), date = as.Date(date), 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 state
agg_data <- data %>% group_by(state) %>% summarise( total_confirmed = sum(confirmed), total_recovered = sum(recovered), total_deaths = sum(death) ) %>% ungroup()
Visualizations
Top states by confirmed cases
top_states_confirmed <- agg_data %>% arrange(desc(total_confirmed)) %>% head(10) ggplot(top_states_confirmed, aes(x=reorder(state, total_confirmed), y=total_confirmed)) + geom_bar(stat="identity", fill="steelblue") + coord_flip() + labs(title="Top 10 States by Confirmed Cases", x="State", y="Total Confirmed Cases")
Top states by deaths
top_states_deaths <- agg_data %>% arrange(desc(total_deaths)) %>% head(10) ggplot(top_states_deaths, aes(x=reorder(state, total_deaths), y=total_deaths)) + geom_bar(stat="identity", fill="red") + coord_flip() + labs(title="Top 10 States by Deaths", x="State", y="Total Deaths")
Top states by recovered cases
top_states_recovered <- agg_data %>% arrange(desc(total_recovered)) %>% head(10) ggplot(top_states_recovered, aes(x=reorder(state, total_recovered), y=total_recovered)) + geom_bar(stat="identity", fill="green") + coord_flip() + labs(title="Top 10 States by Recovered Cases", x="State", 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("State: ", state, "", "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()
Handle NA values
time_series_data <- na.omit(time_series_data)
Rename columns for Prophet
prophet_data <- time_series_data %>% rename(ds = date, y = total_confirmed) if (nrow(prophet_data) < 2) { stop("Error: Not enough data points to fit the model") } else { model <- tryCatch({ prophet(prophet_data) print("Model fitted successfully") }, error = function(e) { print("Error fitting model: ") print(e) stop("Model fitting failed") }) }
- Assistant
- User
Create future dataframe
future <- make_future_dataframe(model, periods = 30) Error in m operator is invalid for atomic vectors correct this error only
- Assistant
- User
fix only this par(mar = c(0, 0, 2, 0))
mapCountryData(map_data, nameColumnToPlot = "total_confirmed",
-
code
mapTitle = "Global Distribution of COVID-19 Confirmed Cases", -
code
colourPalette = "heat")
You asked for 7 quantiles, only 0 could be created in quantiles classification Error in cut.default(dataCategorised, cutVector, include.lowest = TRUE) : invalid number of intervals
-
- Assistant