mhunter1 / dynr

Dynamic Modeling in R
5 stars 6 forks source link

Add an example of regime-switching nonlinear continuous-time models and make sure it works #37

Closed mhunter1 closed 8 years ago

mhunter1 commented 8 years ago

Original report by Lu Ou (Bitbucket: luou, GitHub: luou).


mhunter1 commented 8 years ago

Original comment by Sy-Miin Chow (Bitbucket: symiin, GitHub: symiin).


The current RSLinearODE.R works.

mhunter1 commented 8 years ago

Original comment by Lu Ou (Bitbucket: luou, GitHub: luou).


Sorry, I meant a regime-switching nonlinear ODE model.

mhunter1 commented 8 years ago

Original comment by Sy-Miin Chow (Bitbucket: symiin, GitHub: symiin).


It would be relatively easy for me to work on this. Can you please prioritize implementing the printex method for the prep.formulaDynamics recipe object? You are more familiar with the formulaDynamics object. For me to modify the plot function to show the equation with the estimated parameter values I need to use some of the code in the printex method for this recipe object.

mhunter1 commented 8 years ago

Original comment by Sy-Miin Chow (Bitbucket: symiin, GitHub: symiin).


Wrote an R script for generating data from a RS predator-prey model (SimulateRS-PP.R) Started a recipe script in RS-PPmodel.R But dynr.cook gives me this weird error in compiling the Jacobian C code complaining an xstart. Not sure where that came from. Does anyone have any insights? If not, I will just look more closely later to see whether I made an error elsewhere.

formula=list( list(prey~ r1prey - a12preypredator - a11prey^2, predator~ -r2predator + a21preypredator - a22predator^2), list(prey~ r1prey - a12preypredator, predator~ -r2predator + a21preypredator))

Starting values are on constrained scale

dynm<-prep.formulaDynamics(formula=formula, startval=c(r1=3, r2=2, a12 = 1, a21 = 1, a11 =.5, a22 = .5), isContinuousTime=TRUE) #,jacobian=jacob

ERROR(s) during compilation: source code errors or compiler configuration errors!

Program source: 1: #include Error in CompileCode(code, language, verbose, libLFile) : Compilation ERROR, function(s)/method(s) not created! RSPPmodelRecipe.c.c: In function ‘function_dF_dx’: RSPPmodelRecipe.c.c:59: error: ‘xstart’ undeclared (first use in this function) RSPPmodelRecipe.c.c:59: error: (Each undeclared identifier is reported only once RSPPmodelRecipe.c.c:59: error: for each function it appears in.) make: *** [RSPPmodelRecipe.c.o] Error 1

cat(writeCcode(model$dynamics)$c.string) void function_dx_dt(double t, size_t regime, const gsl_vector x, double param, size_t n_param, const gsl_vector co_variate, gsl_vector F_dx_dt){ switch (regime) { case 0: gsl_vector_set(F_dx_dt,0,param[0] gsl_vector_get(x,0) - param[2] gsl_vector_get(x,0) gsl_vector_get(x,1) - param[4] pow(gsl_vector_get(x,0), 2)); gsl_vector_set(F_dx_dt,1,-param[1] gsl_vector_get(x,1) + param[3] gsl_vector_get(x,0) gsl_vector_get(x,1) - param[5] pow(gsl_vector_get(x,1), 2)); break;

    case 1:
    gsl_vector_set(F_dx_dt,0,param[0] * gsl_vector_get(x,0) - param[2] * gsl_vector_get(x,0) * gsl_vector_get(x,1));
    gsl_vector_set(F_dx_dt,1,-param[1] * gsl_vector_get(x,1) + param[3] * gsl_vector_get(x,0) * gsl_vector_get(x,1));
break;

    }
}

