警告消息:要替换的项目数不是替换长度的倍数

Warning messages: number of items to replace is not a multiple of replacement length

我需要帮助解决这个问题。它发生在我 运行 我的 estimator.And 的循环中 谁能告诉我这个问题是否影响了我的结果?

#Transformed 数据的样本选择##

`s<-sample(1:50,2,replace = FALSE,prob = NULL) s;data$Wy[s] s<-sample(1:50,2,replace = FALSE,prob = NULL) s;data$Wx[s]``

##loop for ratio estimator##
>`for(i in 1:10) 
{
  s[i]<-sample(1:50,2,replace=FALSE, prob = NULL)
  wr[i]<-  mean(data$Wy[s])/mean(data$Wx[s])  
  muwr[i]<-wr[i]*muwx                     ##ratio type mean estimator##
}`

##输出警告信息##

`1: In s[i] <- sample(1:50, 2, replace = FALSE, prob = NULL) :
  number of items to replace is not a multiple of replacement length
2: In s[i] <- sample(1:50, 2, replace = FALSE, prob = NULL) :
  number of items to replace is not a multiple of replacement length
3: In s[i] <- sample(1:50, 2, replace = FALSE, prob = NULL) :
  number of items to replace is not a multiple of replacement length
4: In s[i] <- sample(1:50, 2, replace = FALSE, prob = NULL) :
  number of items to replace is not a multiple of replacement length
5: In s[i] <- sample(1:50, 2, replace = FALSE, prob = NULL) :
  number of items to replace is not a multiple of replacement length
6: In s[i] <- sample(1:50, 2, replace = FALSE, prob = NULL) :
  number of items to replace is not a multiple of replacement length
7: In s[i] <- sample(1:50, 2, replace = FALSE, prob = NULL) :
  number of items to replace is not a multiple of replacement length
8: In s[i] <- sample(1:50, 2, replace = FALSE, prob = NULL) :
  number of items to replace is not a multiple of replacement length
9: In s[i] <- sample(1:50, 2, replace = FALSE, prob = NULL) :
  number of items to replace is not a multiple of replacement length
10: In s[i] <- sample(1:50, 2, replace = FALSE, prob = NULL) :
  number of items to replace is not a multiple of replacement length
> muwr
 [1] 1155.006 1155.021 1154.958 1154.875 1154.774 1154.875 1155.021 1155.021 1155.021
[10] 1155.006
> mean(muwr);var(muwr)
[1] 1154.958
[1] 0.007579488`

s[i] <- sample(1:50, 2, replace=FALSE, prob = NULL)

sample(1:50, 2, ...) 将 return 一个长度为 2 的向量。我猜 s 是一个数字向量,而您正试图将 i 放在位置上,而不是一个,而是两个元素因此您在每次迭代中看到的警告。也许你想要 sample(1:50, 1, ...)?

我不知道最终结果会受到怎样的影响,但我还是建议您解决不一致问题。

s[i] <- 样本 (1:50, 2, replace=FALSE, prob = NULL) 在上面的语句中,我做错的是我使用的是 s[i] 而不是 s,这个问题导致了警告消息并影响了样本选择命令。