岭/套索回归中h(simpleError(msg,call))中的错误

岭/套索回归中h(simpleError(msg,call))中的错误,r,machine-learning,R,Machine Learning,我试图用glmnet和onehot包运行ridge/lasso,但出现了一个错误 library(glmnet) library(onehot) set.seed(123) Sample <- HouseData[1:1460, ] smp_size <- floor(0.5 * nrow(Sample)) train_ind <- sample(seq_len(nrow(Sample)), size = smp_size) train <- Sample[trai

我试图用
glmnet
onehot
包运行ridge/lasso,但出现了一个错误

library(glmnet)
library(onehot)
set.seed(123)

Sample <- HouseData[1:1460, ]  
smp_size <- floor(0.5 * nrow(Sample))
train_ind <- sample(seq_len(nrow(Sample)), size = smp_size)
train <- Sample[train_ind, ]
test <- Sample[-train_ind, ]

############Ridge & Lasso Regressions ################

# Define the response for the training + test set
y_train <- train$SalePrice
y_test <- test$SalePrice

# Define the x training and test
x_train <- train[,!names(train)=="SalePrice"]
x_test <- test[,!names(train)=="SalePrice"]
str(y_train)

## encoding information for training set 
x_train_encoded_data_info <- onehot(x_train,stringsAsFactors = TRUE, max_levels = 50)
x_train_matrix <- (predict(x_train_encoded_data_info,x_train)) 
x_train_matrix <- as.matrix(x_train_matrix)

# create encoding information for x test
x_test_encoded_data_info <- onehot(x_test,stringsAsFactors = TRUE, max_levels = 50)
x_test_matrix <- (predict(x_test_encoded_data_info,x_test)) 
str(x_train_matrix)

###Calculate best lambda 
cv.out <- cv.glmnet(x_train_matrix, y_train,
                    alpha = 0, nlambda = 100,
                    lambda.min.ratio = 0.0001)

best.lambda <- cv.out$lambda.min
best.lambda
model <- glmnet(x_train_matrix, y_train, alpha = 0, lambda = best.lambda)
results_ridge <- predict(model,newx=x_test_matrix)
库(glmnet)
图书馆(onehot)
种子集(123)

示例调试特定错误很困难,因为不完全清楚代码中的
onehot
函数来自何处;它不存在于base
R
glmnet
包中

也就是说,我建议使用旧的内置备用函数
model.matrix
(或者它的稀疏同类
sparse.model.matrix
,如果您有更大的数据集),来创建
glmnet
x
参数<代码>模型.矩阵
将自动为您提供一个热编码因子或分类变量。它需要一个模型公式作为输入,您可以从数据集创建它,如下所示

# create the model formula
y_variable <- "SalePrice"
model_formula <- as.formula(paste(y_variable, "~",
                                  paste(names(train)[names(train) != y_variable], collapse = "+"))) 
# test & train matrices
x_train_matrix <- model.matrix(model_formula, data = train)[, -1]
x_test_matrix <- model.matrix(model_formula, data = test)[, -1]

###Calculate best lambda 
cv.out <- cv.glmnet(x_train_matrix, y_train,
                    alpha = 0, nlambda = 100,
                    lambda.min.ratio = 0.0001)

欢迎来到堆栈溢出。如果您还没有,请查看该页面,并查看是否可以提供。对不起,我忘了将该库添加到其库(onehot)
## option 2: use glmnet built in function to create x matrices
x_matrices <- glmnet::makeX(train = train[, !names(train) == "SalePrice"],
                            test = test[, !names(test) == "SalePrice"])

###Calculate best lambda 
cv.out <- cv.glmnet(x_matrices$x, y_train,
                    alpha = 0, nlambda = 100,
                    lambda.min.ratio = 0.0001)