2017-06-12 55 views
1

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

# 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”),大小= C(30,20,45,15, :所有参数必须具有相同的长度

# extracts the observed data 
getdata(data, st) 

错误is.vector(米):对象 'ST' 未找到

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

错误(ST $ REG):对象“ST”未找到

+0

它正在为我工​​作。我没有看到任何错误消息。 –

回答

2

我复制并粘贴此,并得到了同样的错误,但我键入它的手,它只是工作精细。另外,因为stratanames是单个值,所以应该使用stratanames = "REG"。最后,确保你没有survival包装加载,如果你这样做,你可能需要使用

st <- sampling:::strata(swissmunicipalities, stratanames = "REG", 
         size = c(30, 20, 45, 15, 20, 11, 44), method="srswor") 
table(st) 

1 2 3 4 5 6 7 
30 20 45 15 20 11 44