親愛なるStackoverflowのコミュニティR:いくつかのGLMのlapply出力から素晴らしい結果テーブル(sjPlot)を作成するにはどうすればよいですか?
私はRの研究に取り組んでいます。私はいくつかの二項ロジスティック回帰をさまざまな扶養家族で行っています。これらの分析はマイナーチェンジで繰り返し行われ、結果は私の同僚と、最適に見栄えの良いテーブルで、面倒なR結果ではなく共有しています。私がこれをほんの数回だけ行うつもりならば、すべての分析を単一の回帰として行い、次にsjt.glmを使って素敵なテーブルを作ることができます。これらの同様の分析を何度も繰り返していますが、ループアップを使用して処理を高速化して簡素化しています。残念ながら私はlappplyとsjt.glmを協力させることはできません。最適には、lapplyループから結果を取り出し、sjt.glmを使って水平に整列したテーブルを作成します。
は、例を参照してください(と醜いコーディングのため申し訳ありません)
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
あなたのいずれかが私の問題にきちんとしてエレガントな解決策を持っていますか?
ありがとうございます!
'do.call(what = sjt.glm、args = c(results、file =" LoopSwissFits.html "))'? – Gregor
あなたのために働きますか?私はそれを実行するときだけ私は得る eval(x $ expr、data、x $ env)のエラー: 変数名は10000バイトに制限されています – TAH