Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

posterior_predict() fails with newdata for models estimated with family = mgcv::betar #407

Open
martinmodrak opened this issue Dec 21, 2019 · 4 comments

Comments

@martinmodrak
Copy link

Summary:

Similar to #324, I can run posterior_predict and posterior_linpred for betareg models, but as soon as I specify newdata = XX I get an error.

Description:

Running posterior_linpred with newdata (even if the newdata argument is the same as the data used to estimate the model) results in the following error:

Error in (function (optimizer = "nloptwrap", restart_edge = TRUE, boundary.tol = 1e-05, : unused arguments (tolPwrss = 1e-07, compDev = TRUE, nAGQ0initStep = TRUE, checkControl = list("ignore", "ignore", "ignore", "stop", "ignore", "message+drop.cols", "warning", "stop", "ignore"), checkConv = list(list("ignore", 0.001, NULL), list("ignore", 1e-04), list("ignore", 1e-06)))

The workaround mentioned in #324 (adding class) does not help.

Reproducible Steps:

library(rstanarm)
test <- data.frame(y = c(0.1, 0.3), x = c(TRUE, FALSE))

SEED=101
res <- stan_glmer(y ~ (1|x), family=mgcv::betar(link="logit"), data=test, seed = SEED, iter = 200)

class(res) <- c(class(res), "betareg") #Workaround from issue 324
P <- posterior_linpred(res, newdata=test)

RStanARM Version:

2.19.2

R Version:

3.6.0

Operating System:

Windows 10

@martinmodrak
Copy link
Author

A duplicate of #406.

@jwdorrough
Copy link

@martinmodrak, not sure if this helps solve the issue but posterior_predict and posterior_linpred do run if newdata are specified and the predictions ignore the group level terms i.e. re.form=~0 or NA

library(rstanarm)
test <- data.frame(y = c(0.1, 0.3), x = c(TRUE, FALSE),B1=c(1,2))

SEED=101
res <- stan_glmer(y ~ (1|x), family=mgcv::betar(link="logit"), data=test, seed = SEED, iter = 200)
res1 <- stan_glmer(y ~ B1+(1|x), family=mgcv::betar(link="logit"), data=test, seed = SEED, iter = 200)

P <- posterior_linpred(res, newdata=test)#throws an error
P1<-posterior_linpred(res1, newdata=test)#throws an error
P2<-posterior_linpred(res1, newdata=test, re.form=~0)#runs

@jgabry
Copy link
Member

jgabry commented May 13, 2020

I think this is finally fixed now. Sorry it took so long to get to this! The fix is now on the master branch and will be included in the next release.

@GitCJW
Copy link

GitCJW commented Mar 18, 2022

I am still getting errors when using the posterior_* functions for betar models.
rstanarm: 2.21.1
rstan: 2.26.3
Win10

set.seed(123)
data <- data.frame(y=rbeta(100,100,200),x=rnorm(100))
fit <- rstanarm::stan_glm("y~x",data=data,family=mgcv::betar(link="logit"), chain=1)

# works
rstanarm::posterior_epred(fit, draws=100)
rstanarm::posterior_epred(fit, newdata=data.frame(x=1))

# Error in exp(eta) : non-numeric argument to mathematical function
rstanarm::posterior_predict(fit, draws=100) 
rstanarm::posterior_predict(fit, newdata=data.frame(x=1)) 

class(fit) <- c(class(fit), "betareg")

# works
rstanarm::posterior_epred(fit, draws=100)
rstanarm::posterior_predict(fit, draws=100) 

# Error in terms.formula(formula, data = data) : argument is not a valid model 
rstanarm::posterior_epred(fit, newdata=data.frame(x=1))
rstanarm::posterior_predict(fit, newdata=data.frame(x=1))

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants