rrandomnoisejittermultilevel-analysis

Adding random noise to a group level variable


I'm performing multilevel analysis and, to check for statistical artifacts, I want to create new group level variables adding random noise to some group level variables (like school-level socioeconomic composition).

In R, the jitter() function allows to add random noise to a column easily:

jittered_variable <- jitter(variable)

However, this function add different values to a group level variable within groups.

Is there any way in R to add the same random noise to a group level variable within groups? For example, +0.5 for all the members of group 1, -0.3 for all the members of group 2, and so on.


Solution

  • Here is a way.
    Create a named vector with as many random values as there are groups. The names are the values that define the groups. Then add the random values matching the data with those names.

    df1 <- iris[4:5]
    
    set.seed(2023)
    ngroups <- length(unique(df1$Species))
    noise <- jitter(rep(0, ngroups))
    noise <- setNames(noise, unique(df1$Species))
    noise
    #>       setosa   versicolor    virginica 
    #> -0.001335442 -0.006592362 -0.013487298
    
    df1$newcol <- df1$Petal.Width + noise[match(df1$Species, names(noise))]
    
    # these differences are the random noise values
    df1$newcol - df1$Petal.Width
    #>   [1] -0.001335442 -0.001335442 -0.001335442 -0.001335442 -0.001335442
    #>   [6] -0.001335442 -0.001335442 -0.001335442 -0.001335442 -0.001335442
    #>  [11] -0.001335442 -0.001335442 -0.001335442 -0.001335442 -0.001335442
    #>  [16] -0.001335442 -0.001335442 -0.001335442 -0.001335442 -0.001335442
    #>  [21] -0.001335442 -0.001335442 -0.001335442 -0.001335442 -0.001335442
    #>  [26] -0.001335442 -0.001335442 -0.001335442 -0.001335442 -0.001335442
    #>  [31] -0.001335442 -0.001335442 -0.001335442 -0.001335442 -0.001335442
    #>  [36] -0.001335442 -0.001335442 -0.001335442 -0.001335442 -0.001335442
    #>  [41] -0.001335442 -0.001335442 -0.001335442 -0.001335442 -0.001335442
    #>  [46] -0.001335442 -0.001335442 -0.001335442 -0.001335442 -0.001335442
    #>  [51] -0.006592362 -0.006592362 -0.006592362 -0.006592362 -0.006592362
    #>  [56] -0.006592362 -0.006592362 -0.006592362 -0.006592362 -0.006592362
    #>  [61] -0.006592362 -0.006592362 -0.006592362 -0.006592362 -0.006592362
    #>  [66] -0.006592362 -0.006592362 -0.006592362 -0.006592362 -0.006592362
    #>  [71] -0.006592362 -0.006592362 -0.006592362 -0.006592362 -0.006592362
    #>  [76] -0.006592362 -0.006592362 -0.006592362 -0.006592362 -0.006592362
    #>  [81] -0.006592362 -0.006592362 -0.006592362 -0.006592362 -0.006592362
    #>  [86] -0.006592362 -0.006592362 -0.006592362 -0.006592362 -0.006592362
    #>  [91] -0.006592362 -0.006592362 -0.006592362 -0.006592362 -0.006592362
    #>  [96] -0.006592362 -0.006592362 -0.006592362 -0.006592362 -0.006592362
    #> [101] -0.013487298 -0.013487298 -0.013487298 -0.013487298 -0.013487298
    #> [106] -0.013487298 -0.013487298 -0.013487298 -0.013487298 -0.013487298
    #> [111] -0.013487298 -0.013487298 -0.013487298 -0.013487298 -0.013487298
    #> [116] -0.013487298 -0.013487298 -0.013487298 -0.013487298 -0.013487298
    #> [121] -0.013487298 -0.013487298 -0.013487298 -0.013487298 -0.013487298
    #> [126] -0.013487298 -0.013487298 -0.013487298 -0.013487298 -0.013487298
    #> [131] -0.013487298 -0.013487298 -0.013487298 -0.013487298 -0.013487298
    #> [136] -0.013487298 -0.013487298 -0.013487298 -0.013487298 -0.013487298
    #> [141] -0.013487298 -0.013487298 -0.013487298 -0.013487298 -0.013487298
    #> [146] -0.013487298 -0.013487298 -0.013487298 -0.013487298 -0.013487298
    

    Created on 2023-04-02 with reprex v2.0.2