베이지안 네트워크 활용 - beijian neteuwokeu hwal-yong

베이지안 네트워크(Bayesian network)를 활용하여 King County의 집값에 영향을 미치는 다양한 요소의 인과관계를 확인하고, 분석하는 것이 이 프로젝트의 목적이다. 데이터의 출처는 여기(www.kaggle.com/harlfoxem/housesalesprediction)이고, 아래의 구성 순서대로 분석 및 모델링 과정을 수행할 것이다. 모든 코드는 R로 작성되었다.

1. 데이터 전처리

2. 시각화 및 상관관계 분석

3. 다중회귀분석

4. 베이지안 네트워크 모델링

1. 데이터 전처리

In:

# Statistic library(car) # Data manipulation library(dplyr) library(tidyr) # Visualization library(ggplot2) library(corrplot) library(GGally) library(scales) # Bayesian network library(bnlearn) library(Rgraphviz) # Setting for ggplot theme theme_set(theme_minimal() + theme(plot.title = element_text(face = 'bold', colour = 'grey10'), plot.subtitle = element_text(colour = 'grey25'), panel.grid.major = element_line(colour = 'grey90', size = 1), panel.grid.minor = element_line(colour = 'grey80', size = 0.5, linetype = 'dashed'), legend.position = 'top', legend.spacing.x = unit(0.125, 'cm'), legend.background = element_rect(fill = NULL, linetype = 'dotted'), strip.background = element_blank(), strip.text = element_text(face = 'bold', colour = 'grey25', size = 11.25))) data_kc = read.csv('../input/kc_house_data.csv') str(data_kc) summary(data_kc)

Out:

'data.frame': 21613 obs. of 21 variables: $ id : num 7.13e+09 6.41e+09 5.63e+09 2.49e+09 1.95e+09 ... $ date : Factor w/ 372 levels "20140502T000000",..: 165 221 291 221 284 11 57 252 340 306 ... $ price : num 221900 538000 180000 604000 510000 ... $ bedrooms : int 3 3 2 4 3 4 3 3 3 3 ... $ bathrooms : num 1 2.25 1 3 2 4.5 2.25 1.5 1 2.5 ... $ sqft_living : int 1180 2570 770 1960 1680 5420 1715 1060 1780 1890 ... $ sqft_lot : int 5650 7242 10000 5000 8080 101930 6819 9711 7470 6560 ... $ floors : num 1 2 1 1 1 1 2 1 1 2 ... $ waterfront : int 0 0 0 0 0 0 0 0 0 0 ... $ view : int 0 0 0 0 0 0 0 0 0 0 ... $ condition : int 3 3 3 5 3 3 3 3 3 3 ... $ grade : int 7 7 6 7 8 11 7 7 7 7 ... $ sqft_above : int 1180 2170 770 1050 1680 3890 1715 1060 1050 1890 ... $ sqft_basement: int 0 400 0 910 0 1530 0 0 730 0 ... $ yr_built : int 1955 1951 1933 1965 1987 2001 1995 1963 1960 2003 ... $ yr_renovated : int 0 1991 0 0 0 0 0 0 0 0 ... $ zipcode : int 98178 98125 98028 98136 98074 98053 98003 98198 98146 98038 ... $ lat : num 47.5 47.7 47.7 47.5 47.6 ... $ long : num -122 -122 -122 -122 -122 ... $ sqft_living15: int 1340 1690 2720 1360 1800 4760 2238 1650 1780 2390 ... $ sqft_lot15 : int 5650 7639 8062 5000 7503 101930 6819 9711 8113 7570 ... id date price bedrooms bathrooms sqft_living sqft_lot Min. :1.000e+06 20140623T000000: 142 Min. : 75000 Min. : 0.000 Min. :0.000 Min. : 290 Min. : 520 1st Qu.:2.123e+09 20140625T000000: 131 1st Qu.: 321950 1st Qu.: 3.000 1st Qu.:1.750 1st Qu.: 1427 1st Qu.: 5040 Median :3.905e+09 20140626T000000: 131 Median : 450000 Median : 3.000 Median :2.250 Median : 1910 Median : 7618 Mean :4.580e+09 20140708T000000: 127 Mean : 540088 Mean : 3.371 Mean :2.115 Mean : 2080 Mean : 15107 3rd Qu.:7.309e+09 20150427T000000: 126 3rd Qu.: 645000 3rd Qu.: 4.000 3rd Qu.:2.500 3rd Qu.: 2550 3rd Qu.: 10688 Max. :9.900e+09 20150325T000000: 123 Max. :7700000 Max. :33.000 Max. :8.000 Max. :13540 Max. :1651359 (Other) :20833 floors waterfront view condition grade sqft_above sqft_basement yr_built Min. :1.000 Min. :0.000000 Min. :0.0000 Min. :1.000 Min. : 1.000 Min. : 290 Min. : 0.0 Min. :1900 1st Qu.:1.000 1st Qu.:0.000000 1st Qu.:0.0000 1st Qu.:3.000 1st Qu.: 7.000 1st Qu.:1190 1st Qu.: 0.0 1st Qu.:1951 Median :1.500 Median :0.000000 Median :0.0000 Median :3.000 Median : 7.000 Median :1560 Median : 0.0 Median :1975 Mean :1.494 Mean :0.007542 Mean :0.2343 Mean :3.409 Mean : 7.657 Mean :1788 Mean : 291.5 Mean :1971 3rd Qu.:2.000 3rd Qu.:0.000000 3rd Qu.:0.0000 3rd Qu.:4.000 3rd Qu.: 8.000 3rd Qu.:2210 3rd Qu.: 560.0 3rd Qu.:1997 Max. :3.500 Max. :1.000000 Max. :4.0000 Max. :5.000 Max. :13.000 Max. :9410 Max. :4820.0 Max. :2015 yr_renovated zipcode lat long sqft_living15 sqft_lot15 Min. : 0.0 Min. :98001 Min. :47.16 Min. :-122.5 Min. : 399 Min. : 651 1st Qu.: 0.0 1st Qu.:98033 1st Qu.:47.47 1st Qu.:-122.3 1st Qu.:1490 1st Qu.: 5100 Median : 0.0 Median :98065 Median :47.57 Median :-122.2 Median :1840 Median : 7620 Mean : 84.4 Mean :98078 Mean :47.56 Mean :-122.2 Mean :1987 Mean : 12768 3rd Qu.: 0.0 3rd Qu.:98118 3rd Qu.:47.68 3rd Qu.:-122.1 3rd Qu.:2360 3rd Qu.: 10083 Max. :2015.0 Max. :98199 Max. :47.78 Max. :-121.3 Max. :6210 Max. :871200

