### Load C code

useDynLib(surveillance)

###############
### IMPORTS ###
###############

### Import all packages listed as Depends
### (for utils and polyCub: only selected methods are imported)

import(methods, grDevices, graphics, stats)
import(Rcpp)
## although Rcpp is only used on C-level we need to "ensure that Rcpp is loaded
## so any dynamic linking to its code can be resolved.  (There may be none, but
## there could be, now or in future.)" (B. Ripley, 2013-09-08)

## sp classes & utilities (bbox, coordinates, dimensions, overlay, plot, ...)
## (we "Depend" on package sp since it defines essential data classes & methods)
import(sp)

## we define own methods for generating xtable()'s, which we want to be useable
import(xtable) 


### required generics for own methods (that's why we "Depend" on these packages)

## importFrom(stats, coef, vcov, logLik, nobs, residuals, confint, AIC, extractAIC,
##                   profile, simulate, update, terms, add1, drop1, predict, as.stepfun)
importFrom(utils, head, tail, toLatex)


### required functions from utils and stats

## importFrom(stats, pnorm, cov2cor, ks.test, formula, rnorm, runif, step, dist,
##                   update.formula, terms.formula, rpois, rnbinom, setNames,
##                   na.omit, as.formula, pnbinom, qnbinom, qnorm, sd, glm, optim,
##                   poisson, ppois, qpois, predict.glm, summary.glm, quasipoisson,
##                   glm.fit) ## and many more...
importFrom(utils, packageVersion, modifyList, capture.output, read.table,
                  setTxtProgressBar, txtProgressBar, sessionInfo, head.matrix,
                  str, flush.console, write.table)


### sampling from mv.Gausian for OSAIC weights (twinSIR) and iafplot (twinstim)

importFrom(MASS, mvrnorm)


### disProg-specific

importFrom(MASS, glm.nb)   # for algo.glrnb
##importFrom(msm, msm, hmmPois, viterbi.msm)   # for algo.hmm()
##importFrom(spc, xcusum.arl, xcusum.crit)     # for find.kh()
## (packages msm and spc are now "suggested", not imported)


### hhh4-specific

importFrom(MASS, ginv, negative.binomial)
importFrom(Matrix, Matrix)
importClassesFrom(Matrix, ddiMatrix)
importMethodsFrom(Matrix, coerce, diag, forceSymmetric, solve)
## sparse matrix methods provide a significant speed-up in marFisher


### twinSIR-specific

# for use in computing OSAIC weights by simulation
#importFrom(quadprog, solve.QP)   # moved to "Suggests"


### twinstim-specific

importFrom(spatstat, area.owin, as.im.function, coords.ppp, diameter,
                     diameter.owin, disc, inside.owin, intersect.owin,
                     is.polygonal, runifpoint, shift.owin,
                     spatstat.options, vertices)
importFrom(spatstat, marks)
export(marks)                           # we define an epidataCS-method
importFrom(spatstat, multiplicity)
export(multiplicity)                    # we define a Spatial-method

## Note: we depend on instead of import package polyCub to avoid "::" references
##       in some of the siaf.* generators which create functions in .GlobalEnv
importFrom(polyCub, polyCub, .polyCub.iso, polyCub.SV, polyCub.midpoint, xylist)
importMethodsFrom(polyCub, coerce)

importFrom(MASS, kde2d, truehist)



###############
### EXPORTS ###
###############


### general exports

export(surveillance.options, reset.surveillance.options)
export(animate)          # new S3-generic
export(R0)               # new S3-generic
export(intensityplot)    # new S3-generic
export(fixef)            # new S3-generic (same meaning as in package nlme)
export(ranef)            # new S3-generic (same meaning as in package nlme)
export(formatPval)       # yapf -- yet another p-value formatter
export(anscombe.residuals)
export(magic.dim, primeFactors, bestCombination)   # similar to n2mfrow
export(isoWeekYear) #extract ISO 8601 date
export(formatDate)  #ISO 8601 compatible %G and %V format( ) function. 
export(refvalIdxByDate)
export(ks.plot.unif)
export(checkResidualProcess)       # for twinstim and twinSIR
export(qlomax)                     # quantile function of the Lomax distribution

# spatial utilities
export(discpoly)
#export(runifdisc)           # CAVE: spatstat has similar function of same name
export(unionSpatialPolygons)
export(inside.gpc.poly)
S3method(scale, gpc.poly)   # redefined method for gpc.poly in spatial_stuff.R
S3method(diameter, gpc.poly)
export(nbOrder)
export(poly2adjmat)
export(polyAtBorder)
export(layout.labels)

# randomly break tied event times or coordinates
export(untie)                           # new S3-generic
#export(untie.default, untie.matrix, untie.epidataCS)
S3method(untie, default)
S3method(untie, matrix)
S3method(untie, epidataCS)

# intersection of a polygonal and a circular domain
export(intersectPolyCircle)
S3method(intersectPolyCircle, owin)
S3method(intersectPolyCircle, SpatialPolygons)
S3method(intersectPolyCircle, gpc.poly)

