# Using R's formula style lm() with SPSS

Is there a way to type in a regression formula in SPSS in the same manner as R, with weights?

For example, in R, I would write something like:

lm(y ~ B1 + B2 + B1*B2, data=df, weights=x)


How would I go about recreating this in SPSS/PASW?

-

In recent versions of SPSS you can run R code directly in SPSS on your SPSS datasets.

-
 +1 Thanks for the tip Jeromy, this would certainly help! The version I have access to is through my school PASW 18. Hopefully it also contains this functionality! Wouldn't that make life easy. – Brandon Bertelsen Nov 12 '10 at 6:02 And it works like a charm. You're a life saver. – Brandon Bertelsen Nov 12 '10 at 6:20

In SPSS you can have the GUI write the syntax for you via the paste button. If you go through the regression command in the window there is an option to include weights.

Here is some sample code it just had the program output for me:

  REGRESSION
/MISSING LISTWISE
/REGWGT=VAR3
/STATISTICS COEFF OUTS R ANOVA
/CRITERIA=PIN(.05) POUT(.10)
/NOORIGIN
/DEPENDENT VAR1
/METHOD=ENTER VAR2.


As with any program, I would suggest you check the documentation on how SPSS implements weights in OLS (I personally have no idea).

A comment by Wolfgang below points out that R and SPSS implement weights in the same manner (although I would still suggest checking out the documentation of how they implement weights.)

-
+1 Thanks for pointing that out. I somehow doubt that the implementation of weights within R is the same as that for SPSS – Brandon Bertelsen Nov 12 '10 at 4:06
I know SAS and SPSS are not the same, but that is all I know. – Andy W Nov 12 '10 at 4:14
Actually, they are the same for SPSS and R. Simple example: yi <- c(1,3,2,2,3,5,4,6); xi <- c(1,2,2,3,3,4,5,5); wi <- c(.2,.3,.2,.1,.4,.3,.2,.3); summary(lm(yi ~ xi, weights=wi)) gives the exact same results as using the same data in SPSS with: REGRESSION /MISSING LISTWISE /REGWGT=wi /STATISTICS COEFF OUTS R ANOVA /CRITERIA=PIN(.05) POUT(.10) /NOORIGIN /DEPENDENT yi /METHOD=ENTER xi . – Wolfgang Nov 12 '10 at 13:34
@Wolfgang, thank you you pointing that out. – Andy W Nov 12 '10 at 14:24

1) You can always find out exactly how an algorithm in SPSS is defined mathematically from the Algorithms link on the Help menu.

2) SPSS also has a Weighted Least Square procedure that allows you to model the error variance and correct for heteroscedasticity.

2) In order to run R within SPSS (version 16 or later), you just need to install the R plugin or Essentials (depending on version) from SPSS Developer Central, www.spss.com/devcentral. The integration is free. You can also get SPSS syntax and dialog boxes for a number of useful R packages from the same site.

HTH

-