creating an R survey design object within another function
I am trying unsuccessfully to create an R survey design object (from the R survey
package) using arguments supplied within a new function I am creating. This new function seeks to account for the complex survey design in computing prevalence estimates derived as part of the function. I can successfully get the new function to work if I supply the survey design object as an argument in my function; I would however prefer not to do that as it complicates things. Below is sample data, the function I am trying to create and my unsuccessful attempts at creating the survey design object within the new function. For the examples below, the dataframe is n
, the survey design object I wish to create in the new function is svyob
, the weight variable is wt
, the id variable is psu
, and the strata variable is stratum
. The desired structure of my new function, along with default values, is as follows (note that my outcome will be derived within the function. But for purposes of illustration, assume it is the distribution of sex):
prev = function(data, wt, psu = 1, stratum = NULL) {
#Step1: Derive outcome of interest from dataframe (not shown)
#Step2: Create survey design object
svyobj = survey::svydesign(data = n, weights =~ wt[1], id =~ psu[1], strata =~ stratum[1], nest = T)
#Step 3: Generate summary estimates
svymean(~outcome, svyobj, svyobj, na.rm = T)
}
Data:
structure(list(wt = 1365.61822580832, psu = 600815, strata = structure(9L, .Label = c("101",
"102", "103", "104", "111", "112", "113", "114", "201", "203",
"204", "211", "212", "213", "214"), class = "factor"), age = 1,
sex = 1, school = 1), row.names = 50L, class = "data.frame")
*I have been unsuccessful thus far with supplying the weights variable in step 2 above. I have tried specifying the weight variable as weights = "wt", weights = eval(parse(text = "wt")), weights = eval(quote(wt)), weights = wt[1], etc. All to no avail. Some error statements are below:
weights= as.formula(paste0("~", eval(parse(text = "wt")))) # Error in { : task 1 failed - "(subscript) logical subscript too long"
weights= as.formula(paste0("~", eval(get(wt)))) #Error in get(wt) : object 'wt' not found
I will greatly appreciate any help.
r function survey
add a comment |
I am trying unsuccessfully to create an R survey design object (from the R survey
package) using arguments supplied within a new function I am creating. This new function seeks to account for the complex survey design in computing prevalence estimates derived as part of the function. I can successfully get the new function to work if I supply the survey design object as an argument in my function; I would however prefer not to do that as it complicates things. Below is sample data, the function I am trying to create and my unsuccessful attempts at creating the survey design object within the new function. For the examples below, the dataframe is n
, the survey design object I wish to create in the new function is svyob
, the weight variable is wt
, the id variable is psu
, and the strata variable is stratum
. The desired structure of my new function, along with default values, is as follows (note that my outcome will be derived within the function. But for purposes of illustration, assume it is the distribution of sex):
prev = function(data, wt, psu = 1, stratum = NULL) {
#Step1: Derive outcome of interest from dataframe (not shown)
#Step2: Create survey design object
svyobj = survey::svydesign(data = n, weights =~ wt[1], id =~ psu[1], strata =~ stratum[1], nest = T)
#Step 3: Generate summary estimates
svymean(~outcome, svyobj, svyobj, na.rm = T)
}
Data:
structure(list(wt = 1365.61822580832, psu = 600815, strata = structure(9L, .Label = c("101",
"102", "103", "104", "111", "112", "113", "114", "201", "203",
"204", "211", "212", "213", "214"), class = "factor"), age = 1,
sex = 1, school = 1), row.names = 50L, class = "data.frame")
*I have been unsuccessful thus far with supplying the weights variable in step 2 above. I have tried specifying the weight variable as weights = "wt", weights = eval(parse(text = "wt")), weights = eval(quote(wt)), weights = wt[1], etc. All to no avail. Some error statements are below:
weights= as.formula(paste0("~", eval(parse(text = "wt")))) # Error in { : task 1 failed - "(subscript) logical subscript too long"
weights= as.formula(paste0("~", eval(get(wt)))) #Error in get(wt) : object 'wt' not found
I will greatly appreciate any help.
r function survey
i don't think you needeval(get(
..maybe pass in weight as a character vectorwtvar
and then useweights = as.formula( paste( "~" , wtvar ) )
inside yoursvydesign()
call
– Anthony Damico
Nov 20 '18 at 18:16
Thanks! It worked as you suggested. Many thanks.
– Ter
Nov 21 '18 at 4:48
add a comment |
I am trying unsuccessfully to create an R survey design object (from the R survey
package) using arguments supplied within a new function I am creating. This new function seeks to account for the complex survey design in computing prevalence estimates derived as part of the function. I can successfully get the new function to work if I supply the survey design object as an argument in my function; I would however prefer not to do that as it complicates things. Below is sample data, the function I am trying to create and my unsuccessful attempts at creating the survey design object within the new function. For the examples below, the dataframe is n
, the survey design object I wish to create in the new function is svyob
, the weight variable is wt
, the id variable is psu
, and the strata variable is stratum
. The desired structure of my new function, along with default values, is as follows (note that my outcome will be derived within the function. But for purposes of illustration, assume it is the distribution of sex):
prev = function(data, wt, psu = 1, stratum = NULL) {
#Step1: Derive outcome of interest from dataframe (not shown)
#Step2: Create survey design object
svyobj = survey::svydesign(data = n, weights =~ wt[1], id =~ psu[1], strata =~ stratum[1], nest = T)
#Step 3: Generate summary estimates
svymean(~outcome, svyobj, svyobj, na.rm = T)
}
Data:
structure(list(wt = 1365.61822580832, psu = 600815, strata = structure(9L, .Label = c("101",
"102", "103", "104", "111", "112", "113", "114", "201", "203",
"204", "211", "212", "213", "214"), class = "factor"), age = 1,
sex = 1, school = 1), row.names = 50L, class = "data.frame")
*I have been unsuccessful thus far with supplying the weights variable in step 2 above. I have tried specifying the weight variable as weights = "wt", weights = eval(parse(text = "wt")), weights = eval(quote(wt)), weights = wt[1], etc. All to no avail. Some error statements are below:
weights= as.formula(paste0("~", eval(parse(text = "wt")))) # Error in { : task 1 failed - "(subscript) logical subscript too long"
weights= as.formula(paste0("~", eval(get(wt)))) #Error in get(wt) : object 'wt' not found
I will greatly appreciate any help.
r function survey
I am trying unsuccessfully to create an R survey design object (from the R survey
package) using arguments supplied within a new function I am creating. This new function seeks to account for the complex survey design in computing prevalence estimates derived as part of the function. I can successfully get the new function to work if I supply the survey design object as an argument in my function; I would however prefer not to do that as it complicates things. Below is sample data, the function I am trying to create and my unsuccessful attempts at creating the survey design object within the new function. For the examples below, the dataframe is n
, the survey design object I wish to create in the new function is svyob
, the weight variable is wt
, the id variable is psu
, and the strata variable is stratum
. The desired structure of my new function, along with default values, is as follows (note that my outcome will be derived within the function. But for purposes of illustration, assume it is the distribution of sex):
prev = function(data, wt, psu = 1, stratum = NULL) {
#Step1: Derive outcome of interest from dataframe (not shown)
#Step2: Create survey design object
svyobj = survey::svydesign(data = n, weights =~ wt[1], id =~ psu[1], strata =~ stratum[1], nest = T)
#Step 3: Generate summary estimates
svymean(~outcome, svyobj, svyobj, na.rm = T)
}
Data:
structure(list(wt = 1365.61822580832, psu = 600815, strata = structure(9L, .Label = c("101",
"102", "103", "104", "111", "112", "113", "114", "201", "203",
"204", "211", "212", "213", "214"), class = "factor"), age = 1,
sex = 1, school = 1), row.names = 50L, class = "data.frame")
*I have been unsuccessful thus far with supplying the weights variable in step 2 above. I have tried specifying the weight variable as weights = "wt", weights = eval(parse(text = "wt")), weights = eval(quote(wt)), weights = wt[1], etc. All to no avail. Some error statements are below:
weights= as.formula(paste0("~", eval(parse(text = "wt")))) # Error in { : task 1 failed - "(subscript) logical subscript too long"
weights= as.formula(paste0("~", eval(get(wt)))) #Error in get(wt) : object 'wt' not found
I will greatly appreciate any help.
r function survey
r function survey
asked Nov 20 '18 at 3:24
TerTer
132
132
i don't think you needeval(get(
..maybe pass in weight as a character vectorwtvar
and then useweights = as.formula( paste( "~" , wtvar ) )
inside yoursvydesign()
call
– Anthony Damico
Nov 20 '18 at 18:16
Thanks! It worked as you suggested. Many thanks.
– Ter
Nov 21 '18 at 4:48
add a comment |
i don't think you needeval(get(
..maybe pass in weight as a character vectorwtvar
and then useweights = as.formula( paste( "~" , wtvar ) )
inside yoursvydesign()
call
– Anthony Damico
Nov 20 '18 at 18:16
Thanks! It worked as you suggested. Many thanks.
– Ter
Nov 21 '18 at 4:48
i don't think you need
eval(get(
..maybe pass in weight as a character vector wtvar
and then use weights = as.formula( paste( "~" , wtvar ) )
inside your svydesign()
call– Anthony Damico
Nov 20 '18 at 18:16
i don't think you need
eval(get(
..maybe pass in weight as a character vector wtvar
and then use weights = as.formula( paste( "~" , wtvar ) )
inside your svydesign()
call– Anthony Damico
Nov 20 '18 at 18:16
Thanks! It worked as you suggested. Many thanks.
– Ter
Nov 21 '18 at 4:48
Thanks! It worked as you suggested. Many thanks.
– Ter
Nov 21 '18 at 4:48
add a comment |
1 Answer
1
active
oldest
votes
I finally got my code working with both of the following (went for #2):
#Using (eval(parse())
svyobject = svydesign(data=n, id= as.formula(paste0("~", eval(parse(text = "psu")))) , strata=as.formula(paste0("~", eval(parse(text = "stratum")))), weights= as.formula(paste0("~", eval(parse(text = "wt")))), nest=TRUE)
#Using (eval(get())
svyobject = svydesign(data=n, id= as.formula(paste0("~", eval(get("psu")))) , strata=as.formula(paste0("~", eval(get("stratum")))), weights= as.formula(paste0("~", eval(get("wt")))), nest=TRUE)
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53385767%2fcreating-an-r-survey-design-object-within-another-function%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
I finally got my code working with both of the following (went for #2):
#Using (eval(parse())
svyobject = svydesign(data=n, id= as.formula(paste0("~", eval(parse(text = "psu")))) , strata=as.formula(paste0("~", eval(parse(text = "stratum")))), weights= as.formula(paste0("~", eval(parse(text = "wt")))), nest=TRUE)
#Using (eval(get())
svyobject = svydesign(data=n, id= as.formula(paste0("~", eval(get("psu")))) , strata=as.formula(paste0("~", eval(get("stratum")))), weights= as.formula(paste0("~", eval(get("wt")))), nest=TRUE)
add a comment |
I finally got my code working with both of the following (went for #2):
#Using (eval(parse())
svyobject = svydesign(data=n, id= as.formula(paste0("~", eval(parse(text = "psu")))) , strata=as.formula(paste0("~", eval(parse(text = "stratum")))), weights= as.formula(paste0("~", eval(parse(text = "wt")))), nest=TRUE)
#Using (eval(get())
svyobject = svydesign(data=n, id= as.formula(paste0("~", eval(get("psu")))) , strata=as.formula(paste0("~", eval(get("stratum")))), weights= as.formula(paste0("~", eval(get("wt")))), nest=TRUE)
add a comment |
I finally got my code working with both of the following (went for #2):
#Using (eval(parse())
svyobject = svydesign(data=n, id= as.formula(paste0("~", eval(parse(text = "psu")))) , strata=as.formula(paste0("~", eval(parse(text = "stratum")))), weights= as.formula(paste0("~", eval(parse(text = "wt")))), nest=TRUE)
#Using (eval(get())
svyobject = svydesign(data=n, id= as.formula(paste0("~", eval(get("psu")))) , strata=as.formula(paste0("~", eval(get("stratum")))), weights= as.formula(paste0("~", eval(get("wt")))), nest=TRUE)
I finally got my code working with both of the following (went for #2):
#Using (eval(parse())
svyobject = svydesign(data=n, id= as.formula(paste0("~", eval(parse(text = "psu")))) , strata=as.formula(paste0("~", eval(parse(text = "stratum")))), weights= as.formula(paste0("~", eval(parse(text = "wt")))), nest=TRUE)
#Using (eval(get())
svyobject = svydesign(data=n, id= as.formula(paste0("~", eval(get("psu")))) , strata=as.formula(paste0("~", eval(get("stratum")))), weights= as.formula(paste0("~", eval(get("wt")))), nest=TRUE)
answered Nov 20 '18 at 22:29
TerTer
132
132
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53385767%2fcreating-an-r-survey-design-object-within-another-function%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
i don't think you need
eval(get(
..maybe pass in weight as a character vectorwtvar
and then useweights = as.formula( paste( "~" , wtvar ) )
inside yoursvydesign()
call– Anthony Damico
Nov 20 '18 at 18:16
Thanks! It worked as you suggested. Many thanks.
– Ter
Nov 21 '18 at 4:48