void function_dF_dx(double t, size_t regime, double param, const gsl_vector co_variate, gsl_matrix F_dx_dt_dx){ switch (regime) { case 0: gsl_matrix_set(F_dx_dt_dx,0,0,param[0] - param[2] gsl_vector_get(xstart,1) - param[4] (2 gsl_vector_get(xstart,0))); gsl_matrix_set(F_dx_dt_dx,0,1,-(param[2] gsl_vector_get(xstart,0))); gsl_matrix_set(F_dx_dt_dx,1,0,param[3] gsl_vector_get(xstart,1)); gsl_matrix_set(F_dx_dt_dx,1,1,param[3] gsl_vector_get(xstart,0) - param[1] - param[5] (2 * gsl_vector_get(xstart,1))); break;

case 1:
    gsl_matrix_set(F_dx_dt_dx,0,0,param[0] - param[2] * gsl_vector_get(xstart,1));
    gsl_matrix_set(F_dx_dt_dx,0,1,-(param[2] * gsl_vector_get(xstart,0)));
    gsl_matrix_set(F_dx_dt_dx,1,0,param[3] * gsl_vector_get(xstart,1));
    gsl_matrix_set(F_dx_dt_dx,1,1,param[3] * gsl_vector_get(xstart,0) - param[1]);
break;

    }
}
mhunter1 commented 8 years ago

Original comment by Sy-Miin Chow (Bitbucket: symiin, GitHub: symiin).


mhunter1 commented 8 years ago

Original comment by Sy-Miin Chow (Bitbucket: symiin, GitHub: symiin).


mhunter1 commented 8 years ago

Original comment by Sy-Miin Chow (Bitbucket: symiin, GitHub: symiin).


I was running the single-regime predator-prey model but found another potential bug. Seems like there is an error in the generated dx_dt code, which refers to x_tend and xstart but they are undeclared. The recipe for fitting this single-regime model is in PPmodel.R in the demo folder in Symiin_Lu/dynr. Script for generating the data is called SimulatePP.R in the same folder. Lu, please take a look and see what is going on.

cat(writeCcode(model$dynamics)$c.string) void function_dx_dt(double t, size_t regime, const gsl_vector x, double param, size_t n_param, const gsl_vector co_variate, gsl_vector F_dx_dt){ gsl_vector_set(x_tend,0,param[0] gsl_vector_get(xstart,0) - param[2] gsl_vector_get(xstart,0) gsl_vector_get(xstart,1)); gsl_vector_set(x_tend,1,-param[1] gsl_vector_get(xstart,1) + param[3] gsl_vector_get(xstart,0) gsl_vector_get(xstart,1)); }

/**

ERROR(s) during compilation: source code errors or compiler configuration errors!

Program source: 1: #include Error in CompileCode(code, language, verbose, libLFile) : Compilation ERROR, function(s)/method(s) not created! PPmodelRecipe.c.c: In function ‘function_dx_dt’: PPmodelRecipe.c.c:42: error: ‘x_tend’ undeclared (first use in this function) PPmodelRecipe.c.c:42: error: (Each undeclared identifier is reported only once PPmodelRecipe.c.c:42: error: for each function it appears in.) PPmodelRecipe.c.c:42: error: ‘xstart’ undeclared (first use in this function) PPmodelRecipe.c.c: In function ‘function_dF_dx’: PPmodelRecipe.c.c:51: error: ‘Jx’ undeclared (first use in this function) PPmodelRecipe.c.c:51: error: expected ‘]’ before ‘)’ token PPmodelRecipe.c.c:51: error: expected ‘;’ before ‘)’ token PPmodelRecipe.c.c:51: error: expected statement before ‘)’ token PPmodelRecipe.c.c:52: error: expected ‘]’ before ‘)’ token PPmodelRecipe.c.c:52: error: expected ‘;’ before ‘)’ token PPmodelRecipe.c.c:52: error: expected statement before ‘)’ token PPmodelRecipe.c.c:53: error: expected ��

mhunter1 commented 8 years ago

Original comment by Lu Ou (Bitbucket: luou, GitHub: luou).


This commit c8bf2cd fixed the typos.

mhunter1 commented 8 years ago

Original comment by Lu Ou (Bitbucket: luou, GitHub: luou).


commit c70a4d3 resolved this issue.