How to build a summary table of glm's parameters and AICcWt












1















I have a dataset that I am using to build generalised linear models. The response variable is binary (absence/presence) and the explanatory variables are categorical.



CODE



library(tidyverse)
library(AICcmodavg)

# Data
set.seed(123)
t <- tibble(ID = 1:100,
A = as.factor(sample(c(0, 1), 100, T)),
B = as.factor(sample(c("black", "white"), 100, T)),
C = as.factor(sample(c("pos", "neg", "either"), 100, T)))

# Candidate set of models - Binomial family because response variable
# is binary (0 for absent & 1 for present)
# Global model is A ~ B_black + C_either
m1 <- glm(A ~ 1, binomial, t)
m2 <- glm(A ~ B, binomial, t)
m3 <- glm(A ~ C, binomial, t)
m4 <- glm(A ~ B + C, binomial, t)

# List with all models
ms <- list(null = m1, m_B = m2, m_C = m3, m_BC = m4)

# Summary table
aic_tbl <- aictab(ms)


PROBLEM



I want to build a table like the one below that summarises the coefficients, standard errors, and Akaike weights of the models within my candidate set.



enter image description here



QUESTION



Can anyone suggest how to best build this table using my list of models and AIC table?










share|improve this question




















  • 1





    try the MuMIn package?

    – Ben Bolker
    Dec 31 '18 at 11:41






  • 2





    By the way, it is not advisable to use a name such as t for an object as it is a built-in R function and effectively overwrites it. If any function is going to call it later, it will try to use your dataframe first, instead of the correct call to transpose.

    – coffeinjunky
    Dec 31 '18 at 14:50






  • 1





    ms is a built-in function in lubridate.Might want to change the name too.

    – NelsonGon
    Dec 31 '18 at 15:11
















1















I have a dataset that I am using to build generalised linear models. The response variable is binary (absence/presence) and the explanatory variables are categorical.



CODE



library(tidyverse)
library(AICcmodavg)

# Data
set.seed(123)
t <- tibble(ID = 1:100,
A = as.factor(sample(c(0, 1), 100, T)),
B = as.factor(sample(c("black", "white"), 100, T)),
C = as.factor(sample(c("pos", "neg", "either"), 100, T)))

# Candidate set of models - Binomial family because response variable
# is binary (0 for absent & 1 for present)
# Global model is A ~ B_black + C_either
m1 <- glm(A ~ 1, binomial, t)
m2 <- glm(A ~ B, binomial, t)
m3 <- glm(A ~ C, binomial, t)
m4 <- glm(A ~ B + C, binomial, t)

# List with all models
ms <- list(null = m1, m_B = m2, m_C = m3, m_BC = m4)

# Summary table
aic_tbl <- aictab(ms)


PROBLEM



I want to build a table like the one below that summarises the coefficients, standard errors, and Akaike weights of the models within my candidate set.



enter image description here



QUESTION



Can anyone suggest how to best build this table using my list of models and AIC table?










share|improve this question




















  • 1





    try the MuMIn package?

    – Ben Bolker
    Dec 31 '18 at 11:41






  • 2





    By the way, it is not advisable to use a name such as t for an object as it is a built-in R function and effectively overwrites it. If any function is going to call it later, it will try to use your dataframe first, instead of the correct call to transpose.

    – coffeinjunky
    Dec 31 '18 at 14:50






  • 1





    ms is a built-in function in lubridate.Might want to change the name too.

    – NelsonGon
    Dec 31 '18 at 15:11














1












1








1








I have a dataset that I am using to build generalised linear models. The response variable is binary (absence/presence) and the explanatory variables are categorical.



CODE



library(tidyverse)
library(AICcmodavg)

# Data
set.seed(123)
t <- tibble(ID = 1:100,
A = as.factor(sample(c(0, 1), 100, T)),
B = as.factor(sample(c("black", "white"), 100, T)),
C = as.factor(sample(c("pos", "neg", "either"), 100, T)))

# Candidate set of models - Binomial family because response variable
# is binary (0 for absent & 1 for present)
# Global model is A ~ B_black + C_either
m1 <- glm(A ~ 1, binomial, t)
m2 <- glm(A ~ B, binomial, t)
m3 <- glm(A ~ C, binomial, t)
m4 <- glm(A ~ B + C, binomial, t)

# List with all models
ms <- list(null = m1, m_B = m2, m_C = m3, m_BC = m4)

# Summary table
aic_tbl <- aictab(ms)


PROBLEM



I want to build a table like the one below that summarises the coefficients, standard errors, and Akaike weights of the models within my candidate set.



