code 7.1

alpha<-0.05
mu<-72
n<-25
sigma<-5
xbar<-75

z.right<-qnorm(1-alpha/2)
z.left <-qnorm(alpha/2)

se<-sigma/n^.5;
lcl<-mu+z.left*se;
ucl<-mu+z.right*se;

conclusion<-ifelse(xbar>ucl | xbar<lcl, "기각", "채택")

test<-data.frame(alpha, mu, n, sigma, xbar, z.right, z.left, se, lcl, ucl, conclusion)

test
##   alpha mu  n sigma xbar  z.right    z.left se      lcl      ucl
## 1  0.05 72 25     5   75 1.959964 -1.959964  1 70.04004 73.95996
##   conclusion
## 1       기각

code 7.2

#사용하고자 하는 함수를 먼저 정의한다. 표준정규분포라면 이런거 안해도 된다
norm <- function(x) 
{
  y <-dnorm(x, mean=0, sd=1)
    return(y)
}

#그리자^^

library(ggplot2)

ggplot(data.frame(x=c(-5,5)),aes(x=x)) +
    annotate("rect", xmin=-1.96, xmax=-5, ymin=0, ymax=0.4, alpha=0.5, fill="lightblue") +
    annotate("rect", xmin= 1.96, xmax= 5, ymin=0, ymax=0.4, alpha=0.5, fill="lightblue") +
    stat_function(fun=norm)

code 7.3

alpha<-0.05
mu<-72
n<-25
sigma<-5
xbar<-75

se<-sigma/n^.5
z.test=(xbar-mu)/se
p.value=(1-pnorm(z.test))*2;
conclusion<-ifelse(p.value<alpha, "기각", "채택")

test<-data.frame(alpha, mu, n, sigma, xbar, se, z.test, p.value, conclusion)

test
##   alpha mu  n sigma xbar se z.test     p.value conclusion
## 1  0.05 72 25     5   75  1      3 0.002699796       기각

code 7.4

#사용하고자 하는 함수를 먼저 정의한다. 표준정규분포라면 이런거 안해도 된다

# 1. 점을 이어서 그리는 방법
x<-seq(-4, 4, by=0.01)
y<-dnorm(x, mean=0, sd=1)

norm<-data.frame(x, y)

norm.area1<-norm
norm.area2<-norm

norm.area1$x[x>-3]<-NA
norm.area2$x[x< 3]<-NA

ggplot(norm, aes(x=x, y=y)) + geom_line() +
    geom_area(data=norm.area1, fill="blue",alpha=0.2) + 
    geom_area(data=norm.area2, fill="blue",alpha=0.2) +
    annotate("segment", x= 1.96, xend= 1.96, y=0, yend=dnorm(1.96)) +
    annotate("segment", x=-1.96, xend=-1.96, y=0, yend=dnorm(1.96)) +
    annotate("text", x= 1.96, y=0, label= "Zc=1.96", vjust=1) +
    annotate("text", x=-1.96, y=0, label="-Zc=-1.96", vjust=1) +
    annotate("text", x= 3, y=0, label= "Zt=3", vjust=1) +
    annotate("text", x=-3, y=0, label="-Zt=-3", vjust=1)
## Warning: Removed 700 rows containing missing values (position_stack).
## Warning: Removed 700 rows containing missing values (position_stack).

# 2. 함수를 사용하여 그리는 방법
norm <- function(x) 
{
    y <-dnorm(x, mean=0, sd=1)
    return(y)
}

norm.p.value <- function(x) 
{
    y <-dnorm(x, mean=0, sd=1)
    y[x > -3 & x < 3] <- NA     #특정 벡터에서 기준 아닌 값 제외
    return(y)
}

#그리자^^

library(ggplot2)

ggplot(data.frame(z=c(-4,4)),aes(x=z)) +
    stat_function(fun=norm.p.value,geom="area",fill="blue",alpha=0.2) +
    stat_function(fun=norm) +
    annotate("segment", x= 1.96, xend= 1.96, y=0, yend=dnorm(1.96)) +
    annotate("segment", x=-1.96, xend=-1.96, y=0, yend=dnorm(1.96)) +
    annotate("text", x= 1.96, y=0, label= "Zc=1.96", vjust=1) +
    annotate("text", x=-1.96, y=0, label="-Zc=-1.96", vjust=1) +
    annotate("text", x= 3, y=0, label= "Zt=3", vjust=1) +
    annotate("text", x=-3, y=0, label="-Zt=-3", vjust=1)

