%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% ------------------------------------------------------------------------
% Extra Credit 6501 ******************************************************
% ------------------------------------------------------------------------
% Submitted:      Dr. Eric Suess, Statistics 6502, Winter 2012
% Submitted Date: May 21, 2012
% Accepted Date:  May 21, 2012
% ------------------------------------------------------------------------
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\documentclass[12]{report}

\usepackage{latexsym}
\usepackage[centertags]{amsmath}
\usepackage{amsfonts}
\usepackage{newlfont}
%\usepackage{graphicx,fullpage,subfigure,pstricks}

\setlength{\oddsidemargin}{-0.4cm}
\setlength{\evensidemargin}{0cm}
\setlength{\topmargin}{-0.75cm}
\setlength{\textheight}{8.9in}
\setlength{\textwidth}{6.5in}
\renewcommand{\baselinestretch}{1}
\setlength{\parskip}{3mm}
\setlength{\parindent}{0mm} % indented paragraphs just don't
%\pagestyle{plain}

\begin{document}

\pagestyle{myheadings}
\markright{Statistics 6502, Spring 2012}

\large
\begin{center}
\textbf{Extra Credit}
\end{center}



Examine the robustness of the {\it independent two sample t-test}.

\begin{enumerate}
\item
Simulate two independent samples:

\begin{enumerate}
\item[Sample 1:]
$X_1,...,X_n \sim N(\mu_1, \sigma_1^2)$, with $n = 15$, $\mu_1 = 5$ and $\sigma_1^2 = 2$
\item[Sample 2:]
$Y_1,...,Y_m \sim N(\mu_2, \sigma_2^2)$, with $m = 20$, $\mu_2 = 5$ and $\sigma_2^2 = 3$
\end{enumerate}

\begin{enumerate}
\item
Perform an {\it unequal variance independent two sample t-test}, to test $H_0: \mu_1 = \mu_2$
versus $H_1: \mu_1 \neq \mu_2$.  Determine the {\it p-value} of the test.
\item
Simulate repeated sampling from the two normal populations, under the null hypothesis $H_0: \mu_1 = \mu_2$ versus $H_1: \mu_1 \neq \mu_2$.  What is the distribution of the {\it p-value} under the null hypothesis?
\end{enumerate}


\item
Simulate two independent samples:

\begin{enumerate}
\item[Sample 1:]
$X_1,...,X_n \sim N(\mu_1, \sigma_1^2)$, with $n = 15$, $\mu_1 = 5$ and $\sigma_1^2 = 2$
\item[Sample 2:]
$Y_1,...,Y_m \sim N(\mu_2, \sigma_2^2)$, with $m = 20$, $\mu_2 = 7$ and $\sigma_2^2 = 3$
\end{enumerate}

\begin{enumerate}
\item
Perform an {\it unequal variance independent two sample t-test}, to test $H_0: \mu_1 = \mu_2$
versus $H_1: \mu_1 \neq \mu_2$.  Determine the {\it p-value} of the test.
\item
Simulate repeated sampling from the two normal populations, under the null hypothesis $H_0: \mu_1 = \mu_2$ versus $H_1: \mu_1 \neq \mu_2$.  What is the distribution of the {\it p-value} under the null hypothesis?
\end{enumerate}

\newpage
\begin{verbatim}
# Question 1

n = 15; mu1 = 5; sigma1 = 2;  m = 20; mu2 = 5; sigma2 = 3

x.samp = rnorm(n, mu1, sigma1)
y.samp = rnorm(m, mu2, sigma2)

xy.samp = c(x.samp, y.samp)

grp = c(rep(1,n),rep(2,m))

xy = data.frame(xy = xy.samp)
xy$grp = as.factor(grp)

attach(xy)

boxplot(xy ~ grp, data = xy)

# one-sided independent two sample t-test with unequal variance

t.test(xy.samp[grp=="2"], xy.samp[grp=="1"],
       alternative = "two.sided", mu = 0, var.equal = FALSE)

# Question 2

n = 15; mu1 = 5; sigma1 = 2;  m = 20; mu2 = 7; sigma2 = 3

x.samp = rnorm(n, mu1, sigma1)
y.samp = rnorm(m, mu2, sigma2)

xy.samp = c(x.samp, y.samp)

grp = c(rep(1,n),rep(2,m))

xy = data.frame(xy = xy.samp)
xy$grp = as.factor(grp)

attach(xy)

boxplot(xy ~ grp, data = xy)

# one-sided independent two sample t-test with unequal variance

t.test(xy.samp[grp=="2"], xy.samp[grp=="1"],
       alternative = "two.sided", mu = 0, var.equal = FALSE)
\end{verbatim}

\newpage

\begin{verbatim}

# Distribution of the p-value

B = 10000

p.val = numeric(B)

for(i in 1:B){
	x.samp = rnorm(n, mu1, sigma1)
	y.samp = rnorm(m, mu2, sigma2)

	p.val[i] = t.test(x.samp, y.samp,
       alternative = "two.sided", mu = 0, var.equal = FALSE)$p.value

}

hist(p.val)
\end{verbatim}

\newpage

\item
Simulate two independent samples:

\begin{enumerate}
\item[Sample 1:]
$X_1,...,X_n \sim Poisson(\mu_1)$, with $n = 15$, $\mu_1 = 5$
\item[Sample 2:]
$Y_1,...,Y_m \sim Poisson(\mu_2)$, with $m = 20$, $\mu_2 = 5$
\end{enumerate}

\begin{enumerate}
\item
Perform an {\it unequal variance independent two sample t-test}, to test $H_0: \mu_1 = \mu_2$
versus $H_1: \mu_1 \neq \mu_2$.  Determine the {\it p-value} of the test.
\item
Simulate repeated sampling from the two Poisson populations, under the null hypothesis $H_0: \mu_1 = \mu_2$ versus $H_1: \mu_1 \neq \mu_2$.  What is the distribution of the {\it p-value} under the null hypothesis?
\item
Perform the G.L.R. test.
\end{enumerate}

\item
Simulate two independent samples:

\begin{enumerate}
\item[Sample 1:]
$X_1,...,X_n \sim Poisson(\mu_1)$, with $n = 15$, $\mu_1 = 5$
\item[Sample 2:]
$Y_1,...,Y_m \sim Poisson(\mu_2)$, with $m = 20$, $\mu_2 = 7$
\end{enumerate}

\begin{enumerate}
\item
Perform an {\it unequal variance independent two sample t-test}, to test $H_0: \mu_1 = \mu_2$
versus $H_1: \mu_1 \neq \mu_2$.  Determine the {\it p-value} of the test.
\item
Simulate repeated sampling from the two Poisson populations, under the null hypothesis $H_0: \mu_1 = \mu_2$ versus $H_1: \mu_1 \neq \mu_2$.  What is the distribution of the {\it p-value} under the null hypothesis?
\item
Perform the G.L.R. test.
\end{enumerate}

\newpage

\begin{verbatim}

n = 15; m = 20

set.seed(1)

mux = 5
muy = 7

x = rpois(n,mux);x;length(x);mean(x);var(x)
y = rpois(n,muy);y;length(y);mean(y);var(y)

mean(y)/mean(x)

plot(y,x)

\end{verbatim}




\end{enumerate}

\end{document}
% ------------------------------------------------------------------------
