# Simple and multiple logistic regression

I have almost two questions. I need a single covariate logistic regression (LR) for each of my variables. Should I do it manually in SPSS, selecting each variable and do logistic regression? Is there a "for each" cycle to do it? I should switch to R language to have what I want.

In the multivariables (multi covariates) LR, could I have missing values?

Thanks!!

-

If I understand you correctly, you want to fit two successive simple logistic regression model. I don't know if there's a specific instruction in SPSS that allows to switch the covariate of interest or cycle through them, but I guess you can run the two models in succession. In R, if your data are organized in a matrix or data.frame, this is easily done as

X <- replicate(2, rnorm(100))  # two random deviates
y <- rnorm(100)
apply(X, 2, function(x) lm(y ~ x))


About your second question, models like this are generally estimated using listwise deletion: any individuals having one or more missing observations on the covariates are deleted before estimating model parameters. Again, in R:

X[2,2] <- NA
summary(lm(y ~ X))


shows that one observation has been deleted, yielding 96 DF (instead of 97).

-
Thanks everyone! I realized I should write a script, I did it in Matlab. –  Simone Jan 17 '11 at 11:16

Here is some SPSS code to loop through each of your models.

*creating a simulated dataset.
input program.
loop #i = 1 to 100.
compute yvar = RV.BERNOULLI(.5).
compute xvar1 = RV.NORMAL(100,10).
compute xvar2 = RV.NORMAL(100,10).
compute xvar3 = RV.NORMAL(100,10).
compute listwise = RV.BERNOULLI(.1).
end case.
end loop.
end file.
end input program.
execute.

*using these variables you can run the regressions one equation at a time.
LOGISTIC REGRESSION VARIABLES YVAR
/SELECT=LISTWISE EQ 0
/METHOD=ENTER XVAR1.

*or you can create a macro and loop through your independent variable list.
DEFINE !logit_loop (dep = !TOKENS(1)
/ind = !CMDEND )
!DO !i !IN !ind
LOGISTIC REGRESSION VARIABLES !dep
/SELECT=listwise = 0
/METHOD=ENTER !i.
!DOEND.
!ENDDEFINE.
!logit_loop dep = yvar ind = xvar1 xvar2 xvar3.


Note the loop does a selection on a variable named listwise = 0 , otherwise missing values for any variables would just be dropped (and potentially produce equations with different sets). While looping through your list is pretty simple in SPSS, where I think R is more convienant is the fact that the elements of your different objects are readily available in R (and so it is easier to access them and manipulate them). I'll update with an example later, but basically all the SPSS code does is produce alot of output which you have to read to find the values your interested in. In R you produce objects that have attributes you can extract. You can do the same thing in SPSS, but it is IMO more difficult and requires going into the output XML and parsing the info you want (which I don't know how to do).

So long story short if you are alright with just viewing the output this is easily accomplished in SPSS (or PASW now). If you want to produce a program to summarize specific elements of those models, I think it is easier to learn a solution in R.

-
Attack of the R code syntax highlighting! Is it possible to not have the code highlighted? –  Andy W Jan 10 '11 at 14:50
(+1) For SPSS Code - Go SPSS!! –  suncoolsu Jan 10 '11 at 15:31
@Andy What's wrong with highlighting SPSS code? Code highlighting isn't restricted to R. If you don't want it highlighted, don't indent it... –  Shane Jan 10 '11 at 15:38
@Andy The highlighting engine used on SE guesses language on its own; it is even not capable of highlighting R (it is interpreted as C); this seems to be interpreted as Pascal because of "end". –  mbq Jan 10 '11 at 16:10
@Shane, it is not the blocked code highlighting (the grey background) that is the problem, it is the fact that the wrong keywords within the syntax are highlighted. For instance my loop variable in the simulated data is highlighted with a lighter grey shade, suggesting the code after the pound sign is a comment. I think it is much easier to read as a blocked code segment, but the highlighting can only be confusing. Is there anyway to keep the blocked code but not have highlighting? –  Andy W Jan 10 '11 at 16:22
show 2 more comments

SPSS has some scripting facilities (syntax, sax basic, pyhton, ...). I myself have so far only used syntax. Maybe the link below can help you to construct a loop that does the job, it points to the excellent website of the UCLA. The problem solved there is only slightly different from yours so chances are high that you can modify it according to your needs.

http://www.ats.ucla.edu/stat/spss/faq/looping_parallel_lists.html

psj

-
I wouldn't suggest resorting to python to accomplish this task unless you need it to be standardized to run on multiple datasets with different variable names. The UCLA website is full of useful examples though of interest to anyone who uses SPSS. –  Andy W Jan 10 '11 at 14:58