Home > Mobile >  How to create an APA format output with GAMM4 package in R?
How to create an APA format output with GAMM4 package in R?

Time:03-06

What is the best way to output gamm4::gamm4 results to a Word table?

I used as_flextable for the mgcv::gam outputs but I can't find a package that works with gamm4 is there any workaround other than just copy pasting from R and reformatting in Word?

sample model:

m2 <- gamm4(Mean_DTI ~ s(Age, by = Sex)   Sex,
            data = DF, 
            family = gaussian, 
            random = ~ (1|ID))

sample data:

structure(list(ID = structure(c(1L, 2L, 9L, 10L, 11L, 12L, 14L, 
15L, 16L, 19L, 20L, 22L, 23L, 24L, 25L, 26L, 27L, 28L, 29L, 30L, 
31L, 32L, 33L, 35L, 36L, 37L, 38L, 39L, 40L, 42L, 43L, 46L, 47L, 
48L, 49L, 50L, 51L, 52L, 53L, 55L, 56L, 57L, 58L, 60L, 61L, 62L, 
66L, 67L, 68L, 71L, 72L, 73L, 74L, 75L, 81L, 84L, 85L, 93L, 95L, 
96L, 104L, 107L, 109L, 116L, 126L, 133L, 135L, 138L, 139L, 146L, 
149L, 153L, 163L, 164L, 1L, 2L, 9L, 10L, 11L, 12L, 14L, 15L, 
16L, 19L, 20L, 22L, 23L, 24L, 25L, 26L, 27L, 28L, 29L, 30L, 31L, 
32L, 33L, 35L, 36L, 37L, 38L, 39L, 40L, 42L, 43L, 46L, 47L, 48L, 
49L, 50L, 51L, 52L, 53L, 55L, 56L, 57L, 58L, 60L, 61L, 62L, 66L, 
67L, 68L, 71L, 72L, 73L, 74L, 75L, 81L, 84L, 85L, 93L, 95L, 96L, 
104L, 107L, 109L, 116L, 126L, 133L, 135L, 138L, 139L, 146L, 149L, 
153L, 163L, 164L, 1L, 2L, 9L, 10L, 11L, 12L, 14L, 15L, 16L, 19L, 
20L, 22L, 23L, 24L, 25L, 26L, 27L, 28L, 29L, 30L, 31L, 32L, 33L, 
35L, 36L, 37L, 38L, 39L, 40L, 42L, 43L, 46L, 47L, 48L, 49L, 50L, 
51L, 52L, 53L, 55L, 56L, 57L, 58L, 60L, 61L, 62L, 66L, 67L, 68L, 
71L, 72L, 73L, 74L, 75L, 81L, 84L, 85L, 93L, 95L, 96L, 104L, 
107L, 109L, 116L, 126L, 133L, 135L, 138L, 139L, 146L, 149L, 153L, 
163L, 164L), .Label = c("33714", "35377", "38623", "38806", "39593", 
"39820", "39951", "40286", "40556", "40798", "40800", "40815", 
"43762", "50848", "52183", "52461", "52577", "53202", "53320", 
"53873", "54153", "54206", "54581", "55122", "55267", "55462", 
"55612", "55920", "56022", "56307", "56420", "56679", "57405", 
"57445", "57480", "57725", "57809", "58004", "58215", "58229", 
"58503", "59326", "59327", "59344", "59361", "59865", "60099", 
"60100", "60280", "60384", "60429", "60493", "60503", "60575", 
"60603", "60664", "60846", "61415", "61656", "61749", "61883", 
"62081", "62210", "62285", "62937", "62983", "63327", "63329", 
"64081", "64328", "64418", "64507", "64596", "65178", "65250", 
"65302", "65478", "65480", "65487", "65572", "65802", "65935", 
"65974", "65975", "65978", "65991", "65995", "66013", "66154", 
"66237", "66245", "66389", "66396", "66460", "66572", "66589", 
"67174", "73230", "73525", "73539", "73677", "73942", "73953", 
"74034", "74113", "74114", "74427", "74439", "74607", "74641", 
"74657", "74794", "74800", "74836", "74942", "74952", "74962", 
"74969", "74977", "74985", "74989", "75220", "75229", "75407", 
"75653", "75732", "75735", "75757", "75895", "75898", "76381", 
"76559", "76574", "76594", "76595", "76746", "76751", "76755", 
"76759", "76775", "77088", "77091", "77099", "77134", "77188", 
"77203", "77252", "77304", "77453", "77528", "77556", "77585", 
"77668", "78262", "79724", "79730", "79850", "79977", "80052", 
"80819", "80901", "80932", "81064", "81065", "81071", "81098", 
"81142", "81175"), class = "factor"), Sex = structure(c(1L, 2L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 
1L, 2L, 1L, 2L, 2L, 1L, 2L, 2L, 1L, 2L, 1L, 1L, 2L, 2L, 1L, 1L, 
1L, 2L, 2L, 2L, 1L, 1L, 2L, 2L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 10L, 14L, 15L, 13L, 15L, 13L, 13L, 
8L, 11L, 16L, 16L, 11L, 13L, 18L, 10L, 18L, 18L, 15L, 10L, 8L, 
15L, 12L, 16L, 14L, 16L, 9L, 11L, 11L, 10L, 10L, 11L, 14L, 12L, 
8L, 9L, 9L, 8L, 16L, 9L, 13L, 16L, 13L, 12L, 18L, 13L, 11L, 8L, 
14L, 12L, 13L, 14L, 11L, 14L, 15L, 14L, 18L, 11L, 14L, 12L, 12L, 
13L, 11L, 11L, 15L, 17L, 16L, 15L, 15L, 14L, 11L, 13L, 10L, 11L, 
18L, 11L, 15L, 16L, 14L, 17L, 14L, 14L, 9L, 12L, 18L, 18L, 12L, 
14L, 19L, 11L, 19L, 19L, 16L, 11L, 9L, 17L, 13L, 18L, 16L, 18L, 
11L, 12L, 12L, 11L, 11L, 12L, 16L, 13L, 9L, 11L, 10L, 9L, 17L, 
11L, 14L, 17L, 14L, 13L, 19L, 15L, 12L, 9L, 15L, 14L, 14L, 15L, 
12L, 16L, 17L, 15L, 20L, 12L, 15L, 13L, 13L, 14L, 12L, 12L, 16L, 
19L, 18L, 16L, 16L, 15L, 12L, 14L, 11L, 11L, 19L, 12L, 16L, 17L, 
15L, 18L, 15L, 15L, 10L, 13L), Mean_DTI = c(-3.91040964443843, 
-4.1357309415226, -3.15711439697149, -5.3057953682613, -5.64125379300701, 
-5.14501797062188, 0.244858660867345, 0.0760011434854038, -1.38050716781916, 
-0.765398953185992, -1.47720169386702, -1.64309892713909, -0.396819316883117, 
-1.36975793945299, -1.80785572309015, -0.401914887891913, -0.811404680008261, 
-2.4492910546021, -0.620903118150862, -1.10304119407832, -0.683772758549858, 
-1.47159656575735, -0.823876696667884, 0.229547683217108, -1.91347963071532, 
-1.05594355918274, -2.37962731952471, -1.34893889218848, -0.385827442278431, 
-0.980092345023242, -0.181058281596406, -0.832975930612162, -1.9959308225624, 
-0.12787022611412, -0.205371434695515, -0.609353157197594, -0.477183751078979, 
-1.53245224237668, -0.137187841673061, -1.11978378453579, -1.38506891710323, 
-0.665622819909006, -0.858065551674517, -0.935493966384356, -0.444790478237349, 
-0.0888284463372063, 0.793433276899957, -1.37118955226023, -1.27752810419379, 
0.571630350274245, 0.38391922015976, -0.585743678190174, -0.639562613892594, 
0.637702921021628, 1.02283103079116, 0.509051885361466, 0.256796855802238, 
-0.827467860997893, 1.25436407158129, 0.797121499725372, -1.46470541258355, 
0.72639012119852, -1.77912640845008, 1.31912635264069, 0.135328148802092, 
-0.522413009938002, 0.680529982119362, 0.068624697834574, -0.598312753345204, 
0.141370040141094, -0.392378890718311, 1.88269650621348, -3.0968410713246, 
-0.0622101539722123, -4.1452184094485, -0.954638754603117, -0.106347504757922, 
-0.716821176235476, 0.33910245990621, -1.65370256776216, 0.666505029533247, 
1.11178514183042, 0.0370564222038934, 0.0466166840013479, -0.557572449898689, 
-0.961796818639283, 0.936133529770083, -0.283139554139275, -0.111006312537392, 
0.655416096433155, 0.253351279215336, -1.27117077274472, 0.821677299062997, 
1.30061244464207, -0.571112110007776, -0.978927643078376, -0.0939968112175565, 
1.01351341523222, -0.482667556069396, 0.416191169882131, -1.13218300732386, 
0.0109719515636322, 0.570902411558704, 0.251628490921888, 0.757376045856767, 
-0.286245425992256, -0.850640576775985, 0.323694423760568, 0.33667599752107, 
0.489882832518856, 0.0192219236731108, -0.267852841112888, 1.81167395220041, 
-0.0196014744891418, -0.0975879755475657, 1.11193072957353, 1.43135023795345, 
-0.0901872652728838, 0.562191411596049, 0.0591857591563773, 1.10166679368438, 
-0.140293713526042, 0.0486791770287176, 0.34512008662136, 0.818328780971503, 
-0.238419852381131, -0.368114266866903, 1.03486628422146, 0.539091489689507, 
-0.233227222876931, -0.351614322647946, -0.108579850152251, 1.42892377556831, 
0.96726504217144, -0.436055213650844, 0.565831105173759, -1.54960733143962, 
1.100259445501, -0.614861226811863, -0.361659876922429, 1.40706134947819, 
-0.552573937385301, 0.105482661464863, -0.374326010572864, -0.0292830594058542, 
1.98545718822419, -1.19221368673224, 0.760991474810628, -0.539568099000947, 
-0.0739542319162944, 0.34373700306183, 0.053071073945822, 0.0606901658351647, 
-0.211219209043705, 0.216590374080456, 1.00007081361854, -0.495649129829898, 
0.0867746364754286, -1.67723925289802, -0.826836980777755, 1.18863120556783, 
-0.795681203752549, -0.650869928607352, 0.333545861044237, 0.63103014946249, 
-1.75748236397463, -0.118601139802882, 1.51474775013074, 0.132537717059181, 
-0.677973513449372, 0.467947612557183, 1.56786301174147, -0.118528345931329, 
0.554596584330558, -0.59224659738235, -0.543498968064875, -0.380440695783417, 
0.0643541240367275, -0.210830975062082, 0.503471021875644, -0.660672836643319, 
1.50676468888363, 0.780839937121078, -0.116635705270918, -0.240385286913095, 
0.00044110481212036, 1.4571920623552, -0.350401091455376, 0.0850033189342734, 
0.197882349091022, 0.726511444317778, -0.331741595713643, 0.837497833814114, 
0.609264781867777, 0.532151807268005, 0.65446977610295, -0.581909867621652, 
0.322942220421174, 1.52993740466172, -0.264455793773691, -0.0568476721010507, 
1.05299195823846, 0.915435805624833, 0.297609953120304, -0.114257772133482, 
0.248013061968027, 1.64822744593733, 1.26800079018578, -0.0459528559917708, 
0.720784993088846, -1.05679282101754, 1.38345187047077, -0.64414862780051, 
-1.25411274217718, 0.177548594303543, -0.257734492966852, -0.818028922319694, 
0.409736779937656, -0.216411838547905, 1.32536236097051, -0.500938817829506, 
0.803770006660658)), row.names = c(NA, -222L), class = c("tbl_df", 
"tbl", "data.frame"))

CodePudding user response:

You can use the gtsummary::tbl_regression() to summarize the lme4 portion and mgcv portions of the model separately, then stack the results using gtsummary::tbl_stack(). I took this example from the ?gamm4::gamm4() help file.

library(gtsummary)
library(gamm4)
packageVersion("gtsummary")
#> [1] '1.5.2'

set.seed(0) 
dat <- gamSim(1,n=400,scale=2) ## simulate 4 term additive truth
dat$fac <- fac <- as.factor(sample(1:20,400,replace=TRUE))
dat$y <- dat$y   model.matrix(~fac-1)%*%rnorm(20)*.5

br <- gamm4(y~s(x0) x1 s(x2),data=dat,random=~(1|fac))

tbl <-
  br %>%
  lapply(tbl_regression) %>% # summarize lme4 and gam models separately
  tbl_stack(group_header = c("Random Effects Portion", "GAM Portion")) # stack model summaries

enter image description here Created on 2022-03-05 by the reprex package (v2.0.1)

  • Related