enter image description here



QUESTION



Can anyone suggest how to best build this table using my list of models and AIC table?










share|improve this question
















I have a dataset that I am using to build generalised linear models. The response variable is binary (absence/presence) and the explanatory variables are categorical.



CODE



library(tidyverse)
library(AICcmodavg)

# Data
set.seed(123)
t <- tibble(ID = 1:100,
A = as.factor(sample(c(0, 1), 100, T)),
B = as.factor(sample(c("black", "white"), 100, T)),
C = as.factor(sample(c("pos", "neg", "either"), 100, T)))

# Candidate set of models - Binomial family because response variable
# is binary (0 for absent & 1 for present)
# Global model is A ~ B_black + C_either
m1 <- glm(A ~ 1, binomial, t)
m2 <- glm(A ~ B, binomial, t)
m3 <- glm(A ~ C, binomial, t)
m4 <- glm(A ~ B + C, binomial, t)

# List with all models
ms <- list(null = m1, m_B = m2, m_C = m3, m_BC = m4)

# Summary table
aic_tbl <- aictab(ms)


PROBLEM



I want to build a table like the one below that summarises the coefficients, standard errors, and Akaike weights of the models within my candidate set.



enter image description here



QUESTION



Can anyone suggest how to best build this table using my list of models and AIC table?







r data-manipulation glm






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Jan 1 at 7:00







Darius

















asked Dec 31 '18 at 11:17









DariusDarius

104115




104115








  • 1





    try the MuMIn package?

    – Ben Bolker
    Dec 31 '18 at 11:41






  • 2





    By the way, it is not advisable to use a name such as t for an object as it is a built-in R function and effectively overwrites it. If any function is going to call it later, it will try to use your dataframe first, instead of the correct call to transpose.

    – coffeinjunky
    Dec 31 '18 at 14:50






  • 1





    ms is a built-in function in lubridate.Might want to change the name too.

    – NelsonGon
    Dec 31 '18 at 15:11














  • 1





    try the MuMIn package?

    – Ben Bolker
    Dec 31 '18 at 11:41






  • 2





    By the way, it is not advisable to use a name such as t for an object as it is a built-in R function and effectively overwrites it. If any function is going to call it later, it will try to use your dataframe first, instead of the correct call to transpose.

    – coffeinjunky
    Dec 31 '18 at 14:50






  • 1





    ms is a built-in function in lubridate.Might want to change the name too.

    – NelsonGon
    Dec 31 '18 at 15:11








1




1





try the MuMIn package?

– Ben Bolker
Dec 31 '18 at 11:41





try the MuMIn package?

– Ben Bolker
Dec 31 '18 at 11:41




2




2





By the way, it is not advisable to use a name such as t for an object as it is a built-in R function and effectively overwrites it. If any function is going to call it later, it will try to use your dataframe first, instead of the correct call to transpose.

– coffeinjunky
Dec 31 '18 at 14:50





By the way, it is not advisable to use a name such as t for an object as it is a built-in R function and effectively overwrites it. If any function is going to call it later, it will try to use your dataframe first, instead of the correct call to transpose.

– coffeinjunky
Dec 31 '18 at 14:50




1




1





ms is a built-in function in lubridate.Might want to change the name too.

– NelsonGon
Dec 31 '18 at 15:11





ms is a built-in function in lubridate.Might want to change the name too.

– NelsonGon
Dec 31 '18 at 15:11












1 Answer
1






active

oldest

votes


















3














Just to point it out: broom gets you half-way to where you want to get by turning the model output into a dataframe, which you can then reshape.



library(broom)
bind_rows(lapply(ms, tidy), .id="key")
key term estimate std.error statistic p.value
1 null (Intercept) -0.12014431182649532 0.200 -0.59963969517107030 0.549
2 m_B (Intercept) 0.00000000000000123 0.283 0.00000000000000433 1.000
3 m_B Bwhite -0.24116205496397874 0.401 -0.60071814968372905 0.548
4 m_C (Intercept) -0.47957308026188367 0.353 -1.35892869678271544 0.174
5 m_C Cneg 0.80499548069651150 0.507 1.58784953814722285 0.112
6 m_C Cpos 0.30772282333522433 0.490 0.62856402205887851 0.530
7 m_BC (Intercept) -0.36339654526926718 0.399 -0.90984856337213305 0.363
8 m_BC Bwhite -0.25083209866475475 0.408 -0.61515191157571303 0.538
9 m_BC Cneg 0.81144822536950656 0.508 1.59682131202527056 0.110
10 m_BC Cpos 0.32706970242195277 0.492 0.66527127770403538 0.506


