---
title: "Getting started"
output: rmarkdown::html_vignette
vignette: >
  %\VignetteIndexEntry{Getting started}
  %\VignetteEngine{knitr::rmarkdown}
  %\VignetteEncoding{UTF-8}
---

```{r, include = FALSE}
knitr::opts_chunk$set(
  collapse = TRUE,  comment = NA
)
```

## Installation

You can install autoReg package on github. 

```{r, eval=FALSE}
#install.packages("devtools")
devtools::install_github("cardiomoon/autoReg")
```

## Load package

To load the package, use library() function.
```{r}
library(autoReg)
```

## Main features

### 1.Summarizing baseline characteristics : gaze()

You can make a table summarizing baseline characteristics easily.

```{r}
library(moonBook) # For use of example data acs
gaze(sex~.,data=acs)
```

### For easy reproducible research : myft()

You can make a publication-ready table easily using myft(). It makes a flextable object which can use in either HTML and PDF format. 

```{r}
library(dplyr) # for use of `%>%`
ft=gaze(sex~.,data=acs) %>% myft()
ft
```

You can also make a powerpoint file using rrtable::table2pptx() function.

```{r,eval=FALSE}
library(rrtable)

table2pptx(ft)
```
```
Exported table as Report.pptx
```

You can make a microsoft word file using rrtable::table2docx() function.
```{r,eval=FALSE}
table2docx(ft)
```

```
Exported table as Report.docx
```

### Summarizing baseline characteristics with two or more grouping variables

You can get a table summarizing baseline characteristics with two or more grouping variables.

```{r}
gaze(sex+Dx~.,data=acs) %>% myft()
```

You can also use three or more grouping variables.The resultant table will be too long to review, but you can try. 

```{r}
gaze(sex+DM+HBP~age,data=acs) %>% myft()
```

### 2. For automatic selection of explanatory variables : autoReg()

You can make a table summarizing results of regression analysis. For example, let us perform a logistic regression with the colon cancer data.

```{r}
library(survival)   # For use of data colon
data(cancer)  

fit=glm(status~rx+sex+age+obstruct+perfor+nodes,data=colon,family="binomial")
summary(fit)
```

You can make table with above result.

```{r}
autoReg(fit)
```

Or you can make a publication-ready table.

```{r}
autoReg(fit) %>% myft()
```

If you want make a table with more explanation, you can make categorical variables with numeric variables. For example, the explanatory variables obstruct(obstruction of colon by tumor) and perfor(perforation of colon) is coded as 0 or 1, but it is "No" or "Yes" actually. Also the dependent variable status is coded as 0 or 1, it is "Alive" or "Died".

```{r}
colon$status.factor=factor(colon$status,labels=c("Alive","Died"))
colon$obstruct.factor=factor(colon$obstruct,labels=c("No","Yes"))
colon$perfor.factor=factor(colon$perfor,labels=c("No","Yes"))
colon$sex.factor=factor(colon$sex,labels=c("Female","Male"))

fit=glm(status.factor~rx+sex.factor+age+obstruct.factor+perfor.factor+nodes,data=colon,family="binomial")
result=autoReg(fit) 
result %>% myft()
```
You can add labels to the names of variables with setLabel() function.
```{r}
colon$status.factor=setLabel(colon$status.factor,"Mortality")
colon$rx=setLabel(colon$rx,"Treatment")
colon$age=setLabel(colon$age,"Age(Years)")
colon$sex.factor=setLabel(colon$sex.factor,"Sex")
colon$obstruct.factor=setLabel(colon$obstruct.factor,"Obstruction")
colon$perfor.factor=setLabel(colon$perfor.factor,"Perforation")
colon$nodes=setLabel(colon$nodes,"Positive nodes")

fit=glm(status.factor~rx+sex.factor+age+obstruct.factor+perfor.factor+nodes,data=colon,family="binomial")
result=autoReg(fit) 
result %>% myft()
```

If you do not want to show the reference values in table, you can shorten the table.

```{r}
shorten(result) %>% myft()
```

### Add univariate models to table and automatic selection of explanatory variables

You can add the results of univariate analyses to the table. At this time, the autoReg() function automatically select explanatory variables below the threshold(default value 0.2) and perform multivariate analysis. In this table, the p values of explanatory variables sex.factor and age is above the default threshold(0.2), they are excluded in multivariate model.

```{r}
autoReg(fit, uni=TRUE) %>% myft()
```

If you want to include all explanatory variables in the multivariate model, just set the threshold 1.

```{r}
autoReg(fit, uni=TRUE,threshold=1) %>% myft()
```

You can perform stepwise backward elimination to select variables and make a final model. Just set final=TRUE.

```{r}
autoReg(fit, uni=TRUE,threshold=1, final=TRUE) %>% myft()
```

#### Multiple imputation with mice()

When the argument imputed=TRUE, autoReg() function make a multiple imputed model using mice::mice() function. By default, 20 imputations performed. If you want, you can change the number of imputations with m argument.

```{r}
autoReg(fit, imputed=TRUE) %>% myft()
```


### Summarize regression model results in a plot : modelPlot()

You can draw the plot summarizing the model with modelPlot()

```{r,fig.width=8,fig.height=7}
x=modelPlot(fit)
x
```

You can make powerpoint file with this plot using rrtable::plot2pptx().
```{r,fig.show='hide',eval=FALSE}
plot2pptx(print(x))
```
```
Exported plot as Report.pptx
```


You can summarize models in a plot. If you want to summarize univariate and multivariate model in a plot, just set the uni=TRUE and adjust the threshold. You can decide whether or not show the reference by show.ref argument.

```{r,fig.width=8,fig.height=7}
modelPlot(fit,uni=TRUE,threshold=1,show.ref=FALSE)
```


