Advertisement
Guest User

Untitled

a guest
Sep 16th, 2018
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
R 0.81 KB | None | 0 0
  1. make_model <- function(modelling_data_f){
  2.   train_f <- modelling_data_f[train_idx, ]
  3.   test_f <- modelling_data_f[-train_idx, ]
  4.   # extract feature names
  5.   feature_names_f <- colnames(train_f)[!colnames(train_f) %in% c('reviewid', 'score_to_predict')]
  6.   mymodel_f <- cv.glmnet(y = train_f[,'score_to_predict'],
  7.                          x = train_f[,feature_names_f],
  8.                          family = "gaussian", nfolds = 10, alpha = 1)
  9.  
  10.   # predict on the test data
  11.   pred_f <- predict(mymodel_f, s="lambda.1se", newx = test_f[,feature_names_f], type="response")
  12.   # and calculate model performance metrics
  13.   error_f <- test_f[,'score_to_predict'] - pred_f
  14.   rmse_f <- sqrt(mean(error_f^2))
  15.   print('RMSE:')
  16.   print(rmse_f)
  17.   mae_f <- mean(abs(error_f))
  18.   print('MAE:')
  19.   print(mae_f)
  20.   return(mymodel_f)
  21. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement