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))
library(psych)
pairs.panels(d)

library(ggcorrplot)
##  要求されたパッケージ ggplot2 をロード中です
## 
##  次のパッケージを付け加えます: 'ggplot2'
##  以下のオブジェクトは 'package:psych' からマスクされています:
## 
##     %+%, alpha
library(plotly)
## 
##  次のパッケージを付け加えます: 'plotly'
##  以下のオブジェクトは 'package:ggplot2' からマスクされています:
## 
##     last_plot
##  以下のオブジェクトは 'package:stats' からマスクされています:
## 
##     filter
##  以下のオブジェクトは 'package:graphics' からマスクされています:
## 
##     layout
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軸カテゴリラベル'))
fit <- lm(heart.disease ~ biking + smoking, data = d)
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)

plot(fit)

d <- read.csv('https://stats.dip.jp/01_ds/data/real_estate_price.csv')

datatable(round(d, 1))
fit <- lm(price ~ yr, data = d)

library(sjPlot)
str(d)
## 'data.frame':    414 obs. of  8 variables:
##  $ id     : int  1 2 3 4 5 6 7 8 9 10 ...
##  $ yr     : int  2012 2012 2013 2013 2012 2012 2012 2013 2013 2013 ...
##  $ yrs_old: num  32 19.5 13.3 13.3 5 7.1 34.5 20.3 31.7 17.9 ...
##  $ m_sta  : num  84.9 306.6 562 562 390.6 ...
##  $ nstores: int  10 9 5 5 5 3 7 6 1 3 ...
##  $ lat    : num  25 25 25 25 25 ...
##  $ lon    : num  122 122 122 122 122 ...
##  $ price  : num  37.9 42.2 47.3 54.8 43.1 32.1 40.3 46.7 18.8 22.1 ...
tab_model(fit, show.stat = T, show.aic = T)
  price
Predictors Estimates CI Statistic p
(Intercept) -4809.46 -10547.27 – 928.35 -1.65 0.100
yr 2.41 -0.44 – 5.26 1.66 0.098
Observations 414
R2 / R2 adjusted 0.007 / 0.004
AIC 3338.650
d <- read.csv('https://stats.dip.jp/01_ds/data/car_mileage.csv')
rownames(d) <- paste0('No.', 1:nrow(d))
colnames(d) <- c('km', 'ncy', 'cc', 'hp', 'kg', 'sec', 'yr', 'type', 'name')

library(DT)
datatable(d)
n <- nrow(d)

# 訓練データサイズの全サイズ80%とした。sample関数で非復元無作為抽出する。
ii.tr <- sample(1:n, size = floor(0.8*n))

d.tr <- d[ ii.tr, ] # 訓練データを抽出
d.te <- d[-ii.tr, ] # 試験データを抽出(負のインデックスの要素は除かれる)
fit <- lm(km ~ kg + cc, data = d.tr) # 訓練データでフィッティング

kmhat <- predict(fit, newdata = d.te) # 試験データの説明変数を用いて予測

RMSE <- sqrt(mean((d.te$km -kmhat)^2)) # 予測精度指標(2乗平均平方根誤差)
RMSE
## [1] 1.827347
get.accuracy <- function(yhat, y, digits = 2)
{
  d <- data.frame(MBE  = mean(yhat - y),
                  MAE  = mean(abs(yhat - y)),
                  MAPE = mean(abs((yhat - y) / y)) * 100,
                  RMSE = sqrt(mean((yhat - y)^2)))
  return(round(d, digits))
}

(a <- get.accuracy(d.te$km, kmhat))
##    MBE  MAE  MAPE RMSE
## 1 0.01 1.35 14.68 1.83
fit <- lm(km ~ poly(kg, 2, raw = TRUE), data = d)
#summary(fit)
library(sjPlot)
tab_model(fit)
  km
Predictors Estimates CI p
(Intercept) 26.47 23.96 – 28.97 <0.001
kg [1st degree] -0.02 -0.02 – -0.01 <0.001
kg [2nd degree] 0.00 0.00 – 0.00 <0.001
Observations 392
R2 / R2 adjusted 0.715 / 0.714
kg.p <- seq(0, 3000, 100)

# 信頼区間
conf <- predict(fit, newdata = data.frame(kg = kg.p),
                interval = 'confidence')
# 予測区間
pred <- predict(fit, newdata = data.frame(kg = kg.p),
                interval = 'prediction')
COL <- c(rgb(255,   0,   0,  105, max = 255), # 赤
         rgb(  0,   0, 255,  105, max = 255), # 青
         rgb(  0, 155,   0,  105, max = 255), # 緑
         rgb(140, 140, 140,  105, max = 255), # 暗灰
         rgb(180, 180, 180,  105, max = 255)) # 灰