본문 바로가기
데이터분석/R

[R 데이터분석 with 샤이니] 아파트가격 상관관계 분석 (Shiny)

by 버섯도리 2022. 7. 26.

> ## 12-1 아파트 가격 상관관계 분석하기

> # Step 1 : 데이터 준비하기

> setwd(dirname(rstudioapi::getSourceEditorContext()$path))
> load("./06_geodataframe/06_apt_price.rdata") # 아파트 실거래 데이터
> library(sf)
> apt_price <- st_drop_geometry(apt_price)
> apt_price$py_area <- round(apt_price$area / 3.3, 0) # 크기 변환 (㎡ -> 평)
> head(apt_price[,c("area","py_area")])
  area py_area
1  130      39
2  145      44
3  175      53
4  175      53
5  123      37
6  123      37

> # Step 2 : 사용자 화면 구현하기

> library(shiny)
> ui <- fluidPage(
+   titlePanel("아파트 가격 상관관계 분석"),
+   sidebarPanel(
+     selectInput(
+       inputId = "sel_gu",
+       label = "지역을 선택하세요",
+       choices = unique(apt_price$addr_1),
+       selected = unique(apt_price$addr_1)[1]
+     ),
+     sliderInput(
+       inputId = "range_py",
+       label = "평수",
+       min = 0,
+       max = max(apt_price$py_area),
+       value = c(0, 30)
+     ),
+     selectInput(
+       inputId = "var_x",
+       label = "X축 변수를 선택하세요",
+       choices = list(
+         "매매가(평당)" = "py",
+         "크기(평)" = "py_area",
+         "건축 연도" = "con_year",
+         "층수" = "floor"),
+       selected = "py_area"
+     ),
+     selectInput(
+       inputId = "var_y",
+       label = "Y축 변수를 선택하세요",
+       choices = list(
+         "매매가(평당)" = "py",
+         "크기(평)" = "py_area",
+         "건축 연도" = "con_year",
+         "층수" = "floor"),
+       selected = "py"
+     ),
+     checkboxInput(
+       inputId = "corr_checked",
+       label = strong("상관 계수"),
+       value = TRUE
+     ),
+     checkboxInput(
+       inputId = "reg_checked",
+       label = strong("회귀 계수"),
+       value = TRUE
+     )
+   ),
+   mainPanel(
+     h4("플로팅"),
+     plotOutput("scatterPlot"),
+     h4("상관 계수"),
+     verbatimTextOutput("corr_coef"),
+     h4("회귀 계수"),
+     verbatimTextOutput("reg_fit")
+   )
+ )

> # Step 3 : 서버 구현하기

> server <- function(input, output, session) {
+   #---# 반응식
+   apt_sel <- reactive({
+     subset(apt_price,
+            addr_1 == input$sel_gu &
+              py_area >= input$range_py[1] & py_area <= input$range_py[2])
+   })
+   #---# 플롯
+   output$scatterPlot <- renderPlot({
+     var_name_x <- as.character(input$var_x)
+     var_name_y <- as.character(input$var_y)
+     plot(
+       apt_sel()[, input$var_x],
+       apt_sel()[, input$var_y],
+       xlab = var_name_x,
+       ylab = var_name_y,
+       main = "플로팅"
+     )
+     fit <- lm(apt_sel()[, input$var_y] ~ apt_sel()[, input$var_x])
+     abline(fit, col = "red") # 회귀선 그리기
+   })
+   #---# 상관 계수
+   output$corr_coef <- renderText({
+     if (input$corr_checked) {
+       cor(apt_sel()[, input$var_x], apt_sel()[, input$var_y])
+     }
+   })
+   #---# 회귀 계수
+   output$reg_fit <- renderPrint({
+     if (input$reg_checked) {
+       fit <- lm(apt_sel()[, input$var_y] ~ apt_sel()[, input$var_x])
+       names(fit$coefficients) <- c("Intercept", input$var_x)
+       summary(fit)$coefficients
+     }
+   })
+ }

> # Step 4 : 애플리케이션 실행하기

> shinyApp(ui = ui, server = server)

Listening on http://127.0.0.1:5417



 

 

 

 

 

 

출처 : 김철민, ⌜공공데이터로 배우는 R 데이터분석 with 샤이니⌟, 이지스퍼블리싱, 2022