Reputation: 15
I want to end up with a three dimensional table that shows proportions (ie percentages) for each variable, split by two dichotomous variables. This is the code I've written that I think will produce a simplified version of what I'm looking for
> Testdesign <- svydesign(id = ~V021, strata= ~V022, weights = ~SAMPW, data= testydf)
> Tableone <- svyby( ~ V104 + V025 , ~ V013 + V502 , Testdesign , svyciprop,
vartype="ci", method="beta", na.rm = TRUE
Then to have a readable table I understand it must be passed through ftable
, but I don't understand the error. I saw this explanation that also didn't clear things up for me unfortunately
> ftable(Tableone)
Error in rbind(matrix("", nrow = length(xcv), ncol = length(xrv)),
charQuote(makeNames(xrv)), :
number of columns of matrices must match (see arg 3)
I could try to use the Crosstab
function but I'm not sure how to integrate that with complex survey data (hence the survey
choice to begin with)
stargazer
looks promising but it doesn't seem to read the svyby
output correctly so ftable
still appears to be necessary?
EDIT: Here is a subset that can be used with the above code, sorry about the godawful chunk of text
> testydf <- structure(list(V001 = c(1, 1, 1, 1, 1, 2, 2, 2, 3, 3, 3, 3, 3,
4, 4, 4, 5, 5, 5, 5, 5, 5, 6, 6, 6, 7, 7, 7, 7, 7), V002 = c(124,
166, 178, 178, 216, 2, 2, 214, 34, 149, 167, 221, 248, 62, 72,
196, 6, 14, 51, 101, 188, 221, 129, 167, 186, 14, 18, 26, 97,
111), V003 = c(2, 2, 2, 3, 2, 1, 2, 2, 2, 2, 1, 2, 2, 2, 2, 2,
2, 1, 2, 2, 2, 2, 6, 6, 3, 2, 1, 2, 2, 3), V004 = c(1, 1, 1,
1, 1, 2, 2, 2, 3, 3, 3, 3, 3, 4, 4, 4, 5, 5, 5, 5, 5, 5, 6, 6,
6, 7, 7, 7, 7, 7), V005 = c(118748, 118748, 118748, 118748, 118748,
1258174, 1258174, 1258174, 1931847, 1931847, 1931847, 1931847,
1931847, 47672, 47672, 47672, 911631, 911631, 911631, 911631,
911631, 911631, 2005349, 2005349, 2005349, 1060772, 1060772,
1060772, 1060772, 1060772), V013 = c(3, 3, 3, 1, 3, 5, 1, 5,
3, 2, 7, 2, 4, 2, 2, 7, 5, 3, 2, 5, 2, 4, 1, 1, 2, 2, 4, 7, 3,
1), V106 = c(2, 3, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 2, 1, 1,
0, 1, 2, 0, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1), SEX = c(1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1), V502 = c(0, 1, 1, 0, 1, 2, 0, 1, 0, 1, 1, 1,
1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 2, 2, 1, 1, 1, 1, 0), V025 = c(1,
1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
2, 2, 2, 2, 2, 2, 2, 2), V190A = c(4, 5, 1, 1, 2, 2, 2, 1, 1,
4, 4, 2, 1, 5, 5, 3, 1, 1, 1, 1, 1, 5, 2, 4, 2, 2, 2, 2, 2, 3
), V130 = c(2, 2, 4, 4, 6, 5, 5, 1, 6, 6, 3, 2, 6, 3, 5, 3, 1,
1, 1, 1, 1, 1, 5, 5, 6, 5, 5, 2, 2, 2), V131 = c(8, 1, 1, 1,
1, 1, 1, 1, 5, 5, 5, 1, 5, 10, 2, 4, 1, 1, 1, 1, 1, 1, 2, 1,
5, 8, 8, 8, 8, 8), V732 = c(NA_real_, NA_real_, NA_real_, NA_real_,
NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_,
NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_,
NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_,
NA_real_, NA_real_, NA_real_, NA_real_, NA_real_), V836 = c(1,
1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 2, 2, 1, 1, 1, 1,
0, 1, 1, 1, 1, 1, 1, 1), V754CP = c(0, 0, 0, 0, 1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, 0, 0, 0), V791B = c(0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
), HIV02 = c(6918, 6928, 6882, 6883, 6919, 9523, 9521, 10340,
446, 441, 445, 437, 436, 14071, 14065, 14066, 11443, 12111, 11328,
11395, 11644, 11309, 14547, 14626, 15067, 12464, 12466, 12459,
12462, 12468), HIV03 = structure(c(0, NA, 0, 0, 0, 1, 0, 0, 0,
0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0
), labels = structure(c(0, 1, 2, 3, 4, 5, 6, 7, 9), .Names = c("HIV negative",
"HIV positive", "HIV2 positive", "HIV1 & HIV2 positive", "ERROR : V-, W+, M+",
"ERROR : V-, W+, M-", "ERROR : V-, W-, M+", "Indeterminate",
"Inconclusive")), class = "labelled"), HIV05 = c(109959, 109959,
109959, 109959, 109959, 1212890, 1212890, 1212890, 1856921, 1856921,
1856921, 1856921, 1856921, 45717, 45717, 45717, 864793, 864793,
864793, 864793, 864793, 864793, 1974222, 1974222, 1974222, 1066907,
1066907, 1066907, 1066907, 1066907), HIV06 = structure(c(0, 1,
0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0,
0, 1, 0, 0, 0, 0, 0), labels = structure(c(0, 1), .Names = c("Negative",
"Positive")), class = "labelled"), V104 = c(0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0), TRANSEX = c(NA_real_, NA_real_, NA_real_, NA_real_,
NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_,
NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_,
NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_,
NA_real_, NA_real_, NA_real_, NA_real_, NA_real_), HIVKNOW = c(3,
2, 2, 2, 4, 7, 7, 4, 5, 4, 7, 4, 5, 4, 3, 4, 3, 6, 4, 2, 3, 3,
3, 4, 5, 5, 5, 4, 6, 4), HIVKCAT = c(1, 0, 0, 0, 1, 2, 2, 1,
2, 1, 2, 1, 2, 1, 1, 1, 1, 2, 1, 0, 1, 1, 1, 1, 2, 2, 2, 1, 2,
1), V021 = c(1, 1, 1, 1, 1, 2, 2, 2, 3, 3, 3, 3, 3, 4, 4, 4,
5, 5, 5, 5, 5, 5, 6, 6, 6, 7, 7, 7, 7, 7), V022 = c(18, 18, 18,
18, 18, 13, 13, 13, 33, 33, 33, 33, 33, 11, 11, 11, 25, 25, 25,
25, 25, 25, 9, 9, 9, 27, 27, 27, 27, 27), V023 = c(18, 18, 18,
18, 18, 13, 13, 13, 33, 33, 33, 33, 33, 11, 11, 11, 25, 25, 25,
25, 25, 25, 9, 9, 9, 27, 27, 27, 27, 27), V024 = c(2, 2, 2, 2,
2, 2, 2, 2, 3, 3, 3, 3, 3, 1, 1, 1, 2, 2, 2, 2, 2, 2, 1, 1, 1,
2, 2, 2, 2, 2), SAMPW = c(0.109959, 0.109959, 0.109959, 0.109959,
0.109959, 1.21289, 1.21289, 1.21289, 1.856921, 1.856921, 1.856921,
1.856921, 1.856921, 0.045717, 0.045717, 0.045717, 0.864793, 0.864793,
0.864793, 0.864793, 0.864793, 0.864793, 1.974222, 1.974222, 1.974222,
1.066907, 1.066907, 1.066907, 1.066907, 1.066907)), .Names = c("V001",
"V002", "V003", "V004", "V005", "V013", "V106", "SEX", "V502",
"V025", "V190A", "V130", "V131", "V732", "V836", "V754CP", "V754DP",
"V754JP", "V754WP", "V756", "V823", "V824", "V761", "V525", "V732.1",
"V791", "V791B", "HIV02", "HIV03", "HIV05", "HIV06", "V104",
"TRANSEX", "HIVKNOW", "HIVKCAT", "V021", "V022", "V023", "V024",
"SAMPW"), row.names = c("1", "2", "3", "4", "5", "6", "7", "8",
"9", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19",
"20", "21", "22", "23", "24", "25", "26", "27", "28", "29", "30"
), class = "data.frame")
I tried the factor update to no avail Many Thanks!
Upvotes: 1
Views: 895
Reputation: 6104
?svyciprop
states
formula Model formula specifying a single binary variable
setup
library(survey)
data(api)
dclus1<-svydesign(id=~dnum, fpc=~fpc, data=apiclus1)
correctly breaks
svyciprop( ~ I( api99 > 500 ) + I( api00 > 500 ) , dclus1 )
also correctly breaks
svyby( ~ I( api99 > 500 ) + I( api00 > 500 ) , ~ sch.wide , dclus1, svyciprop)
Upvotes: 2