Looping through dataset variables in svy package

前端 未结 1 1920
野性不改
野性不改 2021-01-17 01:08

I\'ve built an R function that uses the same explanatory variables on a range of columns. I\'ve used the glm function, but now I need to do the same with

相关标签:
1条回答
  • 2021-01-17 01:57

    You need to use as.formula to paste the appropriate columns for evaluation. I created a custom function for your case:

    mysvy <- function(data, columns, ...) {
        model <- lapply(as.list(columns), function(x) {
              summary(svyglm(as.formula(paste0(names(data)[x], "~ell+meals+mobility")), 
                data = data, ...))
            })
        return(model)
    }
    

    Then you can run your your desired columns through the function.

    # To run columns 13 - 15 and get the results into a list
    results <- mysvy(apistrat, 13:15, design = dstrat)
    # should return a list of 3. results[[1]] to see the first
    
    0 讨论(0)
提交回复
热议问题