Sehr geehrte Stackoverflow-CommunityR: Wie mache ich eine nette Ergebnistabelle (sjPlot) aus der lapply-Ausgabe mehrerer GLMs?
Ich arbeite an einer Studie in R, wo ich mehrere binomiale logistische Regressionen mit verschiedenen Abhängigen mache. Diese Analysen werden wiederholt mit geringfügigen Änderungen durchgeführt, und ich teile die Ergebnisse mit meinen Mitarbeitern, am besten in schön aussehenden Tabellen und nicht unordentlichen R-Ergebnissen. Wenn ich das nur ein paar Mal machen würde, könnte ich einfach alle Analysen als einzelne Regressionen durchführen und dann sjt.glm verwenden, um die netten Tabellen zu erstellen. Obwohl ich diese ähnlichen Analysen immer wieder mache, verwende ich Lapply-Schleifen, um den Prozess zu beschleunigen und zu vereinfachen. Leider bin ich nicht in der Lage, lappply und sjt.glm zusammenzuarbeiten. Optimal würde ich einfach die Ergebnisse aus der Lapply-Schleife nehmen und eine nette horizontal ausgerichtete Tabelle mit sjt.glm erstellen.
Siehe das Beispiel (und sorry für die hässliche Codierung)
library(sjPlot)
swiss$y1 <- ifelse(swiss$Fertility < median(swiss$Fertility), 0, 1)
swiss$y2 <- ifelse(swiss$Infant.Mortality < median(swiss$Infant.Mortality), 0, 1)
swiss$y3 <- ifelse(swiss$Agriculture < median(swiss$Agriculture), 0, 1)
#Normal slow way would be
fitOR1 <- glm(y1 ~ Education + Examination + Catholic, data = swiss,
family = binomial(link = "logit"))
fitOR2 <- glm(y2 ~ Education + Examination + Catholic, data = swiss,
family = binomial(link = "logit"))
fitOR3 <- glm(y3 ~ Education + Examination + Catholic, data = swiss,
family = binomial(link = "logit"))
#and then simply use summary and other formulas to look at the results
summary(fitOR1);exp(cbind(OR = coef(fitOR1), confint(fitOR1)))
#but with 20+ dependents, this would become tedious
#Doing the same analysis as a laply loop, is relatively easy (and non-tedious)
varlist <- names(swiss[c(7:9)])
results <- lapply(varlist, function(x){
glm(substitute(i ~ Education + Examination + Catholic, list(i=as.name(x))),
family =binomial, data = swiss)})
for (i in 1:3) print(summary(results[[i]]))
for (i in 1:3) print(exp(cbind(OR = coef(results[[i]]), confint(results[[i]]))))
#Though here is the catch. To get the output/results into a nice table
#I can easily use sjt.glm for the "standard" single logistic regressions.
sjt.glm(fitOR1,fitOR2,fitOR3, file = "SwissFits.html")
#Though I can't think of how I could do this for the loop-results.
#The closest I have come is perhaps something like
for(i in 1:3)(sjt.glm(results[[i]],file="LoopSwissFits.html"))
#but then I only get the results from the last regression.
#One alternative is to do
lapply(varlist,function(x){ sjt.glm(
glm(substitute(i ~ Education + Examination + Catholic, list(i=as.name(x))),
family =binomial, data = swiss), file = paste0("SwissFits_",(i=as.name(x)),".html"))})
#but then I get three separate files, when it would be preferable to
#have the results in one horizontally oriented file
Hat jemand von euch ordentlich und elegante Lösung für mein Problem?
Vielen Dank im Voraus!
'do.call (was = sjt.glm, args = c (Ergebnisse, Datei =" LoopSwissFits.html "))'? – Gregor
Funktioniert es für Sie? Wenn ich es laufe bekomme ich nur Fehler in eval (x $ expr, Daten, x $ env): Variablennamen sind auf 10000 Bytes begrenzt – TAH