knitr::opts_chunk$set(prompt=TRUE, message=FALSE) #実行コードは表示,警告は非表示
# pacmanパッケージのインストールをチェックし、インストールされていない場合はインストール
#if (!require("pacman")) install.packages("pacman")
# pacmanパッケージの読み込み
#library(pacman)
pacman::p_load(tidyverse)
#↑グローバル設定,他のchunkには書かなくてもここで設定したものが適用される
#すべてのチャングにおいてlibrary(tidyverse), library(ggplot2)の省略が可能

シーケンスデータ

  • seq(from, to): fromからtoまでの数値シーケンスを生成
    • seq(1, 10) 出力: 1 2 3 4 5 6 7 8 9 10
  • seq(from, to, by): fromからtoまでbyのステップで数値シーケンスを生成
    • seq(1, 10, by = 2) 出力: 1 3 5 7 9
  • seq(from, to, length.out): fromからtoまでlength.out個の等間隔で生成
    • seq(0, 1, length.out = 11) 出力: 0.0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1.0
> #---------------------------------------
> # -3から3まで、長さ100のシーケンスを生成
> z <- seq(-3, 3, length.out = 100)
> 
> # dnorm関数を使用して確率密度を計算(z-->density)
> density <- dnorm(z)
> 
> # pnorm関数を使用して累積確率を計算(z-->probability)
> cumulative_probability <- pnorm(z)
> 
> # qnorm関数を使用して分位点を計算(probability-->z)
> probabilities <- seq(0, 1, length.out = 100)
> quantiles <- qnorm(probabilities) #mean=0,sd=1省略
> 
> # dnormの視覚化
> pdf_data <- data.frame(z = z, density = density)
> ggplot(pdf_data, aes(x = z, y = density)) +
+   #stat_ecdf(geom = "step", color = "blue") +
+   geom_line() +
+   labs(title = "Probability Density Function", x = "z-value", y = "Density")

> # pnormの視覚化
> cdf_data <- data.frame(z = z, cumulative_probability = cumulative_probability)
> ggplot(cdf_data, aes(x = z, y = cumulative_probability)) +
+   #stat_ecdf(geom = "step", color = "blue") +
+   geom_line() +
+   labs(title = "Cumulative Distribution Function", x = "z-value", y = "Cumulative Probability")

> # qnormの視覚化
> quantile_data <- data.frame(p = probabilities, quantile = quantiles)
> ggplot(quantile_data, aes(x = p, y = quantile)) +
+   #stat_ecdf(geom = "step", color = "blue") +
+   geom_line() +
+   labs(title = "Quantile Function", x = "Probability", y = "z-value")

ランダムデータ

  • random_numbers <- rnorm(n = 100, mean = 0, sd = 1)
    • n: 生成する乱数の数
    • mean: 平均
    • sd: 標準偏差
> # ランダムサンプルデータの生成1
> data1 <- rnorm(1000, mean = 0, sd = 1)
> data2 <- rnorm(1000, mean = 1, sd = 2)
> data3 <- rnorm(1000, mean = 50, sd = 10)
> 
> # 25, 50, 75パーセンタイルの計算(c()の数はいくらでもOK)
> percentiles1 <- quantile(data1, probs = c(0.25, 0.5, 0.75))
> percentiles2 <- quantile(data2, probs = c(0.25, 0.5, 0.75))
> percentiles3 <- quantile(data3, probs = c(0.25, 0.5, 0.75))
> 
> # 2.5, 50, 97.5パーセンタイルの計算(c()の数分、出力される)
> percentiles4 <- quantile(data3, probs = c(0.025, 0.5, 0.975))
> 
> # 25, 50, 75パーセンタイルの計算
> probability_below_60_1 <- pnorm(0, mean = 0, sd = 1)
> probability_below_60_2 <- pnorm(10, mean = 1, sd = 10)
> probability_below_60_3 <- pnorm(70, mean = 50, sd = 10)
> 
> # ↓が分かりやすい四分位数95%の信頼区間をZスコア換算
> # z_score <- qnorm(.975,mean=0,sd=1)
> 
> 
> # ヒストグラムの相対密度プロット
> ggplot(data.frame(value = data1), aes(x = value)) +
+   geom_histogram(aes(y = after_stat(density)), bins = 30, fill = "blue", alpha = 0.2) +
+   geom_density(color="red") +
+   labs(title = "Histogram and Kernel Density Estimation with Relative Density", x = "Value", y = "Relative Density")

