```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```


## Part 1: Run SVM, KNN, and Lasso
```{r}
library(glmnet) # for lasso
library(e1071) # for svm
library(class) # for knn
data(mtcars)

# SVM
model_svm = svm(mpg ~ wt + gear, mtcars, kernel="radial", gamma=1)
## Intuitively, the gamma parameter defines how far the influence of a single training example reaches, with low values meaning ‘far’ and high values meaning ‘close’. The gamma parameters can be seen as the inverse of the radius of influence of samples selected by the model as support vectors.
#predict(model_svm, newdata = )

# KNN
knn(train=mtcars[1:16, c('wt', 'mpg')], 
    cl=mtcars[1:16, 'gear'],
    test=mtcars[17:32, c('wt', 'mpg')], 
    k= 1)

# lasso
model_lasso = glmnet(x=as.matrix(mtcars[c('wt', 'gear')]), y = mtcars$mpg, 
                    alpha = 1, # =0 for ridge
                    lambda = 0.001)
summary(model_lasso)
coef(model_lasso)

cv_model_lasso = cv.glmnet(x=as.matrix(mtcars[c('wt', 'gear')]), y = mtcars$mpg, 
                    alpha = 1)
coef(cv_model_lasso, s = "lambda.min")
```

## Part 2: text preprocessing
```{r}

library(tm)
library(SnowballC) # for stemming
library(slam) # default sparse matrix format in tm
library(Matrix) # alternative sparse matrix format

doc1 = 'The first digital tape measure laser line extension is great for picking up centerlines of round objects'
doc2 = 'this was back in august 26th💁🏼‍♀️ totally forgot i had this on video & decided to share.'
doc3 = 'okay I love potatoes in all forms but there’s something special about a hasselback 34678'
doc4 = '   '
doc5 = '12345'

df = data.frame(doc = c(doc1, doc2, doc3, doc4, doc5), row.names = c(1, 2, 3, 4, 5))
print(df)


## regex to remove whitespace
df = df[-grep("^\\s+$", df$doc),] # ^ Matches the beginning of input.
print(df)

## create corpus
files = VectorSource(df)
corpus = Corpus(files)

# remove numbers, and punctuation, then stemming
corpus.clean = tm_map(corpus, content_transformer(removeNumbers))
corpus.clean$content
corpus.clean = tm_map(corpus.clean, content_transformer(removePunctuation))
corpus.clean$content
corpus.clean = tm_map(corpus.clean, stemDocument)
corpus.clean$content

# Create document-term matrix
dtm_slam = DocumentTermMatrix(corpus.clean,
                              control=list(stopwords=stopwords("english"), ## stopwords
                                           tolower=TRUE,
                                           wordLengths=c(3, Inf) ## wordlength
                                           )
                              )

drop = which(col_sums(dtm_slam>0) < 1) # drop words only appearing in a couple of documents
dtm_slam2 = dtm_slam[,-drop]

# convert to Matrix
dtm = sparseMatrix(i=dtm_slam$i,
                   j=dtm_slam$j,
                   x=dtm_slam$v
                   ,dims=c(nrow(dtm_slam), ncol(dtm_slam)))

dtm_svd = svd(dtm)
dtm_svd$u
dtm_svd$v

```
