Sorry if this has been asked before I have looked throughly but couldn't find anything. My problem is the following. I have survey data and want to perform the same 3 steps with different variables. I always want the output to be separated by gender. So I want to create a function that automates this and returns three new variables. It should look something like this:
myfunction <- function(x) {
x.mean.by.gender <- svyby(~x, ~gender, svymean, design = s.design)
x.boxplot <- svyboxplot(x~gender, varwith=FALSE, design = s.design)
x.ttest <- svyttest(x~gender, design = s.design)}
myfunction(data$income)
This is a working example of what I want to do (boxplot doesn't split by gender but not important):
require("survey")
income <- runif(50, 1000, 2500)
wealth <- runif(50, 10000, 100000)
weight <- runif(50, 1.0, 1.99)
id <- seq(from = 1, to = 50, 1)
gender <- sample(0:1, 50, replace = TRUE)
data <- data.frame(income, wealth, weight, id, gender)
data.w <- svydesign(ids = ~id, data = data, weights = ~weight)
data.w <- update(data.w, count=1)
svytotal(~count, data.w)
# Income difference
income.mean.table <- svyby(~income, ~gender, svymean, design = data.w)
income.mean.table
income.boxplot <- svyboxplot(income~gender, varwith = FALSE, design = data.w)
income.ttest <- svyttest(income~gender, design = data.w)
income.ttest
# Wealth difference
wealth.mean.table <- svyby(~wealth, ~gender, svymean, design = data.w)
wealth.mean.table
wealth.boxplot <- svyboxplot(wealth~gender, varwith = FALSE, design = data.w)
wealth.ttest <- svyttest(wealth~gender, design = data.w)
wealth.ttest
So the function should perform one of those iterations of svyby
, svyboxplot
and svyttest
and create variables with the name of the variable used as input.function (e.g.) income.ttest
. I hope this clears things up. Sorry for the confusion.