ok is this all enough
library(dplyr)
library(lubridate)
library(tidyr)
data <- data %>%
mutate(
state = ifelse(state == "", "Unknown", state),
date = mdy(date),
lat = as.numeric(lat),
lon = as.numeric(lon)
) %>%
filter(
lat >= -90 & lat <= 90 &
lon >= -180 & lon <= 180 &
!(confirmed == 0 & recovered == 0 & death == 0)
)
head(data)
str(data)
summary(data)
agg_data <- data %>%
group_by(country) %>%
summarise(
total_confirmed = sum(confirmed),
total_recovered = sum(recovered),
total_deaths = sum(death)
) %>%
ungroup()
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_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_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")
library(sf)
library(leaflet)
data_sf <- st_as_sf(data, coords = c("lon", "lat"), crs = 4326)
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")
leaflet(data_sf) %>%
addTiles() %>%
addCircleMarkers(
radius = ~log1p(death+1),
color = "red",
popup = ~paste0("Country: ", country, "",
"Confirmed Cases: ", confirmed, "",
"Recovered: ", recovered, "",
"Deaths: ", death)
) %>%
addLegend("bottomright",
colors=c("red"),
labels=c("Deaths"),
opacity=0.5,
title="COVID-19 Deaths")
leaflet(data_sf) %>%
addTiles() %>%
addCircleMarkers(
radius = ~log1p(recovered+1),
color = "green",
popup = ~paste0("Country: ", country, "",
"Confirmed Cases: ", confirmed, "",
"Recovered: ", recovered, "",
"Deaths: ", death)
) %>%
addLegend("bottomright",
colors=c("green"),
labels=c("Recovered"),
opacity=0.5,
title="COVID-19 Recovered")
Load necessary libraries
library(dplyr)
library(lubridate)
library(tidyr)
library(ggplot2)
library(sf)
library(leaflet)
library(rworldmap)
library(htmlwidgets) # For saving Leaflet maps as HTML files
Preprocess the data
data <- data %>%
mutate(
state = ifelse(state == "", "Unknown", state),
date = mdy(date),
lat = as.numeric(lat),
lon = as.numeric(lon)
) %>%
filter(
lat >= -90 & lat <= 90 &
lon >= -180 & lon <= 180 &
!(confirmed == 0 & recovered == 0 & death == 0)
)
Inspect the cleaned data
head(data)
str(data)
summary(data)
Aggregate data by country
agg_data <- data %>%
group_by(country) %>%
summarise(
total_confirmed = sum(confirmed),
total_recovered = sum(recovered),
total_deaths = sum(death)
) %>%
ungroup()
Plotting top 10 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")
Plotting top 10 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")
Plotting top 10 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")
Summary statistics
summary_stats <- agg_data %>%
summarise(
total_confirmed = sum(total_confirmed),
total_recovered = sum(total_recovered),
total_deaths = sum(total_deaths),
mean_confirmed = mean(total_confirmed),
mean_recovered = mean(total_recovered),
mean_deaths = mean(total_deaths)
)
print(summary_stats)
Heatmap of cases
ggplot(data, aes(x = lon, y = lat)) +
geom_hex(bins = 30) +
scale_fill_gradient(low = "blue", high = "red") +
labs(title="Heatmap of COVID-19 Cases",
x="Longitude", y="Latitude")
World Map visualization
map_data <- joinCountryData2Map(agg_data, joinCode = "NAME", nameJoinColumn = "country")
par(mar = c(0, 0, 2, 0))
mapCountryData(map_data, nameColumnToPlot = 'total_confirmed',
mapTitle = 'Global Distribution of COVID-19 Confirmed Cases',
colourPalette = 'heat')
Geospatial interactive maps
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")
Interactive Map of Death Cases
m_deaths <- leaflet(data_sf) %>%
addTiles() %>%
addCircleMarkers(
radius = ~log1p(death+1),
color = "red",
popup = ~paste0("Country: ", country, "",
"Confirmed Cases: ", confirmed, "",
"Recovered: ", recovered, "",
"Deaths: ", death)
) %>%
addLegend("bottomright",
colors = c("red"),
labels = c("Deaths"),
opacity = 0.5,
title="COVID-19 Deaths")
saveWidget(m_deaths, "Death_Cases_Map.html")
Interactive Map of Recovered Cases
Interactive Map of Recovered Cases
m_recovered <- leaflet(data_sf) %>%
addTiles() %>%
addCircleMarkers(
radius = ~log1p(recovered+1),
color = "green",
popup = ~paste0("Country: ", country, "",
"Confirmed Cases: ", confirmed, "",
"Recovered: ", recovered, "",
"Deaths: ", death)
) %>%
addLegend("bottomright",
colors = c("green"),
labels = c("Recovered"),
opacity = 0.5,
title = "COVID-19 Recovered")
saveWidget(m_recovered, "Recovered_Cases_Map.html")
library(prophet)
library(dplyr)
Example of how to load your data
Replace this section with actual data loading step
data <- data.frame(
date = as.Date('2020-01-01') + 0:29, # Example dates
confirmed = sample(100:1000, 30, replace = TRUE) # Example confirmed cases
)
Ensure the data is read correctly
head(data)
Check the data types of the columns
str(data)
Aggregate data by date
time_series_data <- data %>%
group_by(date) %>%
summarise(total_confirmed = sum(confirmed)) %>%
ungroup()
Verify the aggregated data
print(time_series_data)
Check for and handle any NA values in date or confirmed
time_series_data <- na.omit(time_series_data)
Ensure there are still rows in the dataset
if(nrow(time_series_data) == 0) {
stop("Error: Data frame is empty after removing NA values")
}
Rename columns as required by Prophet
prophet_data <- time_series_data %>%
rename(ds = date, y = total_confirmed)
Check final dataset before model fitting
print(head(prophet_data))
Fit the Prophet model
if (nrow(prophet_data) < 2) {
stop("Error: Not enough data points to fit the model")
} else {
model <- prophet(prophet_data)
print("Model fitted successfully")
}
Create a DataFrame for future dates (e.g., predicting for the next 30 days)
future <- make_future_dataframe(model, periods = 30)
print("Future DataFrame:")
print(head(future))
Use the model to make predictions
forecast <- predict(model, future)
print("Forecast DataFrame:")
print(head(forecast))
Visualize the forecast
plot(model, forecast)
prophet_plot_components(model, forecast)
Evaluate the model (optional)
You might want to split your data into training and testing sets and then compare the actual vs predicted values
For demonstration, checking the overall summary metrics
train_data and test_data splits can be added
performance <- performance_metrics(forecast)
print("Performance Metrics:")
print(performance)