Skip to contents

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")

Usage

# S3 method for formula
fit_transformation(
  formula,
  data,
  transformation,
  priors = NULL,
  chains = 1,
  cores = NULL,
  ...
)

Arguments

formula

a symbolic description of the model to be fitted in the format Xdep + Ydep ~ Xind + Yind, where Xdep and Ydep are dependent and Xind and Yind are independent variables

data

a data frame containing variables for the model.

transformation

the transformation to be used: "translation" (both 2D and 3D), "euclidean" (2D), "euclidean_x", "euclidean_y", "euclidean_z" (3D, rotation about, respectively, x, y, and z axis), "affine" (2D and 3D), or "projective" (2D and 3D).

priors

named list of parameters for prior distributions of parameters a (translation, normal distribution), b (all other parameters, normal distribution), and sigma (residual variance, exponential). E.g., list("a" = c(0, 10), "b"= c(0, 1), "sigma"=1). Default priors are "a" = c(0, max_absolute_difference_in_means(d, iv)) / 2), "b" = c(0, max_absolute_difference_in_means(d, iv)) / 2), "sigma" = 1 * sd(dv).

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 sampling function.

Value

A tridim_transformation object

Examples

# Geometric transformations of 2D data
euc2 <- fit_transformation(depV1 + depV2 ~ indepV1 + indepV2,
                                     NakayaData, 'euclidean')
#> 
#> SAMPLING FOR MODEL 'tridim_transformation' NOW (CHAIN 1).
#> Chain 1: 
#> Chain 1: Gradient evaluation took 0 seconds
#> Chain 1: 1000 transitions using 10 leapfrog steps per transition would take 0 seconds.
#> Chain 1: Adjust your expectations accordingly!
#> Chain 1: 
#> Chain 1: 
#> Chain 1: Iteration:    1 / 2000 [  0%]  (Warmup)
#> Chain 1: Iteration:  200 / 2000 [ 10%]  (Warmup)
#> Chain 1: Iteration:  400 / 2000 [ 20%]  (Warmup)
#> Chain 1: Iteration:  600 / 2000 [ 30%]  (Warmup)
#> Chain 1: Iteration:  800 / 2000 [ 40%]  (Warmup)
#> Chain 1: Iteration: 1000 / 2000 [ 50%]  (Warmup)
#> Chain 1: Iteration: 1001 / 2000 [ 50%]  (Sampling)
#> Chain 1: Iteration: 1200 / 2000 [ 60%]  (Sampling)
#> Chain 1: Iteration: 1400 / 2000 [ 70%]  (Sampling)
#> Chain 1: Iteration: 1600 / 2000 [ 80%]  (Sampling)
#> Chain 1: Iteration: 1800 / 2000 [ 90%]  (Sampling)
#> Chain 1: Iteration: 2000 / 2000 [100%]  (Sampling)
#> Chain 1: 
#> Chain 1:  Elapsed Time: 0.108 seconds (Warm-up)
#> Chain 1:                0.103 seconds (Sampling)
#> Chain 1:                0.211 seconds (Total)
#> Chain 1: 
aff2 <- fit_transformation(depV1 + depV2 ~ indepV1 + indepV2,
                                     NakayaData, 'affine')
