R에서 카이제곱 분포를 쉽게 그리는 방법
R에서 카이제곱 분포에 대한 밀도 플롯을 생성하려면 다음 함수를 사용할 수 있습니다.
- dchisq()를 사용하여 확률 밀도 함수 생성
- 확률 밀도 함수를 플롯하는 Curve()
플롯을 생성하기 위해 우리가 해야 할 일은 dchisq() 의 자유도와 curve() 의 아웃 및 백 포인트를 지정하는 것뿐입니다.
예를 들어, 다음 코드는 도표의 x축이 0과 40 사이인 자유도가 10인 카이제곱 분포에 대한 밀도 도표를 생성하는 방법을 보여줍니다.
curve(dchisq(x, df = 10), from = 0, to = 40)
밀도 플롯 편집
제목을 추가하고, Y축 레이블을 변경하고, 선 너비를 늘리고, 선 색상을 변경하여 밀도 플롯을 편집할 수도 있습니다.
curve(dchisq(x, df = 10), from = 0, to = 40, main = 'Chi-Square Distribution (df = 10)', #add title ylab = 'Density', #change y-axis label lwd = 2, #increase line width to 2 col = 'steelblue') #change line color to steelblue
밀도 플롯 채우기
밀도 플롯을 생성하는 것 외에도 시작 및 끝 값을 기반으로 하는 Polygon() 함수를 사용하여 플롯의 일부를 채울 수 있습니다.
다음 코드는 10과 40 사이의 x 값에 대한 플롯의 밀도 부분을 채우는 방법을 보여줍니다.
#create density curve curve(dchisq(x, df = 10), from = 0, to = 40, main = 'Chi-Square Distribution (df = 10)', ylab = 'Density', lwd = 2) #create vector of x values x_vector <- seq(10, 40) #create vector of chi-square density values p_vector <- dchisq(x_vector, df = 10) #fill in portion of the density plot from 0 to 40 polygon(c(x_vector, rev(x_vector)), c(p_vector, rep(0, length(p_vector))), col = adjustcolor('red', alpha=0.3), border = NA)
다음 코드는 0과 10 사이의 x 값에 대한 플롯의 밀도 부분을 채우는 방법을 보여줍니다.
#create density curve curve(dchisq(x, df = 10), from = 0, to = 40, main = 'Chi-Square Distribution (df = 10)', ylab = 'Density', lwd = 2) #create vector of x values x_vector <- seq( 0, 10 ) #create vector of chi-square density values p_vector <- dchisq(x_vector, df = 10) #fill in portion of the density plot from 0 to 10 polygon(c(x_vector, rev(x_vector)), c(p_vector, rep(0, length(p_vector))), col = adjustcolor('red', alpha=0.3), border = NA)
다음 코드는 분포의 중앙 95% 외부에 있는 x 값에 대한 밀도 플롯 부분을 채우는 방법을 보여줍니다.
#create density curve curve(dchisq(x, df = 10), from = 0, to = 40, main = 'Chi-Square Distribution (df = 10)', ylab = 'Density', lwd = 2) #find upper and lower values for middle 95% of distribution lower95 <- qchisq(.025, 10) upper95 <- qchisq(.975, 10) #create vector of x values x_lower95 <- seq(0, lower95) #create vector of chi-square density values p_lower95 <- dchisq(x_lower95, df = 10) #fill in portion of the density plot from 0 to lower 95% value polygon(c(x_lower95, rev(x_lower95)), c(p_lower95, rep(0, length(p_lower95))), col = adjustcolor('red', alpha=0.3), border = NA) #create vector of x values x_upper95 <- seq(upper95, 40) #create vector of chi-square density values p_upper95 <- dchisq(x_upper95, df = 10) #fill in portion of the density plot for upper 95% value to end of plot polygon(c(x_upper95, rev(x_upper95)), c(p_upper95, rep(0, length(p_upper95))), col = adjustcolor('red', alpha=0.3), border = NA)
마지막으로 다음 코드는 분포의 중앙 95% 내에 속하는 x 값에 대한 밀도 플롯 부분을 채우는 방법을 보여줍니다.
#create density curve curve(dchisq(x, df = 10), from = 0, to = 40, main = 'Chi-Square Distribution (df = 10)', ylab = 'Density', lwd = 2) #find upper and lower values for middle 95% of distribution lower95 <- qchisq(.025, 10) upper95 <- qchisq(.975, 10) #create vector of x values x_vector <- seq(lower95, upper95) #create vector of chi-square density values p_vector <- dchisq(x_vector, df = 10) #fill in density plot polygon(c(x_vector, rev(x_vector)), c(p_vector, rep(0, length(p_vector))), col = adjustcolor('red', alpha=0.3), border = NA)