시작하기무료로 시작하기

트리에서 잘라낸 가지 살펴보기

연습 문제 5와 6에서 사용한 cutree() 함수는 h 매개변수를 사용하면 지정한 높이에서 트리를 자를 수도 있어요. 이전 연습에서 생성한 군집을 높이 20과 40 기준으로 다시 살펴보세요.

이 연습은 강의의 일부입니다

R로 배우는 군집 분석

강의 보기

연습 안내

  • h = 20을 사용해 cutree()로 군집 할당 벡터 clusters_h20을 만드세요.
  • 군집 할당을 열 clusterlineup 데이터 프레임에 추가하고, 결과를 lineup_h20_complete라는 새 데이터 프레임에 저장하세요.
  • 위의 두 단계를 높이 40에 대해서도 반복하여 clusters_h40lineup_h40_complete 변수를 생성하세요.
  • ggplot2를 사용해 두 높이에 대한 군집 할당으로 색을 구분한 산점도를 만드세요.

실습형 인터랙티브 연습

이 예제를 이 샘플 코드를 완성하여 풀어보세요.

dist_players <- dist(lineup, method = 'euclidean')
hc_players <- hclust(dist_players, method = "complete")

# Calculate the assignment vector with a h of 20
clusters_h20 <- ___

# Create a new data frame storing these results
lineup_h20_complete <- mutate(lineup, cluster = ___)

# Calculate the assignment vector with a h of 40
clusters_h40 <- ___

# Create a new data frame storing these results
lineup_h40_complete <- ___

# Plot the positions of the players and color them using their cluster for height = 20
ggplot(___, aes(x = ___, y = ___, color = factor(___))) +
  geom_point()

# Plot the positions of the players and color them using their cluster for height = 40

코드 편집 및 실행