Skip to contents

Utilities to generate data with non-constant variance patterns. simulate_hetero simulates a linear regression model with heteroscedastic errors using a user-supplied variance function. The helper functions prefixed by sigma_ implement common variance patterns and can be passed to sigma_func. simulate_arch1 creates a simple ARCH(1) time series.

Usage

simulate_hetero(n, beta0, beta1, sigma_func, seed = NULL)

simulate_arch1(n, mu = 0, alpha0 = 0.5, alpha1 = 0.3, seed = NULL)

sigma_linear(x)

sigma_exponential(x)

sigma_group(x)

sigma_piecewise(x)

sigma_poly(x, a = 0.5, b = 0.1, c = 0.02)

sigma_sin(x, A = 1, B = 0.5, omega = 2 * pi/10, phi = 0)

sigma_multiplicative(x, mu_func, p = 1)

sigma_spatial(coords, gamma0 = 0.5, gamma1 = 0.1)

sigma_logistic(x, L = 2, k = 1, x0 = 5)

sigma_inverse(x, a = 1, b = 1)

sigma_power(x, a = 0.5, p = 0.5)

sigma_step(x, thr = 5, low = 0.5, high = 2)

sigma_u_shape(x, a = 0.1, b = 0.05, center = 5)

sigma_exp_decay(x, a = 2, b = 0.2)

sigma_gaussian_peak(x, base = 0.5, height = 1, mu = 5, sd = 1)

sigma_piecewise_linear(x, thr = 5, slope1 = 0.1, slope2 = 0.3)

Arguments

n

Number of observations for simulate_hetero, or the length of the series for simulate_arch1.

beta0

Intercept of the regression.

beta1

Slope of the regression.

sigma_func

Function returning the standard deviation for each x.

seed

Optional integer seed for reproducibility.

mu

Mean of the ARCH(1) process for simulate_arch1, and the peak location for sigma_gaussian_peak.

alpha0

Constant term in the ARCH variance equation.

alpha1

ARCH coefficient (must be in [0,1)).

x

Numeric vector used by the sigma_* functions.

coords

Matrix or data frame with columns x and y for sigma_spatial.

mu_func

Function returning mean values for sigma_multiplicative.

p

Exponent for sigma_multiplicative or sigma_power.

a,b,c,A,B,omega,phi,L,k,x0,thr,low,high,center,base,height,sd,slope1,slope2,gamma0,gamma1

Additional numeric parameters controlling the shape of the variance patterns.

Value

For simulate_hetero a data frame with columns x and y. For simulate_arch1 a data frame with columns time, y and sigma. The sigma_* helpers return numeric vectors of standard deviations.

Examples

set.seed(1)
hetero <- simulate_hetero(200, 1, 2, sigma_linear)
head(hetero)
#>          x         y
#> 1 2.655087  5.670564
#> 2 3.721239  8.494881
#> 3 5.728534 10.957957
#> 4 9.082078 19.530216
#> 5 2.016819  4.442311
#> 6 8.983897 23.026863

