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.