▷ KC County의 집값 데이터는 21,613개의 행과 21개의 열로 구성되어 있고, 각 열의 의미는 다음과 같다.

[id]: Unique ID for each home sold
[date]: Date of the home sale
[price]: Price of each home sold
[bedrooms]: Number of bedrooms
[bathrooms]: Number of bathrooms, where .5 accounts for a room with a toilet but no shower
[sqft_living]: Square footage of the apartments interior living space
[sqft_lot]: Square footage of the land space
[floors]: Number of floors
[waterfront]: A dummy variable for whether the apartment was overlooking the waterfront or not
[view]: An index from 0 to 4 of how good the view of the property was
[condition]: An index from 1 to 5 on the condition of the apartment,
[grade]: An index from 1 to 13, where 1-3 falls short of building construction and design, 7 has an average level of construction and design, and 11-13 have a high-quality level of construction and design
[sqft_above]: The square footage of the interior housing space that is above ground level
[sqft_basement]: The square footage of the interior housing space that is below ground level
[yr_built]: The year the house was initially built
[yr_renovated]: The year of the house’s last renovation
[zipcode]: What zip code area the house is in
[lat]: Lattitude
[long]: Longitude
[sqft_living15]: The square footage of interior housing living space for the nearest 15 neighbors
[sqft_lot15]: The square footage of the land lots of the nearest 15 neighbors

분석을 수행하기 위해 다음의 전처리 과정을 거쳤다.

In:

