シリーズ: R
r
64 行
· 更新日 2026-02-03
Fractional_Logit.r
R/Fractional_Logit.r
# --- 安裝必要套件(只需跑一次) ---
# install.packages(c("readxl", "ggplot2", "corrplot", "Hmisc", "psych"))
library(readxl)
library(ggplot2)
library(corrplot)
library(Hmisc)
library(psych)
# --- 1. 讀取 Excel ---
df <- read_excel("Fractional Logit.xlsx")
# 移除非數值欄(如果有)
df_numeric <- df[sapply(df, is.numeric)]
# --- 2. 基本摘要統計 ---
summary_stats <- psych::describe(df_numeric)
print(summary_stats)
# --- 3. 計算相關矩陣(Pearson) ---
corr_matrix <- cor(df_numeric, use = "pairwise.complete.obs", method = "pearson")
print(corr_matrix)
# --- Spearman 相關矩陣 ---
corr_spearman <- cor(df_numeric, use = "pairwise.complete.obs", method = "spearman")
# --- 4. 熱力圖(corrplot 版本) ---
corrplot(corr_matrix, method = "color", title = "Correlation Matrix Heatmap", mar=c(0,0,1,0))
# --- 5. Res_Flood 與各變數的兩兩相關(Pearson + Spearman) ---
target <- "Res_Flood"
other_vars <- setdiff(colnames(df_numeric), target)
results <- data.frame(
Variable = character(),
Pearson_r = numeric(),
Pearson_p = numeric(),
Spearman_rho = numeric(),
Spearman_p = numeric(),
stringsAsFactors = FALSE
)
for (v in other_vars) {
pear <- cor.test(df_numeric[[target]], df_numeric[[v]], method = "pearson")
spear <- cor.test(df_numeric[[target]], df_numeric[[v]], method = "spearman")
results <- rbind(results, data.frame(
Variable = v,
Pearson_r = pear$estimate,
Pearson_p = pear$p.value,
Spearman_rho = spear$estimate,
Spearman_p = spear$p.value
))
}
print(results)
# --- 6. 多元線性迴歸模型 ---
formula_str <- paste("Res_Flood ~", paste(other_vars, collapse = " + "))
model <- lm(as.formula(formula_str), data = df_numeric)
summary(model)
# --- 7. 輸出結果成 CSV(可選) ---
write.csv(results, "Res_Flood_correlation_results.csv", row.names = FALSE)
write.csv(corr_matrix, "Correlation_Matrix_Pearson.csv")
関連記事
R
r
更新日 2026-02-03
Fractional_Logit_PCA.r
Fractional_Logit_PCA.r — r source code from the R learning materials (R/Fractional_Logit_PCA.r).
記事を読む →
R
r
更新日 2026-02-03
01-probability-distributions.R
01-probability-distributions.R — r source code from the R learning materials (R/advanced/01-probability-distributions.R).
記事を読む →
R
r
更新日 2026-02-03
02-hypothesis-testing.R
02-hypothesis-testing.R — r source code from the R learning materials (R/advanced/02-hypothesis-testing.R).
記事を読む →
R
r
更新日 2026-02-03
03-linear-regression.R
03-linear-regression.R — r source code from the R learning materials (R/advanced/03-linear-regression.R).
記事を読む →
R
r
更新日 2026-02-03
04-advanced-anova.R
04-advanced-anova.R — r source code from the R learning materials (R/advanced/04-advanced-anova.R).
記事を読む →
R
r
更新日 2026-02-03
05-model-selection.R
05-model-selection.R — r source code from the R learning materials (R/advanced/05-model-selection.R).
記事を読む →