"R MSE 계산"의 두 판 사이의 차이

34번째 줄: 34번째 줄:
</syntaxhighlight>
</syntaxhighlight>


==방법 2: mse() ==
==방법 2: mse()==
=== 예시: 단순회귀분석 ===
=== 예시: 단순회귀분석 ===
<syntaxhighlight lang='r' run>
<syntaxhighlight lang='r' run>

2020년 10월 28일 (수) 00:49 판

1 개요

R MSE 계산

2 방법 1: 계산

[math]\displaystyle{ \operatorname {MSE} ={\frac {1}{n}}\sum _{i=1}^{n}(Y_{i}-{\hat {Y_{i}}})^{2} }[/math]

2.1 예시: 단순회귀분석

options(echo=T)
df = data.frame(
  x = c(1  , 2  , 3  , 4  , 5  , 6  , 7  , 8  , 9  , 10  ),
  y = c(2.5, 4.0, 3.5, 3.0, 4.5, 4.0, 5.5, 7.0, 6.5,  7.0)
)
model = lm(y ~ x, data=df)
pred = predict(model, data=df)
mean((df$y - pred)^2) # MSE = 0.4
# 그림 (optional)
plot(df$x, df$y, pch=16)
abline(model)
[math]\displaystyle{ \operatorname{MSE}=\dfrac{1^2+1^2+1^2+1^2}{10}=\dfrac{4}{10}=0.4 }[/math]

2.2 예시: 다중회귀분석

options(echo=T)
df = data.frame(
  radio_ads = c(3,4,9,4,5,5,2,6,5,3),
  tv_ads    = c(1,3,4,1,4,1,4,2,4,2),
  retention = c(5,1,6,2,8,3,4,9,7,4)
)
model = lm(retention ~ ., data=df)
pred = predict(model, data=df)
mean((df$retention - pred)^2) # MSE = 4.557339

3 방법 2: mse()

3.1 예시: 단순회귀분석

options(echo=T)
df = data.frame(
  x = c(1  , 2  , 3  , 4  , 5  , 6  , 7  , 8  , 9  , 10  ),
  y = c(2.5, 4.0, 3.5, 3.0, 4.5, 4.0, 5.5, 7.0, 6.5,  7.0)
)
model = lm(y ~ x, data=df)
pred = predict(model, data=df)
library(Metrics)
mse(df$y, pred) # MSE = 0.4

3.2 예시: 다중회귀분석

options(echo=T)
df = data.frame(
  radio_ads = c(3,4,9,4,5,5,2,6,5,3),
  tv_ads    = c(1,3,4,1,4,1,4,2,4,2),
  retention = c(5,1,6,2,8,3,4,9,7,4)
)
model = lm(retention ~ ., data=df)
pred = predict(model, data=df)
library(Metrics)
mse(df$retention, pred) # MSE = 4.557339

4 같이 보기

문서 댓글 ({{ doc_comments.length }})
{{ comment.name }} {{ comment.created | snstime }}