hesim-dev / hesim

Health economic simulation modeling and decision analysis
https://hesim-dev.github.io/hesim/
62 stars 17 forks source link

dcast() error: Aggregate function missing, defaulting to 'length' #5

Closed MattheneusHTA closed 5 years ago

MattheneusHTA commented 5 years ago

I get the above error when trying to execute this: nmb_dt <- dcast(ce[sample %in% random_rows & grp == "group 1"], sample ~ strategy_id, value.var = "nmb")

Could you please assist? Kind regards Paul

dincerti commented 5 years ago

Hi Paul, please provide the minimum necessary reproducible code that documents the error/warning, otherwise it is nearly impossible to diagnose.

MattheneusHTA commented 5 years ago

Sorry for that, I tried to send the dataset but could not attach it. Here is the code:

random_rows <- sample(1:n_samples, 10) nmb_dt <- dcast(ce_apa[sample %in% random_rows & patient_id == "PSADT≤6"], sample ~ strategy_id, value.var = "nmb") setnames(nmb_dt, colnames(nmb_dt), c("sample", "nmb1", "nmb2", "nmb3")) nmb_dt <- nmb_dt[, maxj := apply(nmb_dt[, .(nmb1, nmb2, nmb3)], 1, which.max)] nmb_dt <- nmb_dt[, maxj := factor(maxj, levels = c(1, 2, 3))]

And here is the structure of the dataset: Classes ‘data.table’ and 'data.frame': 1200 obs. of 9 variables: $ sample : num 1 1 1 1 1 1 1 1 1 1 ... $ strategy_id: chr "Apa" "Apa" "Apa" "Apa" ... $ patient_id : chr "PSADT≤6" "PSADT≤6" "PSADT≥6" "PSADT≥6" ... $ state_id : int 1 2 1 2 1 2 1 2 1 2 ... $ dr : num 0.03 0.03 0.03 0.03 0.03 0.03 0.03 0.03 0.03 0.03 ... $ qalys : num 0.457 0.476 0.506 0.395 0.496 ... $ category : chr "drug" "drug" "drug" "drug" ... $ total_costs: num 78287 94691 88154 79775 90815 ... $ nmb : num 333223 334073 367339 275444 355917 ...

I can send the dataset itself as well.. please advise

-----------------

In addition, the dcast() function below works great with the same dataset. One difference between these two codes is that the one here below inputs already aggregated data, where as the one above doesn't. Please advise

ce_apa <- ce_apa[, nmb := 900000 * qalys - total_costs] enmb <- ce_apa[, .(enmb = mean(nmb)), by = c("strategy_id", "patient_id")] enmb <- dcast(enmb, strategy_id ~ patient_id, value.var = "enmb")

kind regards

dincerti commented 5 years ago

Hi Paul, since this is a question about using dcast rather than a hesim issue, I'm going to close this. That said, I suspect the issue is that the ce_apa dataset is not aggregated across state_id.