Applying standardisation to LASSO and KNNREG in R

To my Understanding standardisation is used for robustness. However, I’m having difficulty in understanding how to apply standardisation to my data in a LASSO and KNN Regression in the cv.glmnet and knnreg functions from the glmnet and caret libraries and comparing the regression output agaisnt each other.

I first tried using standardize = TRUE in the cv.glmnet function for the LASSO regression and as I understand this standardises the data for the regression then ‘de-standardises’ the regression output.

When I then used the knnreg function I first scaled the numerical data with scale(...) and created numerical dummy variables after and then ran the knn regression. However, the output is presented still in it’s scaled form and as a result the two regressions betas and MSE cannot be compared.

Is there a way to either ‘de-scale’ the result from the knn regression or ‘re-scale’ the results from LASSO, or am I looking at this the wrong way all together?

TIA for any help.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>
##Code for CV LASSO regression
set.seed(34064064)
library(glmnet)
Hitters<-na.omit(Hitters)
x <- model.matrix(Salary~.,Hitters)[,-1]
y <- Hitters$Salary
cv<-cv.glmnet(x,y,lambda=exp(seq(-2, 4, length.out = 30)),nfolds=10,alpha=1,standardize = TRUE,type.measure = "mse")
best.lambda <- cv$lambda.min
fit <- glmnet(x, y, lambda=best.lambda, alpha=1, standardize=TRUE)
y.pred <- predict(fit, newx=x)
training.mse <- mean((y - y.pred)^2)
print(training.mse)
</code>
<code> ##Code for CV LASSO regression set.seed(34064064) library(glmnet) Hitters<-na.omit(Hitters) x <- model.matrix(Salary~.,Hitters)[,-1] y <- Hitters$Salary cv<-cv.glmnet(x,y,lambda=exp(seq(-2, 4, length.out = 30)),nfolds=10,alpha=1,standardize = TRUE,type.measure = "mse") best.lambda <- cv$lambda.min fit <- glmnet(x, y, lambda=best.lambda, alpha=1, standardize=TRUE) y.pred <- predict(fit, newx=x) training.mse <- mean((y - y.pred)^2) print(training.mse) </code>

##Code for CV LASSO regression

set.seed(34064064)

library(glmnet)
Hitters<-na.omit(Hitters)
x <- model.matrix(Salary~.,Hitters)[,-1]
y <- Hitters$Salary

cv<-cv.glmnet(x,y,lambda=exp(seq(-2, 4, length.out = 30)),nfolds=10,alpha=1,standardize = TRUE,type.measure = "mse")

best.lambda <- cv$lambda.min

fit <- glmnet(x, y, lambda=best.lambda, alpha=1, standardize=TRUE)

y.pred <- predict(fit, newx=x)

training.mse <- mean((y - y.pred)^2)

print(training.mse)

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>library(caret)
fn.split <- function(d,p=0.2) {
aux <- 1:length(d[,1])
id.test <- sort(sample(aux,size=floor(p*length(aux)),
replace=FALSE))
d.test <- d[id.test,]
d.train <- d[-id.test,]
return(list(train=d.train,test=d.test))
}
# Standardize numeric columns in train dataset
for (i in 1:ncol(train)) {
if (is.numeric(train[,i])) {
train[,i] <- (train[,i] - mean(train[,i])) / sd(train[,i])
}
}
# Standardize numeric columns in test dataset
for (i in 1:ncol(test)) {
if (is.numeric(test[,i])) {
test[,i] <- (test[,i] - mean(test[,i])) / sd(test[,i])
}
}
#Create dummy variable
train$League<-ifelse(train$League=="N",1,0)
train$NewLeague<-ifelse(train$NewLeague=="N",1,0)
train$Division<-ifelse(train$Division=="W",1,0)
test$League<-ifelse(test$League=="N",1,0)
test$NewLeague<-ifelse(test$NewLeague=="N",1,0)
test$Division<-ifelse(test$Division=="W",1,0)
knn.reg<-knnreg(Salary~.,data=train,k=20)
pred <- predict(knn.reg, newdata = test)
mse<- mean((test$Salary-pred)^2) #MSE on test data
mse
</code>
<code>library(caret) fn.split <- function(d,p=0.2) { aux <- 1:length(d[,1]) id.test <- sort(sample(aux,size=floor(p*length(aux)), replace=FALSE)) d.test <- d[id.test,] d.train <- d[-id.test,] return(list(train=d.train,test=d.test)) } # Standardize numeric columns in train dataset for (i in 1:ncol(train)) { if (is.numeric(train[,i])) { train[,i] <- (train[,i] - mean(train[,i])) / sd(train[,i]) } } # Standardize numeric columns in test dataset for (i in 1:ncol(test)) { if (is.numeric(test[,i])) { test[,i] <- (test[,i] - mean(test[,i])) / sd(test[,i]) } } #Create dummy variable train$League<-ifelse(train$League=="N",1,0) train$NewLeague<-ifelse(train$NewLeague=="N",1,0) train$Division<-ifelse(train$Division=="W",1,0) test$League<-ifelse(test$League=="N",1,0) test$NewLeague<-ifelse(test$NewLeague=="N",1,0) test$Division<-ifelse(test$Division=="W",1,0) knn.reg<-knnreg(Salary~.,data=train,k=20) pred <- predict(knn.reg, newdata = test) mse<- mean((test$Salary-pred)^2) #MSE on test data mse </code>
library(caret)

fn.split <- function(d,p=0.2) {
  aux     <- 1:length(d[,1])
  id.test <- sort(sample(aux,size=floor(p*length(aux)),
                         replace=FALSE))
  d.test  <- d[id.test,]
  d.train <- d[-id.test,]
  return(list(train=d.train,test=d.test))
}


# Standardize numeric columns in train dataset

for (i in 1:ncol(train)) {
  if (is.numeric(train[,i])) {
    train[,i] <- (train[,i] - mean(train[,i])) / sd(train[,i])
  }
}

# Standardize numeric columns in test dataset
for (i in 1:ncol(test)) {
  if (is.numeric(test[,i])) {
    test[,i] <- (test[,i] - mean(test[,i])) / sd(test[,i])
  }
}

#Create dummy variable

train$League<-ifelse(train$League=="N",1,0)
train$NewLeague<-ifelse(train$NewLeague=="N",1,0)
train$Division<-ifelse(train$Division=="W",1,0)
test$League<-ifelse(test$League=="N",1,0)
test$NewLeague<-ifelse(test$NewLeague=="N",1,0)
test$Division<-ifelse(test$Division=="W",1,0)

knn.reg<-knnreg(Salary~.,data=train,k=20)

pred <- predict(knn.reg, newdata = test)

mse<- mean((test$Salary-pred)^2) #MSE on test data

mse

New contributor

SCZZI is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật