Search code examples
rstatisticsregressionlinear-regressionspss

In SPSS, how do I do a bunch of regression analyses by looping through independent variables by their label variables? Is it easier in R?


Here's an example of my dataset in comma-delimited form (with variable names in the top row)...

LABEL,X,Y
bimmy,1,2
bimmy,2,4
bimmy,3,6
jimmy,2,8
jimmy,5,4
jimmy,6,10
marian,3,10
marian,4,9
marian,5,5

I want to do a linear regression analysis of X and Y, for each LABEL. So, I'd do an analyses of X and Y for 'bimmy', then for 'jimmy', then for 'marian'.

Is this possible in SPSS? Is it easier in R?

I've searched Google and Stack Overflow with a similarly-worded query, but found nothing relevant.


Solution

  • yourdata <- read.table(text="LABEL,X,Y
    bimmy,1,2
    bimmy,2,4
    bimmy,3,6
    jimmy,2,8
    jimmy,5,4
    jimmy,6,10
    marian,3,10
    marian,4,9
    marian,5,5",h=T,sep=",")
    
    regression.to.repeat <- function( x ) lm( Y ~ X , data = x ) 
    
    by( yourdata , yourdata$LABEL , regression.to.repeat )