R의 dnorm, pnorm, qnorm 및 rnorm에 대한 안내


정규분포는 통계학에서 가장 흔히 사용되는 분포이다. 이 튜토리얼에서는 dnorm , pnorm , rnormqnorm 함수를 사용하여 R에서 정규 분포를 사용하는 방법을 설명합니다.

끔찍한

dnorm 함수는 임의 변수 x , 모집단 평균 μ 및 모집단 표준 편차 σ 가 주어지면 정규 분포의 확률 밀도 함수(pdf) 값을 반환합니다. dnorm을 사용하는 구문은 다음과 같습니다.

dnorm(x, 평균, sd)

다음 코드는 작동 중인 dnorm 의 몇 가지 예를 보여줍니다.

 #find the value of the standard normal distribution pdf at x=0
dnorm(x=0, mean=0, sd=1)
#[1]0.3989423

#by default, R uses mean=0 and sd=1
dnorm(x=0)
#[1]0.3989423

#find the value of the normal distribution pdf at x=10 with mean=20 and sd=5
dnorm(x=10, mean=20, sd=5)
#[1]0.01079819

일반적으로 정규 분포를 사용하여 확률에 대한 질문을 풀 때 dnorm 대신 pnorm 을 사용하는 경우가 많습니다. 그러나 dnorm 의 유용한 적용은 R에서 정규 분포 도표를 만드는 것입니다. 다음 코드는 이를 수행하는 방법을 보여줍니다.

 #Create a sequence of 100 equally spaced numbers between -4 and 4
x <- seq(-4, 4, length=100)

#create a vector of values that shows the height of the probability distribution
#for each value in x
y <- dnorm(x)

#plot x and y as a scatterplot with connected lines (type = "l") and add
#an x-axis with custom labels
plot(x,y, type = "l", lwd = 2, axes = FALSE, xlab = "", ylab = "")
axis(1, at = -3:3, labels = c("-3s", "-2s", "-1s", "mean", "1s", "2s", "3s"))

그러면 다음 플롯이 생성됩니다.

pnorm

pnorm 함수는 임의 변수 q , 모집단 평균 μ 및 모집단 표준 편차 σ 가 주어지면 정규 분포의 누적 밀도 함수(cdf) 값을 반환합니다. pnorm을 사용하는 구문은 다음과 같습니다.

pnorm(q, 평균, sd)

간단히 말해서, pnorm은 정규 분포에서 주어진 값 x 의 왼쪽에 있는 면적을 반환합니다. 주어진 q 값의 오른쪽 영역에 관심이 있는 경우 lower.tail = FALSE 인수를 추가하면 됩니다.

pnorm(q, 평균, sd, lower.tail = FALSE)

다음 예에서는 pnorm을 사용하여 몇 가지 확률 문제를 해결하는 방법을 보여줍니다.

예 1: 특정 학교에 다니는 남자의 키가 평균 표준 편차

 #find percentage of males that are taller than 74 inches in a population with
#mean = 70 and sd = 2
pnorm(74, mean=70, sd=2, lower.tail=FALSE)

# [1]0.02275013

이 학교의 남성 중 2,275%가 키가 74인치 이상입니다.

예 2: 특정 수달 종의 무게가 평균 표준 편차 로 정규 분포를 따른다고 가정합니다.

 #find percentage of otters that weight less than 22 lbs in a population with
#mean = 30 and sd = 5
pnorm(22, mean=30, sd=5)

# [1]0.05479929

이 수달 종의 약 5.4799%는 무게가 22파운드 미만입니다.

예 3: 특정 지역의 식물 키가 평균 표준 편차

 #find percentage of plants that are less than 14 inches tall, then subtract the
#percentage of plants that are less than 10 inches tall, based on a population
#with mean = 13 and sd = 2
pnorm(14, mean=13, sd=2) - pnorm(10, mean=13, sd=2)

# [1]0.6246553

이 지역 식물의 약 62.4655%는 키가 10~14인치 사이입니다.

qnorm

qnorm 함수는 임의 변수 p , 모집단 평균 μ 및 모집단 표준 편차 σ 가 주어지면 정규 분포의 역 누적 밀도 함수(cdf) 값을 반환합니다. qnorm을 사용하는 구문은 다음과 같습니다.

qnorm(p, 평균, sd)

간단히 말해서 qnorm을 사용하면 정규 분포의 p번째 분위수의 Z 점수가 무엇인지 알아낼 수 있습니다.

다음 코드는 실제 qnorm 의 몇 가지 예를 보여줍니다.

 #find the Z-score of the 99th quantile of the standard normal distribution 
qnorm(.99, mean=0, sd=1)
#[1]2.326348

#by default, R uses mean=0 and sd=1
qnorm(.99)
#[1]2.326348

#find the Z-score of the 95th quantile of the standard normal distribution
qnorm(.95)
#[1]1.644854

#find the Z-score of the 10th quantile of the standard normal distribution
qnorm(.10)
#[1]-1.281552

표준

rnorm 함수는 벡터 길이 n , 모집단 평균 μ , 모집단 표준편차 σ 가 주어지면 정규 분포 확률 변수의 벡터를 생성합니다. rnorm을 사용하는 구문은 다음과 같습니다.

rnorm(n, 평균, sd)

다음 코드는 실행 중인 rnorm 의 몇 가지 예를 보여줍니다.

 #generate a vector of 5 normally distributed random variables with mean=10 and sd=2
five <- rnorm(5, mean = 10, sd = 2)
five
# [1] 10.658117 8.613495 10.561760 11.123492 10.802768

#generate a vector of 1000 normally distributed random variables with mean=50 and sd=5
narrowDistribution <- rnorm(1000, mean = 50, sd = 15)

#generate a vector of 1000 normally distributed random variables with mean=50 and sd=25
wideDistribution <- rnorm(1000, mean = 50, sd = 25)

#generate two histograms to view these two distributions side by side, specify
#50 bars in histogram and x-axis limits of -50 to 150
par(mfrow=c(1, 2)) #one row, two columns
hist(narrowDistribution, breaks=50, xlim=c(-50, 150))
hist(wideDistribution, breaks=50, xlim=c(-50, 150))

그러면 다음과 같은 히스토그램이 생성됩니다.

넓은 분포가 좁은 분포보다 얼마나 넓은지 확인하세요. 실제로 우리는 좁은 분포의 표준편차가 15인 것에 비해 넓은 분포의 표준편차는 25라고 지정했습니다. 또한 두 히스토그램 모두 평균 50을 중심으로 표시됩니다.

의견을 추가하다

이메일 주소는 공개되지 않습니다. 필수 필드는 *로 표시됩니다