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

Automatic Model Generation #35

Open
discoleo opened this issue Feb 11, 2022 · 1 comment
Open

Automatic Model Generation #35

discoleo opened this issue Feb 11, 2022 · 1 comment

Comments

@discoleo
Copy link
Collaborator

discoleo commented Feb 11, 2022

[some initial ideas]

Example:

model = "S ~ Y + O; I ~ .; H ~ .; S %*% I -> I; S %*% H -> I;
   I -> H + R + D; H -> R + D; V ~ .; (time >= t0) & S^0 -> V;"
model = strsplit(model, ";")[[1]];

el = lapply(model, function(e) parse(text = e))
print(el)

# using classic loop:
for(sM in model) {
  eq = parse(text = sM);
}

1.) Stratification:

  • stratified compartment: S ~ Y + O;
  • stratified compartment inheriting strata from parent compartment: I ~ .;

2.) Evolution

  • simple: H -> R + D;
  • interaction: S %*% I -> H + R + D;

Note:

  • R & D: are unstratified;
  • C1 %*% C2: implies C1[compartments] * sum(C2);
  • S^0 -> V: implies dS = - const["SV"]; dV = + const["SV"];
  • TODO:
    time delay: e.g. (time >t0), how to specify "time" and "t0";
  • alternative model specification: package dMod (but it is not practical);
    see https://cran.r-project.org/web/packages/dMod/vignettes/dMod.html
@discoleo
Copy link
Collaborator Author

discoleo commented Feb 13, 2022

strata = function(x) {
	len  = length(x);
	vars = list();
	isStratEq = rep(FALSE, len);
	extract.strata = function(e) {
		sStrata = unique(unlist(lapply(e, as.character)));
		sStrata = sStrata[ ! sStrata %in% c("+", "-")];
	}
	
	for(id in seq(len)) {
		e = x[[id]][[1]];
		# Strata:
		if(as.character(e[[1]]) == "~") {
			isStratEq[id] = TRUE;
			sVar = as.character(e[[2]]);
			v = vars[[sVar]];
			if(is.null(v)) {
				if(as.character(e[[3]] == ".")) {
					vars[[sVar]] = list(hasStrata = TRUE, Strata = character(0));
				} else {
					sStrata = extract.strata(e[[3]]);
					vars[[sVar]] = list(hasStrata = TRUE, Strata = sStrata);
				}
			} else if(v$hasStrata == FALSE) {
				stop(paste0("Variable ", sVar, ": inconsistency in strata!"));
			} else if(length(v$Strata) == 0) {
				if(as.character(e[[3]] == ".")) {
					# NOTHING
				} else {
					sStrata = extract.strata(e[[3]]);
					vars[[sVar]]$Strata = sStrata;
				}
			} else {
				# TODO
				warning(paste0("Variable ", sVar, ": strata already defined!"));
			}
		}
	}
	#
	isStratEq = which(isStratEq);
	attr(vars, "expressions") = isStratEq;
	return(vars)
}

TODO:

  • extract parents;
  • carry forward strata;

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