1 データ

約500の町の心臓病患者の人口割合(heart.disease)(%)と喫煙割合(smoking)(%),自転車通勤割合(biking)(%)について調べたデータ

出典:Scribbr,‘Multiple Linear Regression | A Quick Guide (Examples)’

d <- read.csv('https://stats.dip.jp/01_ds/data/heart.data.csv')[, -1]

library(DT)
datatable(round(d, 1))
# カラーパレット
COL <- c(rgb(255,   0,   0,  255, max = 255), # 赤
         rgb(  0,   0, 255,  255, max = 255), # 青
         rgb(  0, 155,   0,  255, max = 255)) # 緑

[RGB_Color] https://www.rapidtables.com/web/color/RGB_Color.html

1.1 相関分析図

1.1.1 相関図・ヒストグラムもあわせて表示する相関分析図

library(psych)
pairs.panels(d)

1.1.2 インタラクティブグラフ

無相関検定で有意でないものは「☓」マークが付く。

#install.packages("ggcorrplot")
library(ggcorrplot)
library(plotly)
cor(d) |> ggcorrplot(lab = T, hc.order = T, outline.color = "white", p.mat = cor_pmat(d)) |> ggplotly() |>
layout(font  = list(size = 11, color = 'blue', family = 'UD Digi Kyokasho NK-R'),
       title = '主タイトル',
       xaxis = list(title = 'x軸カテゴリラベル'),
       yaxis = list(title = 'y軸カテゴリラベル'))

2 重回帰モデル

\[ \begin{align} Y_i=&\beta_0 + \beta_1 x_{i1} + \beta_2 x_{i2} + \epsilon_i, \epsilon_i \sim \mathbf{N}(0, \sigma^2)\\\\ ここで,&Y_i: 目的変数(心臓病罹患率 heart.disease [%])\\ &x_{i1}: 説明変数1(自転車通勤割合 biking [%])\\ &x_{i2}: 説明変数2(喫煙割合 smoking [%])\\ &\beta_0, \beta_1, \beta_2: 偏回帰係数\\ &\epsilon_i:誤差項\\ &\sigma^2:誤差分散 \end{align} \]

2.1 回帰分析

fit <- lm(heart.disease ~ biking + smoking, data = d)
#summary(fit)
library(sjPlot)
tab_model(fit, show.stat = T, show.aic = T)
  heart.disease
Predictors Estimates CI Statistic p
(Intercept) 14.98 14.83 – 15.14 186.99 <0.001
biking -0.20 -0.20 – -0.20 -146.53 <0.001
smoking 0.18 0.17 – 0.19 50.39 <0.001
Observations 498
R2 / R2 adjusted 0.980 / 0.980
AIC 995.353
plot_model(fit, show.values = T, show.intercept = T, width = 0.1)

2.2 残差分析

グラフの見方については, 第09回-回帰分析(残差分析)の講義資料を参照すること。

plot(fit)

3 演習課題

次のデータで住宅価格を予測する重回帰モデルを作成して回帰分析せよ。


4 データ

新台湾市の住宅価格

出典:【UCI Machine Learning Repository】Real estate valuation data set Data Set

説明変数 内容
id 連番
yr 住宅購入年
yrs_old 築年数
m_sta 最寄り駅までの距離(m)
nstores 近隣コンビニエンスストア数
lat 緯度(度)
lon 経度(度)
price 坪単価(万新台湾$)
d <- read.csv('https://stats.dip.jp/01_ds/data/real_estate_price.csv')

datatable(round(d, 1))