과제 1
1.데이터 불러오기
knitr::opts_chunk$set(echo = TRUE)
data(mtcars)
head(mtcars)
## mpg cyl disp hp drat wt qsec vs am gear carb
## Mazda RX4 21.0 6 160 110 3.90 2.620 16.46 0 1 4 4
## Mazda RX4 Wag 21.0 6 160 110 3.90 2.875 17.02 0 1 4 4
## Datsun 710 22.8 4 108 93 3.85 2.320 18.61 1 1 4 1
## Hornet 4 Drive 21.4 6 258 110 3.08 3.215 19.44 1 0 3 1
## Hornet Sportabout 18.7 8 360 175 3.15 3.440 17.02 0 0 3 2
## Valiant 18.1 6 225 105 2.76 3.460 20.22 1 0 3 1
2.독립변수와 종속변수는 무엇인가?
independent variable: wt(자동차의 중량)/dependent variable:
mpg(연비)
3.선형회귀 모형 생성
knitr::opts_chunk$set(echo = TRUE)
model<-lm(mpg~wt,data=mtcars)
4.선형회귀 모형 출력하고 결과 해석 (summary 함수) à 두 변수 간
관계가 통계적으로 유의한가? 관계가 어떠한가?
knitr::opts_chunk$set(echo = TRUE)
summary(model)
##
## Call:
## lm(formula = mpg ~ wt, data = mtcars)
##
## Residuals:
## Min 1Q Median 3Q Max
## -4.5432 -2.3647 -0.1252 1.4096 6.8727
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 37.2851 1.8776 19.858 < 2e-16 ***
## wt -5.3445 0.5591 -9.559 1.29e-10 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 3.046 on 30 degrees of freedom
## Multiple R-squared: 0.7528, Adjusted R-squared: 0.7446
## F-statistic: 91.38 on 1 and 30 DF, p-value: 1.294e-10
mpg=37.285-5.344*wt
wt의 계수(estimate):-5.344(음의 관계)
p-value: 1.29e-10 (very small> statistically significant!)
R-squared:약 0.753
자동차의 중량이 증가할수록 연비는 감소한다. 이 관계는 통계적으로
유의하다. 따라서 중량은 연비를 설명하는데 중요한 변수이다.
5.산점도와 휘귀선 그리기
knitr::opts_chunk$set(echo = TRUE)
plot(mtcars$wt, mtcars$mpg,
main = "relationship of weight(wt) and fuel efficiency(mpg)",
xlab = "weight (1000 lbs)",
ylab = "fuel efficiency (mpg)",
pch = 19, col = "blue")

과제 2
1.데이터 불러오기
knitr::opts_chunk$set(echo = TRUE)
data("ToothGrowth")
head(ToothGrowth)
## len supp dose
## 1 4.2 VC 0.5
## 2 11.5 VC 0.5
## 3 7.3 VC 0.5
## 4 5.8 VC 0.5
## 5 6.4 VC 0.5
## 6 10.0 VC 0.5
2.독립변수와 종속변수는 무엇인가?
independent variable: dose (섭취한 비타민의 농도)/dependent
variable: len(치아 모세포의 길이)
3.선형회귀 모형 생성
knitr::opts_chunk$set(echo = TRUE)
model <- lm(len ~ dose, data = ToothGrowth)
4.선형회귀 모형 출력하고 결과 해석 (summary 함수) à 두 변수 간
관계가 통계적으로 유의한가? 관계가 어떠한가?
knitr::opts_chunk$set(echo = TRUE)
summary(model)
##
## Call:
## lm(formula = len ~ dose, data = ToothGrowth)
##
## Residuals:
## Min 1Q Median 3Q Max
## -8.4496 -2.7406 -0.7452 2.8344 10.1139
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 7.4225 1.2601 5.89 2.06e-07 ***
## dose 9.7636 0.9525 10.25 1.23e-14 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 4.601 on 58 degrees of freedom
## Multiple R-squared: 0.6443, Adjusted R-squared: 0.6382
## F-statistic: 105.1 on 1 and 58 DF, p-value: 1.233e-14
intercept: dose 가 0일때 치아 길이는 평균적으로 약 7.42mm
기울기: dose 가 1 씩 증가할때마다 치아길이가 평균적으로 9.76mm 씩
증가한다
p-value: 1.233e-14 (very small>statistically significant!)
dose 는 치아 모세포 길이에 유의미한 영향을 미친다.
5. 산점도와 회귀선 그리기
knitr::opts_chunk$set(echo = TRUE)
plot(ToothGrowth$dose, ToothGrowth$len,
main = "Dose vs Tooth Length",
xlab = "Dose (mg/day)",
ylab = "Tooth Length",
pch = 19, col = "blue")
