Data-Mining-using-R / CIS 430 HW / Hw6 BAGAN CIS 430 / HW6-Markdown.tex
HW6-Markdown.tex
Raw
% Options for packages loaded elsewhere
\PassOptionsToPackage{unicode}{hyperref}
\PassOptionsToPackage{hyphens}{url}
%
\documentclass[
]{article}
\usepackage{amsmath,amssymb}
\usepackage{lmodern}
\usepackage{iftex}
\ifPDFTeX
  \usepackage[T1]{fontenc}
  \usepackage[utf8]{inputenc}
  \usepackage{textcomp} % provide euro and other symbols
\else % if luatex or xetex
  \usepackage{unicode-math}
  \defaultfontfeatures{Scale=MatchLowercase}
  \defaultfontfeatures[\rmfamily]{Ligatures=TeX,Scale=1}
\fi
% Use upquote if available, for straight quotes in verbatim environments
\IfFileExists{upquote.sty}{\usepackage{upquote}}{}
\IfFileExists{microtype.sty}{% use microtype if available
  \usepackage[]{microtype}
  \UseMicrotypeSet[protrusion]{basicmath} % disable protrusion for tt fonts
}{}
\makeatletter
\@ifundefined{KOMAClassName}{% if non-KOMA class
  \IfFileExists{parskip.sty}{%
    \usepackage{parskip}
  }{% else
    \setlength{\parindent}{0pt}
    \setlength{\parskip}{6pt plus 2pt minus 1pt}}
}{% if KOMA class
  \KOMAoptions{parskip=half}}
