Skip to content

Commit 2cbba77

Browse files
committed
added attributes to return value
1 parent d37c99f commit 2cbba77

2 files changed

Lines changed: 23 additions & 15 deletions

File tree

confIntVariance/R/main.R

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,16 +24,23 @@ varwci <- function(x, conf.level=0.95) {
2424
if (is.data.frame(x)) {
2525
stopifnot(dim(x)[2] == 1)
2626
x <- as.numeric(data.matrix(as.vector(x)))
27-
}
28-
else stopifnot(is.atomic(x))
27+
} else {stopifnot(is.atomic(x))}
28+
x <- as.vector(x)
2929
n <- length(x)
3030
stopifnot(n>=4)
31-
t <- qt((1+conf.level)/2, df=n-1)
32-
v <- var(x)
3331
varsv <- varianceOfSampleVariance(x)
3432
if (varsv < 0) {
3533
warning("Sample size too small for estimation of the variance of the sample variance")
36-
return(c(NA, NA))
34+
r <- c(NA, NA)
35+
} else {
36+
t <- qt((1+conf.level)/2, df=n-1)
37+
v <- var(x)
38+
r <- c(v-t*sqrt(varsv), v + t*sqrt(varsv))
3739
}
38-
c(v-t*sqrt(varsv), v + t*sqrt(varsv))
40+
attributes(r) <- list(
41+
point.estimator=v,
42+
conf.level=conf.level,
43+
var.SampleVariance=max(0, varsv)
44+
)
45+
r
3946
}

confIntVariance/man/varwci.Rd

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,32 +12,35 @@ varwci(x, conf.level=0.95)
1212
}
1313
\arguments{
1414
\item{x}{A one-dimensional numeric vector}
15-
1615
\item{conf.level}{The confidence level for the confidence
1716
interval. Defaults to 0.95}
1817
}
1918

2019

2120
\value{
22-
Returns a vector with two entries: the lower and the upper bound of the
23-
confidence interval.
21+
Returns a vector with two entries: the lower and the upper bound of the
22+
confidence interval, and the following attributes:
23+
\describe{
24+
\item{point.estimator}{The usual sample variance at the center of the interval}
25+
\item{conf.level}{The confidence level used}
26+
\item{var.SampleVariance}{The estimated variance of the sample variance}
27+
}
2428
}
2529

2630
\references{
27-
www.mathiasfuchs.de/b3.html
31+
http://dx.doi.org/10.1080/15598608.2016.1158675 and https://mathiasfuchs.de/b3.html
2832
}
2933

3034
\author{
3135
Mathias Fuchs
3236
}
3337

3438
\note{
35-
The underlying theory is that of U-statistics
39+
The underlying theory is that of U-statistics. See Hoeffding 1948.
3640
}
3741

3842
\section{Warning }{
39-
On very small sample sizes, the result is NA because there is
40-
insufficient information on the variance estimation
43+
On very small sample sizes, the result is NA because there is insufficient information on the variance estimation
4144
}
4245

4346
\examples{
@@ -63,8 +66,6 @@ trueMeanOfDice <- mean(1:6)
6366
trueVarianceOfDice <- mean((1:6)^2) - trueMeanOfDice^2
6467

6568
## see package description for more details
66-
67-
6869
# number of times we draw a sample and compute a confidence interval
6970
N <- 1e4
7071
trueValueCovered <- rep(NA, N)

0 commit comments

Comments
 (0)