# R - Logistic regression. Coding to get all predictor variables for categorical vars

#### willcoop

##### New Member
I'll preface my question by stating that I am new to using R. I am trying to use logistic regression on my data. I typed glm(formula = purchase ~ predictor vars separated by + sign, family=binomial, data=modeldata)
The solution does not produce the multiple intercept and beta coefficients for up to 10 categorical variables that I am expecting.
I am trying to replicate what I get using a subset of the data on SPSS. SPSS does generate the intercepts for all the levels of the categorical variables that fit the model . SPSS would be too slow to run the entire database.

My questions are:
If R is not reading these variables as categorical, how do I specify them as categorical?
When I get the output from R, will it generate just the final solution after attempting to fit all the predictors? Or will it only include those predictors that have a P(z) < .05?
How do I go about achieving my desired solution? Thanks for your attention.

William Cooper

Last edited:

#### Dason

Re: R - Logistic regression. Coding to get all predictor variables for categorical va

Well what DO you get? It's hard to say what the issue is if we don't have your data and don't have your output. Basically at the moment all we have is "it's not quite giving me what I want" and it's really hard for us to diagnose anything with that amount of information.

#### willcoop

##### New Member
Re: R - Logistic regression. Coding to get all predictor variables for categorical va

When I use the command str(modeldata), I get this
$Age : int 38 36 46 42 68 44 42 54 40 42 ...$ BankCard : int 1 0 1 1 1 1 1 1 1 1 ...
$Cat : int 0 0 0 0 0 0 1 0 1 1 ...$ Dog : int 0 0 0 0 1 0 0 0 1 0 ...
$DwellingType : int 1 1 1 1 1 1 1 1 1 2 ...$ Education : int 0 1 2 0 3 1 2 0 1 1 ...

#### gianmarco

##### TS Contributor
Re: R - Logistic regression. Coding to get all predictor variables for categorical va

Once you have properly prepared your data, you may want to use a R function I have put togheter, which allows to visually display the fitted model's results (i.e., betas and ORs). It also allows to plot some model's diagnostics.
The function is described here: http://cainarchaeology.weebly.com/r-function-for-binary-logistic-regression.html

Short video tutorial here:

In the same site, a couple of functions are also available to perform LR internal validation.

Hope this helps.
Best
gm

#### willcoop

##### New Member
Re: R - Logistic regression. Coding to get all predictor variables for categorical va

Thanks to everyone for their help. Especially for the video, that's the best resource.