df_kc = data_kc %>% select(-id, -date, -zipcode, -lat, -long) df_kc$age = 2015 - df_kc$yr_built + 1 df_kc = df_kc %>% select(-yr_built) df_kc = df_kc %>% mutate(yr_renovated = if_else(yr_renovated == 0, 'No Renovation', if_else(yr_renovated < 1980, 'Under 1980', if_else(yr_renovated < 2000, '1980 - 2000', '2000 - 2015')))) df_kc$yr_renovated = as.factor(df_kc$yr_renovated) df_kc$sqft_above_ratio = with(df_kc, sqft_above/sqft_living)) df_kc = df_kc %>% select(-sqft_above, -sqft_basement) df_kc$waterfront = as.factor(df_kc$waterfront) df_kc[, sapply(df_kc, function(x) is.integer(x))] = lapply(df_kc[, sapply(df_kc, function(x) is.integer(x))], as.double)

▷ id, date, zipcode, lat, long은 분석 대상에서 제외하였다.

▷ 집의 나이를 의미하는 age는 데이터 측정 시점인 2015년에 yr_built를 빼서 생성하고, yr_built를 제외하였다.

▷ yr_renovated의 경우, 보수가 안 된 집은 0, 보수가 된 집은 공사된 해로 나타나 있다. 이를 모델에 하나의 변수로 넣게되면 해석의 문제가 발생하게 된다. 따라서 네 개의 카테고리(No Renovation, Under 1980 - 2000, 2000 - 2015)로 바꾸었다.

▷ sqft_above와 sqft_basement의 합은 sqft_living이다. 즉, 이 세 열의 정보는 중복되어 있다. 따라서 sqft_living만 남겨두고, sqft_above를 sqft_living으로 나누어 거주 공간 중 지상의 차지 비율을 의미하는 sqft_above_ratio라는 변수를 생성하였다.

▷ 범주형 변수는 factor로 변환하고, 이외의 변수는 모두 실수로 변환하였다.

2. 시각화 및 상관관계 분석

In:

df_temp = df_kc %>% select(-yr_renovated, -waterfront) df_temp %>% gather(key = key, value = value, bedrooms:view) %>% ggplot(aes(value, price)) + geom_bin2d() + scale_x_continuous(label = comma) + scale_fill_gradient(trans = 'log') + guides(fill = F) + facet_wrap(~key, scales = 'free', ncol = 2) + labs(x = NULL) df_temp %>% gather(key = key, value = value, condition:sqft_above_ratio) %>% ggplot(aes(value, price)) + geom_bin2d() + scale_x_continuous(label = comma) + scale_fill_gradient(trans = 'log') + guides(fill = F) + facet_wrap(~key, scales = 'free', ncol = 2) + labs(x = NULL) df_temp = df_kc %>% mutate(waterfront = if_else(waterfront == '1', 'TRUE', 'FALSE')) %>% select(price, waterfront, yr_renovated) df_temp %>% gather(key = key, value = value, waterfront, yr_renovated) %>% mutate(value = factor(value, levels = c('Under 1980', '1980 - 2000', '2000 - 2015', 'No Renovation', 'TRUE', 'FALSE'))) %>% ggplot(aes(value, price, colour = value)) + geom_boxplot(size = 1) + scale_y_log10() + facet_wrap(~ key, scale = 'free', ncol = 2) + guides(colour = F)

Out:

▷ sqft_lot, sqft_lot15, sqft_living15는 price와 비선형 관계를 보이고 있다.

▷ 위의 시각화로부터 다음의 경향성을 확인할 수 있다.

- (욕실의 수 / 침실의 수 / 내부 공간 / 평가 등급 / 주변 이웃의 내부 공간)이 클수록 가격이 증가하는 경향을 보인다.

- (땅의 크기 / 주변 이웃의 땅의 크기)가 클수록 가격이 감소하는 경향을 보인다.

- (바다 또는 강이 보이는 뷰를 가지는 경우 / 최근에 보수공사를 진행한 경우에)에 가격이 증가하는 경향을 보인다.

In:

cor.mtest <- function(mat, conf.level = 0.95) { mat <- as.matrix(mat) n <- ncol(mat) p.mat <- lowCI.mat <- uppCI.mat <- matrix(NA, n, n) diag(p.mat) <- 0 diag(lowCI.mat) <- diag(uppCI.mat) <- 1 for (i in 1:(n - 1)) { for (j in (i + 1):n) { tmp <- cor.test(mat[, i], mat[, j], conf.level = conf.level) p.mat[i, j] <- p.mat[j, i] <- tmp$p.value lowCI.mat[i, j] <- lowCI.mat[j, i] <- tmp$conf.int[1] uppCI.mat[i, j] <- uppCI.mat[j, i] <- tmp$conf.int[2] } } return(list(p.mat, lowCI.mat, uppCI.mat)) } res <- cor.mtest(df_kc %>% select(-waterfront, -yr_renovated), 0.95) corrplot(df_kc %>% select(-waterfront, -yr_renovated) %>% cor(method = 'spearman'), p.mat = res[[1]], sig.level = 0.05, method = 'number', type = 'lower')

