-
-
Notifications
You must be signed in to change notification settings - Fork 39
Expand file tree
/
Copy pathsrs_diff_est.Rd
More file actions
97 lines (86 loc) · 3.37 KB
/
Copy pathsrs_diff_est.Rd
File metadata and controls
97 lines (86 loc) · 3.37 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
% Generated by roxygen2: do not edit by hand
% Please edit documentation in R/loo_subsample.R
\name{srs_diff_est}
\alias{srs_diff_est}
\title{Difference estimator with simple random sampling without replacement.}
\usage{
srs_diff_est(y_approx, y, y_idx)
}
\arguments{
\item{y_approx}{(numeric) \code{n} approximated values.}
\item{y}{(numeric) \code{m<n} subsampled values.}
\item{y_idx}{(integerish) The index of \code{y} in \code{y_approx}.}
}
\value{
A named list containing numeric values:
\itemize{
\item \code{y_hat}: estimated mean of \eqn{y} (Eq 7),
\item \code{v_y_hat}: variance of the mean estimate (Eq 8), and
\item \code{hat_v_y}: estimated variance of \eqn{y} (Eq 9).
}
}
\description{
The difference estimator \code{srs_diff_est()} estimates
the expectation \eqn{n E[y]} when we have \eqn{n} approximate values \eqn{\tilde{y}_i},
\eqn{i = 1, \ldots, n} and \eqn{m < n} accurate values \eqn{y_j}, \eqn{j \in \mathcal{S}},
where \eqn{m} is the subsample size and \eqn{\mathcal{S}} is
a simple random subsample without replacement. The original
approach is by Cochran (1977) and we follow the equations 7--9 by
Magnusson et al. (2020).
}
\details{
In Magnusson et al. (2020) Eq (9) first row, the second \code{+} should
be a \code{-}; Supplementary Material Eq (6) has this correct.
As \code{srs_diff_est()} in the \code{loo} package is used for \eqn{n E[y]}, there is
a proportional difference of \eqn{1/n} compared to the paper.
}
\examples{
## This example predicts wine quality (data from Cortez et al., 2009).
# The code is commented as ## Not run: because brm() takes a few seconds to fit.
# Copy the code to your console to execute it.
# A log_lik_matrix is generated from a fit, then it is used for srs_diff_est().
\dontrun{
library(brms)
options(brms.backend = "cmdstanr")
options(mc.cores = 4)
wine_scaled <- read.delim(
"https://archive.ics.uci.edu/ml/machine-learning-databases/wine-quality/winequality-red.csv",
sep = ";"
) |> unique() |> scale() |> as.data.frame()
fitos <- brm(ordered(quality) ~ .,
family = cumulative("logit"),
prior = prior(R2D2(mean_R2 = 1/3, prec_R2 = 3)),
data = wine_scaled,
seed = 1,
silent = 2,
refresh = 0)
log_lik_matrix <- log_lik(fitos)
N <- nrow(wine_scaled)
Nsub <- 100
# posterior log-score
lpd <- elpd(log_lik_matrix)
sum(lpd$pointwise[,"elpd"])
# Use PSIS-LOO for subsample of Nsub randomly selected observations
set.seed(1)
idx <- sample(1:N, Nsub)
elpd_loo_sub <- loo(log_lik_matrix[,idx])
sum(elpd_loo_sub$pointwise[,"elpd_loo"]) / Nsub * N
# Use difference estimator to combine fast result and subsampled accurate result
loo::srs_diff_est(lpd$pointwise[,"elpd"], elpd_loo_sub$pointwise[,"elpd_loo"], idx)
# Comparison to using PSIS-LOO for all observations
loo(log_lik_matrix)
}
}
\references{
Magnusson, M., Riis Andersen, M., Jonasson, J. and Vehtari, A. (2020).
Leave-One-Out Cross-Validation for Model Comparison in Large Data.
In \emph{Proceedings of the 23rd International Conference on Artificial
Intelligence and Statistics (AISTATS)}, PMLR 108:341-351.
Cochran, W. G. (1977). \emph{Sampling Techniques, 3rd Edition}. John Wiley.
Cortez, P., Cerdeira, A.L., Almeida, F., Matos, T., & Reis, J. (2009).
Modeling wine preferences by data mining from physicochemical properties.
\emph{Decis. Support Syst.}, \emph{47}, 547-553.
}
\seealso{
\code{\link[=loo_subsample]{loo_subsample()}}
}