And if you must insist of the layout of your table, I came up with the following (arguably clumsy) way of rearranging everything:



out <- bind_rows(lapply(ms, tidy), .id="mod")

t1 <- out %>% select(mod, term, estimate) %>% spread(term, estimate) %>% base::t
t2 <- out %>% select(mod, term, std.error) %>% spread(term, std.error) %>% base::t
rownames(t2) <- paste0(rownames(t2), "_std_e")
tmp <- rbind(t1, t2[-1,])
new_t <- as.data.frame(tmp[-1,])
colnames(new_t) <- tmp[1,]
new_t


Alternatively, you may want to familiarise yourself with packages that are meant to display model output for publication, e.g. texreg or stargazer come to mind:



library(texreg)
screenreg(ms)

==================================================
null m_B m_C m_BC
--------------------------------------------------
(Intercept) -0.12 0.00 -0.48 -0.36
(0.20) (0.28) (0.35) (0.40)
Bwhite -0.24 -0.25
(0.40) (0.41)
Cneg 0.80 0.81
(0.51) (0.51)
Cpos 0.31 0.33
(0.49) (0.49)
--------------------------------------------------
AIC 140.27 141.91 141.66 143.28
BIC 142.87 147.12 149.48 153.70
Log Likelihood -69.13 -68.95 -67.83 -67.64
Deviance 138.27 137.91 135.66 135.28
Num. obs. 100 100 100 100
==================================================
*** p < 0.001, ** p < 0.01, * p < 0.05





share|improve this answer


























  • Thanks @coffeinjunky for the two different ways of doing what I wanted. These packages for model output display are very handy

    – Darius
    Jan 1 at 11:29











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53986811%2fhow-to-build-a-summary-table-of-glms-parameters-and-aiccwt%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









3














Just to point it out: broom gets you half-way to where you want to get by turning the model output into a dataframe, which you can then reshape.



library(broom)
bind_rows(lapply(ms, tidy), .id="key")
key term estimate std.error statistic p.value
1 null (Intercept) -0.12014431182649532 0.200 -0.59963969517107030 0.549
2 m_B (Intercept) 0.00000000000000123 0.283 0.00000000000000433 1.000
3 m_B Bwhite -0.24116205496397874 0.401 -0.60071814968372905 0.548
4 m_C (Intercept) -0.47957308026188367 0.353 -1.35892869678271544 0.174
5 m_C Cneg 0.80499548069651150 0.507 1.58784953814722285 0.112
6 m_C Cpos 0.30772282333522433 0.490 0.62856402205887851 0.530
7 m_BC (Intercept) -0.36339654526926718 0.399 -0.90984856337213305 0.363
8 m_BC Bwhite -0.25083209866475475 0.408 -0.61515191157571303 0.538
9 m_BC Cneg 0.81144822536950656 0.508 1.59682131202527056 0.110
10 m_BC Cpos 0.32706970242195277 0.492 0.66527127770403538 0.506


And if you must insist of the layout of your table, I came up with the following (arguably clumsy) way of rearranging everything:



out <- bind_rows(lapply(ms, tidy), .id="mod")

t1 <- out %>% select(mod, term, estimate) %>% spread(term, estimate) %>% base::t
t2 <- out %>% select(mod, term, std.error) %>% spread(term, std.error) %>% base::t
rownames(t2) <- paste0(rownames(t2), "_std_e")
tmp <- rbind(t1, t2[-1,])
new_t <- as.data.frame(tmp[-1,])
colnames(new_t) <- tmp[1,]
new_t


Alternatively, you may want to familiarise yourself with packages that are meant to display model output for publication, e.g. texreg or stargazer come to mind:



library(texreg)
screenreg(ms)

==================================================
null m_B m_C m_BC
--------------------------------------------------
(Intercept) -0.12 0.00 -0.48 -0.36
(0.20) (0.28) (0.35) (0.40)
Bwhite -0.24 -0.25
(0.40) (0.41)
Cneg 0.80 0.81
(0.51) (0.51)
Cpos 0.31 0.33
(0.49) (0.49)
--------------------------------------------------
AIC 140.27 141.91 141.66 143.28
BIC 142.87 147.12 149.48 153.70
Log Likelihood -69.13 -68.95 -67.83 -67.64
Deviance 138.27 137.91 135.66 135.28
Num. obs. 100 100 100 100
==================================================
*** p < 0.001, ** p < 0.01, * p < 0.05





share|improve this answer


























  • Thanks @coffeinjunky for the two different ways of doing what I wanted. These packages for model output display are very handy

    – Darius
    Jan 1 at 11:29
