\makeatother
\usepackage{xcolor}
\usepackage[margin=1in]{geometry}
\usepackage{color}
\usepackage{fancyvrb}
\newcommand{\VerbBar}{|}
\newcommand{\VERB}{\Verb[commandchars=\\\{\}]}
\DefineVerbatimEnvironment{Highlighting}{Verbatim}{commandchars=\\\{\}}
% Add ',fontsize=\small' for more characters per line
\usepackage{framed}
\definecolor{shadecolor}{RGB}{248,248,248}
\newenvironment{Shaded}{\begin{snugshade}}{\end{snugshade}}
\newcommand{\AlertTok}[1]{\textcolor[rgb]{0.94,0.16,0.16}{#1}}
\newcommand{\AnnotationTok}[1]{\textcolor[rgb]{0.56,0.35,0.01}{\textbf{\textit{#1}}}}
\newcommand{\AttributeTok}[1]{\textcolor[rgb]{0.77,0.63,0.00}{#1}}
\newcommand{\BaseNTok}[1]{\textcolor[rgb]{0.00,0.00,0.81}{#1}}
\newcommand{\BuiltInTok}[1]{#1}
\newcommand{\CharTok}[1]{\textcolor[rgb]{0.31,0.60,0.02}{#1}}
\newcommand{\CommentTok}[1]{\textcolor[rgb]{0.56,0.35,0.01}{\textit{#1}}}
\newcommand{\CommentVarTok}[1]{\textcolor[rgb]{0.56,0.35,0.01}{\textbf{\textit{#1}}}}
\newcommand{\ConstantTok}[1]{\textcolor[rgb]{0.00,0.00,0.00}{#1}}
\newcommand{\ControlFlowTok}[1]{\textcolor[rgb]{0.13,0.29,0.53}{\textbf{#1}}}
\newcommand{\DataTypeTok}[1]{\textcolor[rgb]{0.13,0.29,0.53}{#1}}
\newcommand{\DecValTok}[1]{\textcolor[rgb]{0.00,0.00,0.81}{#1}}
\newcommand{\DocumentationTok}[1]{\textcolor[rgb]{0.56,0.35,0.01}{\textbf{\textit{#1}}}}
\newcommand{\ErrorTok}[1]{\textcolor[rgb]{0.64,0.00,0.00}{\textbf{#1}}}
\newcommand{\ExtensionTok}[1]{#1}
\newcommand{\FloatTok}[1]{\textcolor[rgb]{0.00,0.00,0.81}{#1}}
\newcommand{\FunctionTok}[1]{\textcolor[rgb]{0.00,0.00,0.00}{#1}}
\newcommand{\ImportTok}[1]{#1}
\newcommand{\InformationTok}[1]{\textcolor[rgb]{0.56,0.35,0.01}{\textbf{\textit{#1}}}}
\newcommand{\KeywordTok}[1]{\textcolor[rgb]{0.13,0.29,0.53}{\textbf{#1}}}
\newcommand{\NormalTok}[1]{#1}
\newcommand{\OperatorTok}[1]{\textcolor[rgb]{0.81,0.36,0.00}{\textbf{#1}}}
\newcommand{\OtherTok}[1]{\textcolor[rgb]{0.56,0.35,0.01}{#1}}
\newcommand{\PreprocessorTok}[1]{\textcolor[rgb]{0.56,0.35,0.01}{\textit{#1}}}
\newcommand{\RegionMarkerTok}[1]{#1}
\newcommand{\SpecialCharTok}[1]{\textcolor[rgb]{0.00,0.00,0.00}{#1}}
\newcommand{\SpecialStringTok}[1]{\textcolor[rgb]{0.31,0.60,0.02}{#1}}
\newcommand{\StringTok}[1]{\textcolor[rgb]{0.31,0.60,0.02}{#1}}
\newcommand{\VariableTok}[1]{\textcolor[rgb]{0.00,0.00,0.00}{#1}}
\newcommand{\VerbatimStringTok}[1]{\textcolor[rgb]{0.31,0.60,0.02}{#1}}
\newcommand{\WarningTok}[1]{\textcolor[rgb]{0.56,0.35,0.01}{\textbf{\textit{#1}}}}
\usepackage{graphicx}
\makeatletter
\def\maxwidth{\ifdim\Gin@nat@width>\linewidth\linewidth\else\Gin@nat@width\fi}
\def\maxheight{\ifdim\Gin@nat@height>\textheight\textheight\else\Gin@nat@height\fi}
\makeatother
% Scale images if necessary, so that they will not overflow the page
% margins by default, and it is still possible to overwrite the defaults
% using explicit options in \includegraphics[width, height, ...]{}
\setkeys{Gin}{width=\maxwidth,height=\maxheight,keepaspectratio}
% Set default figure placement to htbp
\makeatletter
\def\fps@figure{htbp}
\makeatother
\setlength{\emergencystretch}{3em} % prevent overfull lines
\providecommand{\tightlist}{%
  \setlength{\itemsep}{0pt}\setlength{\parskip}{0pt}}
\setcounter{secnumdepth}{-\maxdimen} % remove section numbering
\ifLuaTeX
  \usepackage{selnolig}  % disable illegal ligatures
\fi
\IfFileExists{bookmark.sty}{\usepackage{bookmark}}{\usepackage{hyperref}}
\IfFileExists{xurl.sty}{\usepackage{xurl}}{} % add URL line breaks if available
\urlstyle{same} % disable monospaced font for URLs
\hypersetup{
  pdftitle={HW6 Markdown},
  pdfauthor={Chay Bagan},
  hidelinks,
  pdfcreator={LaTeX via pandoc}}

\title{HW6 Markdown}
\author{Chay Bagan}
\date{2022-09-29}

\begin{document}
\maketitle

\begin{Shaded}
\begin{Highlighting}[]
\CommentTok{\#Chay Bagan Homework 6}

\FunctionTok{library}\NormalTok{(caret)}
\end{Highlighting}
\end{Shaded}

\begin{verbatim}
## Loading required package: ggplot2
\end{verbatim}

\begin{verbatim}
## Loading required package: lattice
\end{verbatim}

\begin{Shaded}
\begin{Highlighting}[]
\FunctionTok{library}\NormalTok{(FNN)}
\NormalTok{housing.df }\OtherTok{\textless{}{-}} \FunctionTok{read.csv}\NormalTok{(}\StringTok{"BostonHousing.csv"}\NormalTok{)}

\FunctionTok{set.seed}\NormalTok{(}\DecValTok{123}\NormalTok{)}
\NormalTok{train.index }\OtherTok{\textless{}{-}} \FunctionTok{sample}\NormalTok{(}\FunctionTok{row.names}\NormalTok{(housing.df), }\FloatTok{0.6}\SpecialCharTok{*}\FunctionTok{dim}\NormalTok{(housing.df)[}\DecValTok{1}\NormalTok{])  }
\NormalTok{valid.index }\OtherTok{\textless{}{-}} \FunctionTok{setdiff}\NormalTok{(}\FunctionTok{row.names}\NormalTok{(housing.df), train.index)  }
\NormalTok{train.df }\OtherTok{\textless{}{-}}\NormalTok{ housing.df[train.index, }\SpecialCharTok{{-}}\DecValTok{14}\NormalTok{]}
\NormalTok{valid.df }\OtherTok{\textless{}{-}}\NormalTok{ housing.df[valid.index, }\SpecialCharTok{{-}}\DecValTok{14}\NormalTok{]}

\NormalTok{train.norm.df }\OtherTok{\textless{}{-}}\NormalTok{ train.df}
\NormalTok{valid.norm.df }\OtherTok{\textless{}{-}}\NormalTok{ valid.df}
\NormalTok{housing.norm.df }\OtherTok{\textless{}{-}}\NormalTok{housing.df}

\NormalTok{norm.values }\OtherTok{\textless{}{-}} \FunctionTok{preProcess}\NormalTok{(train.df, }\AttributeTok{method=}\FunctionTok{c}\NormalTok{(}\StringTok{"center"}\NormalTok{, }\StringTok{"scale"}\NormalTok{))}
\NormalTok{train.norm.df }\OtherTok{\textless{}{-}} \FunctionTok{as.data.frame}\NormalTok{(}\FunctionTok{predict}\NormalTok{(norm.values, train.df))}
\NormalTok{valid.norm.df }\OtherTok{\textless{}{-}} \FunctionTok{as.data.frame}\NormalTok{(}\FunctionTok{predict}\NormalTok{(norm.values, valid.df))}
\NormalTok{housing.norm.df }\OtherTok{\textless{}{-}} \FunctionTok{as.data.frame}\NormalTok{(}\FunctionTok{predict}\NormalTok{(norm.values, housing.df))}

\NormalTok{accuracy.df }\OtherTok{\textless{}{-}} \FunctionTok{data.frame}\NormalTok{(}\AttributeTok{k =} \FunctionTok{seq}\NormalTok{(}\DecValTok{1}\NormalTok{, }\DecValTok{5}\NormalTok{, }\DecValTok{1}\NormalTok{), }\AttributeTok{RMSE =} \FunctionTok{rep}\NormalTok{(}\DecValTok{0}\NormalTok{, }\DecValTok{5}\NormalTok{))}

\ControlFlowTok{for}\NormalTok{(i }\ControlFlowTok{in} \DecValTok{1}\SpecialCharTok{:}\DecValTok{5}\NormalTok{)\{}
\NormalTok{  knn.pred}\OtherTok{\textless{}{-}}\NormalTok{class}\SpecialCharTok{::}\FunctionTok{knn}\NormalTok{(}\AttributeTok{train =}\NormalTok{ train.norm.df[,}\SpecialCharTok{{-}}\DecValTok{13}\NormalTok{],                          }
                       \AttributeTok{test =}\NormalTok{ valid.norm.df[,}\SpecialCharTok{{-}}\DecValTok{13}\NormalTok{],                          }
                       \AttributeTok{cl =}\NormalTok{ train.df[,}\DecValTok{13}\NormalTok{], }\AttributeTok{k =}\NormalTok{ i)}
\NormalTok{  accuracy.df[i,}\DecValTok{2}\NormalTok{]}\OtherTok{\textless{}{-}}\FunctionTok{RMSE}\NormalTok{(}\FunctionTok{as.numeric}\NormalTok{(}\FunctionTok{as.character}\NormalTok{(knn.pred)),valid.df[,}\DecValTok{13}\NormalTok{])}
\NormalTok{\}}

\NormalTok{accuracy.df}
\end{Highlighting}
\end{Shaded}

\begin{verbatim}
##   k     RMSE
## 1 1 4.941440
## 2 2 5.143047
## 3 3 6.191194
## 4 4 6.772547
## 5 5 6.961959
\end{verbatim}

\begin{Shaded}
\begin{Highlighting}[]
\NormalTok{new.df}\OtherTok{\textless{}{-}}\FunctionTok{data.frame}\NormalTok{(}\FloatTok{0.2}\NormalTok{,}\DecValTok{0}\NormalTok{,}\DecValTok{7}\NormalTok{,}\DecValTok{0}\NormalTok{,}\FloatTok{0.538}\NormalTok{,}\DecValTok{6}\NormalTok{,}\DecValTok{62}\NormalTok{,}\FloatTok{4.7}\NormalTok{,}\DecValTok{4}\NormalTok{,}\DecValTok{307}\NormalTok{,}\DecValTok{21}\NormalTok{,}\DecValTok{10}\NormalTok{)}
\FunctionTok{names}\NormalTok{(new.df)}\OtherTok{\textless{}{-}}\FunctionTok{names}\NormalTok{(train.norm.df)[}\SpecialCharTok{{-}}\DecValTok{13}\NormalTok{]}

\NormalTok{new.norm.values }\OtherTok{\textless{}{-}} \FunctionTok{preProcess}\NormalTok{(new.df, }\AttributeTok{method=}\FunctionTok{c}\NormalTok{(}\StringTok{"center"}\NormalTok{, }\StringTok{"scale"}\NormalTok{))}
\end{Highlighting}
\end{Shaded}

\begin{verbatim}
## Warning in preProcess.default(new.df, method = c("center", "scale")): Std.
## deviations could not be computed for: CRIM, ZN, INDUS, CHAS, NOX, RM, AGE, DIS,
## RAD, TAX, PTRATIO, LSTAT
\end{verbatim}

\begin{Shaded}
\begin{Highlighting}[]
\NormalTok{new.norm.df }\OtherTok{\textless{}{-}} \FunctionTok{predict}\NormalTok{(new.norm.values, }\AttributeTok{newdata =}\NormalTok{ new.df)}

\NormalTok{new.knn.pred }\OtherTok{\textless{}{-}}\NormalTok{ class}\SpecialCharTok{::}\FunctionTok{knn}\NormalTok{(}\AttributeTok{train =}\NormalTok{ train.norm.df[,}\SpecialCharTok{{-}}\DecValTok{13}\NormalTok{],}
                           \AttributeTok{test =}\NormalTok{ new.norm.df,}
                           \AttributeTok{cl =}\NormalTok{ train.df}\SpecialCharTok{$}\NormalTok{MEDV, }\AttributeTok{k =} \DecValTok{2}\NormalTok{)}
\NormalTok{new.knn.pred}
\end{Highlighting}
\end{Shaded}

\begin{verbatim}
## [1] 21
## 180 Levels: 5 5.6 6.3 7 7.2 7.5 8.1 8.3 8.4 8.5 8.8 9.5 9.7 10.2 10.5 ... 50
\end{verbatim}

\begin{Shaded}
\begin{Highlighting}[]
\CommentTok{\#C. The error of the training set can be seen below}
\NormalTok{new.accuracy.df}\OtherTok{\textless{}{-}}\FunctionTok{RMSE}\NormalTok{(}\FunctionTok{as.numeric}\NormalTok{(}\FunctionTok{as.character}\NormalTok{(new.knn.pred)),valid.df[,}\DecValTok{13}\NormalTok{])}
\NormalTok{new.accuracy.df}
\end{Highlighting}
\end{Shaded}

\begin{verbatim}
## [1] 9.469794
\end{verbatim}

\end{document}