AM Modeling
Hw1
Gary
5/23/2021
Question 2.1
Digital Marketing in Ecommerce is a space where classification plays a huge role. For example, using website
traffic analytics data to determine if a consumer will add an item to the shopping cart. Some potential
predictos include: - New vs returner customer - Types of Phone - Number of pages visited - Time spent
on the site - Landing page
Question 2.2.1
library(kernlab)
# Import data
df =read.delim("credit_card_data.txt",header=F)
# Transform data into a matrix
data = as.matrix(df)
# Define a function that takes lambda as an argument and return the accuracy
# of the model
svm_with_c = fu n ction (c){
model = ksvm(data[,1:10],as.factor(data[,11]),type="C-svc",kernel="vanilladot",C=c,scaled=TRUE)
pred = predict(model,data[,1:10])
sum(pred == data[,11]) / nrow(data)
}
print(svm_with_c(10))
## Setting default kernel parameters
## [1] 0.8639144
print(svm_with_c(50))
## Setting default kernel parameters
## [1] 0.8639144
print(svm_with_c(1000))
## Setting default kernel parameters
## [1] 0.8623853
1
about:blan 1/
k 6
, 10/15/24, 9:15 Homew ork 1 Intro to Analytical
AM Modeling
# Seems like 50 is a good value for C
model = ksvm(data[,1:10],as.factor(data[,11]),type="C-svc",kernel="vanilladot",C=50,scaled=TRUE)
## Setting default kernel parameters
# calculate a1...am
a = colSums(model@xmatrix[[1]]*model@coef[[1]])
print(a)
## V1 V2 V3 V4 V5
## -0.0010523630 -0.0012025131 -0.0015382662 0.0028761998 1.0052764944
## V6 V7 V8 V9 V10
## -0.0024958086 0.0001810245 -0.0006514829 -0.0013757143 0.1064002847
# calculate a0
a0 = model@b
print(a0)
## [1] -0.08147145
# Get the prediction
pred = predict(model,data[,1:10])
# Calculate the accuracy
print(sum(pred == data[,11]) / nrow(data))
## [1] 0.8639144
2.2.3
# Import kknn package
library('kknn')
# Function that returns the model accuracy for a given k
knn_accuracy_given_k = fu nction (k){
all_pred= c()
fo r (i in 1:nrow(df)){
knn_model = kknn(V11~V1+V2+V3+V4+V5+V6+V7+V8+V9+V10,df[-i,],df[i,],k=k,scale=TRUE)
predicted = fitted(knn_model)
if(predicted>=0.5){
rounded = 1
} else { rounded = 0}
all_pred = c(all_pred, rounded)
}
2
about:blan 2/
k 6