Skip to content

Commit

Permalink
Updates, add fit_models, #14
Browse files Browse the repository at this point in the history
  • Loading branch information
Nelson-Gon committed May 26, 2020
1 parent be198c8 commit a4f9bdd
Show file tree
Hide file tree
Showing 9 changed files with 105 additions and 11 deletions.
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ export(add_model_residuals)
export(agg_by_group)
export(extract_model_info)
export(fit_model)
export(fit_models)
export(get_data_Stats)
export(get_exponent)
export(get_mode)
Expand Down
2 changes: 2 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ output: htlm_document

- **get_this** now works with numeric input and also supports `data.frame` objects.

- **fit_models** extends **fit_model** by building many models at once.


**Other changes**

Expand Down
15 changes: 15 additions & 0 deletions R/fit_model.R
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,22 @@ fit_model <- function (df=NULL, yname=NULL, xname=NULL, modeltype=NULL,...){
}


#' @export
#' @title Fit several models with different response variables
#' @inheritParams fit_model
#' @return A list of model objects that can be used later.
#' @examples
#' fit_models(df=iris,yname=c("Sepal.Length","Sepal.Width"),
#' xname="Petal.Length + Petal.Width",modeltype="lm")
#'
fit_models <- function (df=NULL,yname=NULL, xname=NULL, modeltype=NULL,...){
if(!length(yname)>1){
stop("fit_models is used for several yname, use fit_model for single predictors")
}

lapply(yname, function(x) fit_model(df=df,yname=x,xname=xname,modeltype = modeltype,...))

}



Expand Down
10 changes: 10 additions & 0 deletions README.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,16 @@ iris1 %>%
```


- `fit_models`

This is similar to `fit_model` with the ability to fit many models with many predictors at once. A simple linear model for instance:

```{r}
fit_models(df=iris,yname=c("Sepal.Length","Sepal.Width"),xname="Petal.Length + Petal.Width",modeltype="lm")
```


- `get_var_corr`

Expand Down
48 changes: 37 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
2020-05-16
2020-05-27

# `manymodelr`: Build and Tune Several Models

Expand Down Expand Up @@ -221,34 +221,32 @@ follows:

``` r
extract_model_info(lm_model, "r2")
#> [1] 0.07138289
#> NULL
```

To extract the adjusted r squared, we can do the following:

``` r
extract_model_info(lm_model, "adj_r2")
#> [1] 0.0520367
#> NULL
```

For the p value:

``` r
extract_model_info(lm_model, "p_value")
#> (Intercept) Petal.Length
#> 1.614927e-13 6.069778e-02
#> NULL
```

To extract multiple attributes:

``` r
extract_model_info(lm_model,c("p_value","response"))
#> $p_value
#> (Intercept) Petal.Length
#> 1.614927e-13 6.069778e-02
#> $<NA>
#> NULL
#>
#> $response
#> Sepal.Length
#> [1] "Sepal.Length"
```

This is not restricted to linear models but will work for most model
Expand Down Expand Up @@ -330,6 +328,34 @@ iris1 %>%
#> 4 setosa
#> 5 setosa
#> 6 setosa
```

- `fit_models`

This is similar to `fit_model` with the ability to fit many models with
many predictors at once. A simple linear model for instance:

``` r

fit_models(df=iris,yname=c("Sepal.Length","Sepal.Width"),xname="Petal.Length + Petal.Width",modeltype="lm")
#> [[1]]
#>
#> Call:
#> lm(formula = Sepal.Length ~ Petal.Length + Petal.Width, data = df)
#>
#> Coefficients:
#> (Intercept) Petal.Length Petal.Width
#> 4.1906 0.5418 -0.3196
#>
#>
#> [[2]]
#>
#> Call:
#> lm(formula = Sepal.Width ~ Petal.Length + Petal.Width, data = df)
#>
#> Coefficients:
#> (Intercept) Petal.Length Petal.Width
#> 3.5870 -0.2571 0.3640
```

- `get_var_corr`
Expand Down Expand Up @@ -433,7 +459,7 @@ plot_corr(mtcars,show_which = "corr",
#> Using colour_by for the legend title.
```

![](README_files/figure-gfm/unnamed-chunk-25-1.png)<!-- -->
![](README_files/figure-gfm/unnamed-chunk-26-1.png)<!-- -->

To show significance of the results instead of the correlations
themselves, we can set `show_which` to “signif” as shown below. By
Expand All @@ -451,7 +477,7 @@ plot_corr(mtcars, x="other_var", y="comparison_var",plot_style = "circles",show_
#> "circles", : Using colour_by for the legend title.
```

![](README_files/figure-gfm/unnamed-chunk-26-1.png)<!-- -->
![](README_files/figure-gfm/unnamed-chunk-27-1.png)<!-- -->

To explore more options, please take a look at the documentation.

Expand Down
Binary file modified README_files/figure-gfm/unnamed-chunk-26-1.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified README_files/figure-gfm/unnamed-chunk-27-1.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
30 changes: 30 additions & 0 deletions man/fit_models.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 10 additions & 0 deletions vignettes/manymodelr_vignette.rmd
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,16 @@ iris1 %>%
```


- `fit_models`

This is similar to `fit_model` with the ability to fit many models with many predictors at once. A simple linear model for instance:

```{r}
fit_models(df=iris,yname=c("Sepal.Length","Sepal.Width"),xname="Petal.Length + Petal.Width",modeltype="lm")
```


- `get_var_corr`

Expand Down

0 comments on commit a4f9bdd

Please sign in to comment.