## Simulate data for logistic regression by specifying the response rate and the significant covariates

Solved
Highlighted
Occasional Contributor
Posts: 13

# Simulate data for logistic regression by specifying the response rate and the significant covariates

I read this blog post by @Rick_SAS on simulating data for linear regression.

https://blogs.sas.com/content/iml/2017/01/25/simulate-regression-model-sas.html

I modified the code to simulate data for logistic regression, and it works very well.

``````mu = beta[0]; /* intercept term */

do j = 1 to &ncovar;
mu = mu + beta[j] * x[j]; /* + sum(beta[j]*x[j]) */
end;

prob = 1 / (1 + exp(-mu)); /* specify the probability of success using the logistic function and "mu" */

y = RAND('BERNOULLI', prob); /* simulate binary variables based on "prob" */``````

Thanks, Rick!

However, I now want to

a) add a binary covariate that is way more significant than the other continuous covariates

b) specify the response rate (i.e. the proportion of success for "Y").  For my particular example, I need a very low response rate.

Based on Rick's example, I know how to specify the regression coefficients, but I don't know how to also specify the response rate.  (It may be impossible to do both.)

For my example, I don't need to specify the regression coefficients; I just need the binary covariate to be much more significant than the continuous covariates, and I need a very low response rate in my raw data.

How can I accomplish both of these goals?

Accepted Solutions
Solution
a week ago
SAS Super FREQ
Posts: 4,245

## Re: Simulate data for logistic regression by specifying the response rate and the significant covari

I also wrote several articles on simulating logistic data directly:

https://blogs.sas.com/content/iml/2014/06/25/simulate-logistic-data.html

https://blogs.sas.com/content/iml/2014/06/27/simulate-many-samples-from-a-logistic-regression-model....

For the binary covariate, just use (or simulate) a 0/1 variable and give it a large relative coefficient such as

G = rand("Bern", 0.5);

eta = intercept + 50*G + 0.3*x1 - 0.4*x2;

By "response rate," I assume you mean the relative proportion of 0/1 responses.  That depends on the linear predictor (eta). You can use the Intercept term to raise or lower the rate, but the rate will depends not only on mean(eta) but also on the values of the explanatory variables. Adjusting the response rate is easiest when the explanatory variables are normally distributed with mean 0.

All Replies
Solution
a week ago
SAS Super FREQ
Posts: 4,245

## Re: Simulate data for logistic regression by specifying the response rate and the significant covari

I also wrote several articles on simulating logistic data directly:

https://blogs.sas.com/content/iml/2014/06/25/simulate-logistic-data.html

https://blogs.sas.com/content/iml/2014/06/27/simulate-many-samples-from-a-logistic-regression-model....

For the binary covariate, just use (or simulate) a 0/1 variable and give it a large relative coefficient such as

G = rand("Bern", 0.5);

eta = intercept + 50*G + 0.3*x1 - 0.4*x2;

By "response rate," I assume you mean the relative proportion of 0/1 responses.  That depends on the linear predictor (eta). You can use the Intercept term to raise or lower the rate, but the rate will depends not only on mean(eta) but also on the values of the explanatory variables. Adjusting the response rate is easiest when the explanatory variables are normally distributed with mean 0.

Occasional Contributor
Posts: 13

## Re: Simulate data for logistic regression by specifying the response rate and the significant covari

Hi Rick,

1) Yes, by response rate, I mean the proportion of success.

2) Your first example of simulating data for linear regression uses the normal distribution to simulate the values of the predictors.  Recall your code

``````do j = 1 to dim(x);

x[j] = rand("Normal"); /* 2. Simulate explanatory variables   */

end;``````

3) That's a very good idea to use the intercept to adjust the response rate.  Thank you!

☑ This topic is solved.