Skip to contents

Simulate data with specific characteristics.

Usage

simulate_correlation(n = 100, r = 0.5, mean = 0, sd = 1, names = NULL, ...)

simulate_ttest(n = 100, d = 0.5, names = NULL, ...)

simulate_difference(n = 100, d = 0.5, names = NULL, ...)

Arguments

n

The number of observations to be generated.

r

A value or vector corresponding to the desired correlation coefficients.

mean

A value or vector corresponding to the mean of the variables.

sd

A value or vector corresponding to the SD of the variables.

names

A character vector of desired variable names.

...

Arguments passed to or from other methods.

d

A value or vector corresponding to the desired difference between the groups.

Examples


# Correlation --------------------------------
data <- simulate_correlation(r = 0.5)
plot(data$V1, data$V2)

cor.test(data$V1, data$V2)
#> 
#> 	Pearson's product-moment correlation
#> 
#> data:  data$V1 and data$V2
#> t = 5.7155, df = 98, p-value = 1.18e-07
#> alternative hypothesis: true correlation is not equal to 0
#> 95 percent confidence interval:
#>  0.3366433 0.6341398
#> sample estimates:
#> cor 
#> 0.5 
#> 
summary(lm(V2 ~ V1, data = data))
#> 
#> Call:
#> lm(formula = V2 ~ V1, data = data)
#> 
#> Residuals:
#>      Min       1Q   Median       3Q      Max 
#> -1.94526 -0.58306 -0.01274  0.58380  2.18030 
#> 
#> Coefficients:
#>             Estimate Std. Error t value Pr(>|t|)    
#> (Intercept)  0.00000    0.08704   0.000        1    
#> V1           0.50000    0.08748   5.715 1.18e-07 ***
#> ---
#> Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
#> 
#> Residual standard error: 0.8704 on 98 degrees of freedom
#> Multiple R-squared:   0.25,	Adjusted R-squared:  0.2423 
#> F-statistic: 32.67 on 1 and 98 DF,  p-value: 1.18e-07
#> 

# Specify mean and SD
data <- simulate_correlation(r = 0.5, n = 50, mean = c(0, 1), sd = c(0.7, 1.7))
cor.test(data$V1, data$V2)
#> 
#> 	Pearson's product-moment correlation
#> 
#> data:  data$V1 and data$V2
#> t = 4, df = 48, p-value = 0.000218
#> alternative hypothesis: true correlation is not equal to 0
#> 95 percent confidence interval:
#>  0.2574879 0.6832563
#> sample estimates:
#> cor 
#> 0.5 
#> 
round(c(mean(data$V1), sd(data$V1)), 1)
#> [1] 0.0 0.7
round(c(mean(data$V2), sd(data$V2)), 1)
#> [1] 1.0 1.7
summary(lm(V2 ~ V1, data = data))
#> 
#> Call:
#> lm(formula = V2 ~ V1, data = data)
#> 
#> Residuals:
#>     Min      1Q  Median      3Q     Max 
#> -3.4171 -0.5559  0.0277  0.9316  3.6164 
#> 
#> Coefficients:
#>             Estimate Std. Error t value Pr(>|t|)    
#> (Intercept)   1.0000     0.2104   4.754 1.86e-05 ***
#> V1            1.2143     0.3036   4.000 0.000218 ***
#> ---
#> Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
#> 
#> Residual standard error: 1.487 on 48 degrees of freedom
#> Multiple R-squared:   0.25,	Adjusted R-squared:  0.2344 
#> F-statistic:    16 on 1 and 48 DF,  p-value: 0.000218
#> 

# Generate multiple variables
cor_matrix <- matrix(c(
  1.0, 0.2, 0.4,
  0.2, 1.0, 0.3,
  0.4, 0.3, 1.0
),
nrow = 3
)

data <- simulate_correlation(r = cor_matrix, names = c("y", "x1", "x2"))
cor(data)
#>      y  x1  x2
#> y  1.0 0.2 0.4
#> x1 0.2 1.0 0.3
#> x2 0.4 0.3 1.0
summary(lm(y ~ x1, data = data))
#> 
#> Call:
#> lm(formula = y ~ x1, data = data)
#> 
#> Residuals:
#>      Min       1Q   Median       3Q      Max 
#> -2.51808 -0.53538 -0.02381  0.70861  2.87107 
#> 
#> Coefficients:
#>              Estimate Std. Error t value Pr(>|t|)  
#> (Intercept) 8.216e-17  9.848e-02   0.000    1.000  
#> x1          2.000e-01  9.897e-02   2.021    0.046 *
#> ---
#> Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
#> 
#> Residual standard error: 0.9848 on 98 degrees of freedom
#> Multiple R-squared:   0.04,	Adjusted R-squared:  0.0302 
#> F-statistic: 4.083 on 1 and 98 DF,  p-value: 0.04604
#> 

# t-test --------------------------------
data <- simulate_ttest(n = 30, d = 0.3)
plot(data$V1, data$V0)

round(c(mean(data$V1), sd(data$V1)), 1)
#> [1] 0 1
diff(t.test(data$V1 ~ data$V0)$estimate)
#> mean in group 1 
#>       0.1358166 
summary(lm(V1 ~ V0, data = data))
#> 
#> Call:
#> lm(formula = V1 ~ V0, data = data)
#> 
#> Residuals:
#>      Min       1Q   Median       3Q      Max 
#> -2.06466 -0.72154 -0.00453  0.69239  2.05561 
#> 
#> Coefficients:
#>             Estimate Std. Error t value Pr(>|t|)
#> (Intercept) -0.06338    0.25276  -0.251    0.804
#> V01          0.13582    0.37000   0.367    0.716
#> 
#> Residual standard error: 1.011 on 28 degrees of freedom
#> Multiple R-squared:  0.004789,	Adjusted R-squared:  -0.03075 
#> F-statistic: 0.1347 on 1 and 28 DF,  p-value: 0.7163
#> 
summary(glm(V0 ~ V1, data = data, family = "binomial"))
#> 
#> Call:
#> glm(formula = V0 ~ V1, family = "binomial", data = data)
#> 
#> Deviance Residuals: 
#>    Min      1Q  Median      3Q     Max  
#> -1.220  -1.134  -1.012   1.231   1.309  
#> 
#> Coefficients:
#>             Estimate Std. Error z value Pr(>|z|)
#> (Intercept)  -0.1342     0.3669  -0.366    0.715
#> V1            0.1423     0.3761   0.378    0.705
#> 
#> (Dispersion parameter for binomial family taken to be 1)
#> 
#>     Null deviance: 41.455  on 29  degrees of freedom
#> Residual deviance: 41.311  on 28  degrees of freedom
#> AIC: 45.311
#> 
#> Number of Fisher Scoring iterations: 3
#> 

# Difference --------------------------------
data <- simulate_difference(n = 30, d = 0.3)
plot(data$V1, data$V0)

round(c(mean(data$V1), sd(data$V1)), 1)
#> [1] 0 1
diff(t.test(data$V1 ~ data$V0)$estimate)
#> mean in group 1 
#>             0.3 
summary(lm(V1 ~ V0, data = data))
#> 
#> Call:
#> lm(formula = V1 ~ V0, data = data)
#> 
#> Residuals:
#>    Min     1Q Median     3Q    Max 
#> -1.834 -0.677  0.000  0.677  1.834 
#> 
#> Coefficients:
#>             Estimate Std. Error t value Pr(>|t|)
#> (Intercept)  -0.1500     0.2562  -0.586    0.563
#> V01           0.3000     0.3623   0.828    0.415
#> 
#> Residual standard error: 0.9922 on 28 degrees of freedom
#> Multiple R-squared:  0.0239,	Adjusted R-squared:  -0.01096 
#> F-statistic: 0.6857 on 1 and 28 DF,  p-value: 0.4146
#> 
summary(glm(V0 ~ V1, data = data, family = "binomial"))
#> 
#> Call:
#> glm(formula = V0 ~ V1, family = "binomial", data = data)
#> 
#> Deviance Residuals: 
#>    Min      1Q  Median      3Q     Max  
#> -1.417  -1.151   0.000   1.151   1.417  
#> 
#> Coefficients:
#>              Estimate Std. Error z value Pr(>|z|)
#> (Intercept) 8.207e-17  3.696e-01   0.000    1.000
#> V1          3.251e-01  3.877e-01   0.839    0.402
#> 
#> (Dispersion parameter for binomial family taken to be 1)
#> 
#>     Null deviance: 41.589  on 29  degrees of freedom
#> Residual deviance: 40.865  on 28  degrees of freedom
#> AIC: 44.865
#> 
#> Number of Fisher Scoring iterations: 4
#>