Pregunta

I am trying to index some components in a R formula such like

var<-c("x1", "x2", "x3")

for (i in var) as.formula(i/x4)

But it always come with an error:

Error in i/x4 : non-numeric argument to binary operator

May somebody know how to make it work? so that out with,

x1/x4, x2/x4, x3/x4
¿Fue útil?

Solución

After reading OP's comments, changing answer to OP's needs

for (i in var) {
        form <-sprintf("~ %s/ x4 ",i)
        deltamethod(as.formula(form),...) #other arguments that you need to supply

}

Otros consejos

Based on a comment from the OP it seems these formulas are meant to be used as the arguments to the msm::deltamethod function. From ?msm::deltamethod, we read that an example formula would look like ~ 1 / (x1 + x2):

sapply(var, function(x) as.formula(paste0("~", x, "/x4")))
# $x1
# ~x1/x4
# <environment: 0x1016b07f0>
# 
# $x2
# ~x2/x4
# <environment: 0x1016a3ea0>
# 
# $x3
# ~x3/x4
# <environment: 0x101698630>

Now we have a list of formulas, each of which could be used as the first argument to msm::deltamethod.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top