pomp package COVID SEIR model least squares errors traceback - r

I try to model a SEIR for UK to evaluate the implemented containment measures and found some code with the pomp package here: https://kingaa.github.io/clim-dis/parest/parest.html
I tried to transfer this to my case which adds one stage (E) and three more variables. In the end i want to do a least squared estimation to find the optimal beta.
Data_UK_beta0 consists of the variable date (int from 0 to 165) and new_cases (from John Hopkins University dataset).
Data_UK_pomp_beta0 <- pomp(
data= Data_UK_beta0,
times ="date", t0=0,
skeleton = vectorfield(
Csnippet("
DS=-beta1*S*I/N;
DE= beta1*S*I/N-delta1*E;
DI=delta1*E-(gamma1+eta1)*I;
DR=gamma1*I;")),
rinit = Csnippet("
S=S_0;
E=E_0;
I=I_0;
R=N-S_0-E_0-I_0;"),
statenames = c("S","E","I","R"),
paramnames = c("beta1","delta1","gamma1","eta1","N","S_0","E_0","I_0"))
sse_UK_beta0 <- function (params) {
x <- trajectory(Data_UK_pomp_beta0,params=params)
discrep <- x["I",,]-obs(Data_UK_pomp_beta0)
sum(discrep^2)
}
install.packages("apricom")
library(apricom)
beta_reg <- function (beta0) {
params <- c(beta1=beta0, delta1=1/5.1, gamma1=1, eta1=0.012649, N=67886004, S_0=67886004, E_0=5, I_0=2)
sse(params)
}
beta0 <- seq(from=1,to=40,by=1)
SSE <- sapply(beta0, beta_reg)
got the following error traceback (unfortunately in german, but i guess the message should be clear :)
Fehler bei der Auswertung des Argumentes 'x' bei der Methodenauswahl für Funktion 'as.matrix': Argument "dataset" fehlt (ohne Standardwert)
7.
h(simpleError(msg, call))
6.
.handleSimpleError(function (cond)
.Internal(C_tryCatchHelper(addr, 1L, cond)), "Argument \"dataset\" fehlt (ohne Standardwert)",
base::quote(as.matrix(dataset)))
5.
as.matrix(dataset)
4.
sse(params)
3.
FUN(X[[i]], ...)
2.
lapply(X = X, FUN = FUN, ...)
1.
sapply(beta0, beta_reg)
What did i do wrong?

The sse function you've imported from apricom has nothing (as far as I can see) to do with this problem. (This also doesn't have anything to do with C(++) code compilation, so the [compiler-errors] tag in your question is a little misleading.)
You haven't given us a way to get your Data_UK_beta0 data set so I can't reproduce this, but I assume that you actually want something like:
beta_reg <- function (beta0) {
params <- c(beta1=beta0, delta1=1/5.1, gamma1=1,
eta1=0.012649, N=67886004, S_0=67886004, E_0=5, I_0=2)
sse_UK_beta0(params)
}
beta0 <- seq(from=1,to=40,by=1)
SSE <- sapply(beta0, beta_reg)
You should also be aware that are a bunch of other potentially tricky issues in what you're doing. One that jumps out is that you are likely comparing the prevalence of infection in the modeling (the current number in the I compartment) to case report data, which is a (lagged, biased, imperfect) measure of the incidence of disease, i.e. the number of new cases per unit time ...

Related

Error in match.call - unused argument (..3) when running parametric analysis

I'm trying to run a parametric analysis in EnergyPlus (idf file) through R, by using the eplusr and epluspar packages. But when I set my optimization variables (from EnergyPlus objects) I'm getting an error in match.call that I don't know where is it. Could anyone help me please?
My code is:
dir <- eplus_config(9.6)$dir
path_model <- file.path(dir, "RWTH/SchoolA-calibrated_MO-R01.idf")
path_weather <- file.path(dir, "RWTH/BRA_SP_Campinas-Viracopos.Intl.AP.837210_TMYx.2007-2021.epw")
# read model
idf <- read_idf(path_model)
# define a measure to change the window status
set_window_status <- function (idf, wst) {
wst <- as.character(wst)
idf$set(wst_scenario = list(program_name_1 = wst))
idf
}
# define a measure to change occupancy
set_occupancy <- function (idf, occ) {
occ <- as.character(occ)
idf$set(ocupacao_z1 = list(number_of_people = occ))
}
# combine all measures into one
design_options <- function (idf, window_status, occupancy) {
idf <- set_window_status(idf, window_status)
idf <- set_occupancy(idf, occupancy)
idf
}
# specify design space of parameters
ga$apply_measure(design_options,
window_status = choice_space(as.character(seq(1, 12, 1))),
occupancy = choice_space(as.character(seq(6, 31, 5))),
)
The error appears after I run the "specify design space of parameters" part and it is:
Error in match.call(definition, call, expand.dots, envir) :
unused argument (..3)
The following names are provided from the EnergyPlus idf file:
wst_scenario: name of the object
program_name_1: name of the line I want the parametric variation to occur
ocupacao_z1: name of the object
number_of_people: name of the line I want the parametric variation to occur
I tried to change their names (including/ excluding capital letters, including excluding the "_" in between words) but it didn't work.
Here is the path to the idf and weather files: https://drive.google.com/drive/folders/1bX8ZB2aUXMRrEUqlpMMM6K8avs6LyIdK?usp=share_link

ODE waring messages in simple ODE problem

I am working on a complicated model to study the population dynamics. I am getting some warning messages and not sure why? I am not sure does it have any effect on the solution.
I am reproducing the same error in a sample Lotka-Volterra Model. Please consider this as an example, it may not correspond to actual dynamics of the model.
(1) Could you pleases explain, how to eliminate these warnings?
(2) Does it have any effect on the output?
Thanks for reading. Here is the code:
library(deSolve)
predpreyLV<-function(t,y,p){
N<-y[1]
P<-y[2]
with(as.list(p),{
dNdt<- r*N*(1-(N/1000))-a*P*N
dPdt<- -b*P+f*P*N
return(list(c(dNdt,dPdt)))
})
}
rootfun <- function (t,y,parms){
if (t>=200 && y[2]>130)
return (0)
else
return (1)
}
eventfun <- function (t,y,parms){
y[2] = y[2]*0.99
return (y)
}
r<-0.5; a<-0.01; f<-0.01; b<-0.2;
p<-c(r=r,a=a, b=b, f=f)
y0<-c(N=25, P=5)
times<-seq(0,500,0.01)
LV.out<-ode(y=y0,times,predpreyLV, p,method="lsodar",
rootfunc = rootfun, events = list(func=eventfun, time = seq(198,200,0.01)))
I am getting following warnings and need to why it is happening:
*Warning messages:
1: In checkevents(events, times, Ynames, dllname, TRUE) :
Not all event times 'events$time' are in output 'times' so they are automatically included.
2: In checkevents(events, times, Ynames, dllname, TRUE) :
Some time steps were very close to events - only the event times are used in these cases.*
One method is to round both time vectors to the required precision:
times <- round(seq(0,500,0.01), 2)
evtime <- round(seq(198,200,0.01), 2)
evtime %in% times ## check if all events are in 'times'
LV.out<-ode(y=y0,times,predpreyLV, p,method="lsodar",
rootfunc = rootfun, events = list(func=eventfun, time = evtime))
plot(LV.out)
Hope it helps!

'corMatch' function in package monitoR results in error message [pkg-monitoR]

I've worked through the great tutorial 'A short introduction to acoustic template matching with monitoR'
I'm now attempting to detect calls using spectogram cross correlation within a 30 second sample field recording. The function corMatch is returning the following error...
'Error in !all.equal(template#t.step, t.step, tolerance = t.step/10000) :
invalid argument type'
What have I done wrong?
I've used the following code:
survey <- readWave('20180901_160000.wav', from = 64, to = 64.5, units='minutes')
mtemp1 <- readWave('mew.wav')
mtemp2 <- readWave('mew2.wav')
mtemp1.fp <- file.path(tempdir(), "mtemp1.wav")
writeWave(mtemp1, mtemp1.fp)
mtemp2.fp <- file.path(tempdir(), "mtemp2.wav")
writeWave(mtemp2, mtemp2.fp)
survey.fp <- file.path(tempdir(), "survey2018-09-01_160400_ACDT.wav")
writeWave(survey, survey.fp)
mt1 <- makeCorTemplate(mtemp1.fp, frq.lim=c(6,9), name='m1')
mt2 <- makeCorTemplate(mtemp2.fp, frq.lim=c(5.5,8.5), name='m2')
MewTemps <- combineCorTemplates(mt1, mt2)
MewTempScores <- corMatch(survey.fp, MewTemps)
As per https://github.com/jonkatz2/monitoR/issues/2 - the sampling rate of the survey wave file doesn't match the sampling rate of the template.
You can use either seewave::resamp or monitoR::changeSampRate to resample one to make them match

error message lsmeans for beta mixed regression model with glmmTMB

I am analyzing the ratio of (biomass of one part of a plant community) vs. (total plant community biomass) across different treatments in time (i.e. repeated measures) in R. Hence, it seems natural to use beta regression with a mixed component (available with the glmmTMB package) in order to account for repeated measures.
My problem is about computing post hoc comparisons across my treatments with the function lsmeans from the lsmeans package. glmmTMB objects are not handled by the lsmeans function so Ben Bolker on recommended to add the following code before loading the packages {glmmTMB} and {lsmeans}:
recover.data.glmmTMB <- function(object, ...) {
fcall <- getCall(object)
recover.data(fcall,delete.response(terms(object)),
attr(model.frame(object),"na.action"), ...)}
lsm.basis.glmmTMB <- function (object, trms, xlev, grid, vcov.,
mode = "asymptotic", component="cond", ...) {
if (mode != "asymptotic") stop("only asymptotic mode is available")
if (component != "cond") stop("only tested for conditional component")
if (missing(vcov.))
V <- as.matrix(vcov(object)[[component]])
else V <- as.matrix(.my.vcov(object, vcov.))
dfargs = misc = list()
if (mode == "asymptotic") {
dffun = function(k, dfargs) NA
}
## use this? misc = .std.link.labels(family(object), misc)
contrasts = attr(model.matrix(object), "contrasts")
m = model.frame(trms, grid, na.action = na.pass, xlev = xlev)
X = model.matrix(trms, m, contrasts.arg = contrasts)
bhat = fixef(object)[[component]]
if (length(bhat) < ncol(X)) {
kept = match(names(bhat), dimnames(X)[[2]])
bhat = NA * X[1, ]
bhat[kept] = fixef(object)[[component]]
modmat = model.matrix(trms, model.frame(object), contrasts.arg = contrasts)
nbasis = estimability::nonest.basis(modmat)
}
else nbasis = estimability::all.estble
list(X = X, bhat = bhat, nbasis = nbasis, V = V, dffun = dffun,
dfargs = dfargs, misc = misc)
}
Here is my code and data:
trt=c(rep("T5",13),rep("T4",13),
rep("T3",13),rep("T1",13),rep("T2",13),rep("T1",13),
rep("T2",13),rep("T3",13),rep("T5",13),rep("T4",13))
year=rep(2005:2017,10)
plot=rep(LETTERS[1:10],each=13)
ratio=c(0.0046856237844411,0.00100861922394448,0.032516291436091,0.0136507743972955,0.0940240065096705,0.0141337428305094,0.00746709315018945,0.437009092691189,0.0708021091805216,0.0327952505849285,0.0192685194751524,0.0914696394299481,0.00281889216102303,0.0111928453399615,0.00188119596836005,NA,0.000874623692966351,0.0181192859074754,0.0176635391424644,0.00922358069727823,0.0525280029990213,0.0975006760149882,0.124726170684951,0.0187132600944396,0.00672592365451266,0.106399234215126,0.0401776844073239,0.00015382736648373,0.000293356424756535,0.000923659501144292,0.000897412901472504,0.00315930225856196,0.0636501228611642,0.0129422445492391,0.0143526630252398,0.0136775931834926,0.00159292971508751,0.0000322313783211749,0.00125352390811532,0.0000288862579879126,0.00590690336494395,0.000417043974238875,0.0000695808216192379,0.001301299696752,0.000209355138230326,0.000153151660178623,0.0000646279598274632,0.000596704590065324,9.52943306579156E-06,0.000113476446629278,0.00825405312309618,0.0001025984082064,0.000887617767039489,0.00273668802742924,0.00469409165130462,0.00312377000134233,0.0015579322817235,0.0582615988387306,0.00146933878743163,0.0405139497779372,0.259097955479886,0.00783997376383192,0.110638003652979,0.00454029511918275,0.00728290246595241,0.00104674197030363,0.00550563937846687,0.000121380392484705,0.000831904606687671,0.00475778829159394,0.000402799910756391,0.00259524300745195,0.000210249875492504,0.00550104485802363,0.000272849546913495,0.0025389089622392,0.00129370075116459,0.00132810234020792,0.00523285954007915,0.00506230599388357,0.00774104695265855,0.00098348404576587,0.174079173227248,0.0153486840317039,0.351820365452281,0.00347674458928481,0.147309225196026,0.0418825705903947,0.00591271021100856,0.0207139520537443,0.0563647804012055,0.000560012457272534,0.00191564842393647,0.01493480083524,0.00353400674061077,0.00771828473058641,0.000202009136938048,0.112695841130448,0.00761492172670762,0.038797330459115,0.217367765362878,0.0680958660605668,0.0100870294641921,0.00493875324236991,0.00136539944656238,0.00264262100866192,0.0847732305020654,0.00460985241335143,0.235802638543116,0.16336020383325,0.225776236687456,0.0204568107372349,0.0455390585228863,0.130969863489582,0.00679523322812889,0.0172325334280024,0.00299970176999806,0.00179347656925317,0.00721658257996989,0.00822443690003783,0.00913096724026346,0.0105920192618379,0.0158013204589482,0.00388803567197835,0.00366268607026078,0.0545418725650633,0.00761485067129418,0.00867583194858734,0.0188232707241144,0.018652666214789)
dat=data.frame(trt,year,plot,ratio)
require(glmmTMB)
require(lsmeans)
mod=glmmTMB(ratio~trt*scale(year)+(1|plot),family=list(family="beta",link="logit"),data=dat)
summary(mod)
ls=lsmeans(mod,pairwise~trt)`
Finally, I get the following error message that I've never encountered and on which I could find no information:
In model.matrix.default(trms, m, contrasts.arg = contrasts) :
variable 'plot' is absent, its contrast will be ignored
Could anyone shine their light? Thanks!
This is not an error message, it's a (harmless) warning message. It occurs because the hacked-up method I wrote doesn't exclude factor variables that are only used in the random effects. You should worry more about this output:
NOTE: Results may be misleading due to involvement in interactions
which is warning you that you are evaluating main effects in a model that contains interactions; you have to think about this carefully to make sure you're doing it right.

How to estimate static yield curve with 'termstrc' package in R?

I am trying to estimate the static yield curve for Brazil using termstrc package in R. I am using the function estim_nss.couponbonds and putting 0% coupon-rates and $0 cash-flows, except for the last one which is $1000 (the face-value at maturity) -- as far as I know this is the function to do this, because the estim_nss.zeroyields only calculates the dynamic curve. The problem is that I receive the following error message:
"Error in (pos_cf[i] + 1):pos_cf[i + 1] : NA/NaN argument In addition: Warning message: In max(n_of_cf) : no non-missing arguments to max; returning -Inf "
I've tried to trace the problem using trace(estim_nss.couponbons, edit=T) but I cannot find where pos_cf[i]+1 is calculated. Based on the name I figured it could come from the postpro_bondfunction and used trace(postpro_bond, edit=T), but I couldn't find the calculation again. I believe "cf" comes from cashflow, so there could be some problem in the calculation of the cashflows somehow. I used create_cashflows_matrix to test this theory, but it works well, so I am not sure the problem is in the cashflows.
The code is:
#Creating the 'couponbond' class
ISIN <- as.character(c('ltn_2017','ltn_2018', 'ltn_2019', 'ltn_2021','ltn_2023')) #Bond's identification
MATURITYDATE <- as.Date(c(42736, 43101, 43466, 44197, 44927), origin='1899-12-30') #Dates are in system's format
ISSUEDATE <- as.Date(c(41288,41666,42395, 42073, 42395), origin='1899-12-30') #Dates are in system's format
COUPONRATE <- rep(0,5) #Coupon rates are 0 because these are zero-coupon bonds
PRICE <- c(969.32, 867.77, 782.48, 628.43, 501.95) #Prices seen 'TODAY'
ACCRUED <- rep(0.1,5) #There is no accrued interest in the brazilian bond's market
#Creating the cashflows sublist
CFISIN <- as.character(c('ltn_2017','ltn_2018', 'ltn_2019', 'ltn_2021', 'ltn_2023')) #Bond's identification
CF <- c(1000,1000,1000,1000,1000)# The face-values
DATE <- as.Date(c(42736, 43101, 43466, 44197, 44927), origin='1899-12-30') #Dates are in system's format
CASHFLOWS <- list(CFISIN,CF,DATE)
names(CASHFLOWS) <- c("ISIN","CF","DATE")
TODAY <- as.Date(42646, origin='1899-12-30')
brasil <- list(ISIN,MATURITYDATE,ISSUEDATE,
COUPONRATE,PRICE,ACCRUED,CASHFLOWS,TODAY)
names(brasil) <- c("ISIN","MATURITYDATE","ISSUEDATE","COUPONRATE",
"PRICE","ACCRUED","CASHFLOWS","TODAY")
mybonds <- list(brasil)
class(mybonds) <- "couponbonds"
#Estimating the zero-yield curve
ns_res <-estim_nss.couponbonds(mybonds, 'brasil' ,method = "ns")
#Testing the hypothesis that the error comes from the cashflow matrix
cf_p <- create_cashflows_matrix(mybonds[[1]], include_price = T)
m_p <- create_maturities_matrix(mybonds[[1]], include_price = T)
b <- bond_yields(cf_p,m_p)
Note that I am aware of this question which reports the same problem. However, it is for the dynamic curve. Besides that, there is no useful answer.
Your code has two problems. (1) doesn't name the 1st list (this is the direct reason of the error. But if modifiy it, another error happens). (2) In the cashflows sublist, at least one level of ISIN needs more than 1 data.
# ...
CFISIN <- as.character(c('ltn_2017','ltn_2018', 'ltn_2019',
'ltn_2021', 'ltn_2023', 'ltn_2023')) # added a 6th element
CF <- c(1000,1000,1000,1000,1000, 1000) # added a 6th
DATE <- as.Date(c(42736,43101,43466,44197,44927, 44928), origin='1899-12-30') # added a 6th
CASHFLOWS <- list(CFISIN,CF,DATE)
names(CASHFLOWS) <- c("ISIN","CF","DATE")
TODAY <- as.Date(42646, origin='1899-12-30')
brasil <- list(ISIN,MATURITYDATE,ISSUEDATE,
COUPONRATE,PRICE,ACCRUED,CASHFLOWS,TODAY)
names(brasil) <- c("ISIN","MATURITYDATE","ISSUEDATE","COUPONRATE",
"PRICE","ACCRUED","CASHFLOWS","TODAY")
mybonds <- list(brasil = brasil) # named the list
class(mybonds) <- "couponbonds"
ns_res <-estim_nss.couponbonds(mybonds, 'brasil', method = "ns")
Note: the error came from these lines
bonddata <- bonddata[group] # prepro_bond()'s 1st line (the direct reason).
# cf <- lapply(bonddata, create_cashflows_matrix) # the additional error
create_cashflows_matrix(mybonds[[1]], include_price = F) # don't run

Resources