Skip to contents

Compute asymmetry in weighted probability density

Usage

density_asymmetry(
  dt,
  circ_space = 180,
  weights_sd = 10,
  kernel_bw = NULL,
  xvar = "abs_td_dist",
  yvar = "bias_to_distr_corr",
  by = c(),
  n = 181,
  average = T,
  return_full_density = F
)

Arguments

dt

data.table with the data

circ_space

circular space (180 or 360)

weights_sd

standard deviation of the Gaussian windiw to the use across xvar

kernel_bw

bandwidth for the kernel density estimator across yvar

xvar

x-axis variable (normally, dissimilarity)

yvar

y-axis variable (normally, errors)

by

a vector of grouping variables names

n

the number of steps for the x-axis variable at which the density is computed

average

average the asymmetry for each x-value (default: T)

return_full_density

returns the full data.table with density computed at each point (default: F)

Value

data.table with several variables

Examples


library(data.table)
data(Pascucci_et_al_2019_data)
ex_data <- Pascucci_et_al_2019_data
ex_data[, err := angle_diff_180(reported, orientation)] # response errors
#>       observer orientation reported        rt   err
#>          <int>       <int>    <int>     <num> <num>
#>    1:        1         135      137 1.0829786     2
#>    2:        1          65       56 0.9887931    -9
#>    3:        1          61       65 1.5067748     4
#>    4:        1          27       25 1.9070205    -2
#>    5:        1          22       20 2.0247443    -2
#>   ---                                              
#> 4396:       10          35       26 1.7775651    -9
#> 4397:       10         141      135 2.0365374    -6
#> 4398:       10         178      163 1.1301296   -15
#> 4399:       10         168      168 1.3772832     0
#> 4400:       10          24       28 2.3897599     4
ex_data[, prev_ori := shift(orientation), by = observer] # orientation on previous trial
#>       observer orientation reported        rt   err prev_ori
#>          <int>       <int>    <int>     <num> <num>    <int>
#>    1:        1         135      137 1.0829786     2       NA
#>    2:        1          65       56 0.9887931    -9      135
#>    3:        1          61       65 1.5067748     4       65
#>    4:        1          27       25 1.9070205    -2       61
#>    5:        1          22       20 2.0247443    -2       27
#>   ---                                                       
#> 4396:       10          35       26 1.7775651    -9       68
#> 4397:       10         141      135 2.0365374    -6       35
#> 4398:       10         178      163 1.1301296   -15      141
#> 4399:       10         168      168 1.3772832     0      178
#> 4400:       10          24       28 2.3897599     4      168

# determine the shift in orientations between trials
ex_data[, diff_in_ori := angle_diff_180(prev_ori, orientation)]
#>       observer orientation reported        rt   err prev_ori diff_in_ori
#>          <int>       <int>    <int>     <num> <num>    <int>       <num>
#>    1:        1         135      137 1.0829786     2       NA          NA
#>    2:        1          65       56 0.9887931    -9      135          70
#>    3:        1          61       65 1.5067748     4       65           4
#>    4:        1          27       25 1.9070205    -2       61          34
#>    5:        1          22       20 2.0247443    -2       27           5
#>   ---                                                                   
#> 4396:       10          35       26 1.7775651    -9       68          33
#> 4397:       10         141      135 2.0365374    -6       35          74
#> 4398:       10         178      163 1.1301296   -15      141         -37
#> 4399:       10         168      168 1.3772832     0      178          10
#> 4400:       10          24       28 2.3897599     4      168         -36
ex_data[, abs_diff_in_ori := abs(diff_in_ori)]
#>       observer orientation reported        rt   err prev_ori diff_in_ori
#>          <int>       <int>    <int>     <num> <num>    <int>       <num>
#>    1:        1         135      137 1.0829786     2       NA          NA
#>    2:        1          65       56 0.9887931    -9      135          70
#>    3:        1          61       65 1.5067748     4       65           4
#>    4:        1          27       25 1.9070205    -2       61          34
#>    5:        1          22       20 2.0247443    -2       27           5
#>   ---                                                                   
#> 4396:       10          35       26 1.7775651    -9       68          33
#> 4397:       10         141      135 2.0365374    -6       35          74
#> 4398:       10         178      163 1.1301296   -15      141         -37
#> 4399:       10         168      168 1.3772832     0      178          10
#> 4400:       10          24       28 2.3897599     4      168         -36
#>       abs_diff_in_ori
#>                 <num>
#>    1:              NA
#>    2:              70
#>    3:               4
#>    4:              34
#>    5:               5
#>   ---                
#> 4396:              33
#> 4397:              74
#> 4398:              37
#> 4399:              10
#> 4400:              36
ex_data[, err_rel_to_prev_targ := ifelse(diff_in_ori < 0, -err, err)]
#>       observer orientation reported        rt   err prev_ori diff_in_ori
#>          <int>       <int>    <int>     <num> <num>    <int>       <num>
#>    1:        1         135      137 1.0829786     2       NA          NA
#>    2:        1          65       56 0.9887931    -9      135          70
#>    3:        1          61       65 1.5067748     4       65           4
#>    4:        1          27       25 1.9070205    -2       61          34
#>    5:        1          22       20 2.0247443    -2       27           5
#>   ---                                                                   
#> 4396:       10          35       26 1.7775651    -9       68          33
#> 4397:       10         141      135 2.0365374    -6       35          74
#> 4398:       10         178      163 1.1301296   -15      141         -37
#> 4399:       10         168      168 1.3772832     0      178          10
#> 4400:       10          24       28 2.3897599     4      168         -36
#>       abs_diff_in_ori err_rel_to_prev_targ
#>                 <num>                <num>
#>    1:              NA                   NA
#>    2:              70                   -9
#>    3:               4                    4
#>    4:              34                   -2
#>    5:               5                   -2
#>   ---                                     
#> 4396:              33                   -9
#> 4397:              74                   -6
#> 4398:              37                   15
#> 4399:              10                    0
#> 4400:              36                   -4

err_dens <- density_asymmetry(ex_data[!is.na(err_rel_to_prev_targ)],
  circ_space = 180, weights_sd = 10, xvar = "abs_diff_in_ori",
  yvar = "err_rel_to_prev_targ", by = c("observer")
)

ggplot(err_dens, aes(x = dist, y = delta)) +
  geom_line(stat = "summary", fun = mean) +
  labs(y = "Asymmetry in error probability density, %", x = "Absolute orientation difference, °")