Out:

▷ 범주형 변수인 waterfont와 yr_renovated를 제외하고, 연속형 변수에 대하여 상관관계 분석을 수행하였다. 위의 결과는 비선형 관계를 고려할 수 있는 스피어만 상관계수를 나타낸 것이다.

▷ X 표시는 상관계수 검정결과가 유의수준 0.05에서 유의하지 않은 상관관계를 의미한다.

▷ age와 sqft_above_ratio를 제외한 연속형 변수와 price가 양의 상관관계를 띄는 것으로 나타났다.

3. 다중회귀분석

In:

model_lm = lm(log(price) ~ ., df_kc) summary(model_lm)

Out:

Call: lm(formula = log(price) ~ ., data = df_kc) Residuals: Min 1Q Median 3Q Max -1.38962 -0.20882 0.01414 0.20767 1.28996 Coefficients: Estimate Std. Error t value Pr(>|t|) (Intercept) 1.058e+01 3.298e-02 320.814 < 2e-16 *** bedrooms -2.382e-02 2.895e-03 -8.230 < 2e-16 *** bathrooms 7.230e-02 5.005e-03 14.445 < 2e-16 *** sqft_living 1.078e-04 5.245e-06 20.551 < 2e-16 *** sqft_lot 2.391e-07 7.321e-08 3.266 0.001091 ** floors 1.301e-01 5.390e-03 24.141 < 2e-16 *** waterfront1 3.595e-01 2.664e-02 13.497 < 2e-16 *** view 3.975e-02 3.215e-03 12.364 < 2e-16 *** condition 4.368e-02 3.570e-03 12.236 < 2e-16 *** grade 2.065e-01 3.208e-03 64.386 < 2e-16 *** yr_renovated2000 - 2015 7.886e-02 2.233e-02 3.532 0.000414 *** yr_renovatedNo Renovation -7.368e-03 1.753e-02 -0.420 0.674227 yr_renovatedUnder 1980 -1.155e-01 3.270e-02 -3.532 0.000413 *** sqft_living15 1.141e-04 5.110e-06 22.338 < 2e-16 *** sqft_lot15 -5.139e-07 1.119e-07 -4.592 4.41e-06 *** age 5.423e-03 1.013e-04 53.525 < 2e-16 *** sqft_above_ratio -2.597e-01 1.502e-02 -17.293 < 2e-16 *** --- Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1 Residual standard error: 0.3086 on 21596 degrees of freedom Multiple R-squared: 0.657, Adjusted R-squared: 0.6567 F-statistic: 2585 on 16 and 21596 DF, p-value: < 2.2e-16

▷ 시각화를 통해 price와 다른 연속형 변수의 비선형 관계를 확인할 수 있었다. 따라서 log를 취한 price를 모델의 반응변수로 정하였다.

▷ 이 모델의 R-squared는 0.6567로 전체 변동의 약 66%를 설명한다. F-Statistic를 통해 유의수준 0.05에서 이 모델이 유의하는 결론을 얻을 수 있다.

▷ yr_renovated 변수의 No Renovation는 유의수준 0.05에서 유의하지 않은 것으로 나타났다. 이외의 모든 변수는 유의수준 0.05에서 유의한 것으로 나타났다.

In:

plot(model_lm, 2)

Out:

▷ 왼쪽 아래 끝 부분을 제외한 대부분의 데이터는 직선 상에 위치하는 것을 확인할 수 있다. 따라서 정규성 가정을 만족한다고 볼 수 있다.

In:

vif(model_lm)

Out:

