Skip to content

Commit

Permalink
Merge pull request #16 from kdorheim/temp_smoothing
Browse files Browse the repository at this point in the history
Tried smoothing temp data
  • Loading branch information
ptrscll authored Jun 18, 2024
2 parents cbc95df + 8c0f66c commit 69ee509
Show file tree
Hide file tree
Showing 10 changed files with 381 additions and 7 deletions.
Binary file added results/graph_smooth_temps.jpeg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
39 changes: 39 additions & 0 deletions results/smooth_temps_10yr.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
parameters values
beta 0.528262342844422
q10_rh 1.76
diff 2.4

Objective Function Value: 2.32

CO2 MSE: 4.57
T MSE: 0.0772
RMSE: 2.14

***Key Metrics***
TCRE: 1.51
TCR: 1.76

***Historical Warming and ERF***
GSAT Warming: 0.73
Ocean Heat Content Change: 472
Total Aerosol ERF: -1.24
WMGHG ERF: 3.87
Methane ERF: 0.54

***Future Warming***
scenario start end GSAT
ssp119 2021 2040 0.729
ssp119 2041 2060 0.894
ssp119 2081 2100 0.717
ssp126 2021 2040 0.744
ssp126 2041 2060 1.08
ssp126 2081 2100 1.1
ssp245 2021 2040 0.749
ssp245 2041 2060 1.29
ssp245 2081 2100 1.98
ssp370 2021 2040 0.76
ssp370 2041 2060 1.43
ssp370 2081 2100 2.93
ssp585 2021 2040 0.876
ssp585 2041 2060 1.74
ssp585 2081 2100 3.79
39 changes: 39 additions & 0 deletions results/smooth_temps_164yr.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
parameters values
beta 0.528262550029036
q10_rh 1.76
diff 2.4

Objective Function Value: 2.3

CO2 MSE: 4.57
T MSE: 0.0772
RMSE: 2.14

***Key Metrics***
TCRE: 1.51
TCR: 1.76

***Historical Warming and ERF***
GSAT Warming: 0.73
Ocean Heat Content Change: 472
Total Aerosol ERF: -1.24
WMGHG ERF: 3.87
Methane ERF: 0.54

***Future Warming***
scenario start end GSAT
ssp119 2021 2040 0.729
ssp119 2041 2060 0.894
ssp119 2081 2100 0.717
ssp126 2021 2040 0.744
ssp126 2041 2060 1.08
ssp126 2081 2100 1.1
ssp245 2021 2040 0.749
ssp245 2041 2060 1.29
ssp245 2081 2100 1.98
ssp370 2021 2040 0.76
ssp370 2041 2060 1.43
ssp370 2081 2100 2.93
ssp585 2021 2040 0.876
ssp585 2041 2060 1.74
ssp585 2081 2100 3.79
39 changes: 39 additions & 0 deletions results/smooth_temps_3yr.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
parameters values
beta 0.528263386504877
q10_rh 1.76
diff 2.4

Objective Function Value: 2.32

CO2 MSE: 4.57
T MSE: 0.0772
RMSE: 2.14

***Key Metrics***
TCRE: 1.51
TCR: 1.76

***Historical Warming and ERF***
GSAT Warming: 0.73
Ocean Heat Content Change: 472
Total Aerosol ERF: -1.24
WMGHG ERF: 3.87
Methane ERF: 0.54

***Future Warming***
scenario start end GSAT
ssp119 2021 2040 0.729
ssp119 2041 2060 0.894
ssp119 2081 2100 0.717
ssp126 2021 2040 0.744
ssp126 2041 2060 1.08
ssp126 2081 2100 1.1
ssp245 2021 2040 0.749
ssp245 2041 2060 1.29
ssp245 2081 2100 1.98
ssp370 2021 2040 0.76
ssp370 2041 2060 1.43
ssp370 2081 2100 2.93
ssp585 2021 2040 0.876
ssp585 2041 2060 1.74
ssp585 2081 2100 3.79
39 changes: 39 additions & 0 deletions results/smooth_temps_50yr.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
parameters values
beta 0.528261345579967
q10_rh 1.76
diff 2.4

