R-Blogger블로그·해설한국어2009-09-17
다항 회귀 기법
데이터 준비 및 시각화 10년 동안 이탈리아의 한 도시 인구를 보다 정확히 근사할 다항식 모델을 만들고자 합니다. 아래 표는 해당 데이터입니다. Year Population 1959 4835 1960 4970 1961 5085 1962 5160 1963 5310 1964 5260 1965 5235 1966 5255 1967 5235 1968 5210 1969 5175 먼저 R에 데이터를 입력합니다. Year Population 그리고 sample1이라는 데이터프레임을 생성합니다. sample1 <- data.frame( Year = c(1959,1960,1961,1962,1963,1964,1965,1966,1967,1968,1969), Population = c(4835,4970,5085,5160,5310,5260,5235,5255,5235,5210,5175) ) 그래프를 그려 경향을 확인하고 다항식의 차수를 정하기 위해 Year 컬럼을 0을 중심으로 재조정합니다. sample1$Year <- sample1$Year - 1964 sample1 Year Population 1 -5 4835 2 -4 4970 3 -3 5085 4 -2 5160 5 -1 5310 6 0 5260 7 1 5235 8 2 5255 9 3 5235 10 4 5210 11 5 5175 이제 값을 플롯합니다. plot(sample1$Year, sample1$Population, type="b") 다항 회귀 모델 적합 다항식 회귀를 적합할 때, R에는 두 가지 방법이 있습니다. 두 방법 모두 raw=TRUE를 지정하면 같은 결과를 반환합니다. raw=FALSE일 경우 정규 직교 다항식을 사용합니다. 차수 1, 2, 3의 모델을 다음과 같이 적합합니다. fit1 <- lm(sample1$Population ~ sample1$Year) fit2 <- lm(sample1$Population ~ sample1$Year + I(sample1$Year^2)) fit3 <- lm(sample1$Population ~ sample1$Year + I(sample1$Year^2) + I(sample1$Year^3)) 또는 빠르게 차수 2와 3을 적합할 수도 있습니다. fit2b <- lm(sample1$Population ~ poly(sample1$Year, 2, raw=TRUE)) fit3b <- lm(sample1$Population ~ poly(sample1$Year, 3, raw=TRUE)) 다항식의 일반형은 다음과 같습니다. f(x) = β₀ + β₁x + β₂x² + β₃x³ + … + βₙxⁿ 모델 2의 요약 결과는 다음과 같습니다. summary(fit2) ## Call: lm(formula = sample1$Population ~ sample1$Year + I(sample1$Year^2)) ## Residuals: ## Min 1Q Median 3Q Max ## -46.888 -18.834 -3.159 2.040 86.748 ## Coefficients: ## Estimate Std. Error t value Pr(>|t|) ## (Intercept) 5263.1590 17.6550 298.110 <2e-16 *** ## sample1$Year 29.3180 3.6960 7.933 4.64e-05 *** ## I(sample1$Year^2) -10.5890 1.3230 -8.002 4.36e-05 *** ## --- ## Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1 ## Residual standard error: 38.76 on 8 degrees of freedom ## Multiple R-squared: 0.9407, Adjusted R-squared: 0.9259 ## F-statistic: 63.48 on 2 and 8 DF, p-value: 1.235e-05 따라서 차수 2의 다항식은 f(x) = 5263.1597 + 29.318x – 10.589x² 차수 3의 모델 결과는 다음과 같습니다. summary(fit3) ## Call: lm(formula = sample1$Population ~ sample1$Year + I(sample1$Year^2) + I(sample1$Year^3)) ## Residuals: ## Min 1Q Median 3Q Max ## -32.774 -14.802 -1.253 3.199 72.634 ## Coefficients: ## Estimate Std. Error t value Pr(>|t|) ## (Intercept) 5263.1585 15.0667 349.324 4.16e-16 *** ## sample1$Year 14.3638 8.1282 1.767 0.1205 ## I(sample1$Year^2) -10.5886 1.1293 -9.376 3.27e-05 *** ## I(sample1$Year^3) 0.8401 0.4209 1.996 0.0861 . ## --- ## Residual standard error: 33.08 on 7 degrees of freedom ## Multiple R-squared: 0.9622, Adjusted R-squared: 0.946 ## F-statistic: 59.44 on 3 and 7 DF, p-value:
원문 URL
전체 글은 원문 페이지에서 이어서 읽을 수 있습니다.
- 작성자
- R-Blogger
- 출처
- R-Blogger
- 플랫폼
- R-Blogger
- 분류
- 블로그·해설
- 언어
- 한국어
- 발행일
- 2009-09-17