0

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

ftable(var1, var2, var3, var4)

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

logit.1 <- glm(var4 ~ var3 + var2 + var1, family = binomial(link = "logit")) 摘要(logit.1)

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

strat:stratum(城市或(县)农村)。

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

vill_neigh_code:村庄或社区代码

重量:重量

4

1 回答 1

2
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 )
于 2013-01-02T01:19:10.397 回答