
<div style="display:block;clear:both;" class="rosetta-regr-start"></div>
<div class="rosetta-regr-container">

```{r,eval=TRUE, echo=FALSE}

digits <- x$input$digits;
pvalueDigits=x$input$pvalueDigits;

oldKableViewOption <- getOption("kableExtra_view_html", NULL);
options(kableExtra_view_html = FALSE);

if (!exists('headingLevel') || !is.numeric(headingLevel) || (length(headingLevel) != 1)) {
  headingLevel <- 0;
}

```

```{r, results='asis', eval=TRUE}
ufs::heading("Regression analysis",
             idSlug("rosettaRegr"),
             headingLevel=headingLevel);
```

```{r, echo=echoPartial, results='asis', eval=TRUE}

tab <-
  data.frame(c("Formula:", "Sample size:",
               "Multiple R-squared:", "Test for significance:  \n(of full model)"),
             c(x$intermediate$formula.as.character,
               sum(complete.cases(x$intermediate$dat.raw)),
               paste0(
                 ufs::formatCI(x$output$rsq.ci, noZero=TRUE, digits=digits),
                 " (point estimate = ",
                 round(x$intermediate$summary.raw$r.squared, digits),
                 ", adjusted = ",
                 round(x$intermediate$summary.raw$adj.r.squared, digits),
                 ")", ifelse(x$input$ci.method.note, "*", "")
               ),
               paste0(
                 "F[",
                 x$intermediate$summary.raw$fstatistic[2], ", ",
                 x$intermediate$summary.raw$fstatistic[3], "] = ",
                 round(x$intermediate$summary.raw$fstatistic[1], digits),
                 ", ",
                 ufs::formatPvalue(
                   stats::pf(x$intermediate$summary.raw$fstatistic[1],
                             x$intermediate$summary.raw$fstatistic[2],
                             x$intermediate$summary.raw$fstatistic[3],
                             lower.tail=FALSE),
                   digits=pvalueDigits
                 )
               )
             ),
             stringsAsFactors=FALSE);

ciMsg <- "\n* Note that the confidence interval for R^2 is based on ";
if (x$input$ci.method[1] == 'r.con') {
  ciMsg <- paste0(ciMsg,
                  "the confidence interval for the Pearson Correlation of ",
                  "the multiple correlation using r.con from the 'psych' ",
                  "package because that was specified using the 'ci.method' ",
                  "argument.");
} else if (x$input$ci.method[1] == 'olkinfinn') {
  ciMsg <- paste0(ciMsg,
                  "the formula reported by Olkin and Finn (1995) in their Correlation ",
                  "Redux paper, because this was specified using the 'ci.method' ",
                  "argument. This may not work well for very low values. Set the ",
                  "argument to 'widest' to also compute the confidence interval ",
                  "of the multiple correlation using the r.con function from ",
                  "the 'psych' package and selecting the widest interval.");
} else if (identical(x$output$rsq.ci, x$output$rsq.ci.r.con)) {
  ciMsg <- paste0(ciMsg,
                  "the confidence interval for the Pearson Correlation of ",
                  "the multiple correlation using r.con from the 'psych' ",
                  "package because that was the widest interval, which ",
                  "should be used because the 'ci.method' was set to 'widest'.");
} else if (identical(x$output$rsq.ci, x$output$rsq.ci.olkinfinn)) {
  ciMsg <- paste0(ciMsg,
                  "the formula reported by Olkin and Finn (1995) in their Correlation ",
                  "Redux paper, because this was the widest interval, which ",
                  "should be used because the 'ci.method' was set to 'widest'.");
} else {
  ciMsg <- paste0(ciMsg,
                  " -- I don't know actually, something appears to have gone wrong. ",
                  "The 'ci.method' argument was set to ", ufs::vecTxtQ(x$input$ci.method),
                  ".");
}

if (x$input$ci.method.note) {
  
  cat("\n\n**Summary**\n\n");
  
  kableExtra::add_footnote(
    knitTable(
      tab,
      #caption="Summary",
      col.names = NULL,
      align = c("r", "l"),
      row.names = FALSE,
      escape = TRUE
    ),
    label = ciMsg
  );

  cat("\n\n");
  
} else {
    
  cat("\n\n**Summary**\n\n");

  knitTable(
    tab,
    #caption="Summary",
    col.names = NULL,
    align = c("r", "l"),
    row.names = FALSE,
    escape = TRUE
  );
  
}


```