#> 
#> SAMPLING FOR MODEL 'tridim_transformation' NOW (CHAIN 1).
#> Chain 1: 
#> Chain 1: Gradient evaluation took 0 seconds
#> Chain 1: 1000 transitions using 10 leapfrog steps per transition would take 0 seconds.
#> Chain 1: Adjust your expectations accordingly!
#> Chain 1: 
#> Chain 1: 
#> Chain 1: Iteration:    1 / 2000 [  0%]  (Warmup)
#> Chain 1: Iteration:  200 / 2000 [ 10%]  (Warmup)
#> Chain 1: Iteration:  400 / 2000 [ 20%]  (Warmup)
#> Chain 1: Iteration:  600 / 2000 [ 30%]  (Warmup)
#> Chain 1: Iteration:  800 / 2000 [ 40%]  (Warmup)
#> Chain 1: Iteration: 1000 / 2000 [ 50%]  (Warmup)
#> Chain 1: Iteration: 1001 / 2000 [ 50%]  (Sampling)
#> Chain 1: Iteration: 1200 / 2000 [ 60%]  (Sampling)
#> Chain 1: Iteration: 1400 / 2000 [ 70%]  (Sampling)
#> Chain 1: Iteration: 1600 / 2000 [ 80%]  (Sampling)
#> Chain 1: Iteration: 1800 / 2000 [ 90%]  (Sampling)
#> Chain 1: Iteration: 2000 / 2000 [100%]  (Sampling)
#> Chain 1: 
#> Chain 1:  Elapsed Time: 0.114 seconds (Warm-up)
#> Chain 1:                0.103 seconds (Sampling)
#> Chain 1:                0.217 seconds (Total)
#> Chain 1: 
prj2 <- fit_transformation(depV1 + depV2 ~ indepV1 + indepV2,
                                     NakayaData, 'projective')
#> 
#> SAMPLING FOR MODEL 'tridim_transformation' NOW (CHAIN 1).
#> Chain 1: 
#> Chain 1: Gradient evaluation took 0 seconds
#> Chain 1: 1000 transitions using 10 leapfrog steps per transition would take 0 seconds.
#> Chain 1: Adjust your expectations accordingly!
#> Chain 1: 
#> Chain 1: 
#> Chain 1: Iteration:    1 / 2000 [  0%]  (Warmup)
#> Chain 1: Iteration:  200 / 2000 [ 10%]  (Warmup)
#> Chain 1: Iteration:  400 / 2000 [ 20%]  (Warmup)
#> Chain 1: Iteration:  600 / 2000 [ 30%]  (Warmup)
#> Chain 1: Iteration:  800 / 2000 [ 40%]  (Warmup)
#> Chain 1: Iteration: 1000 / 2000 [ 50%]  (Warmup)
#> Chain 1: Iteration: 1001 / 2000 [ 50%]  (Sampling)
#> Chain 1: Iteration: 1200 / 2000 [ 60%]  (Sampling)
#> Chain 1: Iteration: 1400 / 2000 [ 70%]  (Sampling)
#> Chain 1: Iteration: 1600 / 2000 [ 80%]  (Sampling)
#> Chain 1: Iteration: 1800 / 2000 [ 90%]  (Sampling)
#> Chain 1: Iteration: 2000 / 2000 [100%]  (Sampling)
#> Chain 1: 
#> Chain 1:  Elapsed Time: 0.158 seconds (Warm-up)
#> Chain 1:                0.107 seconds (Sampling)
#> Chain 1:                0.265 seconds (Total)
#> Chain 1: 

# summary of transformation coefficients
coef(euc2)
#>   Coef        Mean         5.5       94.5
#> 1   a1  0.14338042 -0.01984441  0.3029228
#> 2   a2 -0.01037823 -0.17086048  0.1457954
#> 3   b1  1.34308942  1.22779745  1.4586909
#> 4   b2 -0.56907357 -0.66992024 -0.4636741

# statistical comparison via WAIC criterion
loo::loo_compare(waic(euc2), waic(aff2), waic(prj2))
#> Warning: 
#> 17 (89.5%) p_waic estimates greater than 0.4. We recommend trying loo instead.
#> Warning: 
#> 18 (94.7%) p_waic estimates greater than 0.4. We recommend trying loo instead.
#> Warning: 
#> 18 (94.7%) p_waic estimates greater than 0.4. We recommend trying loo instead.
#>        elpd_diff se_diff
#> model1     0.0       0.0
#> model2 -4462.9    2401.7
#> model3 -5202.1    2759.6