# little helper: multiplicity of points
S3method(multiplicity, Spatial)

# Spatio-temporal cluster detection
export(stcd)


### sts(BP)-specific

exportClasses(sts, stsBP)
export(linelist2sts)

# generics for sts class defined in sts.R
exportMethods("[", plot)
exportMethods(toLatex)
exportMethods(nrow, ncol, dim, colnames, epochInYear, year)
exportMethods(aggregate)
exportMethods(as.data.frame)

# methods for accessing/replacing slots of an sts object (cf. AllGeneric.R)
exportMethods(epoch,observed,alarms,upperbound,population,control,multinomialTS,neighbourhood)
exportMethods("epoch<-","observed<-","alarms<-","upperbound<-","population<-","control<-","multinomialTS<-","neighbourhood<-")
# methods for accessing/replacing slots of an stsNC object
exportMethods(reportingTriangle,delayCDF,score)

# plot variants
export(stsplot_space)
export(stsplot_time, stsplot_time1, stsplot_alarm)
export(atChange, at2ndChange, atMedian) #for time axis formatting
export(stsplot_spacetime) # old implementation of (animated) map
S3method(animate, sts)    # S3-method for an S4 class, see ?Methods

# outbreak detection algorithms (sts-interfaces)
export(wrap.algo, farrington, bayes, rki, cusum, glrpois, glrnb, outbreakP, boda) # FIXME: rogerson, hmm ??
export(earsC)
export(farringtonFlexible)
export(categoricalCUSUM, pairedbinCUSUM, pairedbinCUSUM.runlength)
export(nowcast, backprojNP)
## auxiliary functions for farringtonFlexible:
## export(algo.farrington.data.glm, algo.farrington.fitGLM.flexible,
##        algo.farrington.glm, algo.farrington.referencetimepoints,
##        algo.farrington.threshold.farrington, algo.farrington.threshold.noufaily)


### disProg-specific

export(create.disProg, readData, toFileDisProg)
S3method(print, disProg)
S3method(plot, disProg)
S3method(plot, disProg.one)
S3method(aggregate, disProg)

export(sim.pointSource, sim.seasonalNoise)
export(LRCUSUM.runlength, arlCusum, find.kh, findH, hValues, findK)
export(compMatrix.writeTable, correct53to52, enlargeData)
export(makePlot)
export(estimateGLRNbHook)
export(algo.compare, algo.quality, algo.summary)

## outbreak detection algorithms (old disProg implementations)
export(algo.bayes, algo.bayes1, algo.bayes2, algo.bayes3,
       algo.bayesLatestTimepoint,
       algo.call,
       algo.cdc, algo.cdcLatestTimepoint,
       algo.cusum,
       algo.farrington,
       algo.glrnb, algo.glrpois,
       algo.hhh, algo.hhh.grid,
       algo.hmm,
       algo.outbreakP,
       algo.rki, algo.rki1, algo.rki2, algo.rki3, algo.rkiLatestTimepoint,
       algo.rogerson,
       algo.twins)

## auxiliary functions for algo.farrington (FIXME: why export these internals?)
export(algo.farrington.assign.weights,
       algo.farrington.fitGLM, algo.farrington.fitGLM.fast,
       algo.farrington.fitGLM.populationOffset, algo.farrington.threshold)


S3method(plot, atwins)
S3method(plot, survRes)
S3method(plot, survRes.one)
S3method(print, algoQV)
S3method(xtable, algoQV)

export(test, testSim)   # FIXME: remove these test functions? -> Demo?


### conversion between old disProg and new sts classes

export(disProg2sts)
export(sts2disProg)


### twinSIR-specific

export(cox)
export(as.epidata)
S3method(as.epidata, data.frame)
export(as.epidata.data.frame)
S3method(as.epidata, default)
export(as.epidata.default)
export(intersperse)
export(twinSIR)
export(stateplot)
export(simEpidata)

S3method(update, epidata)
S3method("[", epidata)
S3method(print, epidata)
S3method(summary, epidata)
S3method(print, summary.epidata)
S3method(plot, epidata)
S3method(animate, epidata)
S3method(plot, summary.epidata)
S3method(animate, summary.epidata)

S3method(print, twinSIR)
S3method(summary, twinSIR)
S3method(print, summary.twinSIR)
S3method(plot, twinSIR)
S3method(intensityplot, twinSIR)
export(intensityplot.twinSIR)           # for convenience
S3method(profile, twinSIR)
S3method(plot, profile.twinSIR)
S3method(vcov, twinSIR)
S3method(logLik, twinSIR)
S3method(AIC, twinSIR)
S3method(extractAIC, twinSIR)
S3method(simulate, twinSIR)
export(simulate.twinSIR)                # for convenience
S3method(residuals, twinSIR)

S3method(intensityplot, simEpidata)
export(intensityplot.simEpidata)        # for convenience


### twinstim-specific

