*disclaimer
495821
分析手順
RStudioの設定
Knit
- Knit DirectoryをCurrent Working Directoryにしておく
Session
- Set Working Directory で、実際の作業をするディレクトリ(フォルダー)を選んでおく。
データ
library(tidyverse) library(MASS)
場所の確認
- パソコン内のフルパス
読み込み
library(openxlsx) データ名 <- read.xlsx("エクセルファイル名", sheet="シート名") もしくは library(readxl) read_excel("エクセルファイル名", sheet="シート名")
確認
- データ構造
str(データ名)
- データの一部
head(データ名)
- 記述統計
summary(データ名)
- 型の変換
as.factor()
- 欠損値の扱い
分布の可視化
library(ggplot2)
- 応答変数がどのような分布をしているか確認
g <- ggplot(データ) g <- g + aes(x= , y= ) g <- g + geom_ plot(g)
- 確率分布
ggplot(データ, aes(x=横軸, fill=グループ)) + geom_density(alpha=.7)
- 箱ひげ図
ggplot(データ, aes(x=横軸, y=縦軸)) + geom_boxplot()
- 折れ線グラフ
ggplot(データ, aes(x=横軸, y=縦軸, color=ID, group=ID)) + geom_line()
- 回帰分析
ggplot(データ, aes(x=横軸, y=縦軸, color=ID, group=ID)) + geom_point() + geom_smooth(method = "lm", se = F)
モデル
library(lme4) library(lmerTest) library(effects)
分布パタン
- 応答変数がどのような分布をしているかを見極める
https://tjo.hatenablog.com/entry/2013/09/18/235052
- その分布のパタンによってモデル式を選ぶ
https://qiita.com/xolmon/items/bd25b7c62f49ce61c7b5
ランダム効果の有無
## 切片がランダム
モデルa <- lmer(応答変数 ~ 説明変数 + (1|ID), データ)
summary(モデルa)
plot(allEffects(モデルa))
## 傾きも切片もランダム
モデルb <- lmer(応答変数 ~ 説明変数 + (説明変数||ID), データ)
## 比較
anova(モデルa, モデルb)
選択
- フルモデルからstepで減らす
- 理論的にモデルを設定
- AIC
効果の可視化
plot(allEffects(モデル))
- 白黒にするには、, lines=list(col="black")
plot(allEffects(NP.model.all) , main=F, ylab="CN/C", lines=list(col="black"))
検証
library(performance) ## モデルパフォーマンス model_performance(モデル) ## 寄与率 r2(モデル) ## 共線性 check_collinearity(モデル) ### 正規性 check_normality(モデル) ### 正規性のグラフ library(see) plot(check_normality(モデル)) ### 過分散 check_overdispersion(モデル) ### ゼロ過剰 check_zeloinflation(モデル)
https://sugiura-ken.org/wiki/