GVIF Df GVIF^(1/(2*Df)) bedrooms 1.645304 1 1.282694 bathrooms 3.372189 1 1.836352 sqft_living 5.266456 1 2.294876 sqft_lot 2.087090 1 1.444676 floors 1.922529 1 1.386553 waterfront 1.205512 1 1.097958 view 1.377756 1 1.173778 condition 1.224774 1 1.106695 grade 3.226952 1 1.796372 yr_renovated 1.164329 3 1.025682 sqft_living15 2.783465 1 1.668372 sqft_lot15 2.118644 1 1.455556 age 2.010378 1 1.417878 sqft_above_ratio 1.496085 1 1.223146

▷ 모든 변수의 VIF가 10 이하이기 때문에, 다중공선성 문제가 나타나지 않는다고 볼 수 있다.

4. 베이지안 네트워크 모델링

In:

df_input = df_kc %>% mutate(log_price = log(price)) %>% select(-price) model_bn_prototype = hc(df_input, score = 'bic-cg') model_bn_prototype plot_bn = layoutGraph(as.graphNEL(model_bn_prototype)) nodeRenderInfo(plot_bn) <- list(fontsize = 150) renderGraph(plot_bn)

Out:

Bayesian network learned via Score-based methods model: [waterfront][yr_renovated|waterfront][sqft_lot|yr_renovated][sqft_lot15|sqft_lot:waterfront:yr_renovated] [bathrooms|sqft_lot:waterfront:sqft_lot15][sqft_living|bathrooms:sqft_lot:waterfront:sqft_lot15] [sqft_above_ratio|bathrooms:sqft_living:sqft_lot:sqft_lot15][floors|bathrooms:sqft_living:sqft_lot:sqft_lot15:sqft_above_ratio] [age|bathrooms:sqft_living:floors:yr_renovated:sqft_lot15:sqft_above_ratio][condition|bathrooms:floors:yr_renovated:age:sqft_above_ratio] [sqft_living15|sqft_living:sqft_lot:floors:yr_renovated:sqft_lot15:age:sqft_above_ratio] [grade|bathrooms:sqft_living:floors:sqft_living15:sqft_lot15:age:sqft_above_ratio] [log_price|bathrooms:sqft_living:floors:waterfront:condition:grade:sqft_living15:age:sqft_above_ratio] [view|waterfront:yr_renovated:sqft_living15:age:sqft_above_ratio:log_price] [bedrooms|bathrooms:sqft_living:floors:view:condition:grade:sqft_lot15:age:sqft_above_ratio:log_price] nodes: 15 arcs: 71 undirected arcs: 0 directed arcs: 71 average markov blanket size: 12.80 average neighbourhood size: 9.47 average branching factor: 4.73 learning algorithm: Hill-Climbing score: BIC (cond. Gauss.) penalization coefficient: 4.990525 tests used in the learning procedure: 1260 optimized: TRUE

▷ Score는 BIC로, 이를 최대화하는 구조를 찾기 위해 Hill-Climbing 알고리즘을 사용하였다.

▷ 위의 그림은 학습된 네트워크의 구조이고, 우리의 직관과 맞지 않는 관계를 가지고 있다. 위의 네트워크에 따르면 log_price가 bedrooms에 영향을 주다. 즉, 가격이 집의 침대의 개수에 영향을 준다고 보아야 한다. 이 인과관계는 우리의 직관과 맞지 않다. 따라서 데이터의 특성에 맞는 인과관계를 미리 지정하여 합리적인 네트워크를 만들어야 한다.

데이터의 특성을 고려한 베이지안 네트워크의 구성 조건은 다음과 같다.

- bedrooms, bathrooms, sqft_living, waterfront, view, condition, grade, yr_renovated는 log_price에 영향을 준다.- floors, bedrooms, bathrooms, sqft_living, sqft_lot, sqft_above_ratio, sqft_living15, sqft_lot15, waterfront, view, age는 노드의 최상단에 위치하고, 서로 영향을 주고 받을 수 없다.

- log_price는 어느 변수에도 영향을 줄 수 없다.

- condition, grade는 최상단에 위치한 노드에 영향을 줄 수 없다.- yr_renovated는 최상단 노드에 영향을 줄 수 없다.

위의 조건을 고려하여 네트워크의 구조를 학습하여 보자.

In:

