【问题标题】:crosstables for survey data (weighted and unweighted)调查数据的交叉表(加权和未加权)
【发布时间】:2012-06-20 20:29:39
【问题描述】:

我有正在处理的调查数据。我需要对数据做一些表格和回归分析。 附加数据后,这是我用于四个变量的表的代码:

ftable(var1, var2, var3, var4)

这是我用于数据的回归代码:

logit.1

到目前为止,未加权分析的效果很好。但是我怎样才能对加权数据进行相同的分析呢?以下是一些附加信息: 数据集中有四个变量反映了抽样结构。这些是

strat:阶层(城市或(县)农村)。

clust:同一随机游走中的一批访谈

vill_neigh_code:村庄或社区代码

sweight:重量

【问题讨论】:

    标签: regression survey


    【解决方案1】:
    library(survey)
    
    data(api)
    
    # example data set
    head( apiclus2 )
    
    # instead of var1 - var4, use these four variables:
    ftable( apiclus2[ , c( 'sch.wide' , 'comp.imp' , 'both' , 'awards' ) ] )
    
    # move it over to x for faster typing
    x <- apiclus2
    
    
    # also give x a column of all ones
    x$one <- 1
    
    # run the glm() function specified.
    logit.1 <-
        glm( 
            comp.imp ~ target + cnum + growth , 
            data = x ,
            family = binomial( link = 'logit' )
        )
    
    summary( logit.1 )
    
    # now create the survey object you've described
    dclus <-
        svydesign(
            id = ~dnum + snum , # cluster variable(s)
            strata = ~stype ,   # stratum variable
            weights = ~pw ,     # weight variable
            data = x ,
            nest = TRUE
        )
    
    # weighted counts
    svyby( 
        ~one , 
        ~ sch.wide + comp.imp + both + awards , 
        dclus , 
        svytotal 
    )
    
    
    # weighted counts formatted differently
    ftable(
        svyby( 
            ~one , 
            ~ sch.wide + comp.imp + both + awards , 
            dclus , 
            svytotal ,
            keep.var = FALSE
        )
    )
    
    
    # run the svyglm() function specified.
    logit.2 <-
        svyglm( 
            comp.imp ~ target + cnum + growth , 
            design = dclus ,
            family = binomial( link = 'logit' )
        )
    
    summary( logit.2 )
    

    【讨论】:

      猜你喜欢
      • 2017-11-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-03-19
      • 2017-04-03
      • 2021-05-29
      • 2018-06-06
      • 1970-01-01
      相关资源
      最近更新 更多