# 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 ‘data’ is pre-loaded dataframe containing the COVID-19 data
> 
> # 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("<strong>State: </strong>", state, "<br>",
+                         "<strong>Confirmed Cases: </strong>", confirmed, "<br>",
+                         "<strong>Recovered: </strong>", recovered, "<br>",
+                         "<strong>Deaths: </strong>", 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)
> 
> # First ensure your data is correctly structured
> prophet_data <- time_series_data %>%
+     rename(ds = date, y = total_confirmed)
> 
> # Fit the model with error handling
> model <- tryCatch({
+     prophet(prophet_data)  # Fit the prophet model
+ }, error = function(e) { 
+     print("Error fitting model: ")
+     print(e)
+     return(NULL)  # Return NULL if an error occurs
+ })
Disabling yearly seasonality. Run prophet with yearly.seasonality=TRUE to override this.
Disabling daily seasonality. Run prophet with daily.seasonality=TRUE to override this.
n.changepoints greater than number of observations. Using 23
> 
> # Proceed only if model fitting was successful
> if (!is.null(model) && inherits(model, "prophet")) {
+     # Create future dataframe
+     future <- make_future_dataframe(model, periods = 30)
+     future <- future %>%
+         mutate(ds = as.Date(ds, format = "%Y-%m-%d"))
+     
+     # Predict and visualize
+     forecast <- predict(model, future)
+     plot(model, forecast)
+     prophet_plot_components(model, forecast)
+ } else {
+     print("Model fitting failed. Future dataframe and forecast were not created.")
+ }
> 
> # Create future dataframe
> future <- make_future_dataframe(model, periods = 30)
> future <- future %>%
+     mutate(ds = as.Date(ds, format = "%Y-%m-%d")) # Ensure dates are in Date format
> forecast <- predict(model, future)
> 
> # Visualize the forecast
> if (exists("model") && !inherits(model, "error")) {
+     plot(model, forecast)
+     prophet_plot_components(model, forecast)
+ }
> 
> # 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)
# A tibble: 1 × 6
  total_confirmed total_recovered total_deaths mean_confirmed mean_recovered
            <int>           <int>        <int>          <dbl>          <dbl>
1           16067            7424          774          16067           7424
# ℹ 1 more variable: mean_deaths <dbl>
> 
> # Heatmap
> 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")
> 
> # Example: If you had a country column, you could visualize it as follows
> # World Map Visualization
> map_data <- joinCountryData2Map(agg_data, joinCode = "NAME", nameJoinColumn = "state") # Assuming ‘state’ refers to country in actual join
0 codes from your data successfully matched countries in the map
2 codes from your data failed to match with a country code in the map
243 codes from the map weren't represented in your data
> par(mar = c(0, 0, 2, 0))
> mapCountryData(map_data, nameColumnToPlot = "total_confirmed", 
+                mapTitle = "Global Distribution of COVID-19 Confirmed Cases", 
+                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

fix last line error
this is how data look "	state	country	lat	lon	date	confirmed	recovered	death
0	0	Thailand	15	101	1/22/20	2	0	0
1	0	Japan	36	138	1/22/20	2	0	0
2	0	Singapore	1.2833	103.8333	1/22/20	0	0	0
3	0	Nepal	28.1667	84.25	1/22/20	0	0	0
4	0	Malaysia	2.5	112.5	1/22/20	0	0	0
5	British Columbia	Canada	49.2827	-123.1207	1/22/20	0	0	0
6	New South Wales	Australia	-33.8688	151.2093	1/22/20	0	0	0
7	Victoria	Australia	-37.8136	144.9631	1/22/20	0	0	0
8	Queensland	Australia	-28.0167	153.4	1/22/20	0	0	0
9	0	Cambodia	11.55	104.9167	1/22/20	0	0	0
10	0	Sri Lanka	7	81	1/22/20	0	0	0
11	0	Germany	51	9	1/22/20	0	0	0
12	0	Finland	64	26	1/22/20	0	0	0
"
correct and give me the code