glmnet

Comparing the GLMNET output of R with Python using LogisticRegression()

廉价感情. 提交于 2021-01-29 20:46:50
问题 I am using Logistic Regression with the L1 norm (LASSO). I have opted to used the glmnet package in R and the LogisticRegression() from the sklearn.linear_model in python . From my understanding this should give the same results however they are not. Note that I did not scale my data. For python I have used the below link as a reference: https://chrisalbon.com/machine_learning/logistic_regression/logistic_regression_with_l1_regularization/ and for R I have used the below link: http://www

Discripencies in variable importance calculation for glmnet model in R

我们两清 提交于 2021-01-29 12:41:20
问题 I want to calculate variable importance for glmnet model in R. I am using glmnet package for fitting the elastic net model like library(glmnet) library(caret) library(vip) data_y <- as.vector(mtcars$mpg) data_x <- as.matrix(mtcars[-1]) fit.glmnet <- glmnet(data_x, data_y, family="gaussian") set.seed(123) cvfit.glmnet = cv.glmnet(data_x, data_y, standardize=T) cvfit.glmnet$lambda.min coef(cvfit.glmnet, s = "lambda.min") Then I have used vip package for variable importance as #Using vip package

Error in model.frame.default(object, data, xlev = xlev) : object is not a matrix

▼魔方 西西 提交于 2021-01-29 02:28:53
问题 3 days old to R and can't figure out what I'm doing wrong. I'm trying to send some columns with two way interactions into a glmnet cox model. I have some data.frame() called dtable Edit to make the code reproducible xs<-c("Col1", "Col2", "Col3") v<-c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, NA, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, NA, 25, 26, 27, 28, 29, 30) df<-data.frame(matrix(v,ncol=3)) dm<-as.matrix(df) dm<-matrix(dm[complete.cases(dm)], ncol=3) colnames(dm)<-xs dfdata<-data.frame(dm) f

R coefficients of glmnet::cvfit

天大地大妈咪最大 提交于 2021-01-28 09:58:43
问题 As far as I am concerned, cvfit does a K fold cross validation, which means that in each time, it separates all the data into training & validation set. For every fixed lambda, first it uses training data to get a coefficient vector. Then implements this constructed model to predict on the validation set to get the error. Hence, for K fold CV, it has k coefficient vectors (each is generated from a training set). So what does coef(cvfit) get? Here is an example: x <- iris[1:100,1:4] y <- iris

How to specify log link in glmnet?

允我心安 提交于 2020-06-27 07:30:27
问题 I'm running an elastic net on a generalized linear model with the glmnet and caret packages in R. My response variable is cost (where cost > $0) and hence I'd like to specify a Gaussian family with a log link for my GLM. However glmnet doesn't seem to allow me to specify (link="log") as follows: > lasso_fit <- glmnet(x, y, alpha=1, family="gaussian"(link="log"), lambda.min.ratio=.001) I've tried different variants, with and without quotations, but no luck. The glmnet documentation doesn't

How to make all interactions before using glmnet

孤者浪人 提交于 2020-05-09 18:46:30
问题 I have an x-matrix of 8 columns. I want to run glmnet to do a lasso regression. I know I need to call: glmnet(x, y, family = "binomial", ...). However, how do I get x to consider all one way interactions as well? Do I have to manually remake the data frame: if so, is there an easier way? I suppose I was hoping to do something using an R formula. 回答1: Yes, there is a convenient way for that. Two steps in it are important. library(glmnet) # Sample data data <- data.frame(matrix(rnorm(9 * 10),

How to make all interactions before using glmnet

只愿长相守 提交于 2020-05-09 18:46:26
问题 I have an x-matrix of 8 columns. I want to run glmnet to do a lasso regression. I know I need to call: glmnet(x, y, family = "binomial", ...). However, how do I get x to consider all one way interactions as well? Do I have to manually remake the data frame: if so, is there an easier way? I suppose I was hoping to do something using an R formula. 回答1: Yes, there is a convenient way for that. Two steps in it are important. library(glmnet) # Sample data data <- data.frame(matrix(rnorm(9 * 10),