Skip to content

Instantly share code, notes, and snippets.

View tbrotherm's full-sized avatar

Hitoshi Tominaga tbrotherm

  • Kyoto University
  • Kyoto, Japan
View GitHub Profile
@tbrotherm
tbrotherm / scale()sample6.R
Created December 24, 2018 08:45
scale()関数から出力される結果の形式について (R技術メモ)
# データフレーム全体を標準化
mydf_standardized <- scale(mydf[,c(1:4)])
is.data.frame(mydf_standardized) # FALSE
is.matrix(mydf_standardized) # TRUE
# [修正版]データフレーム全体を標準化
mydf_standardized <- data.frame(scale(mydf[,c(1:4)]))
is.data.frame(mydf_standardized) # TRUE
is.matrix(mydf_standardized) # FALSE
@tbrotherm
tbrotherm / scale()sample5.R
Last active December 24, 2018 08:32
scale()関数から出力される結果の形式について (R技術メモ)
# 再度インタラクションプロット作成
fit_iris <- lm(Petal.Length ~ Petal.Width_centered * Species, data = mydf)
summary(fit_iris)
interact_plot(fit_iris, pred = Petal.Width_centered, modx = Species, plot.points = TRUE) # 成功
@tbrotherm
tbrotherm / scale()sample4.R
Last active December 24, 2018 08:29
scale()関数から出力される結果の形式について (R技術メモ)
# 中心化済み変数の形式をチェック
is.numeric(mydf$Petal.Width_centered) # TRUE
is.vector(mydf$Petal.Width_centered) # FALSE
is.matrix(mydf$Petal.Width_centered) # TRUE
# データフレームに投入する直前にベクター形式に変換
mydf$Petal.Width_centered <- as.vector(scale(mydf$Petal.Width, center = T, scale = F))
is.numeric(mydf$Petal.Width_centered) # TRUE
is.vector(mydf$Petal.Width_centered) # TRUE
is.matrix(mydf$Petal.Width_centered) # FALSE
@tbrotherm
tbrotherm / scale()sample3.R
Last active December 24, 2018 08:31
scale()関数から出力される結果の形式について (R技術メモ)
# jtoolsをインストール&読み込み
wants <- c("jtools")
has <- wants %in% rownames(installed.packages())
if(any(!has)) install.packages(wants[!has])
# インタラクションプロット作成
interact_plot(fit_iris, pred = Petal.Width_centered, modx = Species, plot.points = TRUE)
# エラー発生
# Error: variable 'Petal.Width_centered' was fitted with type "nmatrix.1" but type "numeric" was supplied
@tbrotherm
tbrotherm / scale()sample2.R
Last active December 24, 2018 08:30
scale()関数から出力される結果の形式について (R技術メモ)
# lm()を使って回帰分析
fit_iris <- lm(Petal.Length ~ Petal.Width_centered * Species, data = mydf)
summary(fit_iris)
# plot()を使って散布図
plot(mydf[,c("Petal.Width_centered","Petal.Length")])
@tbrotherm
tbrotherm / scale()sample1.R
Last active December 24, 2018 08:29
scale()からでる結果の形式について (R技術メモ)
# データフレームとしてirisを使用
mydf <- iris
# scale()関数で中心化した変数を元のデータフレームに追加
mydf$Petal.Width_centered <- scale(mydf$Petal.Width, center = T, scale = F)