【问题标题】:How to do loop that includes function and data frame in R如何在R中执行包含函数和数据框的循环
【发布时间】:2022-01-14 01:38:06
【问题描述】:

我想制作如下所示的循环功能。我想要的结果是这样的

set.seed(1)
test = data.frame("0" = c(14,22,14,13), "1" = c(12,5,4,12), "2" = c(14,5,12,10),  "3" = c(13,14,7,3))
colnames(test) = c("0", "1", "2", "3")
test["-1"] = 0

solution = data.frame(A = c(0, 1,2,3, 3), B = c(2,1,3,2, 1), C = c(1,3,2,0, NA), D = c(3,1,2,3, NA))
colnames(solution) = c("0", "1", "2", "3")

chosen_spots = colnames(solution)
for (m in chosen_spots) {
  type = solution[,m]
  type[is.na(type)] = -1
  type = as.character(type) 

  data = matrix(0, 1, length(chosen_spots))
  for (x in 1:length(chosen_spots)) {
    model = function(m){
      lm(test[,m] ~ test[,type[1]] + test[,type[2]] + test[,type[3]]+ test[,type[4]], data = test)
    } 
    model(m)
    data[1, x] = model(m)$coefficients[1]
   }
   result = colMeans(data)
   print(paste("The intercept mean is",result))
}
print(paste("The intercept mean is",result))
data

结果是这样的

[1] "The intercept mean is 1.77635683940025e-15"
[1] "The intercept mean is -4.15452614407147e-16"
[1] "The intercept mean is 0"
[1] "The intercept mean is 0"
[1] "The intercept mean is 0"
     [,1] [,2] [,3] [,4]
[1,]    0    0    0    0

似乎我的循环不适用于 data = matrix(0, 1, length(chosen_spots))。似乎只记录了最后一次迭代。你能帮忙解决什么问题吗?我希望“数据”类似于

      [,1] [,2] [,3] [,4]
[1,]    0.1    0.4    0.32    0

其中条目是迭代的所有结果,不仅是最后一次迭代,因此最后我想采用 colMeans(data)。谢谢。

【问题讨论】:

  • test["-1"] = 0 应该做什么?
  • @Harshvardhan 当条目为 NA 时,我将该条目更改为 -1,并且列名 -1 的条目全部为 0。所以我只需添加一列(名为“-1”,所有条目均为 0 )。当我使用 NA 作为列名时,我发现调用该列时出错,因此我将其更改为 -1。但这不是目前的问题
  • 你需要多谈谈你的目标。第三次迭代是您的第一个问题。在该迭代中,m"2"type"1" "3" "2" "0" "-1"。然后,您对一些变量运行回归 test[, "m"] 的模型,其中一个变量是 test[,type[3]],因此您使用 test[, "2"] 作为预测变量回归 test[, "2"]。在这种情况下,截距确实为 0——所有系数都为 0,除了 test[, "2"] 系数为 1,因为test[, "2"] = test[, "2"]。听起来这不是你想要做的,但我不知道你想做什么比 R 更。
  • 我会说你的内部循环中有一个危险信号:for (x in 1:length(chosen_spots))。在循环中使用x 的唯一一次是将结果分配给data[1, x]。如果model 定义中没有x,则每次的结果都相同。它不是“只记录最后一次迭代”,而是你没有在迭代中改变任何东西——每次迭代都运行相同的确切代码。但我不知道你想改变什么......
  • @GregorThomas 是的,我想做迭代,例如当 m = 2(取自 type = c(1,3,2,0,-1))我想要模型 m 的结果存储到称为“数据”的矩阵中,依此类推,直到评估 m 的所有值。我使用 x 希望当 x = 1 时 m = 1, x = 2 时 m = 3, x = 3 时 m = 0, x = 4 时 m = -1。所以矩阵结果的维度将是 1 x 长度(colnames(chosen_spots))。最终目标是取这个数据矩阵的这些值的平均值。

标签: r dataframe function loops iteration


【解决方案1】:

我通过这样的编辑来解决这个问题

set.seed(1)
test = data.frame("0" = c(14,22,14,13), "1" = c(12,5,4,12), "2" = c(14,5,12,10), "3" = c(13,14,7,3))
colnames(test) = c("0", "1", "2", "3")
test["-1"] = 0

solution = data.frame(A = c(0, 1,2,3, 3), B = c(2,1,3,2, 1), C = c(1,3,2,0, NA), D = c(3,1,2,3, NA))
colnames(solution) = c("0", "1", "2", "3")

spots = colnames(solution)
data = matrix(0, 1, length(spots))
for (i in 1:length(spots)){
  chosen_spots = spots[I]
  type = solution[,chosen_spots]
  type[is.na(type)] = -1
  type = as.character(type)
  model = function(chosen_spots){
  lm(test[,chosen_spots] ~ test[,type[1]] + test[,type[2]] + test[,type[3]]+ test[,type[4]], data = test)
  }
model(chosen_spots)
data[1, i] = model(chosen_spots)$coefficients[1]
}
result = rowMeans(data)
print(paste("The intercept mean is",result))

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-09-24
    • 2019-07-26
    • 1970-01-01
    • 1970-01-01
    • 2019-11-27
    • 2021-08-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多