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

Cannot predict on new data after using weights during fit #8

Open
mfansler opened this issue Jul 1, 2022 · 0 comments
Open

Cannot predict on new data after using weights during fit #8

mfansler opened this issue Jul 1, 2022 · 0 comments

Comments

@mfansler
Copy link

mfansler commented Jul 1, 2022

Problem

The predict function does not work if a weights argument is specified during model fitting and the newdata argument is of different length than the original data.

Reproducible Example

library(DirichletReg)
data("ArcticLake")

ArcticLake$Y <- DR_data(ArcticLake[,c(1,2,3)])
wts <- sample(c(1,2), nrow(df_al), replace=TRUE)

model_al <- DirichReg(Y ~ depth, ArcticLake, weights=wts)

predict(model_al, newdata=ArcticLake[1:10,])

which results in the error:

Error in model.frame.default(terms(formula, lhs = lhs, rhs = rhs, data = data,  : 
  variable lengths differ (found for '(weights)')

Additional Info

If data is the same length as the original data it works fine. To verify that the weights make no difference to the actual prediction, we can shuffle the data before or after, observing the same results:

idx_shuffle <- sample.int(nrow(ArcticLake))

pred1 <- predict(model_al, newdata=ArcticLake)[idx_shuffle,]
pred2 <- predict(model_al, newdata=ArcticLake[idx_shuffle,])

all(pred1 == pred2)
## TRUE

System Details

> sessionInfo()
R version 4.1.3 (2022-03-10)
Platform: x86_64-apple-darwin13.4.0 (64-bit)
Running under: macOS Big Sur 11.6.7

Matrix products: default
LAPACK: /Users/mfansler/miniconda3/envs/brms_r41/lib/libopenblasp-r0.3.20.dylib

locale:
[1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
[1] DirichletReg_0.7-1 Formula_1.2-4     

loaded via a namespace (and not attached):
 [1] zoo_1.8-10       compiler_4.1.3   generics_0.1.2   miscTools_0.6-26 sandwich_3.0-2   tools_4.1.3      maxLik_1.5-2    
 [8] grid_4.1.3       digest_0.6.29    lattice_0.20-45 
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

1 participant