# Other variance patterns
simulate_hetero(100, 0, 1, sigma_logistic)
#>             x          y
#> 1   8.1425175 10.9682805
#> 2   9.2877723 10.6239561
#> 3   1.4748105  1.4965473
#> 4   7.4982166  7.1419187
#> 5   9.7565735 12.8854642
#> 6   9.7479246 10.9301434
#> 7   3.5062557  3.0758806
#> 8   3.9394906  3.8594243
#> 9   9.5095101  5.7134616
#> 10  1.0664832  1.0589857
#> 11  9.3476012  4.2291672
#> 12  3.4616210  3.9261761
#> 13  5.3306061  4.5909524
#> 14  5.3879430  4.8755910
#> 15  7.1471795  6.8439626
#> 16  4.0579050  4.4013317
#> 17  1.5278814  1.5687392
#> 18  3.4023276  3.5935084
#> 19  6.2665485  5.3732087
#> 20  0.5737268  0.5415050
#> 21  8.5166764  7.7616545
#> 22  2.1264535  2.1561774
#> 23  5.3946203  4.4112121
#> 24  1.3648759  1.3613371
#> 25  3.2486514  2.9033102
#> 26  6.2107629  6.1979598
#> 27  2.5598225  2.5804805
#> 28  6.3487580  6.1171289
#> 29  4.8567211  4.7045326
#> 30  9.3817692 12.8653176
#> 31  8.5750154 10.0586251
#> 32  3.7088354  4.1882084
#> 33  3.1420183  2.8928660
#> 34  8.2853436  8.6021692
#> 35  4.5184151  5.4004197
#> 36  3.1587841  3.1433077
#> 37  0.9780854  0.9031183
#> 38  0.6490054  0.6577850
#> 39  6.8945737  3.5827090
#> 40  6.6805060  5.3129194
#> 41  9.0454665 11.6479280
#> 42  3.0169327  3.1659073
#> 43  9.3280870 11.4829958
#> 44  2.0198302  2.0494665
#> 45  7.9237876  7.7147040
#> 46  2.2463051  2.1356125
#> 47  0.3075657  0.3364966
#> 48  8.6203404  8.7080143
#> 49  6.8510751  5.6149709
#> 50  9.4207491 11.1306336
#> 51  6.7585376  8.5915906
#> 52  8.4312015 12.1037125
#> 53  3.6189418  3.3767310
#> 54  3.9236589  3.7249424
#> 55  5.6768740  5.1249297
#> 56  0.9515213  0.9386365
#> 57  1.9378407  1.9050673
#> 58  5.8806639  5.4625998
#> 59  7.5150417 10.1829562
#> 60  8.6723879  7.3118893
#> 61  3.7179574  3.5493353
#> 62  7.9881455  9.2306215
#> 63  0.5831439  0.6099768
#> 64  6.2343571  5.0382335
#> 65  3.5664141  3.3707596
#> 66  5.8792793  6.6193362
#> 67  9.1378464 11.1413841
#> 68  1.9944218  1.9707247
#> 69  3.6908362  3.0827263
#> 70  6.7140833  9.6105825
#> 71  7.6814455 10.3676744
#> 72  5.2224828  4.4334133
#> 73  8.2807502  8.1553308
#> 74  5.2709619  3.2745665
#> 75  5.0175483  5.5922699
#> 76  4.1997332  5.1992865
#> 77  3.6229828  2.9631964
#> 78  1.2342891  1.1990100
#> 79  2.9816153  2.8312130
#> 80  2.7667649  2.6348905
#> 81  7.7022536  3.8912278
#> 82  7.7818130  8.7253128
#> 83  1.4378728  1.3533320
#> 84  5.1552615  5.1283272
#> 85  5.9724049  6.8329426
#> 86  5.0584302  4.8544462
#> 87  3.8609963  4.2936264
#> 88  4.2609810  4.2443578
#> 89  0.1175973  0.1078542
#> 90  9.1933177 10.4668117
#> 91  0.7943969  0.7816486
#> 92  5.0737425  6.9116826
#> 93  8.2017162  8.1666247
#> 94  5.9839542  7.2254688
#> 95  4.2415353  4.3724209
#> 96  5.5931027  1.7182745
#> 97  7.8909447  5.3024441
#> 98  1.6771526  1.6476388
#> 99  9.7045173 10.1738749
#> 100 4.7350310  2.7008791
simulate_arch1(50)
#>    time            y     sigma
#> 1     1  0.812782001 0.8451543
#> 2     2 -0.505042596 0.8355743
#> 3     3 -0.571651745 0.7592894
#> 4     4 -1.202997518 0.7733277
#> 5     5 -1.405217374 0.9665200
#> 6     6  0.058876626 1.0451750
#> 7     7  0.360552930 0.7078417
#> 8     8 -1.540194068 0.7341659
#> 9     9 -1.105555449 1.1007540
#> 10   10  0.498778973 0.9309543
#> 11   11 -0.343423070 0.7580463
#> 12   12  1.584395592 0.7316979
#> 13   13  1.394509112 1.1194163
#> 14   14  0.619832132 1.0408634
#> 15   15  0.003831282 0.7843836
#> 16   16  0.197538773 0.7071099
#> 17   17 -0.504960497 0.7153366
#> 18   18  0.476836485 0.7592730
#> 19   19  1.115782799 0.7537983
#> 20   20  1.012581834 0.9346076
#> 21   21 -0.730833159 0.8986638
#> 22   22 -1.315416022 0.8125485
#> 23   23 -0.110697730 1.0095027
#> 24   24  0.312899841 0.7097015
#> 25   25  0.982955491 0.7275795
#> 26   26 -1.171902226 0.8887409
#> 27   27  0.347983747 0.9549903
#> 28   28  0.171002215 0.7323440
#> 29   29  0.851627898 0.7132829
#> 30   30 -0.023642580 0.8471015
#> 31   31 -0.252690806 0.7072253
#> 32   32 -0.826307871 0.7205247
#> 33   33 -0.434397657 0.8395448
#> 34   34 -0.270167245 0.7460633
#> 35   35  1.698097942 0.7224245
#> 36   36  2.858425102 1.1683582
#> 37   37 -0.286379458 1.7178994
#> 38   38 -0.755923620 0.7242955
#> 39   39 -1.616634678 0.8194060
#> 40   40  0.583205471 1.1331603
#> 41   41 -0.846188537 0.7759115
#> 42   42  1.931598748 0.8454647
#> 43   43 -1.126971281 1.2725259
#> 44   44  0.104287414 0.9386263
#> 45   45  2.703048948 0.7094101
#> 46   46 -1.819404041 1.6407139
#> 47   47  0.375819398 1.2219122
#> 48   48 -0.815182552 0.7364591
#> 49   49  0.290734243 0.8362755
#> 50   50 -0.632955918 0.7248158