如何修复:tau * h 中的错误:二元运算符的非数字参数

问题描述 投票:0回答:2

我遇到这个问题是因为我是新用户。请帮我解决这个问题(tau * h 错误:二元运算符的非数字参数) 我附上了 R 代码和我的数据文件,以及您可以看到的随附图片中的错误。 抱歉给您带来不便。

https://drive.google.com/drive/folders/107if4c18KZ0gz9BagxcBlatiUBm7Bw1E?usp=sharing

enter image description here

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

python r read.csv
2个回答
0
投票

错误信息“Error in tau * h : non-numeric argument to binary operator”表明你正试图在非数字对象和数字对象之间进行数学运算。

在您的代码中,唯一可能的罪魁祸首是:

pdf_skill_score <- overlap_area / ((obs_area + gcm_area) / 2 - tau * overlap_area)

tau 好像不是一个数值。要检查这一点,您可以通过在有问题的代码行之前添加行 print(tau) 来打印 tau 的值。

如果 tau 不是数值,您需要找到它的定义位置并确保它被定义为数值。


0
投票

错误消息“non-numeric argument to binary operator”通常表示您正在尝试对非数字对象执行数学运算,例如字符串或因子变量。

没有额外的上下文,很难诊断问题。但是,根据您提供的错误消息,问题似乎与将两个非数字对象相乘有关。

要解决此问题,您可以尝试以下操作:

检查您使用的变量的数据类型。确保它们是数字。

如果变量不是数字,尝试使用 as.numeric() 函数将它们转换为数字。

检查您使用的变量中是否有任何缺失值或 NA 值。如果有缺失值,请考虑估算或删除它们。

检查您的代码是否有拼写错误或语法错误。

如果以上解决方案均无效,请提供有关您的代码和数据的更多信息,以便我们更好地帮助您。

© www.soinside.com 2019 - 2024. All rights reserved.