Skip to content

Commit 087ef0a

Browse files
committed
adding mean.equals.sd arg and updating docs
1 parent 00c3068 commit 087ef0a

17 files changed

+296
-250
lines changed

DESCRIPTION

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
Package: IDSpatialStats
2-
Version: 0.3.4
3-
Date: 2018-06-02
2+
Version: 0.3.5
3+
Date: 2018-06-11
44
Title: Estimate Global Clustering in Infectious Disease
55
Author: Justin Lessler <[email protected]>, Henrik Salje <[email protected]>, John Giles <[email protected]>
66
Maintainer: Justin Lessler <[email protected]>

R/examples/est_transdist_bootstrap_ci.R

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
set.seed(1)
22

3-
# Normally distributed transmission kernel with mean and standard deviation = 100
3+
# Exponentially distributed transmission kernel with mean and standard deviation = 100
44
dist.func <- alist(n=1, a=1/100, rexp(n, a))
55

66
# Simulate epidemic
@@ -19,6 +19,7 @@ b <- est.transdist.bootstrap.ci(epi.data=a,
1919
max.sep=1e10,
2020
max.dist=1e10,
2121
n.transtree.reps=10,
22+
mean.equals.sd=TRUE,
2223
boot.iter=10,
2324
ci.low=0.025,
2425
ci.high=0.975,

R/examples/est_transdist_temporal.R

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,15 @@ b <- est.transdist.temporal(epi.data=a,
2020
t1=0,
2121
max.sep=1e10,
2222
max.dist=1e10,
23-
n.transtree.reps=3,
23+
n.transtree.reps=5,
24+
mean.equals.sd=TRUE,
2425
parallel=FALSE)
2526

26-
plot(b, pch=19, col='grey', ylim=c(min(b, na.rm=TRUE), max(b, na.rm=TRUE)),
27+
plot(b[,1], pch=19, col='grey', ylim=c(min(b[,1], na.rm=TRUE), max(b[,1], na.rm=TRUE)),
2728
xlab='Time step', ylab='Estimated mean of transmission kernel')
29+
abline(h=100, col='red', lty=2)
30+
axis(3, b[,2])
2831

29-
low <- loess(b ~ as.vector(1:length(b)))
30-
low <- predict(low, newdata=data.frame(as.vector(1:length(b))))
32+
low <- loess(b[,1] ~ as.vector(1:length(b[,1])))
33+
low <- predict(low, newdata=data.frame(as.vector(1:length(b[,1]))))
3134
lines(low, lwd=3, col='blue')

R/examples/est_transdist_temporal_bootstrap_ci.R

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
\donttest{
2-
set.seed(1)
2+
set.seed(123)
33

44
# Exponentially distributed transmission kernel with mean and standard deviation = 100
55
dist.func <- alist(n=1, a=1/100, rexp(n, a))
@@ -12,6 +12,8 @@
1212
min.cases=30,
1313
trans.kern.func=dist.func)
1414

15+
a <- a[sample(1:nrow(a), 100),] # subsample a to 100 observations
16+
1517
# Estimate change in mean transmission kernel over time with confidence intervals
1618
nc <- 4 # Run in parallel
1719

@@ -21,15 +23,18 @@
2123
t1=0,
2224
max.sep=1e10,
2325
max.dist=1e10,
24-
n.transtree.reps=3,
25-
boot.iter=5,
26+
n.transtree.reps=10,
27+
mean.equals.sd=TRUE,
28+
boot.iter=10,
2629
ci.low=0.025,
2730
ci.high=0.975,
2831
parallel=TRUE,
2932
n.cores=nc)
3033

31-
plot(b[,1], pch=19, col='grey', ylim=c(min(b, na.rm=TRUE), max(b, na.rm=TRUE)),
34+
plot(b[,1], pch=19, col='grey', ylim=c(min(b[,1:3], na.rm=TRUE), max(b[,1:3], na.rm=TRUE)),
3235
xlab='Time step', ylab='Estimated mean of transmission kernel')
36+
abline(h=100, col='red', lty=2)
37+
axis(3, 1:nrow(b), b[,4])
3338

3439
low <- loess(b[,1] ~ as.vector(1:nrow(b)), span=1)
3540
low <- predict(low, newdata=data.frame(as.vector(1:nrow(b))))

R/transdistfuncs.r

Lines changed: 40 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -268,6 +268,7 @@ get.transdist.theta <-function(wal.teun.mat,
268268
# Clean matrix containing theta estimates
269269
a <- order(as.numeric(rownames(theta.mat))) # reorder by cases
270270
suppressWarnings(theta.mat <- matrix(as.integer(floor(theta.mat[a,a])), n, n))
271+
#suppressWarnings(theta.mat <- matrix(as.integer(theta.mat[a,a])), n, n)
271272
diag(theta.mat) <- NA
272273

273274
# Return theta matrix for unit testing
@@ -505,6 +506,7 @@ est.transdist <- function(
505506
##' @param max.sep maximum number of time steps allowed between two cases (passed to the \code{get.transdist.theta} function)
506507
##' @param max.dist maximum spatial distance between two cases considered in calculation
507508
##' @param n.transtree.reps number of time to simulate transmission trees when estimating the weights of theta (passed to the \code{est.transdist.theta.weights} function, default = 10). Warning: higher values of this parameter cause significant increases in computation time.
509+
##' @param mean.equals.sd logical term indicating if the mean and standard deviation of the transmission kernel are expected to be equal (default = FALSE)
508510
##' @param theta.weights use external matrix of theta weights. If NULL (default) the matrix of theta weights is automatically estimated by calling the \code{est.transdist.theta.weights} function
509511
##' @param boot.iter the number of bootstrapped iterations to perform
510512
##' @param ci.low low end of the confidence interval (default = 0.025)
@@ -532,6 +534,7 @@ est.transdist.bootstrap.ci <- function(
532534
max.sep, # Maximum time between cases considered in calculation
533535
max.dist, # Maximum distance considered in calculation
534536
n.transtree.reps=100, # How many times to simulate transmission trees
537+
mean.equals.sd=FALSE, # logical term indicating if the mean and sd of the transmission kernel are expected to be equal
535538
theta.weights=NULL, # External matrix of theta weights
536539
boot.iter,
537540
ci.low=0.025,
@@ -551,6 +554,9 @@ est.transdist.bootstrap.ci <- function(
551554
theta.weights=theta.weights,
552555
silent=TRUE)
553556

557+
if(mean.equals.sd == TRUE) pt.est <- p$mu.est
558+
if(mean.equals.sd == FALSE) pt.est <- p$bound.mu.est
559+
554560
n <- nrow(epi.data)
555561

556562
if(parallel == FALSE) {
@@ -569,7 +575,10 @@ est.transdist.bootstrap.ci <- function(
569575
n.transtree.reps=n.transtree.reps,
570576
theta.weights=theta.weights,
571577
silent=TRUE)
572-
a$mu.est
578+
579+
if(mean.equals.sd == TRUE) a <- a$mu.est
580+
if(mean.equals.sd == FALSE) a <- a$bound.mu.est
581+
a
573582
}
574583
}
575584

@@ -596,14 +605,16 @@ est.transdist.bootstrap.ci <- function(
596605
theta.weights=theta.weights,
597606
silent=TRUE)
598607

599-
a$mu.est
608+
if(mean.equals.sd == TRUE) a <- a$mu.est
609+
if(mean.equals.sd == FALSE) a <- a$bound.mu.est
610+
a
600611
}
601612
parallel::stopCluster(clust)
602613
}
603614

604615
ci <- quantile(bs, probs=c(ci.low, ci.high))
605616

606-
return(list(mu.est=p$mu.est,
617+
return(list(mu.est=pt.est,
607618
mu.ci.low=ci[1],
608619
mu.ci.high=ci[2]))
609620
}
@@ -620,11 +631,12 @@ est.transdist.bootstrap.ci <- function(
620631
##' @param max.sep maximum number of time steps allowed between two cases (passed to the \code{get.transdist.theta} function)
621632
##' @param max.dist maximum spatial distance between two cases considered in calculation
622633
##' @param n.transtree.reps number of time to simulate transmission trees when estimating the weights of theta (passed to the \code{est.transdist.theta.weights} function, default = 10). Higher values of this parameter cause significant increases in computation time.
634+
##' @param mean.equals.sd logical term indicating if the mean and standard deviation of the transmission kernel are expected to be equal (default = FALSE)
623635
##' @param theta.weights use external matrix of theta weights. If NULL (default) the matrix of theta weights is automatically estimated by calling the \code{est.transdist.theta.weights} function
624636
##' @param parallel run time steps in parallel (default = FALSE)
625637
##' @param n.cores number of cores to use when \code{parallel} = TRUE (default = NULL, which uses half the available cores)
626638
##'
627-
##' @return a vector containing the point estimate for mean transmission distance for each unique time step of the epidemic.
639+
##' @return a numeric matrix containing the point estimate for mean transmission distance for each unique time step of the epidemic and the sample size $n$ used to make the estimate
628640
##' NAs are returned for time steps which contain fewer than three cases
629641
##'
630642
##' @author Justin Lessler, Henrik Salje, and John Giles
@@ -645,6 +657,7 @@ est.transdist.temporal <- function(
645657
max.sep, # Maximum time between cases considered in calculation
646658
max.dist, # Maximum distance considered in calculation
647659
n.transtree.reps=10, # How many times to simulate transmission trees
660+
mean.equals.sd=FALSE,
648661
theta.weights=NULL, # External matrix of theta weights
649662
parallel=FALSE,
650663
n.cores=NULL
@@ -669,7 +682,6 @@ est.transdist.temporal <- function(
669682
out <- foreach::foreach(i=seq_along(unique.times), .combine='rbind') %do% {
670683

671684
d <- epi.data[epi.data[,3] <= unique.times[i],]
672-
n <- nrow(matrix(d, ncol=ncol(epi.data)))
673685

674686
pt.est <- try(est.transdist(epi.data=d,
675687
gen.t.mean=gen.t.mean,
@@ -682,9 +694,10 @@ est.transdist.temporal <- function(
682694
silent=TRUE)
683695
if(class(pt.est) == "try-error") {
684696
pt.est <- NA } else {
685-
pt.est <- pt.est$mu.est
697+
if(mean.equals.sd == TRUE) pt.est <- pt.est$mu.est
698+
if(mean.equals.sd == FALSE) pt.est <- pt.est$bound.mu.est
686699
}
687-
pt.est
700+
x <- c(pt.est, nrow(d))
688701
}
689702
}
690703

@@ -703,7 +716,6 @@ est.transdist.temporal <- function(
703716
out <- foreach::foreach(i=seq_along(unique.times), .combine='rbind') %dopar% {
704717

705718
d <- epi.data[epi.data[,3] <= unique.times[i],]
706-
n <- nrow(matrix(d, ncol=ncol(epi.data)))
707719

708720
pt.est <- try(est.transdist(epi.data=d,
709721
gen.t.mean=gen.t.mean,
@@ -717,13 +729,16 @@ est.transdist.temporal <- function(
717729
if(class(pt.est) == "try-error") {
718730
pt.est <- NA
719731
} else {
720-
pt.est <- pt.est$mu.est
732+
if(mean.equals.sd == TRUE) pt.est <- pt.est$mu.est
733+
if(mean.equals.sd == FALSE) pt.est <- pt.est$bound.mu.est
721734
}
722-
pt.est
735+
x <- c(pt.est, nrow(d))
723736
}
724737
parallel::stopCluster(clust)
725738
}
726-
return(as.vector(out))
739+
rownames(out) <- NULL
740+
colnames(out) <- c('pt.est', 'n')
741+
return(out)
727742
}
728743

729744
##' Bootstrapped confidence intervals for the change in mean transmission distance over time
@@ -738,14 +753,15 @@ est.transdist.temporal <- function(
738753
##' @param max.sep maximum number of time steps allowed between two cases (passed to the \code{get.transdist.theta} function)
739754
##' @param max.dist maximum spatial distance between two cases considered in calculation
740755
##' @param n.transtree.reps number of time to simulate transmission trees when estimating the weights of theta (passed to the \code{est.transdist.theta.weights} function, default = 10). Warning: higher values of this parameter cause significant increases in computation time.
756+
##' @param mean.equals.sd logical term indicating if the mean and standard deviation of the transmission kernel are expected to be equal (default = FALSE)
741757
##' @param theta.weights use external matrix of theta weights. If NULL (default) the matrix of theta weights is automatically estimated by calling the \code{est.transdist.theta.weights} function
742758
##' @param boot.iter the number of bootstrapped iterations to perform
743759
##' @param ci.low low end of the confidence interval (default = 0.025)
744760
##' @param ci.high high end of the confidence interval (default = 0.975)
745761
##' @param parallel run bootstraps in parallel (default = FALSE)
746762
##' @param n.cores number of cores to use when \code{parallel} = TRUE (default = NULL, which uses half the available cores)
747763
##'
748-
##' @return a three-column matrix containing the point estimate for mean transmission distance and low and high bootstrapped confidence intervals
764+
##' @return a four-column numeric matrix containing the point estimate for mean transmission distance, low and high bootstrapped confidence intervals, and the sample size up to each time step
749765
##'
750766
##' @author Justin Lessler, Henrik Salje, and John Giles
751767
##'
@@ -757,7 +773,6 @@ est.transdist.temporal <- function(
757773
##' @example R/examples/est_transdist_temporal_bootstrap_ci.R
758774
##'
759775

760-
761776
est.transdist.temporal.bootstrap.ci <- function(
762777
epi.data, # Three column matrix: coordinates and time of case (must be in x, y, t, order)
763778
gen.t.mean, # Mean of generation time
@@ -766,6 +781,7 @@ est.transdist.temporal.bootstrap.ci <- function(
766781
max.sep, # Maximum time between cases considered in calculation
767782
max.dist, # Maximum distance considered in calculation
768783
n.transtree.reps=100, # How many times to simulate transmission trees
784+
mean.equals.sd=FALSE,
769785
theta.weights=NULL, # External matrix of theta weights
770786
boot.iter,
771787
ci.low=0.025,
@@ -806,7 +822,8 @@ est.transdist.temporal.bootstrap.ci <- function(
806822
silent=TRUE)
807823
if(class(pt.est) == "try-error") {
808824
pt.est <- NA } else {
809-
pt.est <- pt.est$mu.est
825+
if(mean.equals.sd == TRUE) pt.est <- pt.est$mu.est
826+
if(mean.equals.sd == FALSE) pt.est <- pt.est$bound.mu.est
810827
}
811828

812829
bs <- rep(NA, boot.iter)
@@ -825,12 +842,13 @@ est.transdist.temporal.bootstrap.ci <- function(
825842
silent=TRUE)
826843
if(class(a) == "try-error") {
827844
bs[j] <- NA} else {
828-
bs[j] <- a$mu.est
845+
if(mean.equals.sd == TRUE) bs[j] <- a$mu.est
846+
if(mean.equals.sd == FALSE) bs[j] <- a$bound.mu.est
829847
}
830848
}
831849

832850
suppressWarnings(bs.probs <- quantile(as.numeric(bs), probs=c(ci.low, ci.high), na.rm=TRUE))
833-
x <- c(pt.est, bs.probs[1], bs.probs[2])
851+
x <- c(pt.est, bs.probs[1], bs.probs[2], n)
834852
}
835853
}
836854

@@ -863,7 +881,8 @@ est.transdist.temporal.bootstrap.ci <- function(
863881
if(class(pt.est) == "try-error") {
864882
pt.est <- NA
865883
} else {
866-
pt.est <- pt.est$mu.est
884+
if(mean.equals.sd == TRUE) pt.est <- pt.est$mu.est
885+
if(mean.equals.sd == FALSE) pt.est <- pt.est$bound.mu.est
867886
}
868887

869888
bs <- rep(NA, boot.iter)
@@ -883,18 +902,19 @@ est.transdist.temporal.bootstrap.ci <- function(
883902
if(class(a) == "try-error") {
884903
bs[j] <- NA
885904
} else {
886-
bs[j] <- a$mu.est
905+
if(mean.equals.sd == TRUE) bs[j] <- a$mu.est
906+
if(mean.equals.sd == FALSE) bs[j] <- a$bound.mu.est
887907
}
888908
}
889909

890910
suppressWarnings(bs.probs <- quantile(as.numeric(bs), probs=c(ci.low, ci.high), na.rm=TRUE))
891-
x <- c(pt.est, bs.probs[1], bs.probs[2])
911+
x <- c(pt.est, bs.probs[1], bs.probs[2], n)
892912
}
893913
parallel::stopCluster(clust)
894914
}
895915

896916
row.names(out) <- NULL
897-
colnames(out) <- c('mu.est', 'mu.ci.low', 'mu.ci.high')
917+
colnames(out) <- c('mu.est', 'mu.ci.low', 'mu.ci.high', 'n')
898918
return(out)
899919
}
900920

0 commit comments

Comments
 (0)