code 7.5

# 1. 점을 이어 그리는 방법
x<-seq(50, 100, by=.1)
y<-dnorm(x, mean=70, sd=20/25^.5)
y2<-dnorm(x, mean=80, sd=20/25^.5)

error1.y <- ifelse(x<=77.16,0,dnorm(x, mean=70, sd=20/25^.5))
error2.y <- ifelse(x>=77.16,0,dnorm(x, mean=80, sd=20/25^.5))

graph2<-data.frame(x, y, y2)
area1<-data.frame(x, error1.y)
area2<-data.frame(x, error2.y)


ggplot(graph2, aes(x=x, y=y)) + geom_line() + 
    geom_line(data=graph2, aes(x=x, y=y2)) +
    geom_area(data=area2, aes(x=x, y=error2.y), fill="blue", alpha=0.4) +
    geom_area(data=area1, aes(x=x, y=error1.y), fill="red",  alpha=0.4) +
    geom_vline(xintercept=77.16, size=1, color="red")

# 2. R의 내장 그래픽스를 사용하는 방법
x<-seq(50, 100, by=.1)
y<-dnorm(x, mean=70, sd=20/25^.5)
y2<-dnorm(x, mean=80, sd=20/25^.5)

error1.y <- ifelse(x<=77.16,0,dnorm(x, mean=70, sd=20/25^.5))
error2.y <- ifelse(x>=77.16,0,dnorm(x, mean=80, sd=20/25^.5))


plot(x, y, type="line")
## Warning in plot.xy(xy, type, ...): 플랏 타입 'line'은 첫번째 문자에서 잘려
## 질 것입니다
polygon(x,error1.y,col="skyblue")
polygon(x,error2.y,col="orange", fillOddEven=TRUE)
lines(x,y2)
lines(x,y)

# 3. 함수를 짜서 쓰는 방법
#사용하고자 하는 함수를 먼저 정의한다. 표준정규분포라면 이런거 안해도 된다
norm1 <- function(x) 
{
    y <-dnorm(x, mean=70, sd=20/25^.5)
    return(y)
}

norm2 <- function(x) 
{
    y <-dnorm(x, mean=80, sd=20/25^.5)
    return(y)
}

#영역을 나타내는 함수를 짠다
#지정 범위에서는 해당 pdf값을, 그렇지 않은 경우 NA를 반환하는 함수를 짠다
error.1<- function(x)
{
    y <-dnorm(x, mean=80, sd=20/25^.5)
    y[x >= 77.16] <- NA     #특정 벡터에서 기준 아닌 값 제외
    return(y)
}

error.2<- function(x)
{
    y <-dnorm(x, mean=70, sd=20/25^.5)
    y[x <= 77.16] <- NA     #특정 벡터에서 기준 아닌 값 제외
    return(y)
}


#그리자^^

library(ggplot2)

ggplot(data.frame(x=c(50,100)),aes(x=x)) +
    stat_function(fun=norm1) +
    stat_function(fun=norm2) +
    stat_function(fun=error.1, geom="area", fill="blue", alpha=0.2) +
    stat_function(fun=error.2, geom="area", fill="red",  alpha=0.2) +
    geom_vline(xintercept=77.16, size=1, color="red")

##overlay 잘 안 되는 건 ggplot의 버그. 완벽은 아니다. ㅜㅜ

code 7.6

n<-25
h0mu<-70
xbar<-72
sigma<-5
alpha<-0.025
zc<-qnorm(1-alpha, mean=h0mu, sd=sigma/n^.5)

#그리고자 하는 함수를 짠다
power<- function(x)
{
    y  <- 1-pnorm(zc, mean=x, sd=sigma/n^.5)+pnorm(2*h0mu-zc, mean=x, sd=sigma/n^.5)
}

#그리자^^

library(ggplot2)

ggplot(data.frame(x=c(66,74)),aes(x=x)) + ylim(0,1) +
    stat_function(fun=power) +
    geom_hline(yintercept=0.05, linetype=2) +
    xlab("대립가설에서 설정한 모평균값(mu1)") + ylab("검정력(power)") +
    annotate("text", x=70, y=1, label="H0: mu0=70")

