Title: | Bayesian Statistics for 2D/3D Transformations |
---|---|
Description: | Fits 2D and 3D geometric transformations via 'Stan' probabilistic programming engine ( Stan Development Team (2021) <https://mc-stan.org>). Returns posterior distribution for individual parameters of the fitted distribution. Allows for computation of LOO and WAIC information criteria (Vehtari A, Gelman A, Gabry J (2017) <doi:10.1007/s11222-016-9696-4>) as well as Bayesian R-squared (Gelman A, Goodrich B, Gabry J, and Vehtari A (2018) <doi:10.1080/00031305.2018.1549100>). |
Authors: | Alexander Pastukhov [aut, cre] , Claus-Christian Carbon [aut] |
Maintainer: | Alexander Pastukhov <[email protected]> |
License: | GPL-3 |
Version: | 1.0.1 |
Built: | 2024-12-25 04:29:11 UTC |
Source: | https://github.com/alexander-pastukhov/tridim-regression |
Fits 2D and 3D geometric transformations. Provides posterior via Stan. Includes computation of LOO and WAIC information criteria, R-squared.
To fit transformation, call the main function either via a formula that specifies
dependent and independent variables with the data
table or by supplying two tables
one containing all independent variables and one containing all dependent variables.
For the 2D data, you can fit "translation"
(2 parameters for translation only), "euclidean"
(4 parameters: 2 for translation, 1 for scaling, and 1 for rotation),
"affine"
(6 parameters: 2 for translation and 4 that jointly describe scaling, rotation and sheer),
or "projective"
(8 parameters: affine plus 2 additional parameters to account for projection).
For 3D data, you can fit "translation"
(3 for translation only), "euclidean_x"
, "euclidean_y"
,
"euclidean_z"
(5 parameters: 3 for translation scale, 1 for rotation, and 1 for scaling),
"affine"
(12 parameters: 3 for translation and 9 to account for scaling, rotation, and sheer),
and "projective"
(15 parameters: affine plus 3 additional parameters to account for projection).
transformations. For details on transformation matrices and computation of scale and rotation parameters please
see vignette("transformation_matrices", package = "TriDimRegression")
Once the data is fitted, you can extract the transformation coefficients via coef
function and the matrix
itself via transformation_matrix
. Predicted data, either based on the original data or on the new data,
can be generated via predict
. Bayesian R-squared can be computed with or without adjustment via
R2
function. In all three cases, you have choice between summary (mean + specified quantiles) or full
posterior samples. loo
and waic
provide corresponding measures that can be used for comparison
via loo::loo_compare()
function.
Stan Development Team (2020). RStan: the R interface to Stan. R package version 2.19.3. https://mc-stan.org
fit_transformation
fit_transformation_df
tridim_transformation
vignette("transformation_matrices", package = "TriDimRegression")
vignette("calibration", package = "TriDimRegression")
vignette("comparing_faces", package = "TriDimRegression")
# Fitting via formula euc2 <- fit_transformation(depV1 + depV2 ~ indepV1 + indepV2, NakayaData, 'euclidean') aff2 <- fit_transformation(depV1 + depV2 ~ indepV1 + indepV2, NakayaData, 'affine') prj2 <- fit_transformation(depV1 + depV2 ~ indepV1 + indepV2, NakayaData, 'projective') # summary of transformation coefficients coef(euc2) # statistical comparison via WAIC criterion loo::loo_compare(waic(euc2), waic(aff2), waic(prj2)) # Fitting via two tables euc2 <- fit_transformation_df(NakayaData[, 1:2], NakayaData[, 3:4], 'euclidean') tr3 <- fit_transformation_df(Face3D_W070, Face3D_W097, transformation ='translation')
# Fitting via formula euc2 <- fit_transformation(depV1 + depV2 ~ indepV1 + indepV2, NakayaData, 'euclidean') aff2 <- fit_transformation(depV1 + depV2 ~ indepV1 + indepV2, NakayaData, 'affine') prj2 <- fit_transformation(depV1 + depV2 ~ indepV1 + indepV2, NakayaData, 'projective') # summary of transformation coefficients coef(euc2) # statistical comparison via WAIC criterion loo::loo_compare(waic(euc2), waic(aff2), waic(prj2)) # Fitting via two tables euc2 <- fit_transformation_df(NakayaData[, 1:2], NakayaData[, 3:4], 'euclidean') tr3 <- fit_transformation_df(Face3D_W070, Face3D_W097, transformation ='translation')
Example 1 from the domain of aesthetics to show how the method can be utilized for assessing the similarity of two portrayed persons, actually the Mona Lisa in the world famous Louvre version and the only recently re-discovered Prado version.
CarbonExample1Data
CarbonExample1Data
A data frame with 36 observations on the following 4 variables:
numeric vectors, dependent variables
numeric vectors, independent variables
Example 2 originates from the area of geography and inspects the accuracy of different maps of the city of Paris which were created over the last 350 years as compared to a recent map.
CarbonExample2Data
CarbonExample2Data
A data frame with 13 observations on the following 4 variables:
numeric vectors, dependent variables
numeric vectors, independent variables
Example 3 focuses on demonstrating how good a cognitive map recalculated from averaged cognitive distance data fits with a related real map.
CarbonExample3Data
CarbonExample3Data
A data frame with 10 observations on the following 4 variables:
numeric vectors, dependent variables
numeric vectors, independent variables
Posterior distributions for transformation coefficients in full or summarized form.
## S3 method for class 'tridim_transformation' coef( object, summary = TRUE, probs = c(0.055, 0.945), convert_euclidean = FALSE, ... )
## S3 method for class 'tridim_transformation' coef( object, summary = TRUE, probs = c(0.055, 0.945), convert_euclidean = FALSE, ... )
object |
An object of class tridim_transformation. |
summary |
Whether summary statistics should be returned instead of
raw sample values. Defaults to |
probs |
The percentiles used to compute summary, defaults to 89% credible interval. |
convert_euclidean |
Whether to convert matrix coefficients to scale(phi) and rotation(theta). Defaults to |
... |
Unused |
If summary=FALSE, a list with matrix iterationsN x dimensionsN for each variable. If summary=TRUE, a data.frame with columns "dvindex" with mean for each dependent variable plus optional quantiles columns with names "dvindex_quantile".
euc2 <- fit_transformation(depV1+depV2~indepV1+indepV2, data = NakayaData, transformation = 'euclidean') # full posterior distribution transform_posterior <- coef(euc2, summary=FALSE) # coefficients' summary with 89% CI coef(euc2) # scale and rotation coefficients coef(euc2, convert_euclidean=TRUE)
euc2 <- fit_transformation(depV1+depV2~indepV1+indepV2, data = NakayaData, transformation = 'euclidean') # full posterior distribution transform_posterior <- coef(euc2, summary=FALSE) # coefficients' summary with 89% CI coef(euc2) # scale and rotation coefficients coef(euc2, convert_euclidean=TRUE)
A dataset containing a monocular eye gaze recording with calibration sequence. Courtesy of Bamberger Baby Institut: BamBI.
EyegazeData
EyegazeData
A data frame with 365 rows and 6 variables:
sample timestamp, in milliseconds
recorded gaze, in internal eye tracker units
location of the calibration target on the screen, in pixels
index of the target within the sequence
https://www.uni-bamberg.de/entwicklungspsychologie/transfer/babyforschung-bambi/.
Face landmarks, male, #010
Face3D_M010
Face3D_M010
A data frame with 64 landmarks on the following 3 variables:
numeric vectors, coordinates of face landmarks
Carbon, C. C. (2012). The Bamberg DADA Face Database (BaDADA). A standardized high quality Face Database with faces of Different Affective states from Different Angles. Unpublished databank. University of Bamberg, Bamberg.
Face landmarks, male, #101
Face3D_M101
Face3D_M101
A data frame with 64 landmarks on the following 3 variables:
numeric vectors, coordinates of face landmarks
Carbon, C. C. (2012). The Bamberg DADA Face Database (BaDADA). A standardized high quality Face Database with faces of Different Affective states from Different Angles. Unpublished databank. University of Bamberg, Bamberg.
Face landmarks, male, #244
Face3D_M244
Face3D_M244
A data frame with 64 landmarks on the following 3 variables:
numeric vectors, coordinates of face landmarks
Carbon, C. C. (2012). The Bamberg DADA Face Database (BaDADA). A standardized high quality Face Database with faces of Different Affective states from Different Angles. Unpublished databank. University of Bamberg, Bamberg.
Face landmarks, male, #092
Face3D_M92
Face3D_M92
A data frame with 64 landmarks on the following 3 variables:
numeric vectors, coordinates of face landmarks
Carbon, C. C. (2012). The Bamberg DADA Face Database (BaDADA). A standardized high quality Face Database with faces of Different Affective states from Different Angles. Unpublished databank. University of Bamberg, Bamberg.
Face landmarks, female, #070
Face3D_W070
Face3D_W070
A data frame with 64 landmarks on the following 3 variables:
numeric vectors, coordinates of face landmarks
Carbon, C. C. (2012). The Bamberg DADA Face Database (BaDADA). A standardized high quality Face Database with faces of Different Affective states from Different Angles. Unpublished databank. University of Bamberg, Bamberg.
Face landmarks, female, #097
Face3D_W097
Face3D_W097
A data frame with 64 landmarks on the following 3 variables:
numeric vectors, coordinates of face landmarks
Carbon, C. C. (2012). The Bamberg DADA Face Database (BaDADA). A standardized high quality Face Database with faces of Different Affective states from Different Angles. Unpublished databank. University of Bamberg, Bamberg.
Face landmarks, female, #182
Face3D_W182
Face3D_W182
A data frame with 64 landmarks on the following 3 variables:
numeric vectors, coordinates of face landmarks
Carbon, C. C. (2012). The Bamberg DADA Face Database (BaDADA). A standardized high quality Face Database with faces of Different Affective states from Different Angles. Unpublished databank. University of Bamberg, Bamberg.
Face landmarks, female, #243
Face3D_W243
Face3D_W243
A data frame with 64 landmarks on the following 3 variables:
numeric vectors, coordinates of face landmarks
Carbon, C. C. (2012). The Bamberg DADA Face Database (BaDADA). A standardized high quality Face Database with faces of Different Affective states from Different Angles. Unpublished databank. University of Bamberg, Bamberg.
Fits Bidimensional or Tridimensional regression / geometric transformation models using
Stan engine. The formula
described dependent and independent numeric variables in the
data
. See also fit_transformation_df
.
For the 2D data, you can fit "translation"
(2 parameters for translation only), "euclidean"
(4 parameters: 2 for translation, 1 for scaling, and 1 for rotation),
"affine"
(6 parameters: 2 for translation and 4 that jointly describe scaling, rotation and sheer),
or "projective"
(8 parameters: affine plus 2 additional parameters to account for projection).
For 3D data, you can fit "translation"
(3 for translation only), "euclidean_x"
, "euclidean_y"
,
"euclidean_z"
(5 parameters: 3 for translation scale, 1 for rotation, and 1 for scaling),
"affine"
(12 parameters: 3 for translation and 9 to account for scaling, rotation, and sheer),
and "projective"
(15 parameters: affine plus 3 additional parameters to account for projection).
transformations.
For details on transformation matrices and computation of scale and rotation parameters please
see vignette("transformation_matrices", package = "TriDimRegression")
## S3 method for class 'formula' fit_transformation( formula, data, transformation, priors = NULL, chains = 1, cores = NULL, ... )
## S3 method for class 'formula' fit_transformation( formula, data, transformation, priors = NULL, chains = 1, cores = NULL, ... )
formula |
a symbolic description of the model to be fitted in the format |
data |
a data frame containing variables for the model. |
transformation |
the transformation to be used: |
priors |
named list of parameters for prior distributions of parameters |
chains |
Number of chains for sampling. |
cores |
Number of CPU cores to use for sampling. If omitted, all available cores are used. |
... |
Additional arguments passed to |
A tridim_transformation object
# Geometric transformations of 2D data euc2 <- fit_transformation(depV1 + depV2 ~ indepV1 + indepV2, NakayaData, 'euclidean') aff2 <- fit_transformation(depV1 + depV2 ~ indepV1 + indepV2, NakayaData, 'affine') prj2 <- fit_transformation(depV1 + depV2 ~ indepV1 + indepV2, NakayaData, 'projective') # summary of transformation coefficients coef(euc2) # statistical comparison via WAIC criterion loo::loo_compare(waic(euc2), waic(aff2), waic(prj2))
# Geometric transformations of 2D data euc2 <- fit_transformation(depV1 + depV2 ~ indepV1 + indepV2, NakayaData, 'euclidean') aff2 <- fit_transformation(depV1 + depV2 ~ indepV1 + indepV2, NakayaData, 'affine') prj2 <- fit_transformation(depV1 + depV2 ~ indepV1 + indepV2, NakayaData, 'projective') # summary of transformation coefficients coef(euc2) # statistical comparison via WAIC criterion loo::loo_compare(waic(euc2), waic(aff2), waic(prj2))
Fits Bidimensional or Tridimensional regression / geometric transformation models using
Stan engine. Two sets of coordinates are supplied via iv
(for an independent variable)
and dv
(for the dependent one). The two tables must have the same dimensions
(both N×2 or N×3).
For the 2D data, you can fit "translation"
(2 for translation only), "euclidean"
(4 parameters: 2 for translation, 1 for scaling, and 1 for rotation),
"affine"
(6 parameters: 2 for translation and 4 that jointly describe scaling, rotation and sheer),
or "projective"
(8 parameters: affine plus 2 additional parameters to account for projection).
For 3D data, you can fit "translation"
(3 for translation only), "euclidean_x"
, "euclidean_y"
,
"euclidean_z"
(5 parameters: 3 for translation scale, 1 for rotation, and 1 for scaling),
"affine"
(12 parameters: 3 for translation and 9 to account for scaling, rotation, and sheer),
and "projective"
(15 parameters: affine plus 3 additional parameters to account for projection).
transformations.
For details on transformation matrices and computation of scale and rotation parameters please
see vignette("transformation_matrices", package = "TriDimRegression")
fit_transformation_df( iv, dv, transformation, priors = NULL, chains = 1, cores = NULL, ... )
fit_transformation_df( iv, dv, transformation, priors = NULL, chains = 1, cores = NULL, ... )
iv |
a data frame containing independent variable, must by numeric only, N×2 or N×3. |
dv |
a data frame containing dependent variable, must by numeric only, N×2 or N×3. |
transformation |
the transformation to be used: |
priors |
named list of parameters for prior distributions of parameters |
chains |
Number of chains for sampling. |
cores |
Number of CPU cores to use for sampling. If omitted, all available cores are used. |
... |
Additional arguments passed to |
A tridim_transformation object
# Geometric transformations of 2D data euc2 <- fit_transformation_df(NakayaData[, 1:2], NakayaData[, 3:4], 'euclidean') tr3 <- fit_transformation_df(Face3D_W070, Face3D_W097, transformation ='translation')
# Geometric transformations of 2D data euc2 <- fit_transformation_df(NakayaData[, 1:2], NakayaData[, 3:4], 'euclidean') tr3 <- fit_transformation_df(Face3D_W070, Face3D_W097, transformation ='translation')
Data from Friedman, A., & Kohler, B. (2003). Bidimensional regression: Assessing the configural similarity and accuracy of cognitive maps and other two-dimensional data sets. Psychological Methods, 8(4), 468-491. DOI: 10.1037/1082-989X.8.4.468
FriedmanKohlerData1
FriedmanKohlerData1
A data frame with 4 observations on the following 4 variables:
numeric vectors, dependent variables
numeric vectors, independent variables
Data from Friedman, A., & Kohler, B. (2003). Bidimensional regression: Assessing the configural similarity and accuracy of cognitive maps and other two-dimensional data sets. Psychological Methods, 8(4), 468-491. DOI: 10.1037/1082-989X.8.4.468
FriedmanKohlerData2
FriedmanKohlerData2
A data frame with 4 observations on the following 4 variables:
numeric vectors, dependent variables
numeric vectors, independent variables
tridim_transformation
objectChecks if argument is a tridim_transformation
object
is.tridim_transformation(x)
is.tridim_transformation(x)
x |
An R object |
Logical
Computes an efficient approximate leave-one-out cross-validation via loo library. It can be used for a model comparison via loo::loo_compare() function.
## S3 method for class 'tridim_transformation' loo(x, ...)
## S3 method for class 'tridim_transformation' loo(x, ...)
x |
A tridim_transformation object |
... |
unused |
A named list, see loo::loo()
for details.
euc2 <- fit_transformation(depV1+depV2~indepV1+indepV2, NakayaData, transformation = 'euclidean') aff2 <- fit_transformation(depV1+depV2~indepV1+indepV2, NakayaData, transformation = 'affine') loo::loo_compare(loo(euc2), loo(aff2))
euc2 <- fit_transformation(depV1+depV2~indepV1+indepV2, NakayaData, transformation = 'euclidean') aff2 <- fit_transformation(depV1+depV2~indepV1+indepV2, NakayaData, transformation = 'affine') loo::loo_compare(loo(euc2), loo(aff2))
Nakaya, T. (1997) Statistical inferences in bidimensional regression models. Geographical Analysis, 29(2), 169-186.
NakayaData
NakayaData
A data frame with 19 observations on the following 4 variables:
numeric vectors, dependent variables
numeric vectors, independent variables
doi:10.1111/j.1538-4632.1997.tb00954.x
Posterior interval plots for key parameters. Uses bayesplot::mcmc_intervals.
## S3 method for class 'tridim_transformation' plot(x, convert_euclidean = FALSE, ...)
## S3 method for class 'tridim_transformation' plot(x, convert_euclidean = FALSE, ...)
x |
A tridim_transformation object |
convert_euclidean |
Whether to convert matrix coefficients to scale(phi) and rotation(theta). Defaults to |
... |
Extra parameters to be passed to |
A ggplot object produced by bayesplot::mcmc_intervals()
euc2 <- fit_transformation(depV1+depV2~indepV1+indepV2, data = NakayaData, transformation = 'euclidean') plot(euc2) # same but for converted coefficients plot(euc2, convert_euclidean=TRUE)
euc2 <- fit_transformation(depV1+depV2~indepV1+indepV2, data = NakayaData, transformation = 'euclidean') plot(euc2) # same but for converted coefficients plot(euc2, convert_euclidean=TRUE)
Predicted values based on the bi/tridimensional regression model object.
## S3 method for class 'tridim_transformation' predict(object, newdata = NULL, summary = TRUE, probs = NULL, ...)
## S3 method for class 'tridim_transformation' predict(object, newdata = NULL, summary = TRUE, probs = NULL, ...)
object |
An object of class tridim_transformation |
newdata |
An optional two column data frame with independent variables. If omitted, the fitted values are used. |
summary |
Whether summary statistics should be returned instead of
raw sample values. Defaults to |
probs |
The percentiles used to compute summary, defaults to NULL (no CI). |
... |
Unused |
If summary=FALSE, a numeric matrix iterationsN x observationsN x variablesN. If summary=TRUE, a data.frame with columns "dvindex" with mean for each dependent variable plus optional quantiles columns with names "dvindex_quantile".
euc2 <- fit_transformation(depV1+depV2~indepV1+indepV2, NakayaData, transformation = 'euclidean') # prediction summary predictions <- predict(euc2) # full posterior prediction samples predictions <- predict(euc2, summary=FALSE)
euc2 <- fit_transformation(depV1+depV2~indepV1+indepV2, NakayaData, transformation = 'euclidean') # prediction summary predictions <- predict(euc2) # full posterior prediction samples predictions <- predict(euc2, summary=FALSE)
Prints out tridim_transformation object
## S3 method for class 'tridim_transformation' print(x, ...)
## S3 method for class 'tridim_transformation' print(x, ...)
x |
A tridim_transformation object |
... |
Unused |
Nothing, console output only.
euc2 <- fit_transformation(depV1+depV2~indepV1+indepV2, data = NakayaData, transformation = 'euclidean') euc2
euc2 <- fit_transformation(depV1+depV2~indepV1+indepV2, data = NakayaData, transformation = 'euclidean') euc2
Computes R-squared using Bayesian R-squared approach. For detail refer to: Andrew Gelman, Ben Goodrich, Jonah Gabry, and Aki Vehtari (2018). R-squared for Bayesian regression models. The American Statistician, doi:10.1080/00031305.2018.1549100.
## S3 method for class 'tridim_transformation' R2(object, summary = TRUE, probs = c(0.055, 0.945), ...)
## S3 method for class 'tridim_transformation' R2(object, summary = TRUE, probs = c(0.055, 0.945), ...)
object |
An object of class tridim_transformation |
summary |
Whether summary statistics should be returned instead of
raw sample values. Defaults to |
probs |
The percentiles used to compute summary, defaults to 89% credible interval. |
... |
Unused. |
vector of values or a data.frame with summary
euc2 <- fit_transformation(depV1+depV2~indepV1+indepV2, NakayaData, transformation = 'euclidean') R2(euc2)
euc2 <- fit_transformation(depV1+depV2~indepV1+indepV2, NakayaData, transformation = 'euclidean') R2(euc2)
Summary for a tridim_transformation object
## S3 method for class 'tridim_transformation' summary(object, ...)
## S3 method for class 'tridim_transformation' summary(object, ...)
object |
A tridim_transformation object |
... |
Unused |
Nothing, console output only.
euc2 <- fit_transformation(depV1+depV2~indepV1+indepV2, data = NakayaData, transformation = 'euclidean') summary(euc2)
euc2 <- fit_transformation(depV1+depV2~indepV1+indepV2, data = NakayaData, transformation = 'euclidean') summary(euc2)
tridim_transformation
.Geometric transformations fitted with the
fit_transformation
function
represented as a tridim_transformation
object with information about transformation, data dimension,
call formula, and fitted stanfit
object,
See methods(class = "tridim_transformation")
for an overview of available methods.
transformation
A string
with the transformation name.
formula
A formula
object.
Ndim
An integer
with data dimension, either 2
or 3
.
data
A list
containing variables used for the sampling
.
stanmodel
A stanmodel
used for sampling.
stanfit
a stanfit
object.
Computes widely applicable information criterion via loo library. It can be used for a model comparison via loo::loo_compare() function.
## S3 method for class 'tridim_transformation' waic(x, ...)
## S3 method for class 'tridim_transformation' waic(x, ...)
x |
A [tridim_transformation[ |
... |
unused |
A named list, see loo::waic()
for details.
euc2 <- fit_transformation(depV1+depV2~indepV1+indepV2, NakayaData, transformation = 'euclidean') aff2 <- fit_transformation(depV1+depV2~indepV1+indepV2, NakayaData, transformation = 'affine') loo::loo_compare(waic(euc2), waic(aff2))
euc2 <- fit_transformation(depV1+depV2~indepV1+indepV2, NakayaData, transformation = 'euclidean') aff2 <- fit_transformation(depV1+depV2~indepV1+indepV2, NakayaData, transformation = 'affine') loo::loo_compare(waic(euc2), waic(aff2))