-
-
Notifications
You must be signed in to change notification settings - Fork 191
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
Structured covariates for non-linear functions #1488
Comments
This feature is now implemented. Below you can find an example: set.seed(2134)
N <- 100
dat <- data.frame(y=rnorm(N))
dat$X <- matrix(rnorm(N*2), N, 2)
nlfun_stan <- "
real nlfun(real a, real b, real c, row_vector X) {
return a + b * X[1] + c * X[2];
}
"
nlstanvar <- stanvar(scode = nlfun_stan, block = "functions")
# version for R post processing
nlfun <- function(a, b, c, X) {
a + b * X[, , 1] + c * X[, , 2]
}
# fit the model
bform <- bf(y~nlfun(a, b, c, X), a~1, b~1, c~1, nl = TRUE)
fit <- brm(bform, dat, stanvars = nlstanvar)
summary(fit)
# fit benchmark model that should yield the same results up to MCMC error
fit2 <- brm(y~X, dat)
summary(fit2)
# post processing works too
str(posterior_epred(fit))
loo(fit, fit2) |
WOW... it works with integers too: set.seed(2134)
N <- 100
dat <- data.frame(y=rnorm(N))
dat$X <- matrix(as.integer(rnorm(N*2, 0, 10)), N, 2)
nlfun_stan <- "
real nlfun(real a, real b, real c, int[] X) {
return a + b * X[1] + c * X[2];
}
"
nlstanvar <- stanvar(scode = nlfun_stan, block = "functions")
# version for R post processing
nlfun <- function(a, b, c, X) {
a + b * X[, , 1] + c * X[, , 2]
}
# fit the model
bform <- bf(y~nlfun(a, b, c, X), a~1, b~1, c~1, nl = TRUE)
fit <- brm(bform, dat, stanvars = nlstanvar)
summary(fit)
stancode(fit) Awesome! |
FYI... the |
yes, new issue. thank you
wds15 ***@***.***> schrieb am Fr., 26. Mai 2023, 14:01:
… FYI... the expose_functions(model, vectorize=TRUE) does not work any
longer with these structured covariates...new issue?
—
Reply to this email directly, view it on GitHub
<#1488 (comment)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/ADCW2AHV7OXY26TIGAJCLI3XICLRFANCNFSM6AAAAAAXNREJTE>
.
You are receiving this because you modified the open/close state.Message
ID: ***@***.***>
|
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I am working with Pharmacometrics models which require non-linear functions. So the dosing history of each patient is needed to calculate with a non-linear function the drug concentration in blood at any time-point whenever I have an observation of a patient. A solution is a wide data format, but this is inconvenient as the number of dosing time-points which I need to take into account can get large. Below is an example of what I have in mind. So I would like to use the covariates
dose_time
anddose_amt
as argument to a non-linear function.Information on drug dosing is most naturally specified in a long
format. Here is an example for two regimens where we administer 8
units of drug to a patient at two occasions. The time-points are 24
hours apart, but we use a different amount at each time-point.
To use such information in brms the dosing history must be cast
into a wide format.
Only in wide format one can merge the dosing history with
observations from a patient. Here is a pseudo data set as example:
modeling this data-set works in brms as we have all dosing
information available in a given row, which is needed to model the
measured data y…
… but it is super in-convenient, since we need to deal with an
arbitrary wide format here! More doses mean more columns, etc.
Thus a nested format would be much easier to deal with.
So let’s cast data into a nested format such that the dosing
columns themselve contain data vetors in each entry:
tibble
having nested data
now the model data set becomes a lot easier
tibble
having nested data
What would now be needed in brms is to allow for strucutred columns to be passed as covariates to
non-linear functions in brms.
Created on 2023-04-27 with reprex v2.0.2
... and as a fancy add-on... I think it would actually be nice to also allow the response
y
in a brms model to come from such a nested thing.... but that's not directly related...The text was updated successfully, but these errors were encountered: