# Simulate data for logistic regression

**URL:** https://discourse.julialang.org/t/simulate-data-for-logistic-regression/14567
**Category:** General Usage
**Created:** [September 5, 2018, 7:54am UTC](https://discourse.julialang.org/t/simulate-data-for-logistic-regression/14567 "2018-09-05T07:54:38Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![DominiqueMakowski](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dominiquemakowski/32/51410_2.png) [@DominiqueMakowski](https://discourse.julialang.org/u/DominiqueMakowski)
#### Post date: [September 5, 2018, 7:54am UTC](https://discourse.julialang.org/t/simulate-data-for-logistic-regression/14567/1 "2018-09-05T07:54:38Z")

</div>

After successfully implementing a [small function](https://github.com/neuropsychology/Psycho.jl/blob/master/src/simulate/data_correlation.jl#L77) to generate correlated data, I wish now to simulate data for logistic regression (with a 0/1 outcome) with specific coefficients.

I’ve tried to follow two threads (originally for R; see [this](https://stats.stackexchange.com/questions/46523/how-to-simulate-artificial-data-for-logistic-regression) and [this](https://stats.stackexchange.com/questions/12857/generate-random-correlated-data-between-a-binary-and-a-continuous-variable/12858#12858)), but encountered some problems, specifically in the equivalent of the `rbinom()` function.

```julia
# R version
# https://stats.stackexchange.com/questions/46523/how-to-simulate-artificial-data-for-logistic-regression
# > x1 = rnorm(1000) # some continuous variables
# > x2 = rnorm(1000)
# > z = 1 + 2*x1 + 3*x2 # linear combination with a bias
# > pr = 1/(1+exp(-z)) # pass through an inv-logit function
# > y = rbinom(1000,1,pr) # bernoulli response variable

# Julia attempt
# add https://github.com/neuropsychology/Psycho.jl.git

using Psycho, Statistics, Random, GLM

coefs = [0.2]
data = simulate_data_correlation(coefs; n=100)

cor(data[:y], data[:Var1]) # 0.2

data[:y] = 1 ./ (1 .+ exp.(-1 * data[:y])) # pass through an inv-logit function
response = rand.([Distributions.Binomial.(1, x) for x in data[:y]], 1)
data[:y] = [x[1] for x in response]

glm(@formula(y ~ Var1), data, Binomial(), LogitLink()) # Doesn't return the correct coef

```

I am not sure where the errors are… Any advice?

PS: I am not trying to copy the R version per se, but rather just to simulate logistic data with pre-specified coefficients. There could be a better approach for this problem, but I only found these answers for R.

---

<div class="post-metadata">

### Author: ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)
#### Post date: [September 5, 2018, 8:02am UTC](https://discourse.julialang.org/t/simulate-data-for-logistic-regression/14567/2 "2018-09-05T08:02:18Z")

</div>

I think you miscoded the logistic function. But in any case, I think you are overcomplicating this, this is how I would code the R snippet in Julia:

```julia
using StatsFuns: logistic
X = randn(1000, 2)
Z = X * [2, 3] .+ 1
p = logistic.(Z)
y = Float64.(rand(length(p)) .< p)

```

---

<div class="post-metadata">

### Author: ![DominiqueMakowski](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dominiquemakowski/32/51410_2.png) [@DominiqueMakowski](https://discourse.julialang.org/u/DominiqueMakowski)
#### Post date: [September 5, 2018, 8:06am UTC](https://discourse.julialang.org/t/simulate-data-for-logistic-regression/14567/3 "2018-09-05T08:06:44Z")

</div>

😮 impressive! thanks!

---

<div class="post-metadata">

### Author: ![mcreel](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mcreel/32/30088_2.png) [@mcreel](https://discourse.julialang.org/u/mcreel)
#### Post date: [September 5, 2018, 1:37pm UTC](https://discourse.julialang.org/t/simulate-data-for-logistic-regression/14567/4 "2018-09-05T13:37:29Z")

</div>

See also [https://github.com/mcreel/Econometrics/blob/master/Examples/MLE/EstimateLogit.jl](https://github.com/mcreel/Econometrics/blob/master/Examples/MLE/EstimateLogit.jl)  
[Econometrics/LogitDGP.jl at main · mcreel/Econometrics · GitHub](https://github.com/mcreel/Econometrics/blob/master/Examples/MLE/LogitDGP.jl)