3














Just to point it out: broom gets you half-way to where you want to get by turning the model output into a dataframe, which you can then reshape.



library(broom)
bind_rows(lapply(ms, tidy), .id="key")
key term estimate std.error statistic p.value
1 null (Intercept) -0.12014431182649532 0.200 -0.59963969517107030 0.549
2 m_B (Intercept) 0.00000000000000123 0.283 0.00000000000000433 1.000
3 m_B Bwhite -0.24116205496397874 0.401 -0.60071814968372905 0.548
4 m_C (Intercept) -0.47957308026188367 0.353 -1.35892869678271544 0.174
5 m_C Cneg 0.80499548069651150 0.507 1.58784953814722285 0.112
6 m_C Cpos 0.30772282333522433 0.490 0.62856402205887851 0.530
7 m_BC (Intercept) -0.36339654526926718 0.399 -0.90984856337213305 0.363
8 m_BC Bwhite -0.25083209866475475 0.408 -0.61515191157571303 0.538
9 m_BC Cneg 0.81144822536950656 0.508 1.59682131202527056 0.110
10 m_BC Cpos 0.32706970242195277 0.492 0.66527127770403538 0.506


And if you must insist of the layout of your table, I came up with the following (arguably clumsy) way of rearranging everything:



out <- bind_rows(lapply(ms, tidy), .id="mod")

t1 <- out %>% select(mod, term, estimate) %>% spread(term, estimate) %>% base::t
t2 <- out %>% select(mod, term, std.error) %>% spread(term, std.error) %>% base::t
rownames(t2) <- paste0(rownames(t2), "_std_e")
tmp <- rbind(t1, t2[-1,])
new_t <- as.data.frame(tmp[-1,])
colnames(new_t) <- tmp[1,]
new_t


Alternatively, you may want to familiarise yourself with packages that are meant to display model output for publication, e.g. texreg or stargazer come to mind:



library(texreg)
screenreg(ms)

==================================================
null m_B m_C m_BC
--------------------------------------------------
(Intercept) -0.12 0.00 -0.48 -0.36
(0.20) (0.28) (0.35) (0.40)
Bwhite -0.24 -0.25
(0.40) (0.41)
Cneg 0.80 0.81
(0.51) (0.51)
Cpos 0.31 0.33
(0.49) (0.49)
--------------------------------------------------
AIC 140.27 141.91 141.66 143.28
BIC 142.87 147.12 149.48 153.70
Log Likelihood -69.13 -68.95 -67.83 -67.64
Deviance 138.27 137.91 135.66 135.28
Num. obs. 100 100 100 100
==================================================
*** p < 0.001, ** p < 0.01, * p < 0.05





share|improve this answer


























  • Thanks @coffeinjunky for the two different ways of doing what I wanted. These packages for model output display are very handy

    – Darius
    Jan 1 at 11:29














3












3








3







Just to point it out: broom gets you half-way to where you want to get by turning the model output into a dataframe, which you can then reshape.



library(broom)
bind_rows(lapply(ms, tidy), .id="key")
key term estimate std.error statistic p.value
1 null (Intercept) -0.12014431182649532 0.200 -0.59963969517107030 0.549
2 m_B (Intercept) 0.00000000000000123 0.283 0.00000000000000433 1.000
3 m_B Bwhite -0.24116205496397874 0.401 -0.60071814968372905 0.548
4 m_C (Intercept) -0.47957308026188367 0.353 -1.35892869678271544 0.174
5 m_C Cneg 0.80499548069651150 0.507 1.58784953814722285 0.112
6 m_C Cpos 0.30772282333522433 0.490 0.62856402205887851 0.530
7 m_BC (Intercept) -0.36339654526926718 0.399 -0.90984856337213305 0.363
8 m_BC Bwhite -0.25083209866475475 0.408 -0.61515191157571303 0.538
9 m_BC Cneg 0.81144822536950656 0.508 1.59682131202527056 0.110
10 m_BC Cpos 0.32706970242195277 0.492 0.66527127770403538 0.506


And if you must insist of the layout of your table, I came up with the following (arguably clumsy) way of rearranging everything:



out <- bind_rows(lapply(ms, tidy), .id="mod")

t1 <- out %>% select(mod, term, estimate) %>% spread(term, estimate) %>% base::t
t2 <- out %>% select(mod, term, std.error) %>% spread(term, std.error) %>% base::t
rownames(t2) <- paste0(rownames(t2), "_std_e")
tmp <- rbind(t1, t2[-1,])
new_t <- as.data.frame(tmp[-1,])
colnames(new_t) <- tmp[1,]
new_t


