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

CRAN ready #1

Merged
merged 7 commits into from
Dec 23, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .Rbuildignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
^.*\.Rproj$
^\.Rproj\.user$
^\.travis\.yml$
17 changes: 17 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# R for travis: see documentation at https://docs.travis-ci.com/user/languages/r

language: R
r:
- oldrel
- release
- devel
sudo: false
cache: packages

notifications:
email:
recipients:
- fabian.scheipl@stat.uni-muenchen.de
- antonio.gasparrini@lshtm.ac.uk
on_success: change
on_failure: always
29 changes: 23 additions & 6 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,16 +1,33 @@
Package: dlnm
Type: Package
Title: Distributed Lag Non-Linear Models
Version: 2.3.0
Version: 2.3.1
Date: 2016-10-04
Authors@R: c(
person("Antonio","Gasparrini",role=c("aut","cre"),email="antonio.gasparrini@lshtm.ac.uk"),
person("Ben","Armstrong",role="aut",email="ben.armstrong@lshtm.ac.uk"),
person("Fabian","Scheipl",role="ctb",email="fabian.scheipl@stat.uni-muenchen.de"))
Imports: stats, graphics, grDevices, utils, splines, nlme, mgcv, tsModel
Depends: R (>= 3.2)
Suggests: survival, lme4, gee, geepack, mvmeta
Description: Collection of functions for distributed lag linear and non-linear models.
URL: https://github.com/gasparrini/dlnm, http://www.ag-myresearch.com/package-dlnm
Imports:
stats,
graphics,
grDevices,
utils,
splines,
nlme,
lme4,
mgcv,
tsModel
Depends:
R (>= 3.2)
Suggests:
survival,
gee,
geepack,
mvmeta
Description: Collection of functions for distributed lag linear and non-linear
models.
URL: https://github.com/gasparrini/dlnm, http://www.ag-myresearch.com/package-
dlnm
License: GPL (>=2)
LazyData: yes
RoxygenNote: 5.0.1
119 changes: 51 additions & 68 deletions NAMESPACE
Original file line number Diff line number Diff line change
@@ -1,74 +1,57 @@
#################
# EXPORTED
#################
#
export(onebasis,crossbasis,crosspred,crossreduce)
export(plot.crosspred,lines.crosspred,points.crosspred)
export(ps,cr)
# Generated by roxygen2: do not edit by hand

