Rfanatic
Rfanatic

Reputation: 2280

Linear models in R with different combinations of data frame variables

I have a data frame (myvar) with colnames from act1_1 to act1_144(dependent variable) filled in with numeric values and 7 columns with socio-demographics, DVAge, DVHsize, dhhtype, deconact, Income, NumChild and Rooms (independent variables).

independents<-DVType[, 1:144] 
dependents<-DVType[, 145:151]



myvar<-cbind(dependents,independents)

I am trying to generate a linear regression model using the socio-demographic columns variables and trying all the possible combination like Income, Income+Numchild, Income+Rooms,..., dhhtype+deconact .... I am having trouble generating combinations with data frame.

What I managed to do is to regress the dependent variables on the independent variable.

 fit<-lm(as.matrix(dependents) ~ -1 + model.matrix(~ ., data = independents  ))
    require(broom)
    summary(fit)

Output:

    Response DVHsize :

    Call:
    lm(formula = DVHsize ~ -1 + model.matrix(~., data = independents))

    Residuals:
        Min      1Q  Median      3Q     Max 
    -3.1356 -1.0056 -0.2886  0.9597  7.2341 

    Coefficients:
                                                       Estimate Std. Error t value Pr(>|t|)    
    model.matrix(~., data = independents)(Intercept)  3.616e+00  4.300e-02  84.096  < 2e-16 ***
    model.matrix(~., data = independents)act1_1      -2.788e-05  2.911e-05  -0.958  0.33822    
    model.matrix(~., data = independents)act1_2       3.703e-05  2.898e-05   1.278  0.20138    
    model.matrix(~., data = independents)act1_3      -4.458e-06  2.177e-05  -0.205  0.83773    
    model.matrix(~., data = independents)act1_4       2.120e-05  2.557e-05   0.829  0.40705    
    model.matrix(~., data = independents)act1_5       2.327e-05  2.724e-05   0.854  0.39296    
    model.matrix(~., data = independents)act1_6      -4.578e-05  2.299e-05  -1.991  0.04644 *  
    model.matrix(~., data = independents)act1_7       2.087e-05  1.971e-05   1.058  0.28985    
    model.matrix(~., data = independents)act1_8      -4.694e-06  2.019e-05  -0.233  0.81612    
    model.matrix(~., data = independents)act1_9       3.604e-06  1.756e-05   0.205  0.83738    
    model.matrix(~., data = independents)act1_10     -2.924e-06  1.685e-05  -0.174  0.86225    
    model.matrix(~., data = independents)act1_11      4.934e-06  1.671e-05   0.295  0.76782    

....

How can extend this to identify all combinations?

Upvotes: 0

Views: 193

Answers (1)

Alexis
Alexis

Reputation: 5049

If I understand correctly, your formula is wrong, your predictors (independents) should be the 7 columns you mention. I'm not sure if "all possible combinations" is exactly what you want, maybe you only want second-order interactions (and no intercept due to the -1)? In that case, you can probably do the following (see also this question):

fit <- lm(sprintf("cbind(%s) ~ . ^ 2 - 1",
                  toString(paste("act1", 1:144, sep = "_"))),
          data = DVType)

Upvotes: 1

Related Questions