Alternatively, you may want to familiarise yourself with packages that are meant to display model output for publication, e.g. texreg or stargazer come to mind:



library(texreg)
screenreg(ms)

==================================================
null m_B m_C m_BC
--------------------------------------------------
(Intercept) -0.12 0.00 -0.48 -0.36
(0.20) (0.28) (0.35) (0.40)
Bwhite -0.24 -0.25
(0.40) (0.41)
Cneg 0.80 0.81
(0.51) (0.51)
Cpos 0.31 0.33
(0.49) (0.49)
--------------------------------------------------
AIC 140.27 141.91 141.66 143.28
BIC 142.87 147.12 149.48 153.70
Log Likelihood -69.13 -68.95 -67.83 -67.64
Deviance 138.27 137.91 135.66 135.28
Num. obs. 100 100 100 100
==================================================
*** p < 0.001, ** p < 0.01, * p < 0.05





share|improve this answer















Just to point it out: broom gets you half-way to where you want to get by turning the model output into a dataframe, which you can then reshape.



library(broom)
bind_rows(lapply(ms, tidy), .id="key")
key term estimate std.error statistic p.value
1 null (Intercept) -0.12014431182649532 0.200 -0.59963969517107030 0.549
2 m_B (Intercept) 0.00000000000000123 0.283 0.00000000000000433 1.000
3 m_B Bwhite -0.24116205496397874 0.401 -0.60071814968372905 0.548
4 m_C (Intercept) -0.47957308026188367 0.353 -1.35892869678271544 0.174
5 m_C Cneg 0.80499548069651150 0.507 1.58784953814722285 0.112
6 m_C Cpos 0.30772282333522433 0.490 0.62856402205887851 0.530
7 m_BC (Intercept) -0.36339654526926718 0.399 -0.90984856337213305 0.363
8 m_BC Bwhite -0.25083209866475475 0.408 -0.61515191157571303 0.538
9 m_BC Cneg 0.81144822536950656 0.508 1.59682131202527056 0.110
10 m_BC Cpos 0.32706970242195277 0.492 0.66527127770403538 0.506


And if you must insist of the layout of your table, I came up with the following (arguably clumsy) way of rearranging everything:



out <- bind_rows(lapply(ms, tidy), .id="mod")

t1 <- out %>% select(mod, term, estimate) %>% spread(term, estimate) %>% base::t
t2 <- out %>% select(mod, term, std.error) %>% spread(term, std.error) %>% base::t
rownames(t2) <- paste0(rownames(t2), "_std_e")
tmp <- rbind(t1, t2[-1,])
new_t <- as.data.frame(tmp[-1,])
colnames(new_t) <- tmp[1,]
new_t


Alternatively, you may want to familiarise yourself with packages that are meant to display model output for publication, e.g. texreg or stargazer come to mind:



library(texreg)
screenreg(ms)

==================================================
null m_B m_C m_BC
--------------------------------------------------
(Intercept) -0.12 0.00 -0.48 -0.36
(0.20) (0.28) (0.35) (0.40)
Bwhite -0.24 -0.25
(0.40) (0.41)
Cneg 0.80 0.81
(0.51) (0.51)
Cpos 0.31 0.33
(0.49) (0.49)
--------------------------------------------------
AIC 140.27 141.91 141.66 143.28
BIC 142.87 147.12 149.48 153.70
Log Likelihood -69.13 -68.95 -67.83 -67.64
Deviance 138.27 137.91 135.66 135.28
Num. obs. 100 100 100 100
==================================================
*** p < 0.001, ** p < 0.01, * p < 0.05






share|improve this answer














share|improve this answer



share|improve this answer








edited Dec 31 '18 at 15:20

























answered Dec 31 '18 at 14:39









coffeinjunkycoffeinjunky

7,2992042




7,2992042













  • Thanks @coffeinjunky for the two different ways of doing what I wanted. These packages for model output display are very handy

    – Darius
    Jan 1 at 11:29



















  • Thanks @coffeinjunky for the two different ways of doing what I wanted. These packages for model output display are very handy

    – Darius
    Jan 1 at 11:29

















Thanks @coffeinjunky for the two different ways of doing what I wanted. These packages for model output display are very handy

– Darius
Jan 1 at 11:29





Thanks @coffeinjunky for the two different ways of doing what I wanted. These packages for model output display are very handy

– Darius
Jan 1 at 11:29


















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53986811%2fhow-to-build-a-summary-table-of-glms-parameters-and-aiccwt%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Monofisismo

Angular Downloading a file using contenturl with Basic Authentication

Olmecas