楼主: ReneeBK
1174 0

[Survey Sampling Questions]Frequency weighting in R? [推广有奖]

  • 1关注
  • 62粉丝

VIP

已卖:4900份资源

学术权威

14%

还不是VIP/贵宾

-

TA的文库  其他...

R资源总汇

Panel Data Analysis

Experimental Design

威望
1
论坛币
49655 个
通用积分
55.9937
学术水平
370 点
热心指数
273 点
信用等级
335 点
经验
57805 点
帖子
4005
精华
21
在线时间
582 小时
注册时间
2005-5-8
最后登录
2023-11-26

楼主
ReneeBK 发表于 2014-4-30 00:07:17 |AI写论文

+2 论坛币
k人 参与回答

经管之家送您一份

应届毕业生专属福利!

求职就业群
赵安豆老师微信:zhaoandou666

经管之家联合CDA

送您一个全额奖学金名额~ !

感谢您参与论坛问题回答

经管之家送您两个论坛币!

+2 论坛币

I'm trying to analyze data from the University of Minnesota IPUMS dataset for the 1990 US census in R. I'm using the survey package because the data is weighted. Just taking the household data (and ignoring the person variables to keep things simple), I am attempting to calculate the mean of hhincome (household income). To do this I created a survey design object using the svydesign() function with the following code:

> require(foreign)> ipums.household <- read.dta("/path/to/stata_export.dta")> ipums.household[ipums.household$hhincome==9999999, "hhincome"] <- NA # Fix missing > ipums.hh.design <- svydesign(id=~1, weights=~hhwt, data=ipums.household)> svymean(ipums.household$hhincome, ipums.hh.design, na.rm=TRUE)      mean     SE[1,] 37029 17.365

So far so good. However, I get a different standard error if I attempt the same calculation in Stata(using code meant for a different portion of the same dataset):

use "C:\I\Hate\Backslashes\stata_export.dta"replace hhincome = . if hhincome == 9999999(933734 real changes made, 933734 to missing)mean hhincome [fweight = hhwt] # The code from the link above.Mean estimation                     Number of obs    = 91746420--------------------------------------------------------------             |       Mean   Std. Err.     [95% Conf. Interval]-------------+------------------------------------------------    hhincome |   37028.99   3.542749      37022.05    37035.94--------------------------------------------------------------

And, looking at another way to skin this cat, the author of survey, has this suggestion for frequency weighting:

expanded.data<-as.data.frame(lapply(compressed.data,               function(x) rep(x,compressed.data$weights)))

However, I can't seem to get this code to work:

> hh.dataframe <- data.frame(ipums.household$hhincome, ipums.household$hhwt)> expanded.hh.dataframe <- as.data.frame(lapply(hh.dataframe, function(x) rep(x, hh.dataframe$hhwt)))Error in rep(x, hh.dataframe$hhwt) : invalid 'times' argument

Which I can't seem to fix. This may be related to this issue.

So in sum:

  • Why don't I get the same answers in Stata and R?
  • Which one is right (or am I doing something wrong in both cases)?
  • Assuming I got the rep() solution working, would that replicate Stata's results?
  • What's the right way to do it? Kudos if the answer allows me to use the plyr package for doing arbitrary calculations, rather than being limited to the functions implemented in survey(svymean(), svyglm() etc.)

二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

关键词:questions weighting Frequency question Sampling University household following function because

您需要登录后才可以回帖 登录 | 我要注册

本版微信群
加好友,备注jltj
拉您入交流群
GMT+8, 2026-1-28 22:01