-3
$\begingroup$

Being quite new to R I am finding myself stuck once again. I have a dataset that looks like the following.

 ID y   x1  x2  x3  x4  scale
2001    61.78   0.30    0.10    0.02    0.00    200
2001    61.78   0.30    0.10    0.02    0.00    400
2001    61.78   0.31    0.10    0.02    0.00    1000
2006    51.11   0.21    0.11    0.07    0.00    200
2006    51.11   0.20    0.12    0.07    0.00    400
2006    51.11   0.18    0.12    0.06    0.00    1000
2017    58.89   0.05    0.00    0.00    0.00    200
2017    58.89   0.04    0.00    0.00    0.00    400
2017    58.89   0.03    0.00    0.01    0.00    1000
2019    54.78   0.12    0.02    0.08    0.00    200
2019    54.78   0.12    0.02    0.09    0.00    400
2019    54.78   0.10    0.02    0.12    0.00    1000
2021    47.78   0.06    0.01    0.07    0.00    200
2021    47.78   0.06    0.01    0.07    0.00    400
2021    47.78   0.04    0.01    0.08    0.00    1000
2024    63.78   0.09    0.06    0.05    0.00    200
2024    63.78   0.08    0.06    0.05    0.00    400
2024    63.78   0.06    0.05    0.04    0.00    1000

I'm trying to perform univariate glm's where scales are grouped and the model loops through y~x1, y~x2, and so on.
I have been able to perform univariate glm's where scale is grouped using the following code and get the results required.

ddply(dat, .(scale), function (x){
intercept <- coef(summary(glm(y~x1,data=x)))[1]
slope     <- coef(summary(glm(y~x1,data=x)))[2]
p-values  <- coef(summary(glm(y~x1,data=x)))[8]
AIC       <- AIC(glm(y~x1,data=x))
Deviance  <- deviance(glm(y~x1,data=x))
c(intercept,slope,p-value,AIC,Deviance)
})

I can't, however, figure out how to have this code loop through all variables (ie. x1, x2, x3) without writing it directly into the code. My real dataset has 20 variables so being able to automate this would be great. Any advice would be greatly appreciated.

$\endgroup$
1
  • 1
    $\begingroup$ Have you considered submitting this question to StackOverflow instead of Cross Validated? It looks much more like you need to discover how to do something you already know than to discover what to do. $\endgroup$ Feb 2, 2016 at 3:32

1 Answer 1

0
$\begingroup$

I liked this problem, my solution uses nested for loops, but it works great. Might be able to vectorize the loops with enough forethought. In summary for each scale factor I loop through the variables and dynamically create the formula value for each glm fit. Then save the output from each iteration into an R data frame. Best part is I only used the base R package.

# create dummy data set
dat <- data.frame(y = rnorm(10), x1 = rnorm(10)*2, x2 = rnorm(10)*3, x3 = rnorm(10)*4, scale = c(rep(1,5), rep(2,5)))

# create data frame to store results
results <- data.frame()

# loop through the scales and each variable
for(scale in unique(dat$scale)){
    for(var in names(dat)[c(-1,-length(dat))]){
        # dynamically generate formula
        fmla <- as.formula(paste0("y ~ ", var))

        # fit glm model
        fit <- glm(fmla, data=dat[dat$scale = scale,])

        ## capture summary stats
        intercept <- coef(summary(fit))[1]
        slope <- coef(summary(fit))[2]
        p.value <- coef(summary(fit))[8]
        AIC <- AIC(fit)
        Deviance <- deviance(fit)

        # get coefficents of fit
        cfit <- coef(summary(fit))

        # create temporary data frame
        df <- data.frame(var = var, scale = scale, intercept = cfit[1],
                         slope = cfit[2], p.value = cfit[8],
                         AIC = AIC(fit), Deviance = deviance(fit), stringsAsFactors = F)

        # bind rows of temporary data frame to the results data frame
        results <- rbind(results, df)
    }
}
$\endgroup$
2
  • $\begingroup$ It seems as though it performs the loop and gives results for one of the scales and fills that value for all scales. In other words it is not running a seperate glm for y~x1 for scale 1 and then a seperate glm for y~x1 at scale 2. This can be seen in the output of the results where all parameter estimates are the same for each scale $\endgroup$
    – Jdan
    Feb 2, 2016 at 13:24
  • $\begingroup$ @Jdan ah I see, minor mistake. fit <- glm(fmla, data=dat) should subset by the scale factor fit <- glm(fmla, data=dat[dat$scale == scale,]) $\endgroup$
    – Matt L.
    Feb 2, 2016 at 14:03

Not the answer you're looking for? Browse other questions tagged or ask your own question.