Objective Function Value: 2.32

CO2 MSE: 4.57
T MSE: 0.0772
RMSE: 2.14

***Key Metrics***
TCRE: 1.51
TCR: 1.76

***Historical Warming and ERF***
GSAT Warming: 0.73
Ocean Heat Content Change: 472
Total Aerosol ERF: -1.24
WMGHG ERF: 3.87
Methane ERF: 0.54

***Future Warming***
scenario start end GSAT
ssp119 2021 2040 0.729
ssp119 2041 2060 0.894
ssp119 2081 2100 0.717
ssp126 2021 2040 0.744
ssp126 2041 2060 1.08
ssp126 2081 2100 1.1
ssp245 2021 2040 0.749
ssp245 2041 2060 1.29
ssp245 2081 2100 1.98
ssp370 2021 2040 0.76
ssp370 2041 2060 1.43
ssp370 2081 2100 2.93
ssp585 2021 2040 0.876
ssp585 2041 2060 1.74
ssp585 2081 2100 3.79
39 changes: 39 additions & 0 deletions results/smooth_temps_5yr.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
parameters values
beta 0.528263352380768
q10_rh 1.76
diff 2.4

Objective Function Value: 2.32

CO2 MSE: 4.57
T MSE: 0.0772
RMSE: 2.14

***Key Metrics***
TCRE: 1.51
TCR: 1.76

***Historical Warming and ERF***
GSAT Warming: 0.73
Ocean Heat Content Change: 472
Total Aerosol ERF: -1.24
WMGHG ERF: 3.87
Methane ERF: 0.54

***Future Warming***
scenario start end GSAT
ssp119 2021 2040 0.729
ssp119 2041 2060 0.894
ssp119 2081 2100 0.717
ssp126 2021 2040 0.744
ssp126 2041 2060 1.08
ssp126 2081 2100 1.1
ssp245 2021 2040 0.749
ssp245 2041 2060 1.29
ssp245 2081 2100 1.98
ssp370 2021 2040 0.76
ssp370 2041 2060 1.43
ssp370 2081 2100 2.93
ssp585 2021 2040 0.876
ssp585 2041 2060 1.74
ssp585 2081 2100 3.79
29 changes: 28 additions & 1 deletion scripts/error_functions.R
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
# observed values
mse <- function(x, y) {
SE <- (x - y)^2
return(mean(SE))
return(mean(SE[!is.na(SE)]))
}

# rmse - function to find root mean squared error between two vectors
Expand Down Expand Up @@ -72,5 +72,32 @@ mean_T_CO2_mse <- function(obs_data, hector_data) {
var = CONCENTRATIONS_CO2(),
yrs = c(1750, 1850:2014))

return(mean(c(T_mse, CO2_mse)))
}

# smooth_T_CO2_mse: function to find the mean of smoothed temperature and CO2
# MSEs between observed & predicted data for a given variable
#
# args:
# obs_data - data frame of observed data formatted like Hector data frame
# hector_data - data frame outputted by Hector
#
# Returns: MSE between predicted and observed data for var
smooth_T_CO2_mse <- function(obs_data, hector_data) {

# Getting data frame with just smoothed data that works with get_var_mse
smooth_data <- filter(obs_data, variable == "Smooth T")
smooth_data$variable <- GMST()

# Getting mses
T_mse <- get_var_mse(obs_data = smooth_data,
hector_data = hector_data,
var = GMST(),
yrs = 1850:2014)
CO2_mse <- get_var_mse(obs_data = obs_data,
hector_data = hector_data,
var = CONCENTRATIONS_CO2(),
yrs = c(1750, 1850:2014))

return(mean(c(T_mse, CO2_mse)))
}
62 changes: 62 additions & 0 deletions scripts/graph_smooth_temps.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
# Script to make smoothed temperature comparison plot
# Author: Peter Scully
# Date: 6/17/24

# Importing libraries
library(hector)
library(zoo)
library(ggplot2)