S3method(Predict.matrix,cb.smooth)
S3method(coef,crosspred)
S3method(coef,crossreduce)
S3method(lines,crosspred)
S3method(lines,crossreduce)
S3method(plot,crosspred)
S3method(points,crosspred)
S3method(points,crossreduce)
S3method(smooth.construct,cb.smooth.spec)
S3method(summary,crossbasis)
S3method(summary,crosspred)
S3method(summary,crossreduce)
S3method(summary,onebasis)
S3method(vcov,crosspred)
S3method(vcov,crossreduce)
export(cbPen)
export(smooth.construct.cb.smooth.spec,Predict.matrix.cb.smooth)
export(plot.crossreduce,lines.crossreduce,points.crossreduce)
export(summary.onebasis,summary.crossbasis,summary.crosspred,summary.crossreduce)
export(coef.crosspred,vcov.crosspred,coef.crossreduce,vcov.crossreduce)
export(logknots,equalknots)
export(cr)
export(crossbasis)
export(crosspred)
export(crossreduce)
export(equalknots)
export(exphist)
#
#
#################
# NOT EXPORTED
#################
#
# poly,thr,strata,lin, integer
#
#################
# INTERNAL
#################
#
# DOCUMENTED
# checkcrossbasis,checkgroup,checkonebasis,fci,getcoef,getlink,getvcov,mkat,
# mkcen,mklag,mkXpred,seqlag,findrank,mkaddSlag
#
# NOT DOCUMENTED:
#.onAttach
#
#################
# IMPORTED
#################
#
import(stats,graphics,grDevices)
importFrom(utils,packageDescription)
importFrom(utils,modifyList)
importFrom(splines,ns)
importFrom(splines,bs)
importFrom(splines,spline.des)
importFrom(nlme,fixef)
importFrom(tsModel,Lag)
export(exphistint)
export(logknots)
export(onebasis)
export(poly)
export(ps)
importFrom(grDevices,colorRampPalette)
importFrom(grDevices,grey)
importFrom(graphics,abline)
importFrom(graphics,filled.contour)
importFrom(graphics,layout)
importFrom(graphics,lines)
importFrom(graphics,mtext)
importFrom(graphics,par)
importFrom(graphics,polygon)
importFrom(graphics,segments)
importFrom(lme4,fixef)
importFrom(mgcv,Predict.matrix)
importFrom(mgcv,PredictMat)
importFrom(mgcv,s)
importFrom(mgcv,smooth.construct.cr.smooth.spec)
importFrom(mgcv,smooth.construct)
importFrom(mgcv,smooth.construct.cr.smooth.spec)
importFrom(mgcv,smoothCon)
importFrom(mgcv,tensor.prod.model.matrix)
importFrom(mgcv,Predict.matrix)
importFrom(mgcv,PredictMat)
#
#################
# METHODS
#################
#
S3method(summary, onebasis)
S3method(summary, crossbasis)
S3method(summary, crosspred)
S3method(summary, crossreduce)
#
S3method(plot, crosspred)
S3method(lines, crosspred)
S3method(points, crosspred)
S3method(plot, crossreduce)
S3method(lines, crossreduce)
S3method(points, crossreduce)
S3method(plot, crossreduce)
#
S3method(coef, crosspred)
S3method(vcov, crosspred)
S3method(coef, crossreduce)
S3method(vcov, crossreduce)
importFrom(nlme,fixef)
importFrom(splines,ns)
importFrom(splines,spline.des)
importFrom(stats,coef)
importFrom(stats,median)
importFrom(stats,qnorm)
importFrom(stats,quantile)
importFrom(tsModel,Lag)
importFrom(utils,modifyList)
importFrom(utils,packageDescription)
24 changes: 9 additions & 15 deletions R/Predict.matrix.cb.smooth.R
Original file line number Diff line number Diff line change
@@ -1,29 +1,23 @@
###
### R routines for the R package dlnm (c) Antonio Gasparrini and Fabian Scheipl 2016
#
#' @importFrom mgcv PredictMat Predict.matrix tensor.prod.model.matrix
#' @export
Predict.matrix.cb.smooth <- function(object, data) {
#
################################################################################
#
# TERMS AND DIMENSIONS
term <- object$term
dim <- length(term)
#
# BUILD MARGINAL BASES
Xm <- list()
for (i in seq(dim)) {
for (i in seq(dim)) {
margin <- object$margin[[i]]
if(!"onebasis"%in%class(margin)) {
Xm[[i]] <- if(object$mc[i]) PredictMat(margin,data,n=length(data[[1]])) else
Predict.matrix(margin,data)
if (!"onebasis" %in% class(margin)) {
Xm[[i]] <- if (object$mc[i])
PredictMat(margin, data, n = length(data[[1]])) else Predict.matrix(margin, data)
} else {
Xm[[i]] <- do.call("onebasis",c(list(x=data[[term[i]]]),margin))
Xm[[i]] <- do.call("onebasis", c(list(x = data[[term[i]]]), margin))
}
}
#
# NB: NO REPARAMETERIZATION THROUGH XP
# TENSOR (USING mgcv FUNCTION)
# NB: NO REPARAMETERIZATION THROUGH XP TENSOR (USING mgcv FUNCTION)
X <- tensor.prod.model.matrix(Xm)
#
#
return(X)
}
121 changes: 84 additions & 37 deletions R/cbPen.R
Original file line number Diff line number Diff line change
@@ -1,53 +1,100 @@
###
### R routines for the R package dlnm (c) Antonio Gasparrini 2015-2016
#
cbPen <- function(cb, sp=-1, addSlag=NULL) {
#
################################################################################
#
if(all(class(cb)!="crossbasis")&all(class(cb)!="onebasis"))


#' Generate Penalty Matrices for a DLNM
#'
#' This function generates penalty matrices for the two dimensions of predictor
#' and lags, given the functions selected to model the relationship in each
#' space. It can also be used for generating the single penalty matrix for the
#' predictor space of a uni-dimensional basis.
#'
#' This function is used to perform penalized regression models using the
#' \emph{external} method. This involves generating the transformation using
#' \code{\link{onebasis}} or \code{\link{crossbasis}} with functions for
#' penalized splines (either \code{\link{ps}} or \code{\link{cr}}). The
#' function \code{cbPen} is then called to generate a list of the related
#' penalty matrices. The model is performed by penalizing so-called parametric
#' terms in the \code{\link[mgcv]{gam}} function of \pkg{mgcv}, by including
#' the basis or cross-basis matrix in the regression formula and the list of
#' penalty matrices in its \code{paraPen} argument.
#'
#' When \code{cb} is a cross-basis object, the penalty matrices for the two
#' spaces of predictor and lags are expanded accordingly to its tensor
#' product-type structure. A penalty matrix is not defined when using a
#' function different than \code{\link{ps}} or \code{\link{cr}}, thus keeping
#' one of the two dimensions unpenalized.
#'
#' Additional penalties on the lag dimension can be added through the argument
#' \code{addSlag}, either as a single matrix or a list of matrices. If provided
#' as a vector, this is taken as the diagonal of the penalty matrix and
#' expanded accordingly. These objects must have appropriate dimensions in
#' accordance with the basis matrix for the lag space.
#'
#' All the penalty matrices are also appropriately rescaled to improve the
#' estimation process.
#'
#' The vector \code{sp} must have the same length as the number of penalties,
#' including additional penalties on the lags, and it is replicated accordingly
#' if of length 1. Positive or zero elements are taken as fixed smoothing
#' parameters. Negative elements signal that these parameters need to be
#' estimated.
#'
#' @param cb an object of class \code{'onebasis'} or \code{'crossbasis'}.
#' @param sp supplied smoothing parameters. See Details below.
#' @param addSlag matrix or vector (or list of matrices and/or vectors)
#' defining additional penalties on the lag structure. See Details below.
#' @return A list including penalty matrices plus two vectors \code{rank} and
#' \code{sp} defining their rank and the smoothing parameters. This list is
#' consistent with the argument \code{paraPen} in the regression function
#' \code{\link[mgcv]{gam}} function of \pkg{mgcv}.
#' @author Antonio Gasparrini <\email{antonio.gasparrini@@lshtm.ac.uk}>
#' @seealso \code{\link{ps}} and \code{\link{cr}} for penalized spline
#' functions. The \code{\link[=smooth.construct.cb.smooth.spec]{smooth
#' constructor}} for cross-basis spline smooths.
#'
#' See \code{\link{dlnm-package}} for an introduction to the package and for
#' links to package vignettes providing more detailed information.
#' @keywords utilities
#' @export
cbPen <- function(cb, sp = -1, addSlag = NULL) {
#
if (all(class(cb) != "crossbasis") & all(class(cb) != "onebasis"))
stop("first argument must be object of class 'crossbasis' or 'onebasis")
#
# ATTRIBUTES
attr <- attributes(cb)
#
# TRANSFORM ONEBASIS
if(one <- any(class(cb)=="onebasis")) {
ind <- match(names(formals(attr$fun)),names(attr),nomatch=0)
attr <- list(df=c(ncol(cb),1),range=attr$range,lag=c(0,0),
argvar=c(attr[c("fun","cen")],attr[ind]),
arglag=list(fun="strata",df=1,int=TRUE))
}
#
if (one <- any(class(cb) == "onebasis")) {
ind <- match(names(formals(attr$fun)), names(attr), nomatch = 0)
attr <- list(df = c(ncol(cb), 1), range = attr$range, lag = c(0, 0), argvar = c(attr[c("fun", "cen")],
attr[ind]), arglag = list(fun = "strata", df = 1, int = TRUE))
}
# DEFINE PENALTY TERMS
ff <- c(attr$argvar$fun,attr$arglag$fun)
fx <- c(!ff[1]%in%c('ps','cr')||attr$argvar$fx,
!ff[2]%in%c('ps','cr')||attr$arglag$fx)
ff <- c(attr$argvar$fun, attr$arglag$fun)
fx <- c(!ff[1] %in% c("ps", "cr") || attr$argvar$fx, !ff[2] %in% c("ps", "cr") || attr$arglag$fx)
Slist <- list()
if(!fx[1]) Slist <- c(Slist,list(Svar=attr$argvar$S%x%diag(attr$df[2])))
if(!fx[2]) Slist <- c(Slist,list(Slag=diag(attr$df[1])%x%attr$arglag$S))
#
if (!fx[1])
Slist <- c(Slist, list(Svar = attr$argvar$S %x% diag(attr$df[2])))
if (!fx[2])
Slist <- c(Slist, list(Slag = diag(attr$df[1]) %x% attr$arglag$S))
# RESCALING
Slist <- lapply(Slist,function(X)
X/eigen(X,symmetric=TRUE,only.values=TRUE)$values[1])
#
Slist <- lapply(Slist, function(X) X/eigen(X, symmetric = TRUE, only.values = TRUE)$values[1])
# ADDITIONAL PENALTIES ON LAG
if(one&!is.null(addSlag))
if (one & !is.null(addSlag))
stop("penalties on lag not allowed for class 'onebasis")
if(!is.null(addSlag)) Slist <- c(Slist,mkaddSlag(addSlag,attr$df))
#
if (!is.null(addSlag))
Slist <- c(Slist, mkaddSlag(addSlag, attr$df))
# RANK
rank <- sapply(Slist,findrank)
#
# SMOOTHING PARAMETERS
# sp MUST BE NUMERIC AND CONSISTENT WITH NUMBER AND ORDER OF PENALTY TERMS
rank <- sapply(Slist, findrank)
# SMOOTHING PARAMETERS sp MUST BE NUMERIC AND CONSISTENT WITH NUMBER AND ORDER OF PENALTY TERMS
npen <- length(Slist)
if(npen==0L) stop("no penalization defined")
if(length(sp)==1L) sp <- rep(sp,npen)
if(!is.numeric(sp) || length(sp)!=npen)
if (npen == 0L)
stop("no penalization defined")
if (length(sp) == 1L)
sp <- rep(sp, npen)
if (!is.numeric(sp) || length(sp) != npen)
stop("'sp' must be numeric and consistent with number of penalty terms")
names(sp) <- names(Slist)
#
res <- c(Slist,list(rank=rank,sp=sp))
#
res <- c(Slist, list(rank = rank, sp = sp))
return(res)
}
Loading