%% This is shinyTexTemplate.tex, a template for shinyTex.
%% H. Seltman, Summer 2015

% fill in as desired (affects documentation and app title)
\def\author{Howard Seltman}
\def\title{When a secondary DV is treated as an IV}
\def\titleColor{grey}
\def\titleFontWeight{bold}
\def\titleFontPercent{125}
\def\titleFont{serif}


\begin{document}


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%% The goals of the app and how to use it %%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{shinyTab}{Goals and Use}

  \begin{shinyColumns}{5}
    \textbf{Goals}
    
    This app lets you explore a particular common mistake in a multiple regression analysis,
    namely including some form of the dependent variable (DV, or Y) as one of the
    independent variables (IVs or x's).
    
    By interacting with this app, you will develop a deeper understanding of
    the danger of this mistake, what the consequences are, and how to recognize
    and avoid the situation.

\shinyNextColumn{7}
  
  \textbf{How to Use This App}
  
  Note the \textbf{tabs} at the top of this page (Goals and Use, Pre-test, Background, Model Exploration,
  Post-test, and Links).  You can click on any
  of them at any time them to move to the different parts of the app.
  
  Use the \textbf{Pre-test} tab to see if you have sufficient background knowledge to understand this app.
  If you score less than about 75\%, you should go review the concepts referred to in the pre-test
  before proceeding with this app.
  
  Use the \textbf{Background} tab to review the concepts of multiple linear regression.
  
  Use the \textbf{Model Exploration} tab to explore the deleterious effects of
  including a surrogate DV as an IV in a multiple regression analysis.
  
  Use the \textbf{Post-test} tab to quiz yourself on the material in this app.
  
  Use the \textbf{Links} tab to find links to related material.
  
  %Use the \textbf{Concept Map} tab to see a concept map of material related to
  %this app.  Click on the map to explore the concepts further.
\end{shinyColumns}
\end{shinyTab}



%%%%%%%%%%%%%%%%%%
%%%% Pre test %%%%
%%%%%%%%%%%%%%%%%%

\begin{shinyTab}{Pre-test}
\shinyQuiz{bank=yAsXPretest}
\end{shinyTab}



%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%% The statistical background %%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{shinyTab}{Background}
  \textbf{Statistical Background}
    
  \href{Multiple regression}{http://www.stat.cmu.edu/~hseltman/309/Book/chapter10.pdf}
      is used to model the "effects" of some
   \tooltip{independent variables}{observable characteristics that are thought
                                   to affect some other variable(s); also called
                                   explanatory variables}
   (IVs) on a quantitative 
   \tooltip{dependent variable}{observable characteristics that are thought to
                                depend on one or more other variables; also
                                called an outcome variable}
   (DV).  The statistical model for subject $i$ is
   $$Y_i = \beta_0 + x_{1i} \beta_1 + ... + x_{pi} \beta_p + \epsilon_i,$$
  where $Y_i$ is the DV value for the $i$th subject, $x_{pi}$ represents the
  $p$th of the several IV values for the $i$th subject, and 
  the $\epsilon_i$ values are "errors" which are independent of each other and identically
  distributed as Normal(0, $\sigma^2$).
  Using matrix notation, the model for all subjects is $Y = X \beta + \bf \epsilon_i$.
    
  Consider an example where one hundred subjects are sampled
  from a population.  Their ages and genders are recorded, and a test is administered.
  (The test is a state mandated test, and does not affect GPA.)
  Age and gender are the IVs, and test score is the DV.  Exploratory Data Analysis
  \tooltipHref{EDA}{http://www.stat.cmu.edu/~hseltman/309/Book/chapter4.pdf}
              {graphical or non-graphical explorations that do not postulate
               a statistical model, but may help suggest one}
  is performed in the form of the
  \tooltip{scatterplot}{plot showing the relationship between two variables
                        measured on the same subject where the value of one
                        variable is shown on the x-axis, the other on the
                        y-axis, and a point is used to represent each subject}
  shown below.  The dots represent the actual data values.
  As is conventional, the DV, test score, is on the y-axis.
  Age, a \tooltip{quantitative}{having a discrete or continuous value for which
                                all equal paired differences have the same meaning}
  IV, is shown on the x-axis, and gender, a
  \tooltip{categorical}{having a value that does not have a quantitative meaning
                        the meaning is either purely nominal or represents
                        some natural ordering}
  IV, also called a \tooltip{factor}{a categorical explanatory (independent) variable}
  is represented using different colors for the dots.
  Examination of these dots suggests that there may be a linear relationship between
  age and score for each gender, with a fairly large amount of scatter in the scores
  for each combination or age and gender.  It appears that both the intercept and slope
  of the "best fit" lines showing the average relationship between age and score
  are different for males vs. females.
  
  \begin{shinyCode}
  ageGenderScore = read.csv("ageGenderScore.csv")
  bLearnGPA = 0.2  # slope of GPA on learn
  bLearnScore = 2  # slope of score on learn
  \end{shinyCode}
  
  \begin{shinyPlot}
    with(ageGenderScore, plot(age, score, pch=16, col=1+female, ylim=c(0,100)))
    legend("topleft", c("male","female"), pch=16, col=1:2, lwd=2)
    regBeta = lm(score~age*female, data=ageGenderScore)$coef
    abline(regBeta["(Intercept)"], regBeta["age"], lwd=2)
    abline(regBeta["(Intercept)"]+regBeta["female"], 
           regBeta["age"]+regBeta["age:female"], lwd=2, col=2)
  \end{shinyPlot}
  
  A good way to think about this model is to decompose it into the
  \tooltip{structural model}{a component of a statistical model that
                             describes the mean outcome for each set
                             of independent variables without describing
                             variability around that mean}
  and the \tooltip{error model}{a component of a statistical model that describes
                                variability around the mean of the DV for each set of
                                IVs}.
  The structural model comes from taking the
  \href{expected value}{http://en.wikipedia.org/wiki/Expected_value}
  of $Y$ in the above model equation.  This gives
  $$E(Y_i) = \beta_0 + x_{1i} \beta_1 + ... + x_{pi} \beta_p$$
  or in matrix form
  $$E(Y) = X \beta.$$
  We can interpret this structural model as telling us how
  to calculate the mean of the DV (Y) for any given combination of IV (x) values.
  Note that if we have a subject with a specific set of x values for all of the x values
  \textbfem{included in the model} then the structural model specifies
  that all subjects who happen to have all of those same x values
  will have the same expected or predicted Y value.
  So all the subjects who "look the same to the model" have the same predicted Y value.
  
  But any set of subjects who look the same to the model will almost surely not have the
  exact same observed Y values.  Each difference between the observed and expected values (calculated as observed
  minus expected) for any given subject, is called the
  \tooltip{error}{deviation of an individual DV value from its expected value},
  for that subject.
  The errors are not zero because of imprecision of measurement (called "measurement error")
  missing (unmeasured) IVs, and misspecification of the model.
  The error model describes the distribution of the errors, and the standard linear regression
  model assumes that these errors follow a Normal (Gaussian) distribution with a common error
  variance, which is usually denoted as $\sigma^2$.  In the real world, we do not
  have access to the true errors, but instead we use a "sample" version of the errors (where we use
  estimates of the $\beta$ values instead of the true $\beta$ values to calculate
  the expected or predicted values). The sample version of the error is called the
  \tooltip{residual}{an estimate of the true error calculated as observed
                     minus predicated values}.

  Various forms of "model checking", especially the important technique called
  \tooltipHref{residual analysis}{http://www.stat.cmu.edu/~hseltman/309/Book/chapter9.pdf#page=16}
              {a set of graphical and non-graphical techniques for checking model assumptions
               using model residual values}
  are used to verify that the explicit and implicit
  assumptions of the structural and error models are met
  sufficiently well to justify interpretation of the model results.
  
  The basic output of a
  \tooltip{regression analysis}{a statistical technique for modeling the
                                effects of one or more IVs on a quantitative DV}
  whether derived from the "maximum likelihood" principle or the "least squares" approach, is a set of estimates
  of the $\beta$ values, as well as an estimate of $\sigma^2$.  If the goal of the study is
  \tooltip{prediction}{any technique that attempts to estimate the
                       mean of a DV from the information in the IVs}
  then substitution of the beta estimates
  often called "beta hat" and written as $\hat{\beta}$, into the structural model
  yields a prediction equation, which can be used to estimate the mean of the DV (Y)
  for any particular combination of IVs (x's).  Note that
  \tooltip{extrapolation}{making a prediction beyond of the region of available data}
  outside of the observed areas of the IV values can give very poor 
  predictions, because long experience with applying statistic analysis
  to Nature shows that models that fit the data well in one range of x's
  may not fit the data well elsewhere.
  
  A second possible study goal is
  \tooltip{scientific inference}{the basic techniques of science aimed at using
                                 data (observation) to learn about how Nature}
  works by going from the specific to the general
  and this is based on the mathematical interpretations of the beta values (or their estimates)
  in the structural model.  Briefly, the interpretation of the intercept $\beta_0$,
  in a multiple regression model is that it represents "the mean of Y for subjects who have
  all of their x values equal to zero."  The interpretation of the intercept estimate
  $\hat{\beta}_0$ should start with words such as "the best estimate of".  The slopes, e.g.
  $\beta_{age}$ represent the change in the \tooltip{mean}{average or expected value}
  of Y associated with
  a one unit \textem{rise} in that particular x.  Again, make clear that you are
  only talking about an estimate if you are describing the slope estimate derived from
  some sample data.  Note that for a standard
  \tooltip{indicator variable}{a variable that takes on only the
                               values 0 or 1, where 1 represents the named
                               category and 0 represent the other (baseline "category"}
  form of x, i.e., when some x only takes the values 0 and 1 and the name of the variable represents
  the \$1$ state as in the "female" variable in our example, a one unit change in x
  is explicitly a change from the baseline state to the named state for that variable.
  
  A key principle of Statistics is we can only substitute "caused by" for
  "associated with" when the study is a blinded, randomized experiment, and the slope
  in question is for a randomized IV.  (Although there are many techniques such as
  instrumental variables and propensity score analysis which attempt to overcome this
  restriction so as to make causal inferences in observational studies, they all only
  are assured to work when certain untestable assumptions are true.
  
  The pitfall that is the \textbfem{focus of this app} is using an IV as a
  DV (or a Y as an X).  This can occur when there are several possible different outcomes
  or versions of the same IV in a study.  One basic problem with including an IV as a DV in
  your model is that it produces a model that cannot be used for prediction.  The idea here
  is simple: if your prediction equation includes outcomes, you cannot use it to predict
  outcomes \textbfem{before} the outcome is measured, even though that is the goal of any
  prediction scheme.  The second pitfall is trickier, and that is the focus of this app.
  If you include a DV as an IV, it will seriously mislead you about the effects of other IVs.
  Work through the Model Exploration of this app to learn what happens.

\end{shinyTab}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%% Main tab: model exploration %%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{shinyTab}{Model Exploration}

    \textbf{Understanding what you see}
    
    This example simulates data from an educational setting using a model where
    socioeconomic status (SES, coded as a Z-score)
    and gender (coded as an indicator variable for female)
    directly affect learning.  Here learning is an unmeasured, i.e., latent, variable.
    We observe two different outcomes (DVs) that each directly depend on learning,
    namely, "grade point average" (GPA) and a test score.
    These relationships are shown in the DAG (directed acyclic graph) shown below.
    Variables are represented as circles and causal relationships are represented
    as arrows.
    You can use the sliders (to the left of the DAG) to set some aspects of the "true" 
    underlying reality from which the data are simulated.
    The two $\beta$ parameter sliders control the size and direction of the effects of
    SES and female gender on learning.  The two $\sigma$ parameter sliders control the
    amount of variability or noise in the relationship between the latent learning
    value and the observed values for GPA and test score.
    
    Note that the true values describe a "population" and this app will generate
    200 "samples" from that population.  The sample size is set to 50 initially, but
    you can use a slider to change the
    sample size.  Every time you change the sample size or a parameter value or you
    press the "Re-simulate data" button 200 new samples are generated.
    
    Look below the sliders and DAG (with "one sample" chosen under "Show")
    to see the first 5 rows of data from the first of the 200 datasets.
    To the right of that are two sets of multiple regression results (in R format) for
    all of the subjects in the first simulated dataset.  The top regression is for
    the regression model with test score as the DV and both female and SES as the IVs.
    The bottom regression (inappropriately) adds GPA as an IV.  You can move
    the "View which sample?" slider to see other samples.
    
    If you switch to "full sampling distribution", you will see
    four plots which show you the \textbf{sampling distributions}
    of the coefficients and p-values for one of the two main IVs and for both
    the correct and the inappropriate regression models.  (You can use the radio buttons
    to change which IV you want to see in the sampling distribution results.)  These
    sampling distributions summarize results from all 200 random samples drawn from
    the population.  In a real life situation you would see only one sample, but
    the characteristics of that sample vary (e.g., if you or some friends would
    repeat the entire experiment and generate new results).
    \textbf{The sampling distributions allow you to see what would happen if you}
    \textbfem{could} \textbf{repeat the experiment many times.}
    
    The top row of the plots corresponds to the correct model, and the bottom row
    corresponds to the inappropriate regression model.  The left column of plots
    shows the parameter (coefficient) estimate that represents the effect of the IV of interest
    on the DV.  The true value is shown as a vertical line.
    The right column shows the p-values with an estimate of the
    \tooltipHref{power}{http://www.stat.cmu.edu/~hseltman/309/Book/chapter12.pdf}
                {Power is the probability of correctly rejecting
                 the null hypothesis when a particular alternative
                 is true}
    shown in the title.  The conventional p-value cutoff of 0.05 is shown
    as a green line.  If you activate "Also show prior sampling distribution?"
    each plot will show the current 200 values in pink and the prior
    200 values in gray.  The title describes the two situations under
    which the sampling was performed.

\begin{shinyColumns}{4}

  \shinySlider{inputId=bF; label=$\beta_F$; min=-5; max=5; step=1; value=2}
  \shinySlider{inputId=bS; label=$\beta_{ses}$; min=0; max=3; step=0.5; value=1}
  \shinySlider{inputId=sdGPA; label=$\sigma_{GPA}$; min=0; max=0.5; step=0.1; value=0.2}
  \shinySlider{inputId=sdScore; label=$\sigma_{score}$; min=0; max=10; step=0.5; value=3}

\shinyNextColumn{4}

  \shinySlider{inputId=N; label=Number of subjects:; min=10; max=200; step=10; value=50}
  \shinyActionButton{inputId=reSim; label=Re-simulate data}
  \smallskip
  \shinyRadioButtons{inputId=variable; label=Show sampling distributions for:; choices=female, SES; inline=TRUE}

\shinyNextColumn{4}

\shinyImage{filename=DagSchool.png}

\end{shinyColumns}
         
 \hr
 
  % functions to generate and analyze data
  \begin{shinyCode}
   doOne = isolate(function(dummy) {
    N = input$N
    b0 = 50
    sdLearn = 2
    female = rbinom(N, 1, 0.5)
    SES = round(rnorm(N), digits=2)
    learn = round(rnorm(N, b0+input$bF*female+input$bS*SES, sdLearn), digits=2)
    GPA = pmin(4.5, pmax(0, round(rnorm(N, (learn-b0)*bLearnGPA+2.5, input$sdGPA), digits=2)))
    score = pmin(100, pmax(0, round(rnorm(N, (learn-b0)*bLearnScore+55, input$sdScore), digits=1)))
    dtf = data.frame(female, SES, learn, GPA, score)
    regWithout = coef(summary(lm(score ~ female + SES, dtf)))
    regWith = coef(summary(lm(score ~ female + SES + GPA , dtf)))
    return(list(without=regWithout, with=regWith, data5=dtf[1:5,]))
  })

  saveOldResults = isolate(function(oldResults) {
    assign("oldResults", oldResults, envir=parent.env(environment()))
    return(NULL)
  })
  
  getOldResults = isolate(function() {
    if (!exists("oldResults", envir=parent.env(environment()))) return(NULL)
    return(get("oldResults", envir=parent.env(environment())))
  })

  \end{shinyCode}

  \begin{shinyReactive}{simRegs}
    input$reSim
    M = 200
    dat = lapply(1:M, doOne)
    return(dat)
  \end{shinyReactive}


  \shinyDropdown{inputId=showWhat; label=Show; choices=one sample, full sampling distribution}

 
 % panel for one sample
 \begin{shinyConditionalPanel}{inputId=showWhat; value=one sample}

   \shinySlider{inputId=whichOne; label=View which sample?; min=1; max=200; step=1; value=1}

   \begin{shinyColumns}{5}
      \textbf{First 5 rows from the selected dataset:}
    
      \begin{shinyCodeOutput}
        simRegs()[[input$whichOne]][[3]]
      \end{shinyCodeOutput}
    
   \shinyNextColumn{7}
     \begin{shinyCodeOutput}
        myRegs1 = simRegs()[[input$whichOne]]
        cat("Regressions on score for the selected simulated data set:\n\n")
        print(myRegs1[[1]])
        cat("\n")
        print(myRegs1[[2]])
     \end{shinyCodeOutput}
  
   \end{shinyColumns}

\end{shinyConditionalPanel}
 
  % Sampling distribution panel
  \begin{shinyConditionalPanel}{inputId=showWhat; value=full sampling distribution}

    \shinyRadioButtons{inputId=showPrior; label=Also show prior sampling distribution?; choices=no, yes;
       inline=TRUE}

  \begin{shinyPlot}
      input$variable
      oldResults = getOldResults()
      haveOld = !is.null(oldResults)
      simRegs = simRegs()
      var = input$variable
      trueCoef = bLearnScore * ifelse(var=="female", input$bF, input$bS)
      if (haveOld && input$showPrior=="yes") 
        oldTrueCoef = bLearnScore * ifelse(oldResults$variable=="female", oldResults$bF, oldResults$bS)
      par(mfrow=c(2,2), oma=c(0,0,2,0))
      # pull out sampling distributions of beta-hat and p-values sans and cum GPA
      tempBs = sapply(simRegs, function(lst, v) lst[[1]][v, "Estimate"], v=var)
      tempBc = sapply(simRegs, function(lst, v) lst[[2]][v, "Estimate"], v=var)
      tempPs = sapply(simRegs, function(lst, v) lst[[1]][v, "Pr(>|t|)"], v=var)
      tempPc = sapply(simRegs, function(lst, v) lst[[2]][v, "Pr(>|t|)"], v=var)
      saveOldResults(list(tempBs=tempBs, tempBc=tempBc, tempPs=tempPs, tempPc=tempPc,
                          N=input$N, variable=input$variable, bF=input$bF, bS=input$bS, 
                          sdGPA=input$sdGPA, sdScore=input$sdScore))
      # Draw histogram(s) for without/coef
      if (haveOld && input$showPrior=="yes") {
        breaks = pretty(c(tempBs,tempBc,oldResults$tempBs,oldResults$tempBc), n=41)
        tempO = hist(oldResults$tempBs, breaks=breaks, plot=FALSE)
        temp = hist(tempBs, breaks=breaks, plot=FALSE)
        ylim = c(0, max(tempO$counts, temp$counts))
        hist(oldResults$tempBs, breaks=breaks, xlab="coefficient estimates", ylim=ylim,
             main="Without GPA", col="lightgray")
        abline(v=oldTrueCoef, col="black", lwd=2)
        # jitter second histogram  
        temp$breaks = temp$breaks - 0.05*diff(temp$breaks[1:2])
        n = length(temp$counts)
        rect(temp$breaks[1:n], rep(0,n), temp$breaks[2:(n+1)], temp$counts,  
             density=NA, col = rgb(1,0.75,0.8,alpha=0.5), border="red")
        abline(v=trueCoef, col=2, lwd=2)
      } else { # not showing old (for without/coef)
        xlim = range(tempBs,tempBc)
        hist(tempBs, breaks=41, xlab="coefficient estimates", xlim=xlim, main="Without GPA")
        abline(v=trueCoef, col=2, lwd=2) 
      }
      # Add title
      titleCurr = paste("Var=", input$variable, " N=", input$N, 
                        " bF=", input$bF, " bS=", input$bS,
                        " sd(GPA)=", input$sdGPA, " sd(score)=", input$sdScore, sep="")
      if (haveOld && input$showPrior=="yes") {
        title = paste("Old (gray): ",
                      "Var=", oldResults$variable, " N=", oldResults$N,  
                      " bF=", oldResults$bF, " bS=", oldResults$bS,
                      " sd(GPA)=", oldResults$sdGPA, " sd(score)=", oldResults$sdScore, 
                      "  -->  Current (red): ", titleCurr, sep="")
      } else {
        title = titleCurr
      }
      mtext(title, outer=TRUE)
      # Draw histogram(s) for without/p-values
      if (haveOld && input$showPrior=="yes") {
        breaks = pretty(c(tempPs,tempPc,oldResults$tempPs,oldResults$tempPc), n=41)
        tempO = hist(oldResults$tempPs, breaks=breaks, plot=FALSE)
        temp = hist(tempPs, breaks=breaks, plot=FALSE)
        ylim = c(0, max(tempO$counts, temp$counts))
        title = paste("Without GPA  (power=",
                      formatC(100*mean(oldResults$tempPs<=0.05), digits=1, format="f")," \%",
                      "->",
                      formatC(100*mean(tempPs<=0.05), digits=1, format="f")," \%)",
                      sep="")
        hist(oldResults$tempPs, breaks=breaks, xlab="p-values", ylim=ylim,
             xlim=c(0,1), main=title, col="lightgray")
        # jitter second histogram
        temp$breaks = temp$breaks - 0.05*diff(temp$breaks[1:2])
        n = length(temp$counts)
        rect(temp$breaks[1:n], rep(0,n), temp$breaks[2:(n+1)], temp$counts,
             density=NA, col = rgb(1,0.75,0.8,alpha=0.5), border="red")
        abline(v=0.05, col="green", lwd=2)
      } else { # not showing old (for without/coef)
        xlim = range(tempPs,tempPc)
        hist(tempPs, breaks=41, xlab="p-values", xlim=c(0,1), 
             main=paste(main="Without GPA (power = ", 
                        formatC(100*mean(tempPs<=0.05), digits=1, format="f")," \%)",sep=""))
        abline(v=0.05, col="green", lwd=2)
      }
      # Draw histogram(s) for with/coef
      if (haveOld && input$showPrior=="yes") {
        breaks = pretty(c(tempBs,tempBc,oldResults$tempBs,oldResults$tempBc), n=41)
        tempO = hist(oldResults$tempBc, breaks=breaks, plot=FALSE)
        temp = hist(tempBc, breaks=breaks, plot=FALSE)
        ylim = c(0, max(tempO$counts, temp$counts))
        hist(oldResults$tempBc, breaks=breaks, xlab="coefficient estimates", ylim=ylim,
             main="With GPA", col="lightgray")
        abline(v=oldTrueCoef, col="black", lwd=2)
        # jitter second histogram
        temp$breaks = temp$breaks - 0.05*diff(temp$breaks[1:2])
        n = length(temp$counts)
        rect(temp$breaks[1:n], rep(0,n), temp$breaks[2:(n+1)], temp$counts,
             density=NA, col = rgb(1,0.75,0.8,alpha=0.5), border="red")
        abline(v=trueCoef, col=2, lwd=2)
      } else { # not showing old (for with/coef)
        xlim = range(tempBs,tempBc)
        hist(tempBc, breaks=41, xlab="coefficient estimates", xlim=xlim, main="With GPA")
        abline(v=trueCoef, col=2, lwd=2)
      }
      # Draw histogram(s) for with/p-values
      if (haveOld && input$showPrior=="yes") {
        breaks = pretty(c(tempPs,tempPc,oldResults$tempPs,oldResults$tempPc), n=41)
        tempO = hist(oldResults$tempPc, breaks=breaks, plot=FALSE)
        temp = hist(tempPc, breaks=breaks, plot=FALSE)
        ylim = c(0, max(tempO$counts, temp$counts))
        title = paste("With GPA  (power=",
                      formatC(100*mean(oldResults$tempPc<=0.05), digits=1, format="f")," \%",
                      "->",
                      formatC(100*mean(tempPc<=0.05), digits=1, format="f")," \%)",
                      sep="")
        hist(oldResults$tempPc, breaks=breaks, xlab="p-values", ylim=ylim,
             xlim=c(0,1), main=title, col="lightgray")
        # jitter second histogram
        temp$breaks = temp$breaks - 0.05*diff(temp$breaks[1:2])
        n = length(temp$counts)
        rect(temp$breaks[1:n], rep(0,n), temp$breaks[2:(n+1)], temp$counts,
             density=NA, col = rgb(1,0.75,0.8,alpha=0.5), border="red")
        abline(v=0.05, col="green", lwd=2)
      } else { # not showing old (for without/coef)
        xlim = range(tempPs,tempPc)
        hist(tempPc, breaks=41, xlab="p-values", xlim=c(0,1), 
             main=paste(main="With GPA (power = ", 
                        formatC(100*mean(tempPc<=0.05), digits=1, format="f")," \%)",sep=""))
        abline(v=0.05, col="green", lwd=2)
      }
  
  \end{shinyPlot}

  \end{shinyConditionalPanel}
 


    \hr
    \textbf{Suggested activities}
    \begin{enumerate}
      \item Check the effect of sample size on power
      \item Check the effect of the values of the $\beta$ parameters on power.
      \item Check the effect of the values of the $\sigma$ parameters on power.
      \item Check the effect of the values of the $\beta$ parameters on the bias 
      of the parameter estimates in the inappropriate model.
    \end{enumerate}

   After exploring the app, be sure to read the take-away points at the bottom of this tab.

\hr
\shinyCheckbox{inputId=takeAway; label=View take-away points; value=FALSE}
\begin{shinyConditionalPanel}{inputId=takeAway;value=TRUE}
         \textbf{Take-away points:}
         
         \begin{enumerate}
           \item Including a surrogate DV as an IV causes two problems: the estimate of the
              effect of other IVs on the DV is on average incorrect (in is biased), and
              the power to detect an effect of the other IVs on the DV is reduced.
            \item When running regression analyses, you should be on the lookout for
              surrogate DVs, and be sure \textembf{not} to include them as IVs.
            \item Simulated sampling distributions are an educational tool which give a good
              feel for the possible outcomes you might obtain if you could repeat an
              experiment many times.  They require knowledge of true population values
              so they are not available in real life.
            \item From any one experiment we can calculate estimates of parameter effects
              for one sample, and these are likely to differ from the true population values.
            \item Larger absolute values for the true effects of the IVs on the DVs
              ($\beta$ values) yields greater power.
          \end{enumerate}
\end{shinyConditionalPanel}

\end{shinyTab}


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%% Post test quiz               %%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{shinyTab}{Post-test}
\shinyQuiz{bank=yAsXPosttest}
\end{shinyTab}



% use as many tabs as you like
\begin{shinyTab}{Links}
  \textbf{Click to learn more (just a demo -- not yet "live")}
  
  \begin{itemize}
    \item \href{Scientific Question}{http://www.stat.cmu.edu/~hseltman/ScientificQuestion.html}
    \begin{itemize}  
      \item This is interesting and we should look at 
             \href{observables}{http://www.stat.cmu.edu/~hseltman/Observables.html}
             as an example
      \item \href{parameters}{http://www.stat.cmu.edu/~hseltman/Parameters.html}
      \item \href{null and alternative hypotheses}{http://www.stat.cmu.edu/~hseltman/NullAlt.html}
      \item \href{experiment}{http://www.stat.cmu.edu/~hseltman/Experiment.html}
    \end{itemize}
    \item \href{Population vs. sample}{http://www.stat.cmu.edu/~hseltman/PopSamp.html}
    \item \href{Inference}{http://www.stat.cmu.edu/~hseltman/Inference.html}
    \item \href{sample statistics}{http://www.stat.cmu.edu/~hseltman/SampleStatistic.html}
    \item \href{sampling distributions}{http://www.stat.cmu.edu/~hseltman/SamplingDistributions.html}
    \item \href{p-values}{http://www.stat.cmu.edu/~hseltman/p-values.html}
    \item \href{power}{http://www.stat.cmu.edu/~hseltman/power.html}
  \end{itemize}

\end{shinyTab}


\end{document}
