Hello Scientists,
I am facing the problem because I am a new user. Please help me how to fix this (Error in tau * h : non-numeric argument to binary operator)
I have attached the R codes and my data file and the error in the enclosed picture you can see.
Sorry for inconvenience .
`# Load necessary packages
library(dplyr)
library(magrittr)
library(kernlab)
library(Hmisc)
library(KernSmooth)
Load data
obs_data <- read.csv("hadisd_data.csv")
gcm_data <- read.csv("gcm_data.csv")
Convert data to numeric format
obs_data$hadisd.wind <- as.numeric(as.character(obs_data$hadisd.wind))
gcm_data$gcm.wind <- as.numeric(as.character(gcm_data$gcm.wind))
Check for missing values
sum(is.na(obs_data$hadisd.wind))
sum(is.na(gcm_data$gcm.wind))
Impute missing values with mean
obs_data$hadisd.wind[is.na(obs_data$hadisd.wind)] <- mean(obs_data$hadisd.wind, na.rm = TRUE)
gcm_data$gcm.wind[is.na(gcm_data$gcm.wind)] <- mean(gcm_data$gcm.wind, na.rm = TRUE)
Check for missing values again
sum(is.na(obs_data$hadisd.wind))
sum(is.na(gcm_data$gcm.wind))
Check data types
if(!is.numeric(obs_data$hadisd.wind) || !is.numeric(gcm_data$gcm.wind)) {
stop("Input data must be numeric")
}
pdf_skill_score <- function(obs_data, gcm_data) {
Calculate the kernel density estimates for the observation and GCM datasets
obs_dens <- bkde(obs_data, kernel = "normal", bandwidth = "nrd0")
gcm_dens <- bkde(gcm_data, kernel = "normal", bandwidth = "nrd0")
Calculate the integrated area between the observation and GCM PDFs
obs_area <- integrate(obs_dens$estimate, min(obs_data), max(obs_data))$value
gcm_area <- integrate(gcm_dens$estimate, min(gcm_data), max(gcm_data))$value
overlap_area <- integrate(pmin(obs_dens$estimate, gcm_dens$estimate), min(min(obs_data), min(gcm_data)), max(max(obs_data), max(gcm_data)))$value
Calculate the skill score
tau <- 0.5
pdf_skill_score <- overlap_area / ((obs_area + gcm_area) / 2 - tau * overlap_area)
return(pdf_skill_score)
}
Check data types again
if(!is.numeric(obs_data$hadisd.wind) || !is.numeric(gcm_data$gcm.wind)) {
stop("Input data must be numeric")
}
pdf_skill_score_full <- pdf_skill_score(obs_data$hadisd.wind, gcm_data$gcm.wind)
if (exists("pdf_skill_score_full")) {
results <- data.frame(PDF_Skill_Score = pdf_skill_score_full)
write.csv(results, "pdf_skill_score_results.csv", row.names = FALSE)
}
`