kruskal_test(硬币)中的加权数据错误:“无效的对象类‘IndependenceProblem’”

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

我正在尝试对数据集中的每个组执行加权 Kruskal-Wallis 测试,但我不断遇到以下错误:

Error in kruskal_test: Error in validObject(.Object): invalid class “IndependenceProblem” object: FALSE

这是我正在使用的代码:

# Load necessary libraries
library(tidyverse)
library(coin)

# Set seed for reproducibility
set.seed(1)

# Create the dataset
punteggi <- tibble(
  Codice = paste0("cod_", 1:200),
  Regione = sample(c("FVG", "Lazio", "Sicilia"), size = 200, replace = TRUE, prob = c(.4, .2, .4)),
  Genere = sample(c("Femminile", "Maschile"), size = 200, replace = TRUE, prob = c(.6, .4)),
  Area = sample(c("Urban", "Suburban"), size = 200, replace = TRUE, prob = c(.6, .4))
)

punteggi <- punteggi |>
  group_by(Regione, Genere, Area) |>
  mutate(
    mu = runif(1, 4, 7),
    sd = runif(1, 1, 2),
    weight = 1/n()
  ) |> 
  ungroup() |>
  rowwise() |>
  mutate(pt_tot = rnorm(1, mu, sd))

print(punteggi)

# Function to perform the weighted Kruskal-Wallis test for each group
kruskal_per_gruppo <- function(data) {
  test_result <- tryCatch({
    data <- data |> mutate(across(weight, ~./sum(.)))
    kruskal_test(pt_tot ~ value, data = data, weights = ~ weight)
  }, error = function(e) {
    message("Error in kruskal_test: ", e)
    return(NULL)
  })
  return(test_result)
}

# Apply the Kruskal-Wallis test for each group
results <- punteggi |> 
  pivot_longer(c(Regione, Genere, Area)) |> 
  group_by(name) |> 
  mutate(
    across(value, as.factor),
    kruskal = kruskal.test(pt_tot ~ value)$p.value
  ) |> 
  nest() |> 
  mutate(kruskal_weighted = map(data, kruskal_per_gruppo))

print(results)

什么可能导致此错误以及如何解决它?我已检查权重是否正确标准化,但创建 IndependenceProblem 对象似乎存在问题。任何建议将不胜感激!

r statistics survey weighted kruskal-wallis
1个回答
0
投票

所有

coin
包测试的权重必须是整数。它们被视为频率权重 - 即,如果权重为 3,则就像拥有该观察结果的三个副本一样。

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