Giter VIP home page Giter VIP logo

Comments (2)

 avatar commented on July 17, 2024

Furthermore, former when I used the following parameterization, it works

sir<-pomp(data=data.frame(cases=data,time=seq(1,14,by=1)),
          times="time",t0=0,
          dmeasure=Csnippet(dmeas),
          rmeasure=Csnippet(rmeas),
          rprocess=euler.sim(
            step.fun=Csnippet(sir.step),delta.t=1/14/2),
          statenames=c("S","I","R"),
          paramnames=c("popsize","bet","mu","rho","S.0","I.0","R.0","theta"),
          initializer=function(params,t0,...){
            fracs<-params[c("S.0","I.0","R.0")]
            return(setNames(c(round(params["popsize"]*fracs/sum(fracs))),c("S","I","R")))
            setNames(c(round(params["popsize"]*fracs/sum(fracs))),c("S","I","R"))
          },
          params=c(popsize=763,theta=100,bet=2,mu=0.5,rho=3,S.0=757,I.0=3,R.0=3))

however, after I changed the parameterization to above, it doesn't work. Where is the problem?

from pomp.

kingaa avatar kingaa commented on July 17, 2024

The error message tells me that initializer is supposed to return a named numeric vector. Does it?

sir <- pomp(data=data.frame(cases=NA,time=seq(1,14,by=1)),
            times="time",t0=0,dmeasure=Csnippet(dmeas),
            rmeasure=Csnippet(rmeas),
            rprocess=euler.sim(
                step.fun=Csnippet(sir.step),delta.t=1/14/2),
            statenames=c("S","I","R"),
            paramnames=c("popsize","bet","mu","rho","theta1","theta2","theta"),
            initializer=function(params,t0,...){
                ps<-exp(params["theta1"])/(1+exp(params["theta1"])+exp(params["theta2"]))
                pi<-1/(1+exp(params["theta1"])+exp(params["theta2"]))
                pr<-exp(params["theta2"])/(1+exp(params["theta1"])+exp(params["theta2"]))
                x <- setNames(rmultinom(1,params["popsize"],prob=c(ps,pi,pr)),
                              c("S","I","R"))
                print(class(x))
                x
            },
            params=c(popsize=763,theta=100,bet=2,mu=0.5,rho=3,theta1=5,theta2=1)
            )

sir<-simulate(sir,seed = 73691676L)

Which gives:

[1] "matrix"
Error: in ‘simulate’: in 'init.state': user 'initializer' must return a named numeric vector

So, it appears that rmultinom returns a matrix, not a vector. See ?rmultinom to confirm this. If I do instead

sir <- pomp(data=data.frame(cases=NA,time=seq(1,14,by=1)),
            times="time",t0=0,
            dmeasure=Csnippet(dmeas),
            rmeasure=Csnippet(rmeas),
            rprocess=euler.sim(
                step.fun=Csnippet(sir.step),delta.t=1/14/2),
            statenames=c("S","I","R"),
            paramnames=c("popsize","bet","mu","rho","theta1","theta2","theta"),
            initializer=function(params,t0,...){
                ps<-exp(params["theta1"])/(1+exp(params["theta1"])+exp(params["theta2"]))
                pi<-1/(1+exp(params["theta1"])+exp(params["theta2"]))
                pr<-exp(params["theta2"])/(1+exp(params["theta1"])+exp(params["theta2"]))
                setNames(as.numeric(
                    rmultinom(1,params["popsize"],prob=c(ps,pi,pr))),
                    c("S","I","R"))
            },
            params=c(popsize=763,theta=100,bet=2,mu=0.5,rho=3,theta1=5,theta2=1)
            )

sir<-simulate(sir,seed = 73691676L)

all is well.

In your second snippet of code above, note that your second setNames(...) call goes unevaluated due to the previous return call. Also note that this returns a named numeric vector, hence the success where the previous codes failed.

Hope this helps.

from pomp.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.