flowchart TD A[센서] --> B[데이터 수집] B --> C[전송] C --> D[클라우드 저장] D --> E[데이터 분석] E --> F[인사이트 도출] F --> G[액션]
R을 활용한 IoT 빅데이터 분석
9/1/14
IoT의 정의
flowchart TD A[센서] --> B[데이터 수집] B --> C[전송] C --> D[클라우드 저장] D --> E[데이터 분석] E --> F[인사이트 도출] F --> G[액션]
전통적 3V
확장된 2V
Tip
IoT 환경에서는 속도(Velocity)와 다양성(Variety)이 특히 중요합니다.
R의 특징
R의 장점
R의 강점
Python의 강점
데이터 특성
데이터 품질 이슈
# MQTT 클라이언트 설정
library(mqtt)
client <- mqtt_client_new()
mqtt_connect(client, host = "broker.hivemq.com", port = 1883)
mqtt_subscribe(client, "sensors/temperature")
# REST API를 통한 데이터 수집
library(httr)
library(jsonlite)
response <- GET("https://api.iot-device.com/sensors/data")
data <- fromJSON(content(response, "text"))
library(dplyr)
# 결측값 확인
summary(iot_data)
# 결측값 처리
iot_data <- iot_data %>%
mutate(
temperature = ifelse(is.na(temperature),
mean(temperature, na.rm = TRUE),
temperature)
)
# 이상값 탐지 (IQR 방법)
Q1 <- quantile(iot_data$temperature, 0.25)
Q3 <- quantile(iot_data$temperature, 0.75)
IQR <- Q3 - Q1
# 이상값 제거
iot_data <- iot_data %>%
filter(temperature >= Q1 - 1.5 * IQR &
temperature <= Q3 + 1.5 * IQR)
library(dplyr)
library(skimr)
# 데이터 요약
skim(iot_data)
# 그룹별 통계
iot_data %>%
group_by(sensor_type) %>%
summarise(
mean_value = mean(value),
sd_value = sd(value),
min_value = min(value),
max_value = max(value)
)
library(forecast)
# 시계열 객체 생성
ts_data <- ts(iot_data$temperature, frequency = 24)
# 시계열 분해
decomposed <- decompose(ts_data)
plot(decomposed)
# ARIMA 모델
arima_model <- auto.arima(ts_data)
forecast_result <- forecast(arima_model, h = 24)
plot(forecast_result)
library(cluster)
# K-means 클러스터링
set.seed(123)
kmeans_result <- kmeans(iot_data[, c("temperature", "humidity")],
centers = 3)
# 클러스터 시각화
plot(iot_data$temperature, iot_data$humidity,
col = kmeans_result$cluster, pch = 19)
library(ggplot2)
ggplot(iot_data, aes(x = timestamp, y = temperature)) +
geom_line(color = "steelblue") +
geom_smooth(method = "loess", color = "red") +
labs(title = "IoT 센서 온도 변화",
x = "시간", y = "온도 (°C)") +
theme_minimal()
library(reshape2)
# 시간별 센서 데이터 히트맵
heatmap_data <- iot_data %>%
mutate(hour = hour(timestamp)) %>%
group_by(hour, sensor_id) %>%
summarise(avg_temp = mean(temperature))
ggplot(heatmap_data, aes(x = hour, y = sensor_id, fill = avg_temp)) +
geom_tile() +
scale_fill_gradient(low = "blue", high = "red") +
labs(title = "시간별 센서 온도 히트맵")
library(plotly)
# 인터랙티브 시계열 그래프
p <- ggplot(iot_data, aes(x = timestamp, y = temperature)) +
geom_line() +
labs(title = "IoT 센서 데이터")
ggplotly(p)
library(shiny)
library(shinydashboard)
ui <- dashboardPage(
dashboardHeader(title = "IoT 데이터 대시보드"),
dashboardSidebar(
sidebarMenu(
menuItem("실시간 모니터링", tabName = "realtime"),
menuItem("분석 결과", tabName = "analysis")
)
),
dashboardBody(
tabItems(
tabItem(tabName = "realtime",
fluidRow(
valueBoxOutput("current_temp"),
valueBoxOutput("current_humidity")
)
)
)
)
)
library(rkafka)
# Kafka 컨슈머 설정
consumer <- kafka_consumer_new(
topics = "iot-sensors",
bootstrap_servers = "localhost:9092"
)
# 실시간 데이터 수신
while(TRUE) {
message <- kafka_consumer_poll(consumer)
if (!is.null(message)) {
process_iot_data(message)
}
}
# 임계값 설정
temp_threshold <- 35
humidity_threshold <- 80
# 알림 함수
send_alert <- function(sensor_id, value, threshold, type) {
message <- paste("경고:", sensor_id, "센서의", type, "값이",
value, "로 임계값", threshold, "를 초과했습니다.")
# 이메일 발송
send_email(message)
# 슬랙 알림
send_slack_message(message)
}
# 실시간 모니터링
monitor_sensors <- function(data) {
if (data$temperature > temp_threshold) {
send_alert(data$sensor_id, data$temperature,
temp_threshold, "온도")
}
if (data$humidity > humidity_threshold) {
send_alert(data$sensor_id, data$humidity,
humidity_threshold, "습도")
}
}
# 샘플 데이터 생성
smart_home_data <- data.frame(
timestamp = seq(from = as.POSIXct("2024-01-01 00:00:00"),
to = as.POSIXct("2024-01-31 23:59:59"),
by = "min"),
room = sample(c("living_room", "bedroom", "kitchen"),
44640, replace = TRUE),
temperature = rnorm(44640, mean = 22, sd = 3),
humidity = rnorm(44640, mean = 45, sd = 10),
light = sample(0:1, 44640, replace = TRUE),
motion = sample(0:1, 44640, replace = TRUE)
)
# 장비 센서 데이터
equipment_data <- data.frame(
timestamp = seq(from = as.POSIXct("2024-01-01 00:00:00"),
by = "hour", length.out = 8760),
equipment_id = paste0("EQ", sprintf("%03d", 1:100)),
vibration = rnorm(8760, mean = 0.5, sd = 0.1),
temperature = rnorm(8760, mean = 65, sd = 5),
pressure = rnorm(8760, mean = 2.5, sd = 0.3),
current = rnorm(8760, mean = 15, sd = 2),
failure = sample(0:1, 8760, replace = TRUE, prob = c(0.95, 0.05))
)
이메일: aiden.hong@nexr.com (aiden.hong@kt.com) GitHub: https://github.com/euriion
질문과 토론을 환영합니다!
IoT-BigData with R | 홍성학