American-Institutes-for-Research / EdSurvey

https://american-institutes-for-research.github.io/EdSurvey/
GNU General Public License v2.0
9 stars 8 forks source link

Parsing expressions for groupA and groupB args within a function #52

Closed Keddeko closed 1 year ago

Keddeko commented 1 year ago

statement of issue

I have the problem that I cannot parse any expressions for the groupA and groupB outside the function call. In the documentation it states: "an expression or character expression that defines a condition for the subset. This subset will be compared to groupB. If not specified, it will define a whole sample as in data."

A minimal example from the documentation: This will work fine:

sdf <- readNAEP(system.file("extdata/data", "M36NT2PM.dat", package = "NAEPprimer"))
# find the mean score gap in the primer data between males and females
gap("composite", sdf, dsex=="Male", dsex=="Female")

whereas this will not;

grA_character_expression <- 'dsex=="Male"'
grA_character_expression <- 'dsex=="Female"'
gap("composite", sdf, groupA=grA_character_expression, groupB =grA_character_expression )

Describe alternatives you've considered

It will be possible to paste0() an argument and then eval(), but not pretty programming.

Additional context

I would like to make make a function that make use of the gap function. It is even not possible to parse with ... to the gap function for these args - all other like data, variable etc. works out fine.

[cleaned up a bit by PB to remove content from the form]

pdbailey0 commented 1 year ago

I can confirm that this code does not work and would be useful for a loop.

These do work, and I wonder (for right now) if the second one helps:

gap("composite", sdf, groupA="dsex==\"Male\"")
mle <- "Male"
gap("composite", sdfl, dsex==mle, dsex=="Female")
Keddeko commented 1 year ago

It could be part of it, perhaps it can be decomposed, but I can really not figure out why there is a problem

Best regards Christian Christrup Kjeldsen Dr.Phil, Associate Professor Centre director The National Centre for School Research

Aarhus University Faculty of Arts Danish School of Education (DPU) Jens Chr. Skous Vej 4, Nobelparken, building 1483, room 619 8000 Aarhus C T: +45 51 37 01 88 [edited by PB to remove previous email]

pdbailey0 commented 1 year ago

It can be done in this somewhat non-standard way, but it's a bit clunky

grA <- "dsex==\"Male\""
grB <- "dsex==\"Female\""
sdfA <- subset(sdf, grA, inside=TRUE)
sdfB <- subset(sdf, grB, inside=TRUE)
sdf2 <- edsurvey.data.frame.list(list(sdfA, sdfB), labels=c(grA, grB))
gap("composite", sdf2)

the output is not exactly what you would expect, but it is calculating the covariances as if they two data sets are one survey.

pdbailey0 commented 1 year ago

Try the version on this site now. I'd appreciate learning how it works for you.

# You can install the development version from GitHub with:
install.packages("devtools")
devtools::install_github("American-Institutes-for-Research/edsurvey")
Keddeko commented 1 year ago

Dear all

Thank you for the effort. I will try it out and return with my experience.

Best regards Christian Christrup Kjeldsen Dr.Phil, Associate Professor Centre director The National Centre for School Research

Aarhus University Faculty of Arts Danish School of Education (DPU) Jens Chr. Skous Vej 4, Nobelparken, building 1483, room 619 8000 Aarhus C T: +45 51 37 01 88 M: @.**@.>

[A4F33180]http://dk.linkedin.com/pub/christian-christrup-kjeldsen/31/675/270[9E6F138E]http://myskype.info/christian.christrup.kjeldsen [8B8456CC] http://pure.au.dk/portal/da/persons/id(401b523f-a4ff-43e9-aaeb-b38bf32f0fd9).html

