3
我使用R中的survey
包與美國人口普查的PUMS數據集一起工作。我爲每個廣泛的行業創建了一個布爾值,併爲三個值(Stayed
,Left
,Entered
)設置了字符變量MigrationStatus
。我想通過移民狀況來檢查每個行業的工人比例。將lapply,svyby,svyratio相結合,以置信區間計算多個比率
這工作得很好:
AGR_ratio=svyby(~JobAGR, by=~MigrationStatus, denominator=~EmployedAtWork, design=subset(pums_design,EmployedAtWork==1), svyratio, vartype='ci')
但是,這會產生一個錯誤:
varlist=names(pums_design$variables)[32:50]
job_ratios = lapply(varlist, function(x) {
svyby(substitute(~i, list(i = as.name(x))), by=~MigrationStatus, denominator=~EmployedAtWork, design=subset(pums_design,EmployedAtWork==1), svyratio, vartype='ci')
})
#Error in svyby.default(substitute(~i, list(i = as.name(x))), by = ~MigrationStatus, :
#object 'byfactor' not found
varlist
#[1] "JobADM" "JobAGR" "JobCON" "JobEDU" "JobENT" "JobEXT" "JobFIN" "JobINF" "JobMED" "JobMFG" "JobMIL" "JobPRF" "JobRET" "JobSCA" "JobSRV"
#[16] "JobTRN" "JobUNE" "JobUTL" "JobWHL"
太謝謝你了。我會試試看! – davideps