高校数学の質問スレ(医者・東大卒専用) Part438 (882レス)
上下前次1-新
抽出解除 必死チェッカー(本家) (べ) 自ID レス栞 あぼーん
831: 2025/05/10(土) 11:02:46.60 ID:ynDPH7B8(1/2)調 AAS
# ------------------------------------------------------------------------------
# ファイル名:logistic_regression_uraguchi_factors.R
# 目的:裏口入学の決定要因を評価するロジスティック回帰分析
# 考察対象の説明変数:学力、大学ランク (基準カテゴリ: A)、縁故、親の所得、寄付金
# ------------------------------------------------------------------------------
# データ生成 (大学ランクを因子型、基準レベル A)
set.seed(123)
n <- 1000
ranks_char <- sample(LETTERS[1:6], n, replace = TRUE)
ranks_factor <- factor(ranks_char, levels = LETTERS[1:6], ordered = TRUE) # 順序付きファクターとして生成
data <- data.frame(
裏口入学 = rbinom(n, 1, 0.2),
学力 = rnorm(n, mean = 50, sd = 10),
大学ランク = ranks_factor,
縁故 = rbinom(n, 1, 0.1),
親の所得 = rlnorm(n, meanlog = log(5000), sdlog = 0.3),
寄付金 = rlnorm(n, meanlog = log(100000), sdlog = 1.0)
)
# 支払額の生成
rank_numeric <- as.numeric(data$大学ランク) # A=1, B=2, ... 6
data$支払額 <- 10 * pmax(
300 + 100 * data$裏口入学 +
0.5 * (100 - data$学力) +
20 * rank_numeric +
50 * data$縁故 +
0.05 * data$親の所得 +
0.001 * data$寄付金 +
rnorm(n, mean = 0, sd = 50),
0
)
# ロジスティック回帰モデル (大学ランクが因子型として扱われ、基準カテゴリは A)
model_full <- glm(裏口入学 ~ 学力 + 大学ランク + 縁故 + 親の所得 + 寄付金, data = data, family = binomial)
# オッズ比と信頼区間の算出
confint_vals <- exp(confint(model_full))
odds_ratios_ci <- data.frame(
Variable = rownames(confint_vals)[-1],
CI_lower = confint_vals[-1, 1],
CI_upper = confint_vals[-1, 2]
)
# プロット用の変数ラベルを日本語化
label_map <- c(
"学力" = "学力",
"大学ランク.L" = "大学ランク B",
"大学ランク.Q" = "大学ランク C",
"大学ランク.C" = "大学ランク D",
"大学ランク^4" = "大学ランク E",
"大学ランク^5" = "大学ランク F",
"縁故" = "縁故",
"親の所得" = "親の所得",
"寄付金" = "寄付金"
)
odds_ratios_ci$日本語変数名 <- ifelse(odds_ratios_ci$Variable %in% names(label_map),
label_map[odds_ratios_ci$Variable],
odds_ratios_ci$Variable)
832: 2025/05/10(土) 11:02:56.88 ID:ynDPH7B8(2/2)調 AAS
# 現在の par() の設定を保存
current_par <- par(no.readonly = TRUE)
# 指定された mar と bty で描画
par(mar = c(5, 8, 5, 2), bty = 'l')
# plot 関数を使用したオッズ比の信頼区間プロット (1を基準)
n_vars <- nrow(odds_ratios_ci)
y_positions <- n_vars:1
xlim_odds <- range(odds_ratios_ci$CI_lower, odds_ratios_ci$CI_upper)
plot(NA, xlim = xlim_odds,
ylim = c(0.5, n_vars + 0.5),
xlab = "オッズ比 (log scale)",
ylab = "",
main = "ロジスティック回帰分析:オッズ比の95%信頼区間",
log = "x",
yaxt = "n")
segments(x0 = odds_ratios_ci$CI_lower,
x1 = odds_ratios_ci$CI_upper,
y0 = y_positions,
y1 = y_positions,
col = "skyblue",
lwd = 4)
abline(v = 1, lty = "dashed", col = "black")
# y軸のラベルを日本語で追加
axis(side = 2, at = y_positions, labels = odds_ratios_ci$日本語変数名[order(y_positions, decreasing = TRUE)], las = 1)
# 描画後に元の par() の設定に戻す
par(current_par)
上下前次1-新書関写板覧索設栞歴
スレ情報 赤レス抽出 画像レス抽出 歴の未読スレ
ぬこの手 ぬこTOP 0.048s