
<div style="display:block;clear:both;" class="rosetta-log-regr-start"></div>
<div class="rosetta-log-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("Logistic regression analysis",
             idSlug("rosettaLogRegr"), "}", headingLevel=headingLevel);
```

**Summary**

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

knitTable(
  data.frame(c("Formula:", "Sample size:", "Predicting:",
               "Cox & Snell R-squared:", "Nagelkerke R-squared:",
               "Test for significance:  \n(of full model)"),
             c(x$intermediate$formula.as.character,
               sum(complete.cases(x$intermediate$dat.raw)),
               x$intermediate$levelNames[2],
               ufs::formatR(x$intermediate$CoxSnellRsq, digits),
               ufs::formatR(x$intermediate$NagelkerkeRsq, digits),
               paste0(
                 "ChiSq[", x$intermediate$chiDf, "] = ",
                 round(x$intermediate$modelChi, digits),
                 ", ",
                 ufs::formatPvalue(x$intermediate$deltaChisq, digits=pvalueDigits)
               )
             ),
             stringsAsFactors=FALSE),
  #caption="Summary",
  col.names = NULL,
  align = c("r", "l"),
  row.names = FALSE,
  escape = TRUE
);

```

```{r, echo=echoPartial, results='asis', eval=(x$input$crossTabs) }

caption1 <-
  ifelse(
    isTRUE(getOption('knitr.in.progress')) && (knitr::is_latex_output()),
    paste0(
      "Predictions by the null model (",
      round(100 * x$output$proportionCorrect.null, digits),
      "\\% correct)"
    ),
    paste0(
      "Predictions by the null model (",
      round(100 * x$output$proportionCorrect.null, digits),
      "% correct)"
    )
  );

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

cat0("\n\n", caption1, "\n\n");

knitTable(
  x$output$crossTab.null
  #,caption = caption1
  );

caption2 <-
  ifelse(
    isTRUE(getOption('knitr.in.progress')) && (knitr::is_latex_output()),
    paste0(
      "Predictions by the tested model (",
      round(100 * x$output$proportionCorrect.model, digits),
      "\\% correct)"
    ),
    paste0(
      "Predictions by the tested model (",
      round(100 * x$output$proportionCorrect.model, digits),
      "% correct)"
    )
  );

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

cat0("\n\n", caption2, "\n\n");

knitTable(
  x$output$crossTab.model
  # ,caption=caption2
);

```

**Regression coefficients**

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

# ufs::heading("Regression coefficients {regr-coefs-",
#              randomSlug, "}",
#              headingLevel = headingLevel+1);

# ufs::cat0("\n\n*(log odds values, called 'B' in SPSS)*\n\n");

tmpDat <- round(x$output$coef[, 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$p,
                              digits=pvalueDigits,
                              includeP=FALSE);

kableExtra::add_footnote(
  knitTable(
    tmpDat,
#    caption="",
    escape = TRUE
  ),
  "These are log odds values, called 'B' in SPSS."
);

```

**Regression coefficients as odds ratios**

```{r, echo=echoPartial, results='asis', eval=x$input$oddsRatios }

# ufs::heading("Regression coefficients as odds ratios {#regr-coefs-as-ors-",
#              randomSlug, "}",
#              headingLevel = headingLevel+1);

# ufs::cat0("\n\n*(ORs, called 'Exp(B)' in SPSS)*\n\n");

tmpDat2 <- round(x$output$coef_oddsRatios, digits);
tmpDat2[[1]] <- paste0("[", tmpDat2[[1]], "; ", tmpDat2[[2]], "]");
tmpDat2[[2]] <- NULL;
names(tmpDat2) <- c(
  paste0(x$input$conf.level*100, "% conf. int."),
  "Odds Ratio point estimate"
);
row.names(tmpDat2) <- row.names(tmpDat);

kableExtra::add_footnote(
  knitTable(
    tmpDat2,
    #caption = "Regression coefficients as odds ratios",
    escape = TRUE
  ),
  "These are odds ratios, called 'Exp(B)' in SPSS."
);

```

**Collinearity diagnostics**

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

# ufs::heading("Collinearity diagnostics {#collinearity-diag-",
#              randomSlug, "}",
             # headingLevel = headingLevel+1);

collinearityDat <- data.frame(VIF = x$intermediate$vif,
                              Tolerance = x$intermediate$tolerance);
row.names(collinearityDat) <- paste0(ufs::repStr(4), names(x$intermediate$vif));

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="Plot with raw data, (binned) means, and predicted probability as a logistic curve" }

# ufs::heading("Logistic curve plot",
#              idSlug('log-curve-plot'),
#              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-log-regr-end"></div>
