Problem with object types creating a function

Hello. I need to write a sintax that reads a SPSS database and then estimate totals using the svyby() command. The problem appears when I try to do it into a function. First I read the data outside of the function with these commands:

baseTot <- read_sav(here("C:/Base.sav")) %>%
rename(UPMs=Cons, Estrato=Estra, Factor=Fact)
diseño <- svydesign(id=~1, strata=~Estrato, data=baseTot, weights=~Factor
marco <- as_factor(baseTot$TipodeMarco)
cruce <- as_factor(baseTot$Sector)
y <- as_factor(baseTot$Con)

Then I run this function:

estimaciones <- function(baseTot, diseño, y, cruce) {
a_n <- svyby(~y, ~marco, diseño, unwtd.count, keep.var=FALSE)
a_e <- svyby(~y, ~marco, diseño, svytotal, deff=TRUE, keep.var=TRUE, vartype=c("se","ci","cvpct"))
ab_n <- svyby(~y, ~cruce+marco, diseño, unwtd.count, keep.var=FALSE)
ab_e <- svyby(~y, ~cruce+marco, diseño, svytotal, deff=TRUE, keep.var=TRUE, vartype=c("se","ci","cvpct"))

And when I call the function I obtain this error:

est <- estimaciones(baseTot, diseño, ~y, ~cruce)
Error in model.frame.default(x, model.frame(design), na.action = na.pass) :
object is not a matrix
Called from: model.frame.default(x, model.frame(design), na.action = na.pass)

It is important to mention that the commands work properly if they are not into a function. What could be the problem?

I appreciate your help on this issue.

Thank you

Hi, can you provide a reproducible example of baseTot? It would make it easier to help and we don't have your dataset.

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.