export(as.epidataCS)
export(glm_epidataCS)
export(twinstim)
export(simEpidataCS)
export(siaf,
       siaf.constant, siaf.step,
       siaf.gaussian,
       siaf.powerlaw, siaf.powerlawL, siaf.student)
export(tiaf,
       tiaf.constant, tiaf.step,
       tiaf.exponential)
export(epidataCS2sts)

S3method(nobs, epidataCS)
S3method("[", epidataCS)
S3method(update, epidataCS)
export(update.epidataCS)                # for convenience
S3method(head, epidataCS)
S3method(tail, epidataCS)
S3method(print, epidataCS)
S3method(subset, epidataCS)
S3method(summary, epidataCS)
S3method(print, summary.epidataCS)
S3method(as.stepfun, epidataCS)
S3method(animate, epidataCS)
export(animate.epidataCS)               # for convenience
S3method(marks, epidataCS)
export(marks.epidataCS)            # for convenience since its a foreign generic
S3method(plot, epidataCS)
export(epidataCSplot_time, epidataCSplot_space)
S3method(as.epidata, epidataCS)
export(as.epidata.epidataCS)            # for convenience

S3method(print, twinstim)
S3method(summary, twinstim)
export(summary.twinstim)                # for convenience
S3method(print, summary.twinstim)
S3method(toLatex, summary.twinstim)
S3method(xtable, summary.twinstim)
export(xtable.summary.twinstim)         # for xtable.twinstim
S3method(xtable, twinstim)
S3method(plot, twinstim)
export(iafplot)
export(intensity.twinstim)
S3method(intensityplot, twinstim)
export(intensityplot.twinstim)          # for convenience
S3method(profile, twinstim)
S3method(coef, summary.twinstim)
S3method(vcov, twinstim)
S3method(vcov, summary.twinstim)
S3method(logLik, twinstim)
S3method(extractAIC, twinstim)
S3method(nobs, twinstim)
S3method(simulate, twinstim)
export(simulate.twinstim)               # for convenience
S3method(R0, twinstim)
S3method(residuals, twinstim)
S3method(update, twinstim)
export(update.twinstim)                 # for convenience
S3method(terms, twinstim)

export(stepComponent)
S3method(terms, twinstim_stependemic)
S3method(terms, twinstim_stepepidemic)
S3method(update, twinstim_stependemic)
S3method(update, twinstim_stepepidemic)
S3method(add1, twinstim)
S3method(add1, twinstim_stependemic)
S3method(add1, twinstim_stepepidemic)
S3method(drop1, twinstim)
S3method(drop1, twinstim_stependemic)
S3method(drop1, twinstim_stepepidemic)

S3method(residuals, simEpidataCS)
S3method(R0, simEpidataCS)
S3method(intensityplot, simEpidataCS)
export(intensityplot.simEpidataCS)      # for convenience
S3method(print, simEpidataCSlist)
S3method("[[", simEpidataCSlist)
S3method(plot, simEpidataCSlist)


### algo.hhh-specific

export(algo.hhh)
export(algo.hhh.grid)
export(create.grid)

S3method(print, ah)
S3method(coef, ah)
S3method(predict, ah)
S3method(residuals, ah)
S3method(logLik, ah)

S3method(print, ahg)
S3method(coef, ahg)
S3method(predict, ahg)
S3method(residuals, ahg)
S3method(logLik, ahg)

export(simHHH, simHHH.default)
S3method(simHHH, default)
S3method(simHHH, ah)


### hhh4-specific

## main functions
export(hhh4) 
export(addSeason2formula)
export(zetaweights, W_powerlaw)
export(W_np)
export(oneStepAhead, scores)
export(permutationTest)
export(pit)

## S3-methods
S3method(print, hhh4)
S3method(summary, hhh4)
S3method(print, summary.hhh4)
S3method(nobs, hhh4)
S3method(logLik, hhh4)
S3method(formula, hhh4)
S3method(terms, hhh4)
S3method(coef, hhh4)
S3method(vcov, hhh4)
S3method(fixef, hhh4)
S3method(ranef, hhh4)
S3method(confint, hhh4)
S3method(residuals, hhh4)
S3method(predict, hhh4)
S3method(update, hhh4)
S3method(simulate, hhh4)
S3method(plot, hhh4)
export(plotHHH4_fitted, plotHHH4_fitted1,
       plotHHH4_season, getMaxEV_season,
       plotHHH4_maxEV, getMaxEV,
       plotHHH4_ri,
       plotHHH4_neweights)

## backwards-compatibility for old class name "ah4"
S3method(print, ah4)
S3method(summary, ah4)
S3method(print, summary.ah4)
S3method(logLik, ah4)
S3method(terms, ah4)
S3method(coef, ah4)
S3method(fixef, ah4)
S3method(ranef, ah4)
S3method(confint, ah4)
S3method(predict, ah4)
S3method(update, ah4)
S3method(simulate, ah4)
S3method(plot, ah4)
