% This is http://www.stat.cmu.edu/~hseltman/shinyTex/simpleApp.tex.
% It is a very simple demo of shinyTex.
% Howard Seltman, Summer 2015


% These lines are optional, but strongly suggested.
\def\author{Shiny R. Demonstration}
\def\title{A really simple shinyTex App}

% This line identifies the start of your app
\begin{document}

% This app uses two tabs to present it's information.  Typically
% several tabs are used, but a single tab page is allowed.  A
% single tab page may optionally be divided vertically into a sidebar
% and a main page.

% This is the first tab, and is smaller than is typical for
% a lesson section that gets it's own tab.
\begin{shinyTab}{Introduction}

% Note that a "tooltip" shows extra text when the user hovers over the visible text.
This is a \tooltip{simple}{not very complex} 
\href{shinyTex}{http://www.stat.cmu.edu/~hseltman/shinyTex/} app.
It demonstrates the distribution of $\sum_{i=1}^{k}Z_i^2$ when 
$Z_i \sim N(0,1)$ (and independent) and compares this to a $\chi^2_k$
distribution.

This paragraph shows \textbf{boldface} and \textem{italics}, as well as \textsc{
small caps}.  This is \textbfem{bold and italic}.
\end{shinyTab}



% This is the second tab and does some typical simulations
% in R.
\begin{shinyTab}{Main Lesson}

Here we examine the results of simulations of data using
histograms to examine the distribution of a random variable
and to compare it to the theoretical sampling distribution.

You choose $n$, the number of random Normal draws to be summed,
and the program draws that many $Z$ values, squares them and then sums them.  This
entire process is automatically repeated 1000 times, and the results
are saved.  Then the distribution of those 1000 sums is represented
as a histogram (in black) and a particular theoretical density
for a chi square distribution for $k$ degrees of freedom (df)
is overlaid in red.  You can also click the ``redo'' button to
generate a new 1000 simulated sums.

% this generates a ``horizonontal rule'' on the page
\hr

% The next two lines generate ``sliders'' for inputting user choices
\shinySlider{inputId=n; label=number of Z values to sum; min=1; max=10; value=2; step=1}
\shinySlider{inputId=k; label=chi square df; min=1; max=10; value=2; step=1}

% the next line generates a button to initiate a redo of the simulations
\shinyActionButton{inputId=redo; label=redo}

\hr

% This shiny/R code generates the simulations for the current
% ``n'' and ``k'' input choices.  See the documentation for details.
\begin{shinyCode}
NSIM = 1000
\end{shinyCode}
\begin{shinyReactive}{dataInput}
  input$redo
  n = input$n
  mat = matrix(rnorm(1000*n), ncol=n)
  sample = apply(mat^2, 1, sum)
  return(sample)
\end{shinyReactive}

Here are some basic characteristics of the simulated values:
\begin{shinyCodeOutput}
  samp = dataInput()
  cat("n=", NSIM, " mean=", formatC(mean(samp), format="f", digits=2),
      " var=", formatC(var(samp), format="f", digits=2),
      " sd=", formatC(sd(samp), format="f", digits=2), sep="")
\end{shinyCodeOutput}

\medskip
And here is the plot:
\begin{shinyPlot}{densityPlot}
  input$redo
  input$n
  samp = dataInput()
  h = hist(samp, breaks=ifelse(input$n>1,50,100), plot=FALSE)
  seqX = seq(0.01, max(h$breaks, qchisq(p=0.99, df=input$k)), length=200)
  d2 = dchisq(x=seqX, df=input$k)
  main = paste0("Simulated X^2(df=", input$n, ") in black vs. true X^2(df=", input$k, ") in red")
  plot(h, freq=FALSE, xlab="sum of squares", ylab="density", main=main,
       xlim=c(0, max(seqX, h$breaks)), ylim=c(0, max(h$density, d2)))
  lines(seqX, d2, col="red")
\end{shinyPlot}

You should check the relationship between $n$ and $k$, as well
as get a feel for the variability in the sample (black line),
while noting that the red line does not change with resimulation
because it represents a theoretical distribution.

\end{shinyTab}

\end{document}
