I have simulated log-gamma data of various sizes and 'shapes', and then fit a gamma and lognormal model to these simulated data.
Here is my relevant code:
gm_glog <- function(size.i, alpha.i) {
x_i <- runif(size.i, 0, 1) # draw a sample of size 'size'
y.true <- exp(b_0 + b_1*x_i) # produce log gamma data
y_i <- rgamma(size.i, rate = alpha.i/y.true, shape = alpha.i) # random gamma sample
# Gamma Model
log_gamma_model <- glm(y_i ~ x_i, family = Gamma(link = "log"),
control = glm.control(maxit=100, trace = TRUE),
start = c(0.1, 0.2))
log_gamma_summ <- summary(log_gamma_model)
# Lognormal Model
log_norm_model <- glm(y_i ~ x_i, family = gaussian(link = "log"),
control = glm.control(maxit=500, trace = TRUE),
start = c(0.1, 0.2))
log_norm_summ <- summary(log_norm_model)
# DATA FRAME BUILD
data.frame(size = size.i,
alpha = alpha.i,
gamma_mod_int = log_gamma_summ$coefficients["(Intercept)", "Estimate"],
gamma_mod_est = log_gamma_summ$coefficients["x_i", "Estimate"],
gamma_mod_aic = log_gamma_summ$aic,
gamma_mod_dev = log_gamma_summ$deviance.resid[length(log_gamma_summ$deviance.resid)],
gamma_mod_shape = MASS::gamma.shape(log_gamma_model)$alpha,
norm_mod_int = log_norm_summ$coefficients["(Intercept)", "Estimate"],
norm_mod_est = log_norm_summ$coefficients["x_i", "Estimate"],
norm_mod_aic = log_norm_summ$aic,
norm_mod_dev = log_norm_summ$deviance.resid[length(log_norm_summ$deviance.resid)]
)
}
My issue now is that I want to produce a side-by-side comparison of these regression results in a single table, in which each row[1] of my design matrix corresponds to the first row of the function output, and again for row[2], all the way to row[40].
Ideally, it would look like
size | alpha | summary gamma glm | summary lognormal glm
with 40 rows total, one for each combination of size and alpha, for easiest interpretation of the results.
Essentially, I just want to merge design.matrix & the summaries.
Unfortunately, producing a data frame of glm summaries has been difficult and I cannot find a way to merge these results, row by row, like it would like to.
I have seen that using lapply, tidy, and glance gave me all of the information that I want for each of these summaries, but both of these leave me with a list of data frames, and combining them row by row has also eluded me.
If I were to use this method, I would still like to combine row[1] of lapply(model, tidy) with row[1] of lapply(model, glance), row[2] of lapply(model, tidy) with row[2] of lapply(model, glance), etc, even though the rows of each of these lists are tibbles of different dimensions.
How can I best do this? Is there an easier way to achieve what I want?
Edit: I have managed to get the deviance residuals with a list of single-element lists. Still not sure how I can merge these to the AIC values etc.