top_node = c('floors', 'bedrooms', 'bathrooms', 'sqft_living', 'sqft_lot', 'sqft_above_ratio', 'sqft_living15', 'sqft_lot15', 'waterfront', 'view', 'age') df_wl = data.frame(from = c('bedrooms', 'bathrooms', 'sqft_living', 'waterfront', 'view', 'condition', 'grade', 'yr_renovated'), to = 'log_price') df_bl = NULL for (i in top_node) { df_bl = df_bl %>% bind_rows(data.frame(from = top_node[top_node != i], to = i)) } df_bl = df_bl %>% bind_rows(data.frame(from = 'log_price', to = names(df_kc)[names(df_kc) != 'price'])) df_bl = df_bl %>% bind_rows(data.frame(from = 'condition', to = top_node)) df_bl = df_bl %>% bind_rows(data.frame(from = 'grade', to = top_node)) df_bl = df_bl %>% bind_rows(data.frame(from = 'yr_renovated', to = top_node)) model_bn = hc(df_input, blacklist = df_bl, whitelist = df_wl, score = 'bic-cg') plot_bn = layoutGraph(as.graphNEL(model_bn)) nodeRenderInfo(plot_bn) <- list(fontsize = 50) renderGraph(plot_bn)

Out:

▷ 앞의 네트워크 구조 학습 방법과 차이점은 blacklist, whitelist 인자에 네트워크 구성 조건을 준 것이다.

▷ sqft_lot는 네트워크에서 고립되어 어느 노드에도 영향을 주고 받지 않는 것으로 나타났다.

In:

fit_bn = bn.fit(model_bn, df_input) fit_bn

Out:

Bayesian network parameters Parameters of node bedrooms (Gaussian distribution) Conditional density: bedrooms Coefficients: (Intercept) 3.370842 Standard deviation of the residuals: 0.9300618 Parameters of node bathrooms (Gaussian distribution) Conditional density: bathrooms Coefficients: (Intercept) 2.114757 Standard deviation of the residuals: 0.7701632 Parameters of node sqft_living (Gaussian distribution) Conditional density: sqft_living Coefficients: (Intercept) 2079.9 Standard deviation of the residuals: 918.4409 Parameters of node sqft_lot (Gaussian distribution) Conditional density: sqft_lot Coefficients: (Intercept) 15106.97 Standard deviation of the residuals: 41420.51 Parameters of node floors (Gaussian distribution) Conditional density: floors Coefficients: (Intercept) 1.494309 Standard deviation of the residuals: 0.5399889 Parameters of node waterfront (multinomial distribution) Conditional probability table: 0 1 0.992458243 0.007541757 Parameters of node view (Gaussian distribution) Conditional density: view Coefficients: (Intercept) 0.2343034 Standard deviation of the residuals: 0.7663176 Parameters of node condition (conditional Gaussian distribution) Conditional density: condition | bathrooms + floors + yr_renovated + age + sqft_above_ratio Coefficients: 0 1 2 3 (Intercept) 3.0563791874 3.1737338235 3.1973525083 3.8059657879 bathrooms 0.0312814109 -0.0095737742 0.1204390398 0.2832070755 floors 0.0269270784 0.0154644257 -0.1347846515 -0.0639222984 age 0.0024137791 -0.0007246165 0.0090552567 -0.0025437365 sqft_above_ratio -0.0275911248 -0.0854984414 -0.2587305161 -0.4778581940 Standard deviation of the residuals: 0 1 2 3 0.6009327 0.2598014 0.5917577 0.6960968 Discrete parents' configurations: yr_renovated 0 1980 - 2000 1 2000 - 2015 2 No Renovation 3 Under 1980 Parameters of node grade (Gaussian distribution) Conditional density: grade | bedrooms + bathrooms + sqft_living + floors + view + sqft_living15 + sqft_lot15 + age + sqft_above_ratio Coefficients: (Intercept) bedrooms bathrooms sqft_living floors view sqft_living15 sqft_lot15 5.196123e+00 -1.262738e-01 1.360192e-01 6.117853e-04 2.420609e-01 7.028056e-02 4.541821e-04 -1.453307e-06 age sqft_above_ratio -4.804130e-03 3.204805e-01 Standard deviation of the residuals: 0.6555349 Parameters of node yr_renovated (multinomial distribution) Conditional probability table: waterfront yr_renovated 0 1 1980 - 2000 0.014358974 0.159509202 2000 - 2015 0.020885781 0.061349693 No Renovation 0.959347319 0.742331288 Under 1980 0.005407925 0.036809816 Parameters of node sqft_living15 (Gaussian distribution) Conditional density: sqft_living15 Coefficients: (Intercept) 1986.552 Standard deviation of the residuals: 685.3913 Parameters of node sqft_lot15 (Gaussian distribution) Conditional density: sqft_lot15 Coefficients: (Intercept) 12768.46 Standard deviation of the residuals: 27304.18 Parameters of node age (Gaussian distribution) Conditional density: age Coefficients: (Intercept) 44.99486 Standard deviation of the residuals: 29.37341 Parameters of node sqft_above_ratio (Gaussian distribution) Conditional density: sqft_above_ratio Coefficients: (Intercept) 0.8754978 Standard deviation of the residuals: 0.1709693 Parameters of node log_price (conditional Gaussian distribution) Conditional density: log_price | bedrooms + bathrooms + sqft_living + waterfront + view + condition + grade + yr_renovated Coefficients: 0 1 2 3 4 5 6 7 (Intercept) 1.066013e+01 1.139515e+01 1.121704e+01 1.244367e+01 1.083384e+01 1.153135e+01 1.097135e+01 -2.108439e+01 bedrooms 8.894919e-04 1.359918e-01 -2.512079e-02 1.356706e-01 -1.509238e-02 1.677411e-02 -4.520446e-02 1.120495e+00 bathrooms 5.170345e-02 -2.965593e-02 4.840945e-02 1.804738e-01 -9.093745e-04 8.195861e-02 5.097185e-02 2.878328e+01 sqft_living 1.569443e-04 3.193704e-04 1.891389e-04 -9.619998e-05 1.984457e-04 1.835918e-04 1.766649e-04 -1.798751e-03 view 4.760609e-02 -2.261726e-02 3.788562e-02 -5.117922e-01 7.375035e-02 8.165891e-02 1.121705e-01 2.645102e+01 condition 7.673433e-02 1.196717e-01 -1.013576e-01 6.484588e-02 1.019852e-01 4.940656e-02 -7.447056e-02 -4.044361e+01 grade 2.443866e-01 1.168730e-01 2.517358e-01 3.300950e-01 1.929564e-01 1.407172e-01 2.775896e-01 0.000000e+00 Standard deviation of the residuals: 0 1 2 3 4 5 6 7 0.3428534 0.3922664 0.3361898 0.5091354 0.3331445 0.3407674 0.3259330 0.0000000 Discrete parents' configurations: waterfront yr_renovated 0 0 1980 - 2000 1 1 1980 - 2000 2 0 2000 - 2015 3 1 2000 - 2015 4 0 No Renovation 5 1 No Renovation 6 0 Under 1980 7 1 Under 1980

