R-Blogger블로그·해설한국어2023-12-17
R을 활용한 난방 및 냉방도수 추세 분석
소개 기온 차(°F)를 기준 온도(미국에서는 65°F)와 비교하여 건물 난방·냉방 수요를 측정하는 지표가 바로 디그리데이입니다. 예를 들어, 오늘의 평균 온도가 40°F라면 난방 디그리데이는 25 HDD가 됩니다. 평균 온도가 85°F인 여름날은 20 CDD가 됩니다. 디그리데이는 보통 가정·상업용 건물의 난방·냉방 에너지 사용량과 잘 상관관계가 있습니다. 저는 디그리데이 데이터를 수집·분석해 시간에 따른 추세가 있는지 살펴보고자 했습니다. 기후변화로 지구 평균 온도가 상승함에 따라 CDD가 증가하고 HDD가 감소할 것이라고 가정합니다. 난방·냉방 디그리데이의 변화는 앞으로 필요한 에너지량, 전력망 수요, 탄소 배출에 영향을 미칠 수 있습니다. 미국 에너지정보국(EIA)에서 인구 가중치를 적용한 미국 전체 데이터(1997~2022)를 사용했습니다. 자세한 계산 방법은 여기를 참고하십시오. R 코드 및 데이터 준비 필요한 패키지를 로드하고 그래픽 테마를 설정합니다. suppressPackageStartupMessages(library(tidyverse)) ggplot2::theme_set(theme_grey(base_size = 15)) suppressPackageStartupMessages(library(janitor)) library(broom) library(DT) suppressPackageStartupMessages(library(plotly)) 데이터를 전처리하고 1997~2022년 완전한 연도만 추출합니다. region_data %>% rename(CDD = paste0('cooling_degree_days_',region, '_cooling_degree_days_cooling_degree_days')) %>% rename(HDD = paste0('heating_degree_days_',region, '_heating_degree_days_heating_degree_days')) %>% mutate(date = lubridate::my(month)) %>% select(-month) %>% mutate(month = lubridate::month(date)) %>% mutate(year = lubridate::year(date)) %>% filter(year > 1996, year) %>% DT::datatable(options = list(pageLength = 5), rownames = FALSE) 연도별 합계를 계산합니다. dd_yearly <- dd %>% filter(year > 1996) %>% group_by(year) %>% summarise(HDD = sum(HDD, na.rm = TRUE), CDD = sum(CDD, na.rm = TRUE)) dd_yearly %>% DT::datatable(options = list(pageLength = 5), rownames = FALSE) 난방 디그리데이(HDD) 분석 월별 분포 다음 박스플롯은 매월의 HDD 분포를 보여줍니다. 겨울 월에 HDD가 높고 연도별 변동성이 존재합니다. dd %>% mutate(month_name = lubridate::month(date, label = TRUE)) %>% ggplot(aes(month_name, HDD, group = month_name)) + geom_boxplot() + labs(title = 'Monthly Heating Degree Days for US (1997-2022)', x = 'Month', y = "Heating Degree Days") 연도별 추세 연간 총 HDD를 시계열로 표시하고 선형 회귀선을 그립니다. 회귀선은 부정적 경향을 나타냅니다. ggplot(aes(year, HDD)) + geom_point(size = 4, alpha = 0.5) + geom_smooth(method = 'lm', formula = 'y~x') + labs(title = "US Annual Heating Degree Days") 회귀 결과를 확인하면 p.value가 0.05보다 작으므로 통계적으로 유의한 감소 추세가 확인됩니다. hdd_yearly_fit <- lm(HDD ~ year, data = dd_yearly) hdd_yearly_fit %>% broom::tidy() %>% mutate_if(is.numeric, ~round(.x, digits = 3)) 또는 요약 통계: broom::glance(hdd_yearly_fit) %>% mutate_if(is.numeric, ~round(.x, digits = 3)) 월별 추세(겨울월) 겨울 월(11~4)마다 시계열을 그리고 회귀선을 그립니다. 일부 월에 부정적 경향이 보입니다. dd %>% filter(month %in% c(11,12,1,2,3,4)) %>% mutate(month_name = lubridate::month(date, label = TRUE)) %>% ggplot(aes(year, HDD, group = month_name)) + geom_point(size = 3, alpha = 0.5) + geom_smooth(method = 'lm', formula = 'y ~ x') + facet_wrap('month_name', scales = 'free') + guides(x = guide_axis(angle = 45)) 월별 선형 회귀를 한 번에 수행하고 유의한 경향을 찾습니다. dd_fit_hdd <- dd %>% group_by(month) %>% nest() %>% mutate(fit = map(data, ~ lm(HDD ~ year, data = .x)), tidied = map(fit, broom::tidy), glanced = map(fit, broom::glance)) %>% unnest(tidied) %>% ungroup() dd_fit_hdd %>% mutate_if(is.numeric, ~round(.x, digits = 3)) %>% DT::datatable(rownames = FALSE, options = list(pageLength = 5)) 유의한 경향이 있는 월은 6월, 8월, 9월, 10월입니다. 이를 시각화하면 다음과 같습니다. dd_fit_hdd %>% filter(term == 'year') %>% filter(p.value < 0.05) %>% mutate_if(is.numeric, ~round(.x, digits = 3)) %>%
원문 URL
전체 글은 원문 페이지에서 이어서 읽을 수 있습니다.
- 작성자
- R-Blogger
- 출처
- R-Blogger
- 플랫폼
- R-Blogger
- 분류
- 블로그·해설
- 언어
- 한국어
- 발행일
- 2023-12-17