<html xmlns:v="urn:schemas-microsoft-com:vml" xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:w="urn:schemas-microsoft-com:office:word" xmlns:m="http://schemas.microsoft.com/office/2004/12/omml" xmlns="http://www.w3.org/TR/REC-html40"><head><meta http-equiv=Content-Type content="text/html; charset=iso-8859-1"><meta name=Generator content="Microsoft Word 15 (filtered medium)"><!--[if !mso]><style>v\:* {behavior:url(#default#VML);}
o\:* {behavior:url(#default#VML);}
w\:* {behavior:url(#default#VML);}
.shape {behavior:url(#default#VML);}
</style><![endif]--><style><!--
/* Font Definitions */
@font-face
{font-family:"Cambria Math";
panose-1:2 4 5 3 5 4 6 3 2 4;}
@font-face
{font-family:Calibri;
panose-1:2 15 5 2 2 2 4 3 2 4;}
@font-face
{font-family:"Lucida Console";
panose-1:2 11 6 9 4 5 4 2 2 4;}
/* Style Definitions */
p.MsoNormal, li.MsoNormal, div.MsoNormal
{margin:0cm;
margin-bottom:.0001pt;
font-size:11.0pt;
font-family:"Calibri",sans-serif;
mso-fareast-language:EN-US;}
a:link, span.MsoHyperlink
{mso-style-priority:99;
color:#0563C1;
text-decoration:underline;}
a:visited, span.MsoHyperlinkFollowed
{mso-style-priority:99;
color:#954F72;
text-decoration:underline;}
pre
{mso-style-priority:99;
mso-style-link:"Pré-formatação HTML Char";
margin:0cm;
margin-bottom:.0001pt;
font-size:10.0pt;
font-family:"Courier New";}
span.EstiloDeEmail17
{mso-style-type:personal-compose;
font-family:"Calibri",sans-serif;
color:windowtext;}
span.Pr-formataoHTMLChar
{mso-style-name:"Pré-formatação HTML Char";
mso-style-priority:99;
mso-style-link:"Pré-formatação HTML";
font-family:"Courier New";
mso-fareast-language:PT-BR;}
span.gem3dmtclgb
{mso-style-name:gem3dmtclgb;}
span.gem3dmtclfb
{mso-style-name:gem3dmtclfb;}
.MsoChpDefault
{mso-style-type:export-only;
font-family:"Calibri",sans-serif;
mso-fareast-language:EN-US;}
@page WordSection1
{size:612.0pt 792.0pt;
margin:70.85pt 3.0cm 70.85pt 3.0cm;}
div.WordSection1
{page:WordSection1;}
--></style><!--[if gte mso 9]><xml>
<o:shapedefaults v:ext="edit" spidmax="1026" />
</xml><![endif]--><!--[if gte mso 9]><xml>
<o:shapelayout v:ext="edit">
<o:idmap v:ext="edit" data="1" />
</o:shapelayout></xml><![endif]--></head><body lang=PT-BR link="#0563C1" vlink="#954F72"><div class=WordSection1><p class=MsoNormal>Título da msg explica<o:p></o:p></p><p class=MsoNormal><o:p> </o:p></p><table class=MsoNormalTable border=0 cellspacing=0 cellpadding=0 width=761 style='width:570.75pt;background:white'><tr><td valign=top style='padding:0cm 0cm 6.0pt 4.5pt'><p class=MsoNormal style='word-break:break-all'><span lang=EN-US style='font-size:10.0pt;font-family:"Lucida Console";color:blue;mso-fareast-language:PT-BR'>> sessionInfo()<o:p></o:p></span></p><p class=MsoNormal style='word-break:break-all'><span lang=EN-US style='font-size:10.0pt;font-family:"Lucida Console";color:black;mso-fareast-language:PT-BR'>R version 3.2.3 (2015-12-10)<o:p></o:p></span></p><p class=MsoNormal style='word-break:break-all'><span lang=EN-US style='font-size:10.0pt;font-family:"Lucida Console";color:black;mso-fareast-language:PT-BR'>Platform: x86_64-w64-mingw32/x64 (64-bit)<o:p></o:p></span></p><p class=MsoNormal style='word-break:break-all'><span lang=EN-US style='font-size:10.0pt;font-family:"Lucida Console";color:black;mso-fareast-language:PT-BR'>Running under: Windows >= 8 x64 (build 9200)<o:p></o:p></span></p><p class=MsoNormal style='word-break:break-all'><span lang=EN-US style='font-size:10.0pt;font-family:"Lucida Console";color:black;mso-fareast-language:PT-BR'><o:p> </o:p></span></p><p class=MsoNormal style='word-break:break-all'><span style='font-size:10.0pt;font-family:"Lucida Console";color:black;mso-fareast-language:PT-BR'>locale:<o:p></o:p></span></p><p class=MsoNormal style='word-break:break-all'><span style='font-size:10.0pt;font-family:"Lucida Console";color:black;mso-fareast-language:PT-BR'>[1] LC_COLLATE=Portuguese_Brazil.1252 LC_CTYPE=Portuguese_Brazil.1252 <o:p></o:p></span></p><p class=MsoNormal style='word-break:break-all'><span lang=EN-US style='font-size:10.0pt;font-family:"Lucida Console";color:black;mso-fareast-language:PT-BR'>[3] LC_MONETARY=Portuguese_Brazil.1252 LC_NUMERIC=C <o:p></o:p></span></p><p class=MsoNormal style='word-break:break-all'><span lang=EN-US style='font-size:10.0pt;font-family:"Lucida Console";color:black;mso-fareast-language:PT-BR'>[5] LC_TIME=Portuguese_Brazil.1252 <o:p></o:p></span></p><p class=MsoNormal style='word-break:break-all'><span lang=EN-US style='font-size:10.0pt;font-family:"Lucida Console";color:black;mso-fareast-language:PT-BR'><o:p> </o:p></span></p><p class=MsoNormal style='word-break:break-all'><span lang=EN-US style='font-size:10.0pt;font-family:"Lucida Console";color:black;mso-fareast-language:PT-BR'>attached base packages:<o:p></o:p></span></p><p class=MsoNormal style='word-break:break-all'><span lang=EN-US style='font-size:10.0pt;font-family:"Lucida Console";color:black;mso-fareast-language:PT-BR'>[1] stats graphics grDevices utils datasets methods base <o:p></o:p></span></p><p class=MsoNormal style='word-break:break-all'><span lang=EN-US style='font-size:10.0pt;font-family:"Lucida Console";color:black;mso-fareast-language:PT-BR'><o:p> </o:p></span></p><p class=MsoNormal style='word-break:break-all'><span lang=EN-US style='font-size:10.0pt;font-family:"Lucida Console";color:black;mso-fareast-language:PT-BR'>other attached packages:<o:p></o:p></span></p><p class=MsoNormal style='word-break:break-all'><span lang=EN-US style='font-size:10.0pt;font-family:"Lucida Console";color:black;mso-fareast-language:PT-BR'>[1] nnet_7.3-11 caret_6.0-64 ggplot2_2.0.0.9001 lattice_0.20-33 <o:p></o:p></span></p><p class=MsoNormal style='word-break:break-all'><span lang=EN-US style='font-size:10.0pt;font-family:"Lucida Console";color:black;mso-fareast-language:PT-BR'>[5] mlbench_2.1-1 RevoUtilsMath_3.2.3<o:p></o:p></span></p><p class=MsoNormal style='word-break:break-all'><span lang=EN-US style='font-size:10.0pt;font-family:"Lucida Console";color:black;mso-fareast-language:PT-BR'><o:p> </o:p></span></p><p class=MsoNormal style='word-break:break-all'><span lang=EN-US style='font-size:10.0pt;font-family:"Lucida Console";color:black;mso-fareast-language:PT-BR'>loaded via a namespace (and not attached):<o:p></o:p></span></p><p class=MsoNormal style='word-break:break-all'><span lang=EN-US style='font-size:10.0pt;font-family:"Lucida Console";color:black;mso-fareast-language:PT-BR'> [1] Rcpp_0.12.3 compiler_3.2.3 nloptr_1.0.4 plyr_1.8.3 <o:p></o:p></span></p><p class=MsoNormal style='word-break:break-all'><span lang=EN-US style='font-size:10.0pt;font-family:"Lucida Console";color:black;mso-fareast-language:PT-BR'> [5] iterators_1.0.8 tools_3.2.3 digest_0.6.9 lme4_1.1-10 <o:p></o:p></span></p><p class=MsoNormal style='word-break:break-all'><span lang=EN-US style='font-size:10.0pt;font-family:"Lucida Console";color:black;mso-fareast-language:PT-BR'> [9] nlme_3.1-124 gtable_0.1.2 mgcv_1.8-10 Matrix_1.2-3 <o:p></o:p></span></p><p class=MsoNormal style='word-break:break-all'><span lang=EN-US style='font-size:10.0pt;font-family:"Lucida Console";color:black;mso-fareast-language:PT-BR'>[13] foreach_1.4.3 yaml_2.1.13 parallel_3.2.3 SparseM_1.7 <o:p></o:p></span></p><p class=MsoNormal style='word-break:break-all'><span lang=EN-US style='font-size:10.0pt;font-family:"Lucida Console";color:black;mso-fareast-language:PT-BR'>[17] stringr_1.0.0.9000 knitr_1.12 MatrixModels_0.4-2 stats4_3.2.3 <o:p></o:p></span></p><p class=MsoNormal style='word-break:break-all'><span lang=EN-US style='font-size:10.0pt;font-family:"Lucida Console";color:black;mso-fareast-language:PT-BR'>[21] grid_3.2.3 rmarkdown_0.9.4 minqa_1.2.4 reshape2_1.4.1.9000<o:p></o:p></span></p><p class=MsoNormal style='word-break:break-all'><span lang=EN-US style='font-size:10.0pt;font-family:"Lucida Console";color:black;mso-fareast-language:PT-BR'>[25] car_2.1-1 magrittr_1.5 scales_0.3.0 codetools_0.2-14 <o:p></o:p></span></p><p class=MsoNormal style='word-break:break-all'><span lang=EN-US style='font-size:10.0pt;font-family:"Lucida Console";color:black;mso-fareast-language:PT-BR'>[29] htmltools_0.3 MASS_7.3-45 splines_3.2.3 rsconnect_0.4.1.4 <o:p></o:p></span></p><p class=MsoNormal style='word-break:break-all'><span style='font-size:10.0pt;font-family:"Lucida Console";color:black;mso-fareast-language:PT-BR'>[33] pbkrtest_0.4-5 colorspace_1.2-6 quantreg_5.19 stringi_1.0-1 <o:p></o:p></span></p><p class=MsoNormal style='word-break:break-all'><span style='font-size:10.0pt;font-family:"Lucida Console";color:black;mso-fareast-language:PT-BR'>[37] munsell_0.4.2 <o:p></o:p></span></p></td></tr><tr><td valign=top style='padding:0cm 0cm 6.0pt 4.5pt'></td></tr><tr><td valign=top style='padding:0cm 0cm 6.0pt 4.5pt'><table class=MsoNormalTable border=0 cellspacing=0 cellpadding=0 width=761 style='width:570.75pt'><tr><td width=1 valign=top style='width:.75pt;padding:0cm 0cm 0cm 0cm'><p class=MsoNormal><span style='font-size:12.0pt;font-family:"Lucida Console";color:blue;mso-fareast-language:PT-BR'>> <o:p></o:p></span></p></td></tr></table></td></tr></table><p class=MsoNormal>____ código<o:p></o:p></p><p class=MsoNormal><o:p> </o:p></p><p class=MsoNormal><o:p> </o:p></p><p class=MsoNormal><o:p> </o:p></p><p class=MsoNormal>---<o:p></o:p></p><p class=MsoNormal>title: "Neural Network example"<o:p></o:p></p><p class=MsoNormal>author: "Leonard Mendonça de Assis"<o:p></o:p></p><p class=MsoNormal>date: "21 de janeiro de 2016"<o:p></o:p></p><p class=MsoNormal><span lang=EN-US>output:<o:p></o:p></span></p><p class=MsoNormal><span lang=EN-US> pdf_document:<o:p></o:p></span></p><p class=MsoNormal><span lang=EN-US> highlight: pygments<o:p></o:p></span></p><p class=MsoNormal><span lang=EN-US> number_sections: yes<o:p></o:p></span></p><p class=MsoNormal><span lang=EN-US> html_document:<o:p></o:p></span></p><p class=MsoNormal><span lang=EN-US> highlight: pygments<o:p></o:p></span></p><p class=MsoNormal><span lang=EN-US> number_sections: yes<o:p></o:p></span></p><p class=MsoNormal><span lang=EN-US> word_document:<o:p></o:p></span></p><p class=MsoNormal><span lang=EN-US> highlight: pygments<o:p></o:p></span></p><p class=MsoNormal><span lang=EN-US>---<o:p></o:p></span></p><p class=MsoNormal><span lang=EN-US># Session start<o:p></o:p></span></p><p class=MsoNormal><span lang=EN-US><o:p> </o:p></span></p><p class=MsoNormal><span lang=EN-US>```{r}<o:p></o:p></span></p><p class=MsoNormal><span lang=EN-US>require(MASS)<o:p></o:p></span></p><p class=MsoNormal><span lang=EN-US>require(neuralnet)<o:p></o:p></span></p><p class=MsoNormal><span lang=EN-US>require(plyr)<o:p></o:p></span></p><p class=MsoNormal><span lang=EN-US>require(boot)<o:p></o:p></span></p><p class=MsoNormal><span lang=EN-US>require(knitr)<o:p></o:p></span></p><p class=MsoNormal><span lang=EN-US><o:p> </o:p></span></p><p class=MsoNormal><span lang=EN-US>data <- Boston<o:p></o:p></span></p><p class=MsoNormal><span lang=EN-US><o:p> </o:p></span></p><p class=MsoNormal><span lang=EN-US>kable(head(data))<o:p></o:p></span></p><p class=MsoNormal><span lang=EN-US><o:p> </o:p></span></p><p class=MsoNormal><span lang=EN-US>kable(<o:p></o:p></span></p><p class=MsoNormal><span lang=EN-US> apply(data,2,function(x) sum(is.na(x))),<o:p></o:p></span></p><p class=MsoNormal><span lang=EN-US> caption='NAs per variables'<o:p></o:p></span></p><p class=MsoNormal><span lang=EN-US>)<o:p></o:p></span></p><p class=MsoNormal><span lang=EN-US><o:p> </o:p></span></p><p class=MsoNormal><span lang=EN-US>```<o:p></o:p></span></p><p class=MsoNormal><span lang=EN-US><o:p> </o:p></span></p><p class=MsoNormal><span lang=EN-US># Adjusting a linear model<o:p></o:p></span></p><p class=MsoNormal><span lang=EN-US><o:p> </o:p></span></p><p class=MsoNormal><span lang=EN-US>```{r}<o:p></o:p></span></p><p class=MsoNormal><span lang=EN-US>set.seed(500)<o:p></o:p></span></p><p class=MsoNormal><span lang=EN-US><o:p> </o:p></span></p><p class=MsoNormal><span lang=EN-US>index <- sample(1:nrow(data),round(0.75*nrow(data)))<o:p></o:p></span></p><p class=MsoNormal><span lang=EN-US>train <- data[index,]<o:p></o:p></span></p><p class=MsoNormal><span lang=EN-US>test <- data[-index,]<o:p></o:p></span></p><p class=MsoNormal><span lang=EN-US>lm.fit <- glm(medv~., data=train)<o:p></o:p></span></p><p class=MsoNormal><span lang=EN-US>summary(lm.fit)<o:p></o:p></span></p><p class=MsoNormal><span lang=EN-US>pr.lm <- predict(lm.fit,test)<o:p></o:p></span></p><p class=MsoNormal><span lang=EN-US>MSE.lm <- sum((pr.lm - test$medv)^2)/nrow(test)<o:p></o:p></span></p><p class=MsoNormal><span lang=EN-US>```<o:p></o:p></span></p><p class=MsoNormal><span lang=EN-US><o:p> </o:p></span></p><p class=MsoNormal><span lang=EN-US># Adjusting a Neural Net<o:p></o:p></span></p><p class=MsoNormal><span lang=EN-US><o:p> </o:p></span></p><p class=MsoNormal><span lang=EN-US>As a first step, we are going to address data preprocessing. It is good practice to normalize your data before training a neural network. I cannot emphasize enough how important this step is: depending on your dataset, avoiding normalization may lead to useless results or to a very difficult training process (most of the times the algorithm will not converge before the number of maximum iterations allowed). You can choose different methods to scale the data (z-normalization, min-max scale, etc…). I chose to use the min-max method and scale the data in the interval [0,1].<o:p></o:p></span></p><p class=MsoNormal><span lang=EN-US>Usually scaling in the intervals [0,1] or [-1,1] tends to give better results. We therefore scale and split the data before moving on:<o:p></o:p></span></p><p class=MsoNormal><span lang=EN-US><o:p> </o:p></span></p><p class=MsoNormal><span lang=EN-US>```{r}<o:p></o:p></span></p><p class=MsoNormal><span lang=EN-US>maxs <- apply(data, 2, max) <o:p></o:p></span></p><p class=MsoNormal><span lang=EN-US>mins <- apply(data, 2, min)<o:p></o:p></span></p><p class=MsoNormal><span lang=EN-US><o:p> </o:p></span></p><p class=MsoNormal><span lang=EN-US>scaled <- as.data.frame(scale(data, center = mins, scale = maxs - mins))<o:p></o:p></span></p><p class=MsoNormal><span lang=EN-US><o:p> </o:p></span></p><p class=MsoNormal><span lang=EN-US>train_ <- scaled[index,]<o:p></o:p></span></p><p class=MsoNormal><span lang=EN-US>test_ <- scaled[-index,]<o:p></o:p></span></p><p class=MsoNormal><span lang=EN-US>```<o:p></o:p></span></p><p class=MsoNormal><span lang=EN-US><o:p> </o:p></span></p><p class=MsoNormal><span lang=EN-US>Note that scale returns a matrix that needs to be coerced into a data.frame. <o:p></o:p></span></p><p class=MsoNormal><span lang=EN-US><o:p> </o:p></span></p><p class=MsoNormal><span lang=EN-US>```{r}<o:p></o:p></span></p><p class=MsoNormal><span lang=EN-US>n <- names(train_)<o:p></o:p></span></p><p class=MsoNormal><span lang=EN-US>f <- as.formula(paste("medv ~", paste(n[!n %in% "medv"], collapse = " + ")))<o:p></o:p></span></p><p class=MsoNormal><span lang=EN-US>nn <- neuralnet(f,data=train_,hidden=c(5,3),linear.output=T)<o:p></o:p></span></p><p class=MsoNormal><span lang=EN-US>```<o:p></o:p></span></p><p class=MsoNormal><span lang=EN-US><o:p> </o:p></span></p><p class=MsoNormal><span lang=EN-US>And the Neural Nework looks like this:<o:p></o:p></span></p><p class=MsoNormal><span lang=EN-US><o:p> </o:p></span></p><p class=MsoNormal><span lang=EN-US>```{r}<o:p></o:p></span></p><p class=MsoNormal><span lang=EN-US>plot(nn)<o:p></o:p></span></p><p class=MsoNormal><span lang=EN-US>```<o:p></o:p></span></p><p class=MsoNormal><span lang=EN-US><o:p> </o:p></span></p><p class=MsoNormal><span lang=EN-US>```{r}<o:p></o:p></span></p><p class=MsoNormal><span lang=EN-US>sessionInfo()<o:p></o:p></span></p><p class=MsoNormal>```<o:p></o:p></p><p class=MsoNormal><o:p> </o:p></p><p class=MsoNormal><span style='mso-fareast-language:PT-BR'><img width=151 height=151 id="Imagem_x0020_1" src="cid:image001.png@01D15527.632EC4F0"><o:p></o:p></span></p><p class=MsoNormal><o:p> </o:p></p></div></body></html>