> # 累積確率のプロット
> ggplot(data.frame(value = data3), aes(x = value)) +
+   stat_ecdf(geom = "step", color = "blue") +
+   geom_vline(xintercept = 60, color = "red", linetype = "dashed", linewidth = 1) +
+   annotate("text", x = 60 , y = 0, label = paste0("P(X <= 60) = ", round(pnorm(60, mean = 50, sd = 10), 2)), vjust = -1) +
+   labs(title = "ECDF with Probability", x = "Value", y = "Cumulative Probability") +
+   theme_minimal()

> # データフレームの作成
> dataF <- data.frame(
+   value = c(data1, data2,data3),
+   group = factor(rep(c("Group 1", "Group 2","Group 3"), each = 1000))
+ )
> #dataF
> 
> # カーネル密度推定の相対密度プロット
> ggplot(dataF, aes(x = value, color = group)) +
+   geom_density() +
+   labs(title = "Kernel Density Estimation with Relative Density", x = "Value", y = "Relative Density")

> # ヒストグラムにパーセンタイル線を追加
> ggplot(data.frame(value = data1), aes(x = value)) +
+   geom_histogram(fill = "blue", alpha = 0.2) +
+   geom_vline(aes(xintercept = percentiles1[1]), color = "red", linetype = "dashed", linewidth  = 1) +
+   geom_vline(aes(xintercept = percentiles1[2]), color = "green", linetype = "dashed", linewidth  = 1) +
+   geom_vline(aes(xintercept = percentiles1[3]), color = "purple", linetype = "dashed", linewidth  = 1) +
+   annotate("text", x = percentiles1[1], y = 0, label = paste0("Lo = ", round(percentiles1[1], 3))) +
+   annotate("text", x = percentiles1[3], y = 0, label = paste0("Up = ", round(percentiles1[3], 3))) +
+   labs(title = "Histogram with Percentiles", x = "Value", y = "Frequency") +
+   theme_minimal()

> # カーネル密度プロットにパーセンタイル線を追加
> ggplot(data.frame(value = data2), aes(x = value)) +
+   geom_density(fill = "blue", alpha = 0.2) +
+   geom_vline(aes(xintercept = percentiles2[1]), color = "red", linetype = "dashed", linewidth  = 1) +
+   geom_vline(aes(xintercept = percentiles2[2]), color = "green", linetype = "dashed", linewidth  = 1) +
+   geom_vline(aes(xintercept = percentiles2[3]), color = "purple", linetype = "dashed", linewidth  = 1) +
+   annotate("text", x = percentiles2[1], y = 0, label = paste0("Lo = ", round(percentiles2[1], 3))) +
+   annotate("text", x = percentiles2[3], y = 0, label = paste0("Up = ", round(percentiles2[3], 3))) +
+   labs(title = "Kernel Density Plot with Percentiles", x = "Value", y = "Density") +
+   theme_minimal()

> # カーネル密度プロットにパーセンタイル線を追加
> ggplot(data.frame(value = data3), aes(x = value)) +
+   geom_density(fill = "blue", alpha = 0.2) +
+   geom_vline(aes(xintercept = percentiles4[1]), color = "red", linetype = "dashed", linewidth  = 1) +
+   geom_vline(aes(xintercept = percentiles4[2]), color = "green", linetype = "dashed", linewidth  = 1) +
+   geom_vline(aes(xintercept = percentiles4[3]), color = "purple", linetype = "dashed", linewidth  = 1) +
+   annotate("text", x = percentiles4[1], y = 0, label = paste0("Lower = ", round(percentiles4[1], 3))) +
+   annotate("text", x = percentiles4[3], y = 0, label = paste0("Upper = ", round(percentiles4[3], 3))) +
+   labs(title = "Kernel Density Plot with Percentiles", x = "Value", y = "Density") +
+   theme_minimal()