【问题标题】:How to scale stacked model approach for each country in data set?如何为数据集中的每个国家/地区缩放堆叠模型方法?
【发布时间】:2018-12-30 11:08:29
【问题描述】:
fitControl <- trainControl(
  method = "cv",
  number = 5,
  savePredictions = 'final',
  classProbs = F)

predictors<-c("Age", "Quantile","label1","label2")
outcomeName<-'Life_expt'

model_rf<-train(Life_expt ~ Age+Quantile+label1+label2,Train2[country==.BY],method='rf',trControl=fitControl,tuneLength=3)

.prepareFastSubset(isub = isub, x = x, enclos = parent.frame(), 中的错误: == 的 RHS 是长度 0,它不是 1 或 nrow (559)。为了坚固,不允许再循环(长度为 1 RHS 除外)。请考虑使用 %in%。

我正在尝试在每个国家/地区扩展它。我想使用堆叠方法和模型(rf、svmRadial、glm)。我怎样才能在没有错误的情况下为每个国家/地区执行此操作?

谢谢

【问题讨论】:

  • 能否请您提供更多详细信息(即集成和缩放是什么意思?)并通过分享一些数据/代码提出您的问题reproducible?
  • @Ozan147i 已添加代码和错误。
  • 您的示例无法重现,因为我们仍然缺少样本数据。除了Train2[country==.BY] 的部分看起来很奇怪;什么是 .BY 和 country ,它们是如何定义的?你应该检查Train2[country==.BY]。我想你想子集Train2。试试Train2[Train2$country == some_country_to_match, ]。
  • 您能否将前 20 行数据的 dput(head(Train2, 20)) 输出添加到您的问题中?

标签: r machine-learning statistics ensemble-learning


【解决方案1】:

您可以使用by 函数在您所在的国家/地区按factor 进行扩展:

# Data frame simulation
set.seed(123)
source <- data.frame(Country = factor(rep(c("Morocco", "Egypt", "Somali"), each = 4)),
                 Age = sample(18:100, 12, replace = TRUE),
                 Quantile = sample(1:100 / 100, 12, replace = TRUE))

# Apply scale for each group of rows with the same value of factor (country)
df_lists <- by(data = source, INDICES = source$Country, FUN = function(x){
  x$Scaled <- scale(x[, c(2, 3)])
  x
  }
)

# Combine data frame from the list
Train2 <- do.call(rbind, df_lists)
Train2

输出:

          Country Age Quantile  Scaled.Age Scaled.Quantile
Egypt.5     Egypt  42     0.15 -0.68003487     -1.47468600
Egypt.6     Egypt  30     0.42 -1.03102061      0.62092042
Egypt.7     Egypt  97     0.42  0.92864977      0.62092042
Egypt.8     Egypt  92     0.37  0.78240571      0.23284516
Morocco.1 Morocco  72     0.76  0.43965779      1.47808959
Morocco.2 Morocco  76     0.22  1.14311024     -0.65035942
Morocco.3 Morocco  63     0.32 -1.14311024     -0.25620220
Morocco.4 Morocco  67     0.24 -0.43965779     -0.57152797
Somali.9   Somali  75     0.16  0.56497964     -0.61136845
Somali.10  Somali  84     0.14  0.88278069     -0.74355623
Somali.11  Somali  20     0.24 -1.37713788     -0.08261736
Somali.12  Somali  57     0.47 -0.07062246      1.43754204

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-10-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多