```{r, echo=echoPartial, results='asis', eval=("raw" %in% x$input$coefficients) }

# ufs::heading("Raw regression coefficients",
#              headingLevel = headingLevel+1);
# 
# ufs::cat0("\n\n*(unstandardized beta values, called 'B' in SPSS)*\n\n");

tmpDat <- round(x$output$coef.raw[, 1:5], digits);
tmpDat[[1]] <- paste0("[", tmpDat[[1]], "; ", tmpDat[[2]], "]");
tmpDat[[2]] <- NULL;
names(tmpDat)[1] <- paste0(x$input$conf.level*100, "% conf. int.");
tmpDat$p <- ufs::formatPvalue(x$output$coef.raw$p,
                              digits=pvalueDigits,
                              includeP=FALSE);

cat("\n\n**Raw regression coefficients**\n\n");

kableExtra::add_footnote(
  knitTable(
    tmpDat,
    #caption = "Raw regression coefficients",
    escape = TRUE
  ),
  label = "These are unstandardized beta values, called 'B' in SPSS."
);

```

```{r, echo=echoPartial, results='asis', eval=("scaled" %in% x$input$coefficients) }

# ufs::heading("Scaled regression coefficients",
#              headingLevel = headingLevel+1);
# 
# ufs::cat0("\n\n*(standardized beta values, called 'Beta' in SPSS)*\n\n");

tmpDat <- round(x$output$coef.scaled[, 1:5], digits);
tmpDat[[1]] <- paste0("[", tmpDat[[1]], "; ", tmpDat[[2]], "]");
tmpDat[[2]] <- NULL;
names(tmpDat)[1] <- paste0(x$input$conf.level*100, "% conf. int.");
tmpDat$p <- ufs::formatPvalue(x$output$coef.scaled$p,
                              digits=pvalueDigits,
                              includeP=FALSE);

cat("\n\n**Scaled regression coefficients**\n\n");

kableExtra::add_footnote(
  knitTable(
    tmpDat,
    #caption = "Scaled regression coefficients",
    escape = TRUE
  ),
  label = "These are standardized beta values, called 'Beta' in SPSS."
);

```

```{r, echo=echoPartial, results='asis', eval=(x$input$collinearity && ((!is.null(x$intermediate$vif.raw)) || (!is.null(x$intermediate$vif.scaled)))) }

# ufs::heading("Collinearity diagnostics",
#              headingLevel = headingLevel+1);

cat("\n\n**Collinearity diagnostics**\n\n");

if (is.vector(x$intermediate$vif.raw)) {
  if ("raw" %in% x$input$coefficients) {

    collinearityDat <- data.frame(VIF = x$intermediate$vif.raw,
                                  Tolerance = x$intermediate$tolerance.raw);
    row.names(collinearityDat) <- paste0(ufs::repStr(4), names(x$intermediate$vif.raw));
    
    knitTable(
      collinearityDat,
      #caption = "Collinearity diagnostics",
      row.names=TRUE,
      escape = TRUE,
      digits=digits
    );

  }
} else if (is.vector(x$intermediate$vif.scaled)) {
  if ("scaled" %in% x$input$coefficients) {

    collinearityDat <- data.frame(
      VIF = x$intermediate$vif.scaled,
      Tolerance = x$intermediate$tolerance.scaled
    );
    row.names(collinearityDat) <- paste0(ufs::repStr(4), names(x$intermediate$vif.scaled));
    
    knitTable(
      collinearityDat,
      #caption="Collinearity diagnostics",
      row.names=TRUE,
      escape = TRUE,
      digits=digits
    );

  }
}

```

```{r, echo=echoPartial, results='asis', eval=(!is.null(x$output$plot)), fig.width=5, fig.height=5, fig.cap="Scatterplot with regression line" }

# ufs::heading("Scatterplot",
#              headingLevel = headingLevel+1);

x$output$plot;

```

```{r echo=FALSE}
if (!is.null(oldKableViewOption)) {
  options(kableExtra_view_html = oldKableViewOption);
}
```

</div>
<div style="display:block;clear:both;" class="rosetta-regr-end"></div>
