How to fix; Error in tau * h : non-numeric argument to binary operator

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 .

Wind Data file - Google Drive

`# 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)
}
`

I think I see the problems but I have never used {kernlab} or(KernSmooth)

The first problem is here:

 # 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")

bkde requires a vector and you are submitting the entire data.frame. "bandwidth" requires a numeric value. See ?bdke.

Something like this will run:

obs_dens <- bkde(obs_data[, 2], kernel = "normal", bandwidth = 0.25)

Or if you have defined nrd0 earlier then then it should should work.

obs_dens <- bkde(obs_data[, 2], kernel = "normal", bandwidth = nrd0)

Here

 obs_area <- integrate(obs_dens, min(obs_data[, 2]), max(obs_data[ , 2])) $value

you are trying to integrate a two element list when it looks like integrate() needs a formula. I have never used integrate() so I have no idea what to do.

Try this to get a feel for the structure of obs_dens.

obs_dens
class( obs_dens)
names(obs_dens)
1 Like

This topic was automatically closed 42 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.