code 7.7

n1<-25
n2<-500
h0mu<-70
xbar<-72
sigma1<-5
sigma2<-10
alpha<-0.025
zc.n1<-qnorm(1-alpha, mean=h0mu, sd=sigma1/n1^.5)
zc.n2<-qnorm(1-alpha, mean=h0mu, sd=sigma1/n2^.5)
zc.sigma1<-qnorm(1-alpha, mean=h0mu, sd=sigma1/n1^.5)
zc.sigma2<-qnorm(1-alpha, mean=h0mu, sd=sigma2/n1^.5)


#그리고자 하는 함수를 짠다
power.n25<- function(x)
{
    y  <- 1-pnorm(zc.n1, mean=x, sd=sigma1/n1^.5)+pnorm(2*h0mu-zc.n1, mean=x, sd=sigma1/n1^.5)
}

power.n500<- function(x)
{
    y  <- 1-pnorm(zc.n2, mean=x, sd=sigma1/n2^.5)+pnorm(2*h0mu-zc.n2, mean=x, sd=sigma1/n2^.5)
}


#그리자^^

library(ggplot2)

ggplot(data.frame(x=c(66,74)),aes(x=x)) + ylim(0,1) +
    stat_function(fun=power.n25) +
    stat_function(fun=power.n500, color="red") +
    geom_hline(yintercept=0.05, linetype=2) +
    xlab("대립가설에서 설정한 모평균값(mu1)") + ylab("검정력(power)") +
    annotate("text", x=70, y=1, label="H0: mu0=70")

code 7.8

n1<-25
n2<-500
h0mu<-70
xbar<-72
sigma1<-5
sigma2<-10
alpha<-0.025
zc.n1<-qnorm(1-alpha, mean=h0mu, sd=sigma1/n1^.5)
zc.n2<-qnorm(1-alpha, mean=h0mu, sd=sigma1/n2^.5)
zc.sigma1<-qnorm(1-alpha, mean=h0mu, sd=sigma1/n1^.5)
zc.sigma2<-qnorm(1-alpha, mean=h0mu, sd=sigma2/n1^.5)

#그리고자 하는 함수를 짠다
power.sigma.5<- function(x)
{
    y  <- 1-pnorm(zc.sigma1, mean=x, sd=sigma1/n1^.5)+pnorm(2*h0mu-zc.sigma1, mean=x, sd=sigma1/n1^.5)
}

power.sigma.10<- function(x)
{
    y  <- 1-pnorm(zc.sigma2, mean=x, sd=sigma2/n1^.5)+pnorm(2*h0mu-zc.sigma2, mean=x, sd=sigma2/n1^.5)
}


#그리자^^

library(ggplot2)

ggplot(data.frame(x=c(66,74)),aes(x=x)) + ylim(0,1) +
    stat_function(fun=power.sigma.5) +
    stat_function(fun=power.sigma.10, color="red") +
    geom_hline(yintercept=0.05, linetype=2) +
    xlab("대립가설에서 설정한 모평균값(mu1)") + ylab("검정력(power)") +
    annotate("text", x=70, y=1, label="H0: mu0=70")

code 7.10

mu<-250
alpha<-0.05


#그리고자 하는 함수를 짠다
power.sigma.5<- function(x)
{
    y  <- 1-pnorm(zc.sigma1, mean=x, sd=sigma1/n1^.5)+pnorm(2*h0mu-zc.sigma1, mean=x, sd=sigma1/n1^.5)
}

power.sigma.10<- function(x)
{
    y  <- 1-pnorm(zc.sigma2, mean=x, sd=sigma2/n1^.5)+pnorm(2*h0mu-zc.sigma2, mean=x, sd=sigma2/n1^.5)
}


#그리자^^

library(ggplot2)

ggplot(data.frame(x=c(66,74)),aes(x=x)) + ylim(0,1) +
    stat_function(fun=power.sigma.5) +
    stat_function(fun=power.sigma.10, color="red") +
    geom_hline(yintercept=0.05, linetype=2) +
    xlab("대립가설에서 설정한 모평균값(mu1)") + ylab("검정력(power)") +
    annotate("text", x=70, y=1, label="H0: mu0=70")