\documentclass[12pt]{article}\usepackage[]{graphicx}\usepackage[]{xcolor}
% maxwidth is the original width if it is less than linewidth
% otherwise use linewidth (to make sure the graphics do not exceed the margin)
\makeatletter
\def\maxwidth{ %
  \ifdim\Gin@nat@width>\linewidth
    \linewidth
  \else
    \Gin@nat@width
  \fi
}
\makeatother

\definecolor{fgcolor}{rgb}{0.345, 0.345, 0.345}
\newcommand{\hlnum}[1]{\textcolor[rgb]{0.686,0.059,0.569}{#1}}%
\newcommand{\hlstr}[1]{\textcolor[rgb]{0.192,0.494,0.8}{#1}}%
\newcommand{\hlcom}[1]{\textcolor[rgb]{0.678,0.584,0.686}{\textit{#1}}}%
\newcommand{\hlopt}[1]{\textcolor[rgb]{0,0,0}{#1}}%
\newcommand{\hlstd}[1]{\textcolor[rgb]{0.345,0.345,0.345}{#1}}%
\newcommand{\hlkwa}[1]{\textcolor[rgb]{0.161,0.373,0.58}{\textbf{#1}}}%
\newcommand{\hlkwb}[1]{\textcolor[rgb]{0.69,0.353,0.396}{#1}}%
\newcommand{\hlkwc}[1]{\textcolor[rgb]{0.333,0.667,0.333}{#1}}%
\newcommand{\hlkwd}[1]{\textcolor[rgb]{0.737,0.353,0.396}{\textbf{#1}}}%
\let\hlipl\hlkwb

\usepackage{framed}
\makeatletter
\newenvironment{kframe}{%
 \def\at@end@of@kframe{}%
 \ifinner\ifhmode%
  \def\at@end@of@kframe{\end{minipage}}%
  \begin{minipage}{\columnwidth}%
 \fi\fi%
 \def\FrameCommand##1{\hskip\@totalleftmargin \hskip-\fboxsep
 \colorbox{shadecolor}{##1}\hskip-\fboxsep
     % There is no \\@totalrightmargin, so:
     \hskip-\linewidth \hskip-\@totalleftmargin \hskip\columnwidth}%
 \MakeFramed {\advance\hsize-\width
   \@totalleftmargin\z@ \linewidth\hsize
   \@setminipage}}%
 {\par\unskip\endMakeFramed%
 \at@end@of@kframe}
\makeatother

\definecolor{shadecolor}{rgb}{.97, .97, .97}
\definecolor{messagecolor}{rgb}{0, 0, 0}
\definecolor{warningcolor}{rgb}{1, 0, 1}
\definecolor{errorcolor}{rgb}{1, 0, 0}
\newenvironment{knitrout}{}{} % an empty environment to be redefined in TeX

\usepackage{alltt}
\usepackage[spanish]{babel}\decimalpoint
\usepackage{atkinson}
\usepackage[margin=9mm,includefoot]{geometry}
\newenvironment{resoluciOn}{\begin{quotation}\sffamily}{\end{quotation}}
% \usepackage{verbatim}
% \let\resoluciOn=\comment
% \let\endresoluciOn=\endcomment
\IfFileExists{upquote.sty}{\usepackage{upquote}}{}
\begin{document}

\begin{enumerate}
\item Sea $X$ una población continua
  de la que se extrae la muestra 0.76, 1.18, 0.15, 0.14, 0.44, 2.89, 1.23, 0.54, 0.96, 0.15, 1.39, 0.76. Sea $\eta$ la mediana de $X$.
  \begin{enumerate}
    \item Contrasta $H_0:\eta=1$, $H_1:\eta\neq1$.
    \begin{resoluciOn}
      En primer lugar, haremos un contraste de gausianidad\footnote{Si fuera
        gausiana,
        la mediana sería igual a la media y podríamos hacer una prueba $t$.}.
      El contraste habitualmente
      más potente (y el único implementado en el R básico) es
      el de Shapiro:
\begin{knitrout}
\definecolor{shadecolor}{rgb}{0.969, 0.969, 0.969}\color{fgcolor}\begin{kframe}
\begin{alltt}
\hlkwd{shapiro.test}\hlstd{(x)}
\end{alltt}
\begin{verbatim}
## 
## 	Shapiro-Wilk normality test
## 
## data:  x
## W = 0.83724, p-value = 0.02564
\end{verbatim}
\end{kframe}
\end{knitrout}
      A nivel de significación $\alpha=0.05$ se rechaza la gausianidad.

      Podemos comprobar otras distribuciones.  A la vista del histograma,
\begin{knitrout}
\definecolor{shadecolor}{rgb}{0.969, 0.969, 0.969}\color{fgcolor}\begin{kframe}
\begin{alltt}
    \hlkwd{hist}\hlstd{(x)}
\end{alltt}
\end{kframe}
\includegraphics[width=\maxwidth]{figure/unnamed-chunk-3-1} 
\end{knitrout}
      Se aprecia cierta asimetría, por lo que podemos probar una distribución
      exponencial.  La idea de Lilliefors podía emplearse para exponenciales.
      En primer lugar, definamos el estadístico
\begin{knitrout}\footnotesize
\definecolor{shadecolor}{rgb}{0.969, 0.969, 0.969}\color{fgcolor}\begin{kframe}
\begin{alltt}
\hlstd{Dn} \hlkwb{<-} \hlkwa{function} \hlstd{(}\hlkwc{muestra}\hlstd{)} \hlkwd{ks.test}\hlstd{(muestra, pexp,} \hlnum{1}\hlopt{/}\hlkwd{mean}\hlstd{(muestra))} \hlopt{$} \hlstd{statistic}
\hlstd{(Dx} \hlkwb{<-} \hlkwd{Dn}\hlstd{(x))}
\end{alltt}


{\ttfamily\noindent\color{warningcolor}{\#\# Warning in ks.test(muestra, pexp, 1/mean(muestra)): ties should not be present for the Kolmogorov-Smirnov test}}\begin{verbatim}
##         D 
## 0.1606744
\end{verbatim}
\end{kframe}
\end{knitrout}
Aunque la población sea continua, parece que los datos se han redondeado
a dos decimales.  No va a haber mucha diferencia, pero podemos tenerlo en
cuenta usando \verb+round+ en nuestra implementación (y, de paso, eliminamos
el mensaje de aviso):
\begin{knitrout}
\definecolor{shadecolor}{rgb}{0.969, 0.969, 0.969}\color{fgcolor}\begin{kframe}
\begin{alltt}
\hlstd{Dn} \hlkwb{<-} \hlkwa{function} \hlstd{(}\hlkwc{muestra}\hlstd{)}
\hlstd{\{}
    \hlstd{muestra} \hlkwb{<-} \hlkwd{round}\hlstd{(muestra,} \hlnum{2}\hlstd{)}
    \hlkwd{suppressWarnings}\hlstd{(}\hlkwd{ks.test}\hlstd{(muestra, pexp,} \hlnum{1}\hlopt{/}\hlkwd{mean}\hlstd{(muestra))} \hlopt{$} \hlstd{statistic)}
\hlstd{\}}
\hlstd{(Dx} \hlkwb{<-} \hlkwd{Dn}\hlstd{(x))} \hlcom{# no cambia porque "x" ya está redondeada}
\end{alltt}
\begin{verbatim}
##         D 
## 0.1606744
\end{verbatim}
\begin{alltt}
\hlstd{distri} \hlkwb{<-} \hlkwd{replicate} \hlstd{(}\hlnum{1e4}\hlstd{,} \hlkwd{Dn}\hlstd{(}\hlkwd{rexp}\hlstd{(}\hlkwd{length}\hlstd{(x))))}
\hlkwd{mean} \hlstd{(distri} \hlopt{>=} \hlstd{Dx)} \hlcom{# p-valor}
\end{alltt}
\begin{verbatim}
## [1] 0.733
\end{verbatim}
\end{kframe}
\end{knitrout}
      Si decidimos considerar que $X$ es exponencial, entonces
      el contraste del enunciado podría adaptarse a un contraste sobre
      su parámetro habitual.  Es decir, si su mediana es $\eta$, entonces
      $1/2$ $=$ $\Pr[\mbox{Exp}(\lambda)\le\eta]$
      $=$ $1-e^{-\lambda\eta}$ y $\eta=1$ $\Rightarrow$
      $1/2=1-e^{-\lambda}$ $\Leftrightarrow$
      $1/2=e^{-\lambda}$ $\Leftrightarrow$
      $-\ln2={-\lambda}$  $\Leftrightarrow$
      $\ln2={\lambda}$, luego podemos contrastar $H_0:\lambda=\ln2$,
      $H_0:\lambda=\ln2$ mediante algún método ya visto. Por ejemplo,
      bajo $H_0$ se tiene que $\sum X_i\hookrightarrow\gamma(n,\ln2)$,
      luego tendríamos la región crítica RC${}=[\sum X_i<c_1\cup\sum X_i<c_2]$,
\begin{knitrout}
\definecolor{shadecolor}{rgb}{0.969, 0.969, 0.969}\color{fgcolor}\begin{kframe}
\begin{alltt}
\hlstd{(c1} \hlkwb{<-} \hlkwd{qgamma}\hlstd{(}\hlnum{.025}\hlstd{, n,} \hlkwd{log}\hlstd{(}\hlnum{2}\hlstd{)))}
\end{alltt}
\begin{verbatim}
## [1] 8.945539
\end{verbatim}
\begin{alltt}
\hlstd{(c2} \hlkwb{<-} \hlkwd{qgamma}\hlstd{(}\hlnum{.975}\hlstd{, n,} \hlkwd{log}\hlstd{(}\hlnum{2}\hlstd{)))}
\end{alltt}
\begin{verbatim}
## [1] 28.39518
\end{verbatim}
\begin{alltt}
\hlstd{(t} \hlkwb{<-} \hlkwd{sum}\hlstd{(x))} \hlcom{# no pertenece a RC}
\end{alltt}
\begin{verbatim}
## [1] 10.59
\end{verbatim}
\begin{alltt}
\hlstd{p} \hlkwb{<-} \hlkwd{pgamma}\hlstd{(t, n,} \hlkwd{log}\hlstd{(}\hlnum{2}\hlstd{))}
\hlnum{2} \hlopt{*} \hlkwd{min} \hlstd{(p,} \hlnum{1}\hlopt{-}\hlstd{p)} \hlcom{# p-valor}
\end{alltt}
\begin{verbatim}
## [1] 0.1404994
\end{verbatim}
\end{kframe}
\end{knitrout}
      
      Para realizar el contraste del enunciado sin suponer que la población
      sigue una distribución concreta, usaremos la prueba de los signos:
\begin{knitrout}
\definecolor{shadecolor}{rgb}{0.969, 0.969, 0.969}\color{fgcolor}\begin{kframe}
\begin{alltt}
\hlstd{eta} \hlkwb{<-} \hlnum{1}
\hlstd{(n} \hlkwb{<-} \hlkwd{length}\hlstd{(x))}
\end{alltt}
\begin{verbatim}
## [1] 12
\end{verbatim}
\begin{alltt}
\hlstd{(U} \hlkwb{<-} \hlkwd{sum}\hlstd{(x}\hlopt{<}\hlstd{eta))}
\end{alltt}
\begin{verbatim}
## [1] 8
\end{verbatim}
\begin{alltt}
\hlkwd{binom.test}\hlstd{(U, n)}
\end{alltt}
\begin{verbatim}
## 
## 	Exact binomial test
## 
## data:  U and n
## number of successes = 8, number of trials = 12, p-value = 0.3877
## alternative hypothesis: true probability of success is not equal to 0.5
## 95 percent confidence interval:
##  0.3488755 0.9007539
## sample estimates:
## probability of success 
##              0.6666667
\end{verbatim}
\begin{alltt}
\hlcom{## equivalentemente:}
\hlkwd{library}\hlstd{(BSDA)}
\end{alltt}


{\ttfamily\noindent\itshape\color{messagecolor}{\#\# Loading required package: lattice}}

{\ttfamily\noindent\itshape\color{messagecolor}{\#\# \\\#\# Attaching package: 'BSDA'}}

{\ttfamily\noindent\itshape\color{messagecolor}{\#\# The following object is masked from 'package:datasets':\\\#\# \\\#\# \ \ \ \ Orange}}\begin{alltt}
\hlkwd{SIGN.test}\hlstd{(x,} \hlkwc{md}\hlstd{=}\hlnum{1}\hlstd{)}
\end{alltt}
\begin{verbatim}
## 
## 	One-sample Sign-Test
## 
## data:  x
## s = 4, p-value = 0.3877
## alternative hypothesis: true median is not equal to 1
## 95 percent confidence interval:
##  0.1808455 1.2246818
## sample estimates:
## median of x 
##        0.76 
## 
## Achieved and Interpolated Confidence Intervals: 
## 
##                   Conf.Level L.E.pt U.E.pt
## Lower Achieved CI     0.8540 0.4400 1.1800
## Interpolated CI       0.9500 0.1808 1.2247
## Upper Achieved CI     0.9614 0.1500 1.2300
\end{verbatim}
\end{kframe}
\end{knitrout}
    No se rechaza $H_0$.

    Podríamos considerar hacer el contraste $H_0:(\eta=1)\cap (X\mbox{ simétrica})$, $H_1:(\eta\neq1)\cup(X\mbox{asimétrica})$:
\begin{knitrout}\footnotesize
\definecolor{shadecolor}{rgb}{0.969, 0.969, 0.969}\color{fgcolor}\begin{kframe}
\begin{alltt}
\hlkwd{wilcox.test}\hlstd{(x,} \hlkwc{mu}\hlstd{=}\hlnum{1}\hlstd{)}
\end{alltt}


{\ttfamily\noindent\color{warningcolor}{\#\# Warning in wilcox.test.default(x, mu = 1): cannot compute exact p-value with ties}}\begin{verbatim}
## 
## 	Wilcoxon signed rank test with continuity correction
## 
## data:  x
## V = 23, p-value = 0.2237
## alternative hypothesis: true location is not equal to 1
\end{verbatim}
\end{kframe}
\end{knitrout}
    En este caso, el resultado es el mismo que con la prueba de los signos.
  \end{resoluciOn}
\item  Contrasta $H_0:\eta=1$, $H_1:\eta<1$.
  \begin{resoluciOn}
    Téngase en cuenta que $U=\sum[X_i<\eta]$ tiende a crecer cuanto menor
    es $\eta$.
\begin{knitrout}\footnotesize
\definecolor{shadecolor}{rgb}{0.969, 0.969, 0.969}\color{fgcolor}\begin{kframe}
\begin{alltt}
\hlkwd{binom.test}\hlstd{(U, n,} \hlkwc{alternative}\hlstd{=}\hlstr{"greater"}\hlstd{)}
\end{alltt}
\begin{verbatim}
## 
## 	Exact binomial test
## 
## data:  U and n
## number of successes = 8, number of trials = 12, p-value = 0.1938
## alternative hypothesis: true probability of success is greater than 0.5
## 95 percent confidence interval:
##  0.3908623 1.0000000
## sample estimates:
## probability of success 
##              0.6666667
\end{verbatim}
\begin{alltt}
\hlcom{## equivalentemente:}
\hlkwd{SIGN.test}\hlstd{(x,} \hlkwc{md}\hlstd{=}\hlnum{1}\hlstd{,} \hlkwc{alternative}\hlstd{=}\hlstr{"less"}\hlstd{)}
\end{alltt}
\begin{verbatim}
## 
## 	One-sample Sign-Test
## 
## data:  x
## s = 4, p-value = 0.1938
## alternative hypothesis: true median is less than 1
## 95 percent confidence interval:
##      -Inf 1.201409
## sample estimates:
## median of x 
##        0.76 
## 
## Achieved and Interpolated Confidence Intervals: 
## 
##                   Conf.Level L.E.pt U.E.pt
## Lower Achieved CI     0.9270   -Inf 1.1800
## Interpolated CI       0.9500   -Inf 1.2014
## Upper Achieved CI     0.9807   -Inf 1.2300
\end{verbatim}
\begin{alltt}
\hlcom{## si hubiese simetría en la población:}
\hlkwd{wilcox.test}\hlstd{(x,} \hlkwc{mu}\hlstd{=}\hlnum{1}\hlstd{,} \hlkwc{alternative}\hlstd{=}\hlstr{"less"}\hlstd{)}
\end{alltt}


{\ttfamily\noindent\color{warningcolor}{\#\# Warning in wilcox.test.default(x, mu = 1, alternative = "{}less"{}): cannot compute exact p-value with ties}}\begin{verbatim}
## 
## 	Wilcoxon signed rank test with continuity correction
## 
## data:  x
## V = 23, p-value = 0.1118
## alternative hypothesis: true location is less than 1
\end{verbatim}
\end{kframe}
\end{knitrout}
 \end{resoluciOn}
\end{enumerate}

\item Sea $X$ una población continua
  de la que se extrae la muestra 0.17, 4.3, 0.87, 0.8, 0.63, 0.73, 0.45, 0.05, 0.51, 0.84, 2.68, 0.29, 0.4, 0.43, 0.34, 1.05, 0.06, 0.13, 1.84, 1.01, 0.24, 1.25, 5.54, 0.17, 1.83, 0.06, 0.39, 1.25, 0.49, 0.56. Sea $\eta$ la mediana de $X$.
  Contrasta $H_0:\eta=1$, $H_1:\eta\neq1$.
  \begin{resoluciOn}
\begin{knitrout}\footnotesize
\definecolor{shadecolor}{rgb}{0.969, 0.969, 0.969}\color{fgcolor}\begin{kframe}
\begin{alltt}
\hlkwd{hist}\hlstd{(x)}
\end{alltt}
\end{kframe}
\includegraphics[width=\maxwidth]{figure/unnamed-chunk-11-1} 
\begin{kframe}\begin{alltt}
\hlkwd{summary}\hlstd{(x)}
\end{alltt}
\begin{verbatim}
##    Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
##  0.0500  0.3025  0.5350  0.9787  1.0400  5.5400
\end{verbatim}
\begin{alltt}
\hlstd{(n} \hlkwb{<-} \hlkwd{length}\hlstd{(x))}
\end{alltt}
\begin{verbatim}
## [1] 30
\end{verbatim}
\begin{alltt}
\hlstd{(U} \hlkwb{<-} \hlkwd{sum}\hlstd{(x}\hlopt{<}\hlstd{eta))}
\end{alltt}
\begin{verbatim}
## [1] 21
\end{verbatim}
\begin{alltt}
\hlkwd{binom.test}\hlstd{(U, n)}
\end{alltt}
\begin{verbatim}
## 
## 	Exact binomial test
## 
## data:  U and n
## number of successes = 21, number of trials = 30, p-value = 0.04277
## alternative hypothesis: true probability of success is not equal to 0.5
## 95 percent confidence interval:
##  0.5060410 0.8526548
## sample estimates:
## probability of success 
##                    0.7
\end{verbatim}
\begin{alltt}
\hlcom{## equivalentemente:}
\hlkwd{SIGN.test}\hlstd{(x,} \hlkwc{md}\hlstd{=}\hlnum{1}\hlstd{)}
\end{alltt}
\begin{verbatim}
## 
## 	One-sample Sign-Test
## 
## data:  x
## s = 9, p-value = 0.04277
## alternative hypothesis: true median is not equal to 1
## 95 percent confidence interval:
##  0.3912912 0.8661264
## sample estimates:
## median of x 
##       0.535 
## 
## Achieved and Interpolated Confidence Intervals: 
## 
##                   Conf.Level L.E.pt U.E.pt
## Lower Achieved CI     0.9013 0.4000 0.8400
## Interpolated CI       0.9500 0.3913 0.8661
## Upper Achieved CI     0.9572 0.3900 0.8700
\end{verbatim}
\begin{alltt}
\hlcom{## si hubiese simetría en la población:}
\hlkwd{wilcox.test}\hlstd{(x,} \hlkwc{mu}\hlstd{=}\hlnum{1}\hlstd{)}
\end{alltt}


{\ttfamily\noindent\color{warningcolor}{\#\# Warning in wilcox.test.default(x, mu = 1): cannot compute exact p-value with ties}}\begin{verbatim}
## 
## 	Wilcoxon signed rank test with continuity correction
## 
## data:  x
## V = 148, p-value = 0.08401
## alternative hypothesis: true location is not equal to 1
\end{verbatim}
\end{kframe}
\end{knitrout}
    A pesar de ser mucho más potente que la prueba de los signos, la de Wilcoxon no va bien porque la distribución no es
    nada simétrica.
  \end{resoluciOn}
  \end{enumerate}
\end{document}
