无法在R中运行strata()

时间:2017-06-12 15:14:56

标签: r

我正在学习R中的地层采样,并试图运行地层功能。每次我尝试运行这个函数时都会遇到同样的错误 - 当我从教授提供的类注释中运行strata()时,运行一个示例来自?strata - 下面是help(strata)中的示例和错误消息我试图运行时收到。我很困惑。无论我运行哪个层(),我都会得到相同的错误信息 - 任何想法如何克服这一点?任何建议都表示赞赏。

# Uses the 'swissmunicipalities' data as population for drawing a sample of units

data(swissmunicipalities)

# the variable 'REG' has 7 categories in the population
# it is used as stratification variable
# Computes the population stratum sizes
table(swissmunicipalities$REG)
1   2   3   4   5   6   7 
589 913 321 171 471 186 245 

# do not run
#  1   2   3   4   5   6   7 
# 589 913 321 171 471 186 245 
# sort the data to obtain the same order of the regions in the sample

data=swissmunicipalities
data=data[order(data$REG),]

# the sample stratum sizes are given by size=c(30,20,45,15,20,11,44)
# 30 units are drawn in the first stratum, 20 in the second one, etc.
# the method is simple random sampling without replacement 
# (equal probability, without replacement)  

st=strata(data,stratanames=c("REG"),size=c(30,20,45,15,20,11,44), method="srswor") 
  

分层中的错误(数据,stratanames = c(“REG”),size = c(30,20,45,15,   :所有参数必须是相同的长度

# extracts the observed data 
getdata(data, st) 
  

is.vector(m)出错:找不到对象'st'

# see the result using a contingency table 
table(st$REG)
  

表中的错误(st $ REG):找不到对象'st'

1 个答案:

答案 0 :(得分:3)

我复制并粘贴了这个,并得到了同样的错误,但我手工输入它并且它工作得很好。此外,由于.commandsTable { color: whitesmoke; background-color: black; margin-left: auto; margin-right: auto; } .commandsTable td { background-color: #039be5; } .commandsTable tr:hover { background-color: black; } 是单个值,因此您应使用stratanames。最后,确保您没有加载stratanames = "REG"打包,如果这样做,您可能需要使用

survival