Last updated: 2022-04-05
Checks: 7 0
Knit directory: stat34800/analysis/
This reproducible R Markdown analysis was created with workflowr (version 1.7.0). The Checks tab describes the reproducibility checks that were applied when the results were created. The Past versions tab lists the development history.
Great! Since the R Markdown file has been committed to the Git repository, you know the exact version of the code that produced these results.
Great job! The global environment was empty. Objects defined in the global environment can affect the analysis in your R Markdown file in unknown ways. For reproduciblity it’s best to always run the code in an empty environment.
The command set.seed(20180411)
was run prior to running the code in the R Markdown file. Setting a seed ensures that any results that rely on randomness, e.g. subsampling or permutations, are reproducible.
Great job! Recording the operating system, R version, and package versions is critical for reproducibility.
Nice! There were no cached chunks for this analysis, so you can be confident that you successfully produced the results during this run.
Great job! Using relative paths to the files within your workflowr project makes it easier to run your code on other machines.
Great! You are using Git for version control. Tracking code development and connecting the code version to the results is critical for reproducibility.
The results in this page were generated with repository version 7b25754. See the Past versions tab to see a history of the changes made to the R Markdown and HTML files.
Note that you need to be careful to ensure that all relevant files for the analysis have been committed to Git prior to generating the results (you can use wflow_publish
or wflow_git_commit
). workflowr only checks the R Markdown file, but you know if there are other scripts or data files that it depends on. Below is the status of the Git repository when the results were generated:
Ignored files:
Ignored: .Rhistory
Ignored: .Rproj.user/
Untracked files:
Untracked: analysis/currency_analysis.Rmd
Untracked: analysis/haar.Rmd
Untracked: analysis/stocks_analysis.Rmd
Note that any generated files, e.g. HTML, png, CSS, etc., are not included in this status report because it is ok for generated content to have uncommitted changes.
These are the previous versions of the repository in which changes were made to the R Markdown (analysis/overfitting.Rmd
) and HTML (docs/overfitting.html
) files. If you’ve configured a remote Git repository (see ?wflow_git_remote
), click on the hyperlinks in the table below to view the files as they were in that past version.
File | Version | Author | Date | Message |
---|---|---|---|---|
Rmd | 7b25754 | Matthew Stephens | 2022-04-05 | workflowr::wflow_publish(“analysis/overfitting.Rmd”) |
I took this code illustrating overfitting from https://www.r-bloggers.com/2017/06/machine-learning-explained-overfitting/
My intention here is to run the code and show the figures it produced (since the plots are not shown in the html above). I commented out some output to make it less verbose.
###Overfitting
require(data.table)
Loading required package: data.table
Warning: package 'data.table' was built under R version 4.1.1
library(rpart)
require(ggplot2)
Loading required package: ggplot2
Warning: package 'ggplot2' was built under R version 4.1.1
set.seed(456)
##Reading data
overfitting_data=data.table(airquality)
ggplot(overfitting_data,aes(Wind,Ozone))+geom_point()+ggtitle("Ozone vs wind speed")
Warning: Removed 37 rows containing missing values (geom_point).
data_test=na.omit(overfitting_data[,.(Wind,Ozone)])
train_sample=sample(1:nrow(data_test),size = 0.7*nrow(data_test))
###creation of polynomial models
degree_of_poly=1:20
degree_to_plot=c(1,3,5,10,20)
polynomial_model=list()
df_result=NULL
for (degree in degree_of_poly)
{
fm=as.formula(paste0("Ozone~poly(Wind,",degree,",raw=T)"))
polynomial_model=c(polynomial_model,list(lm(fm,data_test[train_sample])))
Polynomial_degree=paste0(degree)
data_fitted=tail(polynomial_model,1)[[1]]$fitted.values
new_df=data.table(Wind=data_test[train_sample,Wind],Ozone_real=data_test[train_sample,Ozone],Ozone_fitted=tail(polynomial_model,1)[[1]]$fitted.values,degree=as.factor(degree))
if (is.null(df_result))
df_result=new_df
else
df_result=rbind(df_result,new_df)
}
gg=ggplot(df_result[degree%in%degree_to_plot],aes(x=Wind))+geom_point(aes(y=Ozone_real))+geom_line(aes(color=degree,y=Ozone_fitted))
gg+ggtitle('Ozone vs wind for several polynomial regressions')+ylab('Ozone')
###Computing SE
SE_train_list=c()
SE_test_list=c()
for (poly_mod in polynomial_model)
{
#print(summary(poly_mod))
SE_train_list=c(SE_train_list,sqrt(mean(poly_mod$residuals^2)))
SE_test=sqrt(mean((data_test[-train_sample]-predict(poly_mod,data_test[-train_sample,]))^2))
SE_test_list=c(SE_test_list,SE_test)
}
Warning in mean.default((data_test[-train_sample] - predict(poly_mod,
data_test[-train_sample, : argument is not numeric or logical: returning NA
Warning in mean.default((data_test[-train_sample] - predict(poly_mod,
data_test[-train_sample, : argument is not numeric or logical: returning NA
Warning in mean.default((data_test[-train_sample] - predict(poly_mod,
data_test[-train_sample, : argument is not numeric or logical: returning NA
Warning in mean.default((data_test[-train_sample] - predict(poly_mod,
data_test[-train_sample, : argument is not numeric or logical: returning NA
Warning in mean.default((data_test[-train_sample] - predict(poly_mod,
data_test[-train_sample, : argument is not numeric or logical: returning NA
Warning in mean.default((data_test[-train_sample] - predict(poly_mod,
data_test[-train_sample, : argument is not numeric or logical: returning NA
Warning in mean.default((data_test[-train_sample] - predict(poly_mod,
data_test[-train_sample, : argument is not numeric or logical: returning NA
Warning in mean.default((data_test[-train_sample] - predict(poly_mod,
data_test[-train_sample, : argument is not numeric or logical: returning NA
Warning in mean.default((data_test[-train_sample] - predict(poly_mod,
data_test[-train_sample, : argument is not numeric or logical: returning NA
Warning in mean.default((data_test[-train_sample] - predict(poly_mod,
data_test[-train_sample, : argument is not numeric or logical: returning NA
Warning in mean.default((data_test[-train_sample] - predict(poly_mod,
data_test[-train_sample, : argument is not numeric or logical: returning NA
Warning in predict.lm(poly_mod, data_test[-train_sample, ]): prediction from a
rank-deficient fit may be misleading
Warning in mean.default((data_test[-train_sample] - predict(poly_mod,
data_test[-train_sample, : argument is not numeric or logical: returning NA
Warning in predict.lm(poly_mod, data_test[-train_sample, ]): prediction from a
rank-deficient fit may be misleading
Warning in mean.default((data_test[-train_sample] - predict(poly_mod,
data_test[-train_sample, : argument is not numeric or logical: returning NA
Warning in predict.lm(poly_mod, data_test[-train_sample, ]): prediction from a
rank-deficient fit may be misleading
Warning in mean.default((data_test[-train_sample] - predict(poly_mod,
data_test[-train_sample, : argument is not numeric or logical: returning NA
Warning in predict.lm(poly_mod, data_test[-train_sample, ]): prediction from a
rank-deficient fit may be misleading
Warning in mean.default((data_test[-train_sample] - predict(poly_mod,
data_test[-train_sample, : argument is not numeric or logical: returning NA
Warning in predict.lm(poly_mod, data_test[-train_sample, ]): prediction from a
rank-deficient fit may be misleading
Warning in mean.default((data_test[-train_sample] - predict(poly_mod,
data_test[-train_sample, : argument is not numeric or logical: returning NA
Warning in predict.lm(poly_mod, data_test[-train_sample, ]): prediction from a
rank-deficient fit may be misleading
Warning in mean.default((data_test[-train_sample] - predict(poly_mod,
data_test[-train_sample, : argument is not numeric or logical: returning NA
Warning in predict.lm(poly_mod, data_test[-train_sample, ]): prediction from a
rank-deficient fit may be misleading
Warning in mean.default((data_test[-train_sample] - predict(poly_mod,
data_test[-train_sample, : argument is not numeric or logical: returning NA
Warning in predict.lm(poly_mod, data_test[-train_sample, ]): prediction from a
rank-deficient fit may be misleading
Warning in mean.default((data_test[-train_sample] - predict(poly_mod,
data_test[-train_sample, : argument is not numeric or logical: returning NA
Warning in predict.lm(poly_mod, data_test[-train_sample, ]): prediction from a
rank-deficient fit may be misleading
Warning in mean.default((data_test[-train_sample] - predict(poly_mod,
data_test[-train_sample, : argument is not numeric or logical: returning NA
data_plot=data.table(SE_test_list,SE_train_list,degree_of_poly)
ggplot(data_plot[degree_of_poly<=8])+geom_line(aes(x=degree_of_poly,y=SE_test_list),color='red')+geom_line(aes(x=degree_of_poly,y=SE_train_list))+ylab('MSE')+xlab('Degrees of polynomial')
Warning: Removed 8 row(s) containing missing values (geom_path).
sessionInfo()
R version 4.1.0 Patched (2021-07-20 r80657)
Platform: aarch64-apple-darwin20 (64-bit)
Running under: macOS Monterey 12.2
Matrix products: default
BLAS: /Library/Frameworks/R.framework/Versions/4.1-arm64/Resources/lib/libRblas.0.dylib
LAPACK: /Library/Frameworks/R.framework/Versions/4.1-arm64/Resources/lib/libRlapack.dylib
locale:
[1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8
attached base packages:
[1] stats graphics grDevices utils datasets methods base
other attached packages:
[1] ggplot2_3.3.5 rpart_4.1-15 data.table_1.14.2 workflowr_1.7.0
loaded via a namespace (and not attached):
[1] tidyselect_1.1.1 xfun_0.28 purrr_0.3.4 colorspace_2.0-2
[5] vctrs_0.3.8 generics_0.1.1 htmltools_0.5.2 yaml_2.2.1
[9] utf8_1.2.2 rlang_0.4.12 jquerylib_0.1.4 later_1.3.0
[13] pillar_1.6.4 glue_1.5.0 withr_2.4.2 DBI_1.1.1
[17] lifecycle_1.0.1 stringr_1.4.0 munsell_0.5.0 gtable_0.3.0
[21] evaluate_0.14 labeling_0.4.2 knitr_1.36 callr_3.7.0
[25] fastmap_1.1.0 httpuv_1.6.3 ps_1.6.0 fansi_0.5.0
[29] highr_0.9 Rcpp_1.0.7 promises_1.2.0.1 scales_1.1.1
[33] farver_2.1.0 fs_1.5.0 digest_0.6.28 stringi_1.7.5
[37] processx_3.5.2 dplyr_1.0.7 getPass_0.2-2 rprojroot_2.0.2
[41] grid_4.1.0 tools_4.1.0 magrittr_2.0.2 tibble_3.1.6
[45] crayon_1.4.2 whisker_0.4 pkgconfig_2.0.3 ellipsis_0.3.2
[49] assertthat_0.2.1 rmarkdown_2.11 httr_1.4.2 rstudioapi_0.13
[53] R6_2.5.1 git2r_0.29.0 compiler_4.1.0