Rで解析:論文用統計表とフォレストプロットを自動生成できる「summata」パッケージの紹介
医学や生物学などの研究では、解析結果を論文用の表やフォレストプロットにまとめる作業に手間がかかります。「summata」パッケージは、記述統計から多変量解析までを一貫した操作で実行し、そのまま出版に使える高品質な図表を生成します。線形モデルやCox比例ハザードモデル、一般化線形モデルなど多様な統計モデルに対応し、PDFやWord、PowerPointなど複数の形式への書き出しが可能です。また、data.tableを基盤とした処理により、変数の多い結果でも効率的に表を作成できます。本パッケージの利用で、論文用の表や図の作成を効率化できるのではないかと考えます。
パッケージバージョンは0.12.0。Windows 11 x64 (build 26200)のR version 4.6.1で確認しています。
<おすすめのRに関する書籍です>
パッケージのインストール
下記コマンドを実行してください。
# パッケージのインストール
install.packages("summata")
# パッケージの読み込み
library("summata")
# コマンド例実行で必要なパッケージを読み込み
library("survival")コマンド例
詳細はコメント、パッケージのヘルプを確認してください。
Cox proportional hazards(Cox比例ハザードモデル)は、生存時間解析において、ある事象が発生するまでの時間を解析するためのモデルです。 Generalized linear models(一般化線形モデル)は、線形モデルを拡張し、正規分布以外の分布を扱うための統計モデルです。
これらの関数は、さまざまな統計モデルの解析結果をフォレストプロットとして作成・保存するための機能群です。特に、モデルの種類に応じたプロット作成から、最終的なPDF形式への出力までの一連の流れをサポートします。
表のPDF形式への出力:table2pdfコマンド
表形式のデータを、論文やレポートにそのまま引用可能な高品質なPDF形式で出力します。
| オプション | 意味 | 初期値 |
|---|---|---|
| table | 出力するデータフレーム、データテーブル、または行列 | なし |
| file | 出力するPDFのファイル名の指定 | なし |
| orientation | “portrait” または “landscape” によるページ方向の指定 | “portrait” |
| paper | “letter”、”a4″、”auto”のいずれかの用紙サイズを指定 | “letter” |
| margins | 上下左右の余白をインチ単位で指定 | NULL |
| fit_to_page | テキスト幅に合わせて表を縮小するかどうかの指定 | TRUE |
| font_size | 表の基本フォントサイズをポイント単位で指定 | 8 |
| caption | 表の下に表示するキャプションの文字列 | NULL |
| caption_size | キャプションのフォントサイズをポイント単位で指定 | NULL |
| format_headers | 列ヘッダーへの自動書式設定の適用有無 | TRUE |
| variable_padding | 変数間の視覚的なグループ化のための垂直間隔の追加 | FALSE |
| cell_padding | セル内の垂直方向の余白の指定 | “normal” |
| bold_significant | 有意水準以下のp値への太字の適用 | TRUE |
| bold_variables | 変数名を太字で表示するかどうかの指定 | FALSE |
| p_threshold | p値を太字にするための閾値 | 0.05 |
| align | 列の配置(”l”、”c”、”r”)の指定 | NULL |
| indent_groups | 階層構造を表示するためのインデントの有無 | FALSE |
| condense_table | 連続変数やバイナリ変数をまとめて表を簡略化するか | FALSE |
| condense_quantitative | 連続変数と生存変数を1行にまとめ、カテゴリ変数は維持するか | FALSE |
| zebra_stripes | 視認性向上のため変数ごとに背景色を交互に変えるか | FALSE |
| stripe_color | ゼブラストライプ用のLaTeXカラー指定 | “gray!20” |
| dark_header | ヘッダー行を黒背景に白文字にするか | FALSE |
| show_logs | PDF作成後にLaTeXのログや補助ファイルを残すか | FALSE |
| quiet | 進捗や確認メッセージの表示を抑制するかどうか | FALSE |
| … | xtableに渡す追加の引数 | なし |
# 臨床試験データセットを読み込む
data(clintrial)
# 変数ラベルのデータセットを読み込む
data(clintrial_labels)
# 例示用の回帰モデル結果を作成する
results <- fit(
data = clintrial,
outcome = "readmission_30d",
predictors = c("age", "sex", "treatment", "stage"),
labels = clintrial_labels
)
# LaTeXコンパイラが正常に動作するかテストする
has_latex <- local({
# pdflatexコマンドが存在しない場合はFALSEを返す
if (!nzchar(Sys.which("pdflatex"))) return(FALSE)
# テスト用のLaTeXファイルのパスを生成する
test_tex <- file.path(tempdir(), "summata_latex_test.tex")
# 最小限のLaTeXドキュメントをファイルに書き込む
writeLines(c(
"\\documentclass{article}", "\\usepackage{booktabs}",
"\\begin{document}", "test", "\\end{document}"
), test_tex)
# pdflatexを実行し、終了コードが0(成功)かどうかを判定する
tryCatch(
system2("pdflatex", c(
"-interaction=nonstopmode",
paste0("-output-directory=", tempdir()), test_tex
),
stdout = FALSE, stderr = FALSE
),
error = function(e) 1L
) == 0L
})
# 例1: 基本的なPDFエクスポート
if (has_latex) {
# 結果をPDFファイルとして保存する
table2pdf(results, file.path(tempdir(), "basic_results.pdf"))
}
# 例2: 広い表向けの横方向レイアウト
if (has_latex) {
# 横方向(landscape)のPDFを生成する
table2pdf(results, file.path(tempdir(), "wide_results.pdf"),
orientation = "landscape"
)
}
# 例3: キャプション付き
if (has_latex) {
# キャプションを指定してPDFを生成する
table2pdf(results, file.path(tempdir(), "captioned.pdf"),
caption = "Table 1: Multivariable logistic regression results"
)
}
# 例4: フォーマット付きの複数行キャプション
if (has_latex) {
# 改行と注釈を含むキャプションでPDFを生成する
table2pdf(results, file.path(tempdir(), "formatted_caption.pdf"),
caption = "Table 1: Risk Factors for Mortality\\\\
aOR = adjusted odds ratio; CI = confidence interval"
)
}
# 例5: 自動サイズ調整PDF(固定ページサイズなし)
if (has_latex) {
# ページサイズを自動調整してPDFを生成する
table2pdf(results, file.path(tempdir(), "autosize.pdf"),
paper = "auto"
)
}
# 例6: A4用紙とカスタム余白
if (has_latex) {
# A4サイズで余白を指定してPDFを生成する
table2pdf(results, file.path(tempdir(), "a4_custom.pdf"),
paper = "a4",
margins = c(0.75, 0.75, 0.75, 0.75)
)
}
# 例7: 可読性向上のための大きなフォント
if (has_latex) {
# フォントサイズを11ptに設定してPDFを生成する
table2pdf(results, file.path(tempdir(), "large_font.pdf"),
font_size = 11
)
}
# 例8: 階層表示のインデント
if (has_latex) {
# グループをインデント表示してPDFを生成する
table2pdf(results, file.path(tempdir(), "indented.pdf"),
indent_groups = TRUE
)
}
# 例9: 凝縮された表(高さ削減)
if (has_latex) {
# 表を凝縮表示してPDFを生成する
table2pdf(results, file.path(tempdir(), "condensed.pdf"),
condense_table = TRUE
)
}
# 例10: ゼブラストライプ付き
if (has_latex) {
# 縞模様(ゼブラストライプ)を適用してPDFを生成する
table2pdf(results, file.path(tempdir(), "striped.pdf"),
zebra_stripes = TRUE,
stripe_color = "gray!15"
)
}
# 例11: ダークヘッダースタイル
if (has_latex) {
# ヘッダーをダークスタイルにしてPDFを生成する
table2pdf(results, file.path(tempdir(), "dark_header.pdf"),
dark_header = TRUE
)
}
# 例12: フォーマットオプションの組み合わせ
if (has_latex) {
# 複数のフォーマットオプションを組み合わせる
table2pdf(results, file.path(tempdir(), "publication_ready.pdf"),
orientation = "portrait",
paper = "letter",
font_size = 9,
caption = "Table 2: Multivariable Analysis\\\\
Model adjusted for age, sex, and clinical factors",
indent_groups = TRUE,
zebra_stripes = TRUE,
bold_significant = TRUE,
p_threshold = 0.05
)
}
# 例13: セルパディングの調整
if (has_latex) {
# セルパディングを「relaxed」に設定してPDFを生成する
table2pdf(results, file.path(tempdir(), "relaxed_padding.pdf"),
cell_padding = "relaxed"
)
}
# 例14: スケーリングなし(自然な表幅)
if (has_latex) {
# ページにフィットさせず自然な幅でPDFを生成する
table2pdf(results, file.path(tempdir(), "no_scale.pdf"),
fit_to_page = FALSE,
font_size = 10
)
}
# 例15: 有意性の太字表示を非表示
if (has_latex) {
# 有意な結果の太字表示を無効にしてPDFを生成する
table2pdf(results, file.path(tempdir(), "no_bold.pdf"),
bold_significant = FALSE
)
}
# 例16: カスタムな列揃え
if (has_latex) {
# 全列を中央揃えにしてPDFを生成する
table2pdf(results, file.path(tempdir(), "custom_align.pdf"),
align = c("c", "c", "c", "c", "c", "c", "c")
)
}
# 例17: 記述統計表
if (has_latex) {
# 治療グループ別の記述統計表を作成する
desc_table <- desctable(clintrial,
by = "treatment",
variables = c("age", "sex", "bmi", "stage"), labels = clintrial_labels
)
# 記述統計表をPDFとして保存する
table2pdf(desc_table, file.path(tempdir(), "descriptive.pdf"),
caption = "Table 1: Baseline Characteristics by Treatment Group",
orientation = "landscape"
)
}
# 例18: モデル比較表
if (has_latex) {
# 比較対象のモデルリストを定義する
models <- list(
base = c("age", "sex"),
full = c("age", "sex", "bmi", "treatment")
)
# 情報基準は共通サンプルを前提とするため、候補予測変数を完全ケースに制限する
comparison_data <- na.omit(clintrial[, c("os_status", "age", "sex", "bmi", "treatment")])
# モデル比較を実行する
comparison <- compfit(
data = comparison_data,
outcome = "os_status",
model_list = models,
verbose = FALSE
)
# モデル比較結果をPDFとして保存する
table2pdf(comparison, file.path(tempdir(), "model_comparison.pdf"),
caption = "Table 3: Model Comparison Statistics"
)
}
# 例19: 非常に広い表の積極的なフィット
if (has_latex) {
# 多くの予測変数を含む広いモデルを作成する
wide_model <- fit(
data = clintrial,
outcome = "os_status",
predictors = c(
"age", "sex", "race", "bmi", "smoking",
"hypertension", "diabetes", "treatment", "stage"
)
)
# 広い表をページにフィットさせるよう設定してPDFを生成する
table2pdf(wide_model, file.path(tempdir(), "very_wide.pdf"),
orientation = "landscape",
font_size = 7,
fit_to_page = TRUE,
condense_table = TRUE
)
}
# 例20: キャプションサイズ制御
if (has_latex) {
# キャプションのフォントサイズを個別に制御してPDFを生成する
table2pdf(results, file.path(tempdir(), "caption_size.pdf"),
font_size = 8,
caption_size = 6,
caption = "Table 4: Results with Compact Caption\\\\
Smaller caption fits better on constrained pages"
)
}
# 例21: トラブルシューティング - ログ保持
if (has_latex) {
# ログ出力を有効にしてPDFを生成する
table2pdf(results, file.path(tempdir(), "debug.pdf"),
show_logs = TRUE
)
}
モデルを自動検知したフォレストプロットの作成:autoforestコマンド
モデルの種類を自動的に判別し、適切な統計量(係数、ハザード比、オッズ比など)に基づいてフォレストプロットを生成します。
| オプション | 意味 | 初期値 |
|---|---|---|
| x | glmやlm、coxphなどの適合済みモデルオブジェクトまたは特定の解析結果オブジェクト | なし |
| data | 元のデータを含むデータフレームまたはデータテーブル | NULL |
| title | プロットのタイトルとなる文字列 | NULL |
| … | 検知したモデルに応じた各forest系関数へ渡す追加の引数 | なし |
# clintrialデータを読み込む
data(clintrial)
# clintrial_labelsデータを読み込む
data(clintrial_labels)
# 例示用のモデルを作成する
# ロジスティック回帰モデルを構築する
glm_model <- glm(surgery ~ age + sex + bmi + smoking,
family = binomial, data = clintrial
)
# 例題1:ロジスティック回帰モデル
# autoforestを実行し、GLMを検知してglmforest()へ振り分ける
p <- autoforest(glm_model, data = clintrial)
Recommended plot dimensions: width = 13.3 in, height = 5.0 in
# 例題2:Cox比例ハザードモデル
# Cox比例ハザードモデルを構築する
cox_model <- coxph(Surv(os_months, os_status) ~ age + sex + treatment + stage,
data = clintrial
)
# autoforestを実行し、coxphを検知してcoxforest()へ振り分ける
plot2 <- autoforest(cox_model, data = clintrial)
Recommended plot dimensions: width = 12.9 in, height = 5.5 in
# 例題3:線形回帰モデル
# 線形回帰モデルを構築する
lm_model <- lm(biomarker_x ~ age + sex + bmi + treatment, data = clintrial)
# autoforestを実行し、lmを検知してlmforest()へ振り分ける
plot3 <- autoforest(lm_model, data = clintrial)
Recommended plot dimensions: width = 13.0 in, height = 5.0 in
# 例題4:カスタムラベルとフォーマットオプションの適用
# カスタムラベル、タイトル、縞模様、グループのインデントを設定してautoforestを実行する
plot4 <- autoforest(
cox_model,
data = clintrial,
labels = clintrial_labels,
title = "Prognostic Factors for Overall Survival",
zebra_stripes = TRUE,
indent_groups = TRUE
)
Recommended plot dimensions: width = 11.8 in, height = 6.2 in
# 例題5:fit()の結果からデータとラベルを自動抽出
# fit関数を使用して、データ、アウトカム、予測因子、ラベルを定義する
fit_result <- fit(
data = clintrial,
outcome = "surgery",
predictors = c("age", "sex", "bmi", "treatment"),
labels = clintrial_labels
)
# fitの結果からautoforestを実行する
plot5 <- autoforest(fit_result)
Recommended plot dimensions: width = 15.3 in, height = 5.0 in
# 推奨されるサイズで結果を保存する
# 指定したパスにPDFとして保存する
forestsave(plot5, file.path(tempdir(), "forest.pdf"))
Forest plot saved to C:\Users\AppData\Local\Temp\RtmpMPEZTP/forest.pdf (width = 15.3 in, height = 5.0 in)Cox比例ハザードモデルのフォレストプロット作成:coxforestコマンド
Cox比例ハザードモデルの解析結果を、ハザード比と信頼区間を用いたフォレストプロットとして可視化します。
| オプション | 意味 | 初期値 |
|---|---|---|
| x | Coxモデル、fit()、またはfullfit()のいずれかの結果オブジェクト | なし |
| data | モデルの適合に使用した元のデータを含むデータフレームまたはデータテーブル | NULL |
| title | プロット上部に表示するタイトルを指定する文字列 | “Cox Proportional Hazards Model” |
| effect_label | フォレストプロットの軸に表示する効果量のラベル | “Hazard Ratio” |
| digits | ハザード比と信頼区間の小数点以下の桁数 | 2 |
| p_digits | p値の小数点以下の桁数 | 3 |
| conf_level | 信頼区間の信頼水準(0から1の範囲) | 0.95 |
| font_size | すべてのテキスト要素の基本フォントサイズを制御する倍率 | 1 |
| annot_size | データ注釈の相対的なフォントサイズ | 3.88 |
| header_size | 列ヘッダーの相対的なフォントサイズ | 5.82 |
| title_size | メインプロットのタイトルの相対的なフォントサイズ | 23.28 |
| plot_width | レイアウト最適化のための出力幅 | NULL |
| plot_height | 出力の高さ | NULL |
| table_width | プロットの全幅に対するデータテーブルの割合(0から1の間) | 0.6 |
| show_n | グループ固有のサンプルサイズを表示するかどうか | TRUE |
| show_events | 各グループのイベント数(死亡、失敗など)を表示するかどうか | TRUE |
| indent_groups | 階層構造を作成するために因子レベルをインデントするかどうか | FALSE |
| condense_table | TRUEの場合、二値変数を非参照レベルのみに集約して表示 | FALSE |
| bold_variables | TRUEの場合、変数名を太字で表示 | FALSE |
| center_padding | 表とフォレストプロット間の水平方向の余白 | 4 |
| zebra_stripes | TRUEの場合、可読性向上のため背景に交互の網掛けを適用 | TRUE |
| ref_label | 参照カテゴリに表示する文字列 | “reference” |
| labels | 変数に対するカスタム表示ラベルの指定 | NULL |
| color | フォレストプロットにおけるハザード比の点推定値の色 | “#8A61D8” |
| qc_footer | TRUEの場合、フッターにモデルの品質管理統計を表示 | TRUE |
| units | プロット寸法の単位として “in”、”cm”、”mm” のいずれかを選択 | “in” |
| number_format | 千の位と小数の区切り文字の指定(”us”、”eu”、”space”、”none” またはカスタム指定) | NULL |
# データセットの読み込み
data(clintrial)
# ラベル情報の読み込み
data(clintrial_labels)
# サンプルモデルの作成
model1 <- coxph(
survival::Surv(os_months, os_status) ~ age + sex + treatment,
data = clintrial
)
# 例1: 基本的なCoxモデルのフォレストプロット
p <- coxforest(model1, data = clintrial)
Recommended plot dimensions: width = 12.9 in, height = 5.0 in
# 元の幅の設定を保存
old_width <- options(width = 180)
# 例2: カスタムラベルとタイトル
plot2 <- coxforest(
x = model1,
data = clintrial,
title = "Prognostic Factors for Overall Survival",
labels = clintrial_labels
)
Recommended plot dimensions: width = 13.7 in, height = 5.0 in
# 例3: インデントされたレイアウトを含む包括的なモデル
model3 <- coxph(
Surv(os_months, os_status) ~ age + sex + bmi + smoking +
treatment + stage + grade,
data = clintrial
)
plot3 <- coxforest(
x = model3,
data = clintrial,
labels = clintrial_labels,
indent_groups = TRUE,
zebra_stripes = TRUE
)
Recommended plot dimensions: width = 13.7 in, height = 8.5 in
# 例4: 多くのバイナリ予測変数のための凝縮されたレイアウト
model4 <- coxph(
Surv(os_months, os_status) ~ age + sex + smoking +
hypertension + diabetes + surgery,
data = clintrial
)
plot4 <- coxforest(
x = model4,
data = clintrial,
condense_table = TRUE,
labels = clintrial_labels
)
Recommended plot dimensions: width = 12.2 in, height = 5.2 in
# 例5: 層別化されたCoxモデル
model5 <- coxph(
Surv(os_months, os_status) ~ age + sex + treatment + strata(site),
data = clintrial
)
plot5 <- coxforest(
x = model5,
data = clintrial,
title = "Stratified by Study Site",
labels = clintrial_labels
)
Recommended plot dimensions: width = 13.7 in, height = 5.0 in
# 例6: 推奨サイズで保存
forestsave(plot5, file.path(tempdir(), "survival_forest.pdf"))
Forest plot saved to C:\Users\AppData\Local\Temp\RtmpMPEZTP/survival_forest.pdf (width = 13.7 in, height = 5.0 in)
# 幅の設定を元に戻す
options(old_width)一般化線形モデルのフォレストプロット作成:glmforestコマンド
一般化線形モデル(GLM)の解析結果を、オッズ比やリスク比などの効果量を反映したフォレストプロットとして可視化します。
| オプション | 意味 | 初期値 |
|---|---|---|
| x | 適合済みGLMオブジェクト、fit_result、またはfullfit_result | なし |
| data | モデルの適合に使用した元のデータを含むデータフレームまたはデータテーブル | NULL |
| title | グラフ上部に表示するタイトルを指定する文字列 | “Generalized Linear Model” |
| effect_label | フォレストプロットの軸に表示する効果量のラベル | NULL |
| digits | データテーブルの推定値と信頼区間の小数点以下の桁数 | 2 |
| p_digits | p値の小数点以下の桁数 | 3 |
| conf_level | 信頼区間の信頼水準(0から1の範囲) | 0.95 |
| font_size | すべてのテキスト要素の基本フォントサイズの倍率 | 1 |
| annot_size | データ注釈(変数名やセル内の値)の相対的なフォントサイズ | 3.88 |
| header_size | 列ヘッダーの相対的なフォントサイズ | 5.82 |
| title_size | メインタイトル用の相対的なフォントサイズ | 23.28 |
| plot_width | レイアウトとサイズ最適化のための出力幅 | NULL |
| plot_height | 行数に基づき自動調整される出力の高さ | NULL |
| table_width | データテーブルに割り当てる横幅の割合(0から1の間) | 0.6 |
| show_n | グループ別のサンプルサイズを表示するかの指定 | TRUE |
| show_events | 各グループのイベント数を表示するかの指定 | TRUE |
| indent_groups | 因子レベルを階層構造で表示するかの指定 | FALSE |
| condense_table | バイナリ変数を1行にまとめて表示するかの指定 | FALSE |
| bold_variables | TRUEの場合、変数名を太字で表示 | FALSE |
| center_padding | データテーブルとフォレストプロット間の水平間隔 | 4 |
| zebra_stripes | 変数のグループ化を助けるための背景色の交互配置 | TRUE |
| ref_label | 参照カテゴリの表示用文字列 | “reference” |
| labels | 変数に対するカスタム表示ラベルのリスト | NULL |
| color | フォレストプロットの推定値マーカーの色の指定 | NULL |
| exponentiate | TRUEの場合にオッズ比やリスク比として指数変換を行うかどうかの指定 | NULL |
| qc_footer | モデルの品質管理統計量をフッターに表示するかどうかの指定 | TRUE |
| units | プロット寸法の単位として”in”、”cm”、”mm”のいずれかを選択 | “in” |
| number_format | 千の位と小数の区切り文字の指定(”us”、”eu”、”space”、”none” またはカスタム指定) | NULL |
# clintrialデータを読み込む
data(clintrial)
# clintrial_labelsデータを読み込む
data(clintrial_labels)
# サンプルモデルを作成する
# ロジスティック回帰モデルを定義する
model1 <- glm(os_status ~ age + sex + bmi + treatment,
data = clintrial, family = binomial
)
# 例題1:基本的なロジスティック回帰のフォレストプロットを作成する
# glmforest関数を用いてプロットを作成する
p <- glmforest(model1, data = clintrial)
Recommended plot dimensions: width = 13.5 in, height = 5.0 in
# グラフの幅のオプションを保存する
old_width <- options(width = 180)
# 例題2:カスタム変数ラベルを適用する
# glmforest関数にタイトルとラベルを指定してプロットを作成する
plot2 <- glmforest(
x = model1,
data = clintrial,
title = "Risk Factors for Mortality",
labels = clintrial_labels
)
Recommended plot dimensions: width = 15.3 in, height = 5.0 in
# 例証3:書式設定オプションを適用したインデントレイアウト
# glmforest関数にインデント、縞模様、色、ラベルを指定してプロットを作成する
plot3 <- glmforest(
x = model1,
data = clintrial,
indent_groups = TRUE,
zebra_stripes = TRUE,
color = "#D62728",
labels = clintrial_labels
)
Recommended plot dimensions: width = 13.4 in, height = 5.2 in
# 例題4:多くのバイナリ変数のための凝縮されたレイアウト
# 複数の共変量を含むロジスティック回帰モデルを定義する
model4 <- glm(
os_status ~ age + sex + smoking + hypertension +
diabetes + surgery,
data = clintrial,
family = binomial
)
# glmforest関数でテーブルを凝縮してプロットを作成する
plot4 <- glmforest(
x = model4,
data = clintrial,
condense_table = TRUE,
labels = clintrial_labels
)
Recommended plot dimensions: width = 12.7 in, height = 5.2 in
# バイナリ変数を単一の行に表示する
# 例題5:カウントデータのためのポアソン回帰
# ポアソン回帰モデルを定義する
model5 <- glm(ae_count ~ age + treatment + diabetes + surgery,
data = clintrial,
family = poisson
)
# glmforest関数を用いてポアソン回帰のプロットを作成する
plot5 <- glmforest(
x = model5,
data = clintrial,
title = "Rate Ratios for Adverse Events",
labels = clintrial_labels
)
Recommended plot dimensions: width = 14.9 in, height = 5.0 in
# 例題6:推奨されるサイズで保存する
# プロットをPDFファイルとして保存する
forestsave(plot5, file.path(tempdir(), "forest.pdf"))
Forest plot saved to C:\Users\AppData\Local\Temp\RtmpMPEZTP/forest.pdf (width = 14.9 in, height = 5.0 in)
# グラフの幅のオプションを元の状態に戻す
options(old_width)
<おすすめのRに関する書籍です>
線形モデルのフォレストプロット作成:lmforestコマンド
線形モデル(LM)の解析結果を、回帰係数と信頼区間を用いたフォレストプロットとして可視化します。
| オプション | 意味 | 初期値 |
|---|---|---|
| x | 線形モデル、fit_result、またはfullfit_resultオブジェクト | なし |
| data | モデルの適合に使用した元のデータを含むデータフレームまたはデータテーブル | NULL |
| title | プロット上部に表示するタイトルの文字列 | “Linear Model” |
| effect_label | フォレストプロットの軸に表示する効果量のラベル | “Coefficient” |
| digits | 係数と信頼区間の小数点以下の桁数 | 2 |
| p_digits | p値の小数点以下の桁数 | 3 |
| conf_level | 信頼区間の信頼水準(0から1の範囲) | 0.95 |
| font_size | すべてのテキスト要素の基本フォントサイズを制御する倍率 | 1 |
| annot_size | データ注釈の相対的なフォントサイズ | 3.88 |
| header_size | 列ヘッダーの相対的なフォントサイズ | 5.82 |
| title_size | メインプロットのタイトルの相対的なフォントサイズ | 23.28 |
| plot_width | 指定した単位での出力幅の数値 | NULL |
| plot_height | 指定した単位での出力高さの数値 | NULL |
| table_width | プロットの全幅に対するデータテーブルの占有割合(0から1の間) | 0.6 |
| show_n | グループ固有のサンプルサイズを表示するかどうか | TRUE |
| indent_groups | 階層構造を作成し “Group” 列を非表示にするかどうかの指定 | FALSE |
| condense_table | バイナリなカテゴリ変数を1行にまとめるかどうかの指定 | FALSE |
| bold_variables | TRUEの場合、変数名を太字で表示 | FALSE |
| center_padding | 表とフォレストプロット間の水平方向の余白 | 4 |
| zebra_stripes | 異なる変数に交互のグレーの背景色を適用するかどうかの指定 | TRUE |
| ref_label | 因子変数の参照カテゴリに表示する文字列 | “reference” |
| labels | 変数に対するカスタム表示ラベルの指定 | NULL |
| units | プロット寸法の単位として “in”、”cm”、”mm” のいずれかを選択 | “in” |
| color | フォレストプロットの係数推定値の色の指定 | “#5A8F5A” |
| qc_footer | フッターにモデルの品質管理統計量を表示するかどうかの指定 | TRUE |
| number_format | 千の位と小数の区切り文字の指定(”us”、”eu”、”space”、”none” またはカスタム指定) | NULL |
# clintrialデータを読み込む
data(clintrial)
# clintrial_labelsデータを読み込む
data(clintrial_labels)
# 例題1用のモデルを作成する
# 線形モデル(lm)を定義する
model1 <- lm(bmi ~ age + sex + smoking, data = clintrial)
# 例題1:基本的な線形モデルのフォレストプロットを作成する
# lmforest関数を実行する
p <- lmforest(model1, data = clintrial)
Recommended plot dimensions: width = 13.0 in, height = 5.0 in
# グラフの幅のオプションを保存する
old_width <- options(width = 180)
# 例題2:カスタムラベルとタイトルを適用する
# lmforest関数を実行する
plot2 <- lmforest(
# モデルを指定する
x = model1,
# データを指定する
data = clintrial,
# グラフのタイトルを設定する
title = "Predictors of Body Mass Index",
# 効果のラベルを設定する
effect_label = "Change in BMI (kg/m^2)",
# ラベルのリストを指定する
labels = clintrial_labels
)
Recommended plot dimensions: width = 13.7 in, height = 5.0 in
# 例題3:インデントレイアウトを含む包括的なモデル
# 線形モデル(lm)を定義する
model3 <- lm(
bmi ~ age + sex + smoking + hypertension + diabetes + creatinine,
data = clintrial
)
# lmforest関数を実行する
plot3 <- lmforest(
x = model3,
data = clintrial,
labels = clintrial_labels,
# グループをインデントする
indent_groups = TRUE,
# ゼブラ模様のストライプを適用する
zebra_stripes = TRUE
)
Recommended plot dimensions: width = 12.7 in, height = 6.8 in
# 例材4:凝縮されたレイアウト
# lmforest関数を実行する
plot4 <- lmforest(
x = model3,
data = clintrial,
# テーブルを凝縮する
condense_table = TRUE,
labels = clintrial_labels
)
Recommended plot dimensions: width = 12.7 in, height = 5.2 in
# 例題5:異なるアウトカム(ヘモグロビン)
# 線形モデル(lm)を定義する
model5 <- lm(
hemoglobin ~ age + sex + bmi + smoking + creatinine,
data = clintrial
)
# lmforest関数を実行する
plot5 <- lmforest(
x = model5,
data = clintrial,
# グラフのタイトルを設定する
title = "Predictors of Baseline Hemoglobin",
# 効果のラベルを設定する
effect_label = "Change in Hemoglobin (g/dL)",
labels = clintrial_labels
)
Recommended plot dimensions: width = 16.0 in, height = 5.0 in
# 推奨されるサイズで保存する
# ファイルパスを指定して保存する
forestsave(plot5, file.path(tempdir(), "linear_forest.pdf"))
Forest plot saved to C:\Users\AppData\Local\Temp\RtmpMPEZTP/linear_forest.pdf (width = 16.0 in, height = 5.0 in)
# グラフィックの幅のオプションを元に戻す
options(old_width)単変量スクリーニングのフォレストプロット作成:uniforestコマンド
単変量解析による変数選択の過程を、有意性の有無を強調したフォレストプロットとして可視化します。
| オプション | 意味 | 初期値 |
|---|---|---|
| x | uniscreen()の結果オブジェクト | なし |
| title | プロットのタイトルを指定する文字列 | “Univariable Screening” |
| effect_label | フォレストプロットの軸における効果量のラベル | NULL |
| digits | 効果推定値と信頼区間の小数点以下の桁数 | 2 |
| p_digits | p値の小数点以下の桁数 | 3 |
| conf_level | 信頼区間の信頼水準(0から1の範囲) | 0.95 |
| font_size | すべてのテキスト要素の基本フォントサイズを制御する倍率 | 1 |
| annot_size | データ注釈の相対的なフォントサイズ | 3.88 |
| header_size | 列ヘッダーの相対的なフォントサイズ | 5.82 |
| title_size | メインプロットのタイトルの相対的なフォントサイズ | 23.28 |
| plot_width | レイアウト最適化のための出力幅 | NULL |
| plot_height | 出力の高さ | NULL |
| table_width | プロットの全幅に対するデータテーブルの割合(0から1の間) | 0.6 |
| show_n | サンプルサイズの列を含めるか否か | TRUE |
| show_events | 各行のイベント数を表示する列を含めるか否か | NULL |
| indent_groups | 因子レベルを親変数名の下にインデントして表示するか | FALSE |
| condense_table | バイナリ変数を1行にまとめて表示するかの指定 | FALSE |
| bold_variables | TRUEの場合、変数名を太字で表示 | FALSE |
| center_padding | 表とフォレストプロット間の水平方向の余白 | 4 |
| zebra_stripes | TRUEの場合、可読性向上のため背景に交互の網掛けを適用 | TRUE |
| color | フォレストプロットにおける点推定値の色 | NULL |
| null_line | 参照線の位置を指定する数値 | NULL |
| log_scale | x軸を対数スケールにするかどうかの論理値 | NULL |
| labels | 変数の表示用ラベルの名称付き文字ベクトル | NULL |
| show_footer | 結果変数の名前をフッターに表示するかの論理値 | TRUE |
| units | プロット寸法の単位として “in”、”cm”、”mm” のいずれかを選択 | “in” |
| number_format | 千の位と小数の区切り文字の指定(”us”、”eu”、”space”、”none” またはカスタム指定) | NULL |
# clintrialデータを読み込む
data(clintrial)
# clintrial_labelsデータを読み込む
data(clintrial_labels)
# uniscreenの結果を例として作成する
# uniscreen関数を実行する
uni_results <- uniscreen(
data = clintrial,
outcome = "os_status",
predictors = c("age", "sex", "smoking", "treatment", "stage"),
labels = clintrial_labels,
parallel = FALSE
)
# 例1:基本的な単変量フォレストプロットを作成する
# uniforest関数を実行し、死亡との単変量関連のプロットを作成する
p <- uniforest(uni_results, title = "Univariable Associations with Mortality")
Recommended plot dimensions: width = 12.2 in, height = 6.2 in
# 現在のグラフの幅の設定を保存する
old_width <- options(width = 180)
# 例2:生存時間解析を行う
# surv_resultsを計算する
surv_results <- uniscreen(
data = clintrial,
outcome = "Surv(os_months, os_status)",
predictors = c("age", "sex", "treatment", "stage"),
model_type = "coxph",
labels = clintrial_labels,
parallel = FALSE
)
# uniscreenによって付与されたラベルを自動的に適用してプロットを作成する
p2 <- uniforest(surv_results, title = "Univariable Survival Analysis")
Recommended plot dimensions: width = 12.0 in, height = 5.5 in
# 例3:線形回帰を行う
# lm_resultsを計算する
lm_results <- uniscreen(
data = clintrial,
outcome = "los_days",
predictors = c("age", "sex", "surgery", "diabetes"),
model_type = "lm",
labels = clintrial_labels,
parallel = FALSE
)
# 入院期間の予測因子のプロットを作成する
p3 <- uniforest(lm_results, title = "Predictors of Length of Stay")
Recommended plot dimensions: width = 11.0 in, height = 5.0 in
# 例4:外観をカスタマイズする
# カスタマイズを適用してプロットを作成する
p4 <- uniforest(
uni_results,
title = "Crude Associations with Mortality",
color = "#E74C3C",
indent_groups = TRUE,
zebra_stripes = TRUE,
bold_variables = TRUE
)
Recommended plot dimensions: width = 10.7 in, height = 7.2 in
# 例5:推奨されるサイズで保存する
# 指定したパスにPDFとして保存する
forestsave(p4, file.path(tempdir(), "univariable_forest.pdf"))
Forest plot saved to C:\Users\AppData\Local\Temp\RtmpMPEZTP/univariable_forest.pdf (width = 10.7 in, height = 7.2 in)
# グラフの幅の設定を元の状態に戻す
options(old_width)フォレストプロットのファイル保存:forestsaveコマンド
作成したフォレストプロットを、指定した解像度やサイズで画像やPDFなどのファイルとして保存します。
| オプション | 意味 | 初期値 |
|---|---|---|
| plot | forest系関数で生成されたフォレストプロット | なし |
| file | 出力ファイルのパスを指定する文字列 | なし |
| width | プロットの幅と高さの数値 | NULL |
| height | プロットの幅と高さの数値 | NULL |
| units | 幅と高さの単位(”in”、”cm”、”mm”)の指定 | NULL |
| dpi | ラスタ形式の解像度(dpi) | 300 |
| device | 使用するグラフィックスデバイスの指定 | NULL |
| embed_fonts | PDF出力時にフォントを埋め込むかどうかの指定 | FALSE |
| quiet | 使用された寸法の報告メッセージを抑制するかの指定 | FALSE |
| … | ggplot2::ggsave()に渡す追加の引数 | なし |
# clintrialデータセットを読み込む
data(clintrial)
# clintrial_labelsデータセットを読み込む
data(clintrial_labels)
# ロジスティック回帰モデルを構築する
model <- stats::glm(readmission_30d ~ age + sex + stage,
data = clintrial, family = stats::binomial
)
# モデルをフォレストプロット用に変換する
p <- glmforest(model, data = clintrial, labels = clintrial_labels)
Recommended plot dimensions: width = 13.9 in, height = 5.0 in
# 推奨されるサイズでPDFとして保存する
forestsave(p, file.path(tempdir(), "forest.pdf"))
Forest plot saved to C:\Users\AppData\Local\Temp\RtmpMPEZTP/forest.pdf (width = 13.9 in, height = 5.0 in)
# 出版用に高解像度(600dpi)のPNGとして保存する
forestsave(p, file.path(tempdir(), "forest.png"), dpi = 600)
Forest plot saved to C:\Users\AppData\Local\Temp\RtmpMPEZTP/forest.png (width = 13.9 in, height = 5.0 in)
# 指定したサイズ(cm)でPDFとして保存する
forestsave(p, file.path(tempdir(), "forest_sized.pdf"),
width = 24, height = 16, units = "cm"
)
Forest plot saved to C:\Users\AppData\Local\Temp\RtmpMPEZTP/forest_sized.pdf (width = 24.0 cm, height = 16.0 cm)
# フォントを埋め込んでPDFとして保存する
forestsave(p, file.path(tempdir(), "forest_embedded.pdf"),
embed_fonts = TRUE
)
Forest plot saved to C:\Users\AppData\Local\Temp\RtmpMPEZTP/forest_embedded.pdf (width = 13.9 in, height = 5.0 in)
Fonts embedded in C:\Users\AppData\Local\Temp\RtmpMPEZTP/forest_embedded.pdf多変量解析のフォレストプロット作成:multiforestコマンド
多変量解析の結果を、調整後の係数や共変量の情報を整理した構造的なフォレストプロットとして可視化します。
| オプション | 意味 | 初期値 |
|---|---|---|
| x | multifit()の実行結果オブジェクト | なし |
| title | プロットのタイトルを指定する文字列 | “Multivariate Analysis” |
| effect_label | フォレストプロットの軸における効果量のラベル | NULL |
| column | “adjusted”または”unadjusted”のいずれかの結果をプロットするかの指定 | “adjusted” |
| digits | 効果推定値と信頼区間の小数点以下の桁数 | 2 |
| p_digits | p値の小数点以下の桁数 | 3 |
| conf_level | 信頼区間の信頼水準(0から1の範囲) | 0.95 |
| font_size | すべてのテキスト要素の基本フォントサイズを制御する倍率 | 1 |
| annot_size | データ注釈の相対的なフォントサイズ | 3.88 |
| header_size | 列ヘッダーの相対的なフォントサイズ | 5.82 |
| title_size | メインプロットのタイトルの相対的なフォントサイズ | 23.28 |
| plot_width | レイアウト最適化のための出力幅 | NULL |
| plot_height | 出力の高さ | NULL |
| table_width | プロットの全幅に対するデータテーブルの割合(0から1の間) | 0.6 |
| show_n | サンプルサイズの列を含めるか否か | TRUE |
| show_events | 各行のイベント数を表示する列を含めるかどうかの指定 | NULL |
| show_predictor | 予測因子のレベルを表示する列を含めるかどうかの指定 | NULL |
| covariates_footer | 調整済みモデルで使用された共変量のフッターを表示するかの指定 | TRUE |
| indent_predictor | 予測因子のレベルをアウトカムの下にインデントして階層表示するかの指定 | FALSE |
| bold_variables | TRUEの場合、変数名を太字で表示 | TRUE |
| center_padding | 表とフォレストプロット間の水平方向の余白 | 4 |
| zebra_stripes | 可読性向上のため、異なる結果に交互の背景色を適用するかの指定 | TRUE |
| color | フォレストプロットにおける点推定値の色 | NULL |
| null_line | 参照線の位置を指定する数値 | NULL |
| log_scale | x軸を対数スケールにするかどうかの論理値 | NULL |
| labels | 結果や変数に表示用のカスタムラベルを割り当てる名前付き文字ベクトル | NULL |
| units | プロット寸法の単位として “in”、”cm”、”mm” のいずれかを選択 | “in” |
| number_format | 千の位と小数の区切り文字の指定(”us”、”eu”、”space”、”none” またはカスタム指定) | NULL |
# clintrialデータを読み込む
data(clintrial)
# clintrial_labelsデータを読み込む
data(clintrial_labels)
# multifitの結果を生成する
# multifit関数を実行する
result <- multifit(
# 解析対象のデータセットを指定する
data = clintrial,
# 評価するアウトカムのリストを指定する
outcomes = c("surgery", "pfs_status", "os_status"),
# 予測変数を指定する
predictor = "treatment",
# 共変量を指定する
covariates = c("age", "sex", "stage"),
# 並列処理を無効にする
parallel = FALSE
)
# 例1:基本的な多変量フォレストプロットを作成する
# multiforest関数を実行する
p <- multiforest(result)
Recommended plot dimensions: width = 14.6 in, height = 5.0 in
# 現在のグラフの幅の設定を保存する
old_width <- options(width = 180)
# 例2:カスタムタイトルとラベルを適用する
# multiforest関数を実行する
plot2 <- multiforest(
# 解析結果を指定する
result,
# グラフのタイトルを設定する
title = "Treatment Effects Across Clinical Outcomes",
# ラベルの定義を適用する
labels = clintrial_labels
)
Recommended plot dimensions: width = 16.7 in, height = 5.0 in
# 例3:外観をカスタマイズする
# multiforest関数を実行する
plot3 <- multiforest(
# 解析結果を指定する
result,
# 線の色を赤色に指定する
color = "#E74C3C",
# 縞模様の背景を有効にする
zebra_stripes = TRUE,
# ラベルの定義を適用する
labels = clintrial_labels
)
Recommended plot dimensions: width = 16.7 in, height = 5.0 in
# 例4:推奨されるサイズで保存する
# forestsave関数を実行し、PDFとして保存する
forestsave(plot3, file.path(tempdir(), "multioutcome_forest.pdf"))
Forest plot saved to C:\Users\AppData\Local\Temp\RtmpMPEZTP/multioutcome_forest.pdf (width = 16.7 in, height = 5.0 in)
# グラフの幅の設定を元の状態に戻す
options(old_width)
<おすすめのRに関する書籍です>
この記事が誰かの役に立ちますように。