R'de ki-kare dağılımını kolayca çizme
R’de Ki-kare dağılımı için yoğunluk grafiği oluşturmak için aşağıdaki işlevleri kullanabiliriz:
- olasılık yoğunluk fonksiyonunu oluşturmak için dchisq()
- Olasılık yoğunluk fonksiyonunu çizmek için Curve()
Grafiği oluşturmak için yapmamız gereken tek şey, dchisq() için serbestlik derecelerinin yanı sıra curve() için çıkış ve arka noktaları belirtmektir.
Örneğin, aşağıdaki kod, grafiğin x ekseninin 0 ile 40 arasında olduğu 10 serbestlik derecesine sahip bir Ki-kare dağılımı için yoğunluk grafiğinin nasıl oluşturulacağını gösterir:
curve(dchisq(x, df = 10), from = 0, to = 40)
Yoğunluk grafiğini düzenleme
Ayrıca bir başlık ekleyerek, Y ekseni etiketini değiştirerek, çizgi genişliğini artırarak ve çizgi rengini değiştirerek yoğunluk grafiğini düzenleyebiliriz:
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
Yoğunluk grafiğini doldurun
Yoğunluk grafiğini oluşturmanın yanı sıra, bir başlangıç ve bitiş değerine dayalı olarak polygon() fonksiyonunu kullanarak grafiğin bir kısmını doldurabiliriz.
Aşağıdaki kod, 10 ile 40 arasındaki x değerleri için grafiğin yoğunluk kısmının nasıl doldurulacağını gösterir:
#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)
Aşağıdaki kod, 0 ile 10 arasındaki x değerleri için grafiğin yoğunluk kısmının nasıl doldurulacağını gösterir:
#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)
Aşağıdaki kod, dağılımın merkezi %95’inin dışındaki x değerleri için yoğunluk grafiğinin kısmının nasıl doldurulacağını gösterir:
#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)
Son olarak, aşağıdaki kod, dağılımın merkezi %95’inin içine giren x değerleri için yoğunluk grafiğinin kısmının nasıl doldurulacağını gösterir:
#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)