# Setting up file paths
COMP_DATA_DIR <- file.path(here::here(), "comparison_data")
SCRIPTS_DIR <- file.path(here::here(), "scripts")
RESULTS_DIR <- file.path(here::here(), "results")

CO2_PATH <- file.path(COMP_DATA_DIR,
"Supplementary_Table_UoM_GHGConcentrations-1-1-0_annualmeans_v23March2017.csv")
TEMP_PATH <-
file.path(COMP_DATA_DIR,
"HadCRUT.5.0.2.0.analysis.summary_series.global.annual.csv")

INI_FILE <- system.file("input/hector_ssp245.ini", package = "hector")

# Setting other constants
PARAMS <- c(BETA(), Q10_RH(), DIFFUSIVITY())
BEST_PARS <- c(0.528263386504877, 1.76, 2.4) # from 3 year results

OUTPUT <- file.path(RESULTS_DIR, "graph_smooth_temps.jpeg")



source(file.path(SCRIPTS_DIR, "major_functions.R"))

### Getting observational data ###
temp_data <- get_temp_data(TEMP_PATH)

obs_data <- temp_data

# Getting smoothed temperatures
for (k in c(3, 10)) {
smoothed_temps <- rollmean(temp_data$value,
k = k,
align = "center",
fill = NA)
smoothed_temp_data <- temp_data
smoothed_temp_data$value <- smoothed_temps
smoothed_temp_data$scenario <- paste("Smoothed, k =", k)
obs_data <- rbind(obs_data, smoothed_temp_data)
}

### Getting Hector data ###
hector_data <- run_hector(ini_file = INI_FILE,
params = PARAMS,
vals = BEST_PARS,
yrs = 1850:2014,
vars = GMST())
hector_data$scenario <- "Fit to Smoothed (and unsmoothed) Data"

### Plotting data ###
comb_data <- rbind(obs_data, hector_data)
ggplot(data = comb_data) +
geom_line(aes(x = year, y = value, color = scenario))
ggsave(OUTPUT, width = 9)
17 changes: 11 additions & 6 deletions scripts/major_functions.R
Original file line number Diff line number Diff line change
Expand Up @@ -317,7 +317,10 @@ objective_fn <- function(obs_data, ini_file, params, par, yrs, vars, error_fn,
# params - vector of Hector parameters to modify
# par - vector of initial values for params. Default is NULL, meaning
# that Hector default parameters will be used as initial values
# sd - vector of standard derivs for par. Only used if using LBFGS-B
# sd - vector of standard derivs for par. Only used if using LBFGS-B.
# If NULL, use provided lower and upper bounds
# lower - lower bound for using LBFGS-B
# upper - upper bound for using LBFGS-B
# yrs - year range to get Hector data from
# vars - Hector variables to get data on
# error_fn - function to calculate error between observed/predicted vals
Expand All @@ -330,9 +333,9 @@ objective_fn <- function(obs_data, ini_file, params, par, yrs, vars, error_fn,
# function value to given output file
#
# note: uses an error function from error_functions.R
run_optim <- function(obs_data, ini_file, params, par = NULL, sd = NULL, yrs,
vars, error_fn, include_unc = F, method = "Nelder-Mead",
output_file) {
run_optim <- function(obs_data, ini_file, params, par = NULL, sd = NULL,
lower = NULL, upper = NULL, yrs, vars, error_fn,
include_unc = F, method = "Nelder-Mead", output_file) {
# Creating vector of default parameters
if (is.null(par)) {
default_core <- newcore(ini_file)
Expand All @@ -352,8 +355,10 @@ run_optim <- function(obs_data, ini_file, params, par = NULL, sd = NULL, yrs,
error_fn = error_fn,
include_unc = include_unc)
} else if (method == "L-BFGS-B") {
lower <- par - sd
upper <- par + sd
if (!is.null(sd)) {
lower <- par - sd
upper <- par + sd
}
optim_output <- optim(par = par,
fn = objective_fn,
obs_data = obs_data,
Expand Down
Loading

0 comments on commit 69ee509

Please sign in to comment.