#Q1. Describe and cite your data. Discuss how the data was collected and the implications of this on the generalizability of your analysis, error in the dataset, and bias in the dataset.
#For this project, my goal demonstrate the correlation between global climate trends, and determine if there is a relationship between CO2 emissions and the occurrence of natural disasters/extreme weather events. Datasets are csv files, and include change in global annual surface temperature and frequency of global environmental disasters, from International Monetary Fund - CLimate Change Dashboard, and global CO2 emissions per capita, from Global Carbon Budget. Datasets were adjusted to only display data from the time frame 1980 - 2020.
#figure out how to adjust dataframe permanently
#Q2. Is your data tidy? Why or why not? What steps must be taken to clean your data? Take these steps and provide code.
library(tidyverse)
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ dplyr 1.1.4 ✔ readr 2.1.5
## ✔ forcats 1.0.0 ✔ stringr 1.5.1
## ✔ ggplot2 3.4.4 ✔ tibble 3.2.1
## ✔ lubridate 1.9.3 ✔ tidyr 1.3.1
## ✔ purrr 1.0.2
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag() masks stats::lag()
## ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
library(ggplot2)
setwd('C:/Users/aklig/OneDrive/Desktop/R studio final project/data')
surftemp <- read.csv('C:/Users/aklig/OneDrive/Desktop/R studio final project/data/Annual_Surface_Temperature_Change.csv')
disaster <- read.csv('C:/Users/aklig/OneDrive/Desktop/R studio final project/data/Climate-related_Disasters_Frequency.csv')
co2 <- read.csv('C:/Users/aklig/OneDrive/Desktop/R studio final project/data/co-emissions-per-capita.csv')
#Data is not tidy. Data frames have different numbers of rows and columns, and not all columns represent the same value. To tidy up the data, I will account for missing data and I will remove columns that do not hae valuable information. Data should be gathered together so that each data frame has three columns that each represent the same thing -
#omit missing value
surftemp <- na.omit(surftemp)
disaster <- na.omit(disaster)
co2 <- na.omit(co2)
#remove unncessary columns
library(dplyr)
surftemp2 <- surftemp %>% select(-c(ObjectId, ISO2, ISO3, Indicator, Unit, Source, CTS_Code, CTS_Name, CTS_Full_Descriptor))
disaster2 <- disaster %>% select(-c(ObjectId, ISO2, ISO3, Indicator, Unit, Source, CTS_Code, CTS_Name, CTS_Full_Descriptor))
co2_ <- co2 %>% select(-c(Code))
#tidy data, make new variable column for year instead of having each year listed individally
library(tidyr)
library(ggplot2)
library(readr)
tidy_disaster <- gather(data = disaster2, key = year, value = temp, F1980:F2022)
tidy_temp <- gather(data = surftemp2, key = year, value = temp, F1961:F2022)
#rename last column from temp to value to better represent all three data frames
colnames(tidy_temp) <- c('Country', 'Year', 'Value')
colnames(tidy_disaster) <- c('Country', 'Year', 'Value')
colnames(co2_) <- c('Country', 'Year', 'Value')
#remove years from before 1980
#tidy_temp[!(tidy_temp$Year %in% c("F1961", "F1962", "F1963", "F1964", "F1965", "F1966", "F1967", "F1968", "F1969", "F1970", "F1971", "F1972", "F1973", "F1974", "F1975", "F1976", "F1977", "F1978", "F1979")),]
#co2_[!(co2_$Year %in% c("1750":"1979")),]
#Q3. List at least two research questions you could answer with your data. Describe the variables used to answer these questions and, if relevant, any additional data you may need to collect or construct.
#1 How have climate trends changed since the 1980s?
#2 Has there been an increase in frequency of extreme weather events over the past decade, and if so, is that correlated to rise in global temperature?
#Variables include country, year (from 1980-2020), and value. Value can represent change in degrees C, change in tons CO2, and incident of a natural disaster.
#Q4. For each question, do a “methods brainstorm.” How might you answer these questions based on what you know so far about spatial analysis?
#Gather and view data
#Ensure data is tidy.
#Graph has boxplot and remove outliers
#Graph both temperature and co2 data together in a bar graph and line plot, and analyze trends
#Merge temperature and disaster data together, and graph together.
#Display data as graph from both 1980 and 2020.
#Plot data frame son same graph to visualize correlation.
#perform statistical analysis - line regression, multiple regression, histogram
#calculate mean and standard deviation
#Q5. Visualize the center and spread of key variables. Describe the distribution of each variable.
#histogram
hist(co2_$Value)
number_of_bins = 200
hist(tidy_disaster$Value)
hist(tidy_temp$Value)
#Co2 data set shows only one bar,
#need to try to eliminate outliers or convert to larger values
#Disaster data set is positively right skewed because it involves disaster events per year. The range is from 0 to 40, meaning that some years there were no disasters and some there were as many as 40 occurrences.
#Temperature data set is right skewed, ranging from a value of -1.5 to 3. These values represent 1.5 degree C cooling to 3 degree C warming.
#Q6. Develop a strategy to identify outliers. Query data to see if there are any shared characteristics of outlier observations.
#boxplot
co2_new <- filter_if(co2_, is.numeric, all_vars((.) != 0))
boxplot(co2_$Value)
boxplot(tidy_disaster$Value)
boxplot(tidy_temp$Value)
detect_outlier <- function(x) {
Quantile1 <- quantile(x, probs=.25)
Qunatile3 <- quantile(x, probs-.75)
IQR = Quantile3 - Quantile1
x > Quantile3 + (IQR * 1.5) | x < Quantile1 - (IQR * 1.5)
}
remove_outlier <- function(co2_, columns = names(co2_)) {
for (col in columns) {
co2_ <- co2_[!detect_outlier(co2_[[col]]),]
}
("Remove outliers")
head(co2_)
}
#zoom in view if possible?
#Q7. Group the data. Compute and visualize summary statistics for each group.
library(dplyr)
temp_disaster <- merge(tidy_temp,tidy_disaster,by="Year")
temp_disaster2 <- merge(tidy_temp,tidy_disaster,by="Country")
temp_co2 <- merge(tidy_temp, co2_, by="Year")
temp_co2_2 <- merge(tidy_temp, co2_, by="Country")
temp_tbl <- tbl_df(tidy_temp)
## Warning: `tbl_df()` was deprecated in dplyr 1.0.0.
## ℹ Please use `tibble::as_tibble()` instead.
## Call `lifecycle::last_lifecycle_warnings()` to see where this warning was
## generated.
disasters_tbl <- tbl_df(tidy_disaster)
## Warning: `tbl_df()` was deprecated in dplyr 1.0.0.
## ℹ Please use `tibble::as_tibble()` instead.
## Call `lifecycle::last_lifecycle_warnings()` to see where this warning was
## generated.
summarise(temp_tbl, mean(Year), mean(Value))
## Warning: There was 1 warning in `summarise()`.
## ℹ In argument: `mean(Year)`.
## Caused by warning in `mean.default()`:
## ! argument is not numeric or logical: returning NA
## # A tibble: 1 × 2
## `mean(Year)` `mean(Value)`
## <dbl> <dbl>
## 1 NA 0.505
temp_grouped <- group_by(temp_tbl, Year)
#merge disaster dataset with temp dataset, combine columns of years and values
#Q8. Compute and visualize correlation between two variables.
library(ggplot2)
ggplot(tidy_temp,
aes(x = Year, y = Value)) +
geom_point() +
labs(title = "Change in Global Surface Temperature")
ggplot(tidy_disaster,
aes(x = Year, y = Value)) +
geom_point() +
labs(title = "Environmental Disaster Frequency")
ggplot(co2_,
aes(x = Year, y = Value)) +
geom_point() +
labs(title = "Change in Global CO2 Emissions")
#remove unnecessary years from data frame
#Q9. Create an exploratory and multivariate visualization of your dataset based on one or more of your research questions.
library(ggplot2)
ggplot() +
geom_line(data=tidy_temp, aes(x="Year", y="Value"), color='blue') +
geom_line(data=tidy_disaster, aes(x="Year", y="Value"), color='red') +
geom_line(data=co2_, aes(x="Year", y="Value"), color='green')
#data frames must be on same scale
```