▷ bn.fit 함수를 이용하여, 학습된 구조로부터 파라미터를 학습할 수 있다. 위의 결과는 파라미터의 학습 결과이다. 각 Conditional density의 계수로부터 변수간의 관계를 확인할 수 있다.

In:

pred_output = predict(model_lm, df_kc) rmse_lm = sum(abs(pred_output - log(df_kc$price))^2)/nrow(df_kc) df_lm = data.frame(log_price = log(df_kc$price), pred_log_price = pred_output, model = 'Multiple Linear Regression') pred_output = predict(fit_bn, node = 'log_price', data = df_input) rmse_bn = sum(abs(pred_output - df_input$log_price)^2)/nrow(df_input) df_bn = data.frame(log_price = df_input$log_price, pred_log_price = pred_output, model = 'Bayeisan Network') df_total = df_lm %>% bind_rows(df_bn) print(rmse_lm) print(rmse_bn) df_total %>% ggplot(aes(log_price, pred_log_price, colour = model)) + geom_point(alpha = 0.05) + geom_abline(slope = 1, intercept = 0, linetype = 'dashed', size = 0.75) + guides(colour = F) + facet_wrap(~ model)

Out:

[1] 0.09514861 [1] 0.1108972

▷ 다중회귀 모델과 학습 결과의 RMSE가 베이지안 네트워크 모델보다 더 낮은 것으로 나타났지만, 큰 차이는 없는 것을 알 수 있다.

▷ 두 모델의 잔차의 분포가 비슷한 형태를 띄는 것을 확인할 수 있다.

Toplist

최신 우편물

태그