Fra: Paul Bailey @.> Sendt: 19. april 2023 01:44 Til: American-Institutes-for-Research/EdSurvey @.> Cc: Christian Christrup Kjeldsen @.>; Author @.> Emne: Re: [American-Institutes-for-Research/EdSurvey] Parsing expressions for groupA and groupB args within a function (Issue #52)

Try the version on this site now. I'd appreciate learning how it works for you.

You can install the development version from GitHub with:

install.packages("devtools")

devtools::install_github("American-Institutes-for-Research/edsurvey")

— Reply to this email directly, view it on GitHubhttps://github.com/American-Institutes-for-Research/EdSurvey/issues/52#issuecomment-1513916438, or unsubscribehttps://github.com/notifications/unsubscribe-auth/A4DGIPQV3HR4UG23QAYVMG3XB4RKXANCNFSM6AAAAAAXC6NWEA. You are receiving this because you authored the thread.Message ID: @.**@.>>

Keddeko commented 1 year ago

Dear all Thank you for the update. I tried it out and its nearly there in order to fulfill the expectations in regards of the three dots argument that will forward arguments from one function to the other is still not working out. I guess it would be a good thing if the package as such supported this feature. A minimal example:

Will work, both for character and expression inputs - thanks

gra <- expression(language_at_home=="Always or almost always") grb <- 'language_at_home == "Sometimes or never"' gap(variable = "mmat", data = timss_dnk_trend, groupA =gra, groupB = grb )

This is the more generic approach with the dynamic dots argument (forward args)

NewFunc <- function(...){ gap(...) }

This part will work with no issues

NewFunc(variable = "mmat", data = timss_dnk_trend)

This will not

NewFunc(variable = "mmat", data = timss_dnk_trend,groupA =language_at_home=="Always or almost always", groupB = language_at_home == "Sometimes or never" )

Best regards Christian Christrup Kjeldsen Dr.Phil, Associate Professor Centre director The National Centre for School Research

Aarhus University Faculty of Arts Danish School of Education (DPU) Jens Chr. Skous Vej 4, Nobelparken, building 1483, room 619 8000 Aarhus C T: +45 51 37 01 88 M: @.**@.>

[A4F33180]http://dk.linkedin.com/pub/christian-christrup-kjeldsen/31/675/270[9E6F138E]http://myskype.info/christian.christrup.kjeldsen [8B8456CC] http://pure.au.dk/portal/da/persons/id(401b523f-a4ff-43e9-aaeb-b38bf32f0fd9).html

Fra: Paul Bailey @.> Sendt: 19. april 2023 01:44 Til: American-Institutes-for-Research/EdSurvey @.> Cc: Christian Christrup Kjeldsen @.>; Author @.> Emne: Re: [American-Institutes-for-Research/EdSurvey] Parsing expressions for groupA and groupB args within a function (Issue #52)

Try the version on this site now. I'd appreciate learning how it works for you.

You can install the development version from GitHub with:

install.packages("devtools")

devtools::install_github("American-Institutes-for-Research/edsurvey")

— Reply to this email directly, view it on GitHubhttps://github.com/American-Institutes-for-Research/EdSurvey/issues/52#issuecomment-1513916438, or unsubscribehttps://github.com/notifications/unsubscribe-auth/A4DGIPQV3HR4UG23QAYVMG3XB4RKXANCNFSM6AAAAAAXC6NWEA. You are receiving this because you authored the thread.Message ID: @.**@.>>

pdbailey0 commented 1 year ago

@keddeko, I've run into this a lot with ... behaving not quite how I expect. In this case the warning makes me think it is subset not EdSurvey causing the problem.

require(EdSurvey)
sdf <- readNAEP(system.file("extdata/data", "M36NT2PM.dat", package = "NAEPprimer"))

NewFunc <- function(...){
  gap(...)
}

gap(variable = "composite", data = sdf,groupA = dsex=="Male")
# valid output
NewFunc(variable = "composite", data = sdf,groupA = dsex=="Male")
# Error in eval(subset) : the ... list contains fewer than 3 elements

Using a string does work thought because that doesn't get evaluated until the subset gets evaluated

NewFunc(variable = "composite", data = sdf,groupA = "dsex==\"Male\"")

But I'm not convinced this isn't general R weirdness, not EdSurvey weirdness. In particular, you need to delay the evaluation of the argument in a way that just pitching it into ... won't achieve. Most of the things we've had to do are in gap itself, so you can look there. It looks like we call substitute a lot.