R glmnet : (list) object cannot be coerced to type 'double'
cv.glmnet
expects a matrix of predictors, not a data frame. Generally you can obtain this via
X <- model.matrix(<formula>, data=<data>)
but in your case, you can probably get there more easily with
X <- as.matrix(t2[,-c(1,2,7,12)])
since you don't appear to have any factor variables or other issues that might complicate matters.
Since this answer is getting plenty of hits: the glmnetUtils package provides a formula-based interface to glmnet, like that used for most R modelling functions. It includes methods for glmnet
and cv.glmnet
, as well as a new cva.glmnet
function to do crossvalidation for both alpha and lambda.
The above would become
cv.glmnet(X2 ~ ., data=t2[-1], family="multinomial")
NA's are handled automatically, so you don't have to exclude columns with missing values.
Error: (list) object cannot be coerced to type 'double' with multiple variables
We can do:
res<-sapply(iris[,-5], as.numeric)
attr(res,"dimnames") <- NULL
Or as @markus suggests simply:
unname(as.matrix(iris[,1:4]))
Result:
[,1] [,2] [,3] [,4]
[1,] 5.1 3.5 1.4 0.2
[2,] 4.9 3.0 1.4 0.2
[3,] 4.7 3.2 1.3 0.2
[4,] 4.6 3.1 1.5 0.2
In the R, meet problem 'list' object cannot be coerced to type 'double' when using 'by' function
by
works on the data.frame that is split by mtcars$am
, so your function needs to work on a data.frame instead of a vector, so example below I use sapply to go through each column and calculate mean and sd:
by(mtcars[vars],mtcars$am, FUN = function(u)sapply(u,dstats))
mtcars$am: 0
mpg hp wt
mean 17.147368 160.2632 3.7688947
sd 3.833966 53.9082 0.7774001
------------------------------------------------------------
mtcars$am: 1
mpg hp wt
mean 24.392308 126.84615 2.4110000
sd 6.166504 84.06232 0.6169816
It's not so easy to get back the same format as your output for aggregate which is a nested data.frame:
res = aggregate(mtcars[vars],by=list(am=mtcars$am), FUN = dstats)
class(res$hp)
[1] "matrix"
# to get mean hp for different groups, you have to do:
res$hp[,"mean"]
[1] 160.2632 126.8462
To get something similar with by, it's a bit more elaborate:
res = do.call(rbind,
by(mtcars[vars],mtcars$am,function(i){
unlist(lapply(i,dstats))
})
)
res = data.frame(am=levels(factor(mtcars$am)),res)
am mpg.mean mpg.sd hp.mean hp.sd wt.mean wt.sd
0 0 17.14737 3.833966 160.2632 53.90820 3.768895 0.7774001
1 1 24.39231 6.166504 126.8462 84.06232 2.411000 0.6169816
How to solve error: Error in storage.mode(x) - double : 'list' object cannot be coerced to type 'double' and get results
You should coerce the object to a matrix or a data frame.
From the kmeans()
documentation:
x = numeric matrix of data, or an object that can be coerced to
such a matrix (such as a numeric vector or a data frame with
all numeric columns)
somClusters2 <- kmeans(data.frame(som_model2$codes), centers = 6)
somClusters2
K-means clustering with 6 clusters of sizes 14, 42, 11, 3, 3, 27
Cluster means:
MONEY VISIT CROSS API
1 -0.4217639 -0.5810061 -0.8014610 1.1764434
2 -0.3080303 -0.3977217 -0.4555428 -0.1649521
3 1.1239411 1.6704112 1.6129638 -0.7312019
4 -0.4606414 -0.6480549 -0.9548480 3.5595079
5 5.1169992 3.9174431 2.7584212 -0.8306366
6 0.1443774 0.2317915 0.5778101 -0.5416495
Error in logistic regression in R: 'list' object cannot be coerced to type 'double'
You can't compare model1
with 0.5
This is model1 structure:
model1
Call: glm(formula = id ~ speed + dist, family = "binomial", data = cars)
Coefficients:
(Intercept) speed dist
-1158.863 73.588 1.366
Degrees of Freedom: 49 Total (i.e. Null); 47 Residual
Null Deviance: 69.31
Residual Deviance: 2.932e-08 AIC: 6
You have to pass new data to the model and than compare the prediction (using function predict
) value with 0.5
Related Topics
Reordering Columns in a Large Dataframe
To Find Whether a Column Exists in Data Frame or Not
How to Apply Function Over Each Matrix Element's Indices
Ggplot2 Legend to Bottom and Horizontal
Change the Color of Action Button in Shiny
Label Minimum and Maximum of Scale Fill Gradient Legend with Text: Ggplot2
How to Identify the Distribution of the Given Data Using R
Install a Local R Package with Dependencies from Cran Mirror
How to Make Object Created Within Function Usable Outside
Extract Random Effect Variances from Lme4 Mer Model Object
R: Legend with Points and Lines Being Different Colors (For the Same Legend Item)
Remove Strip Background Keep Panel Border
Remove Spacing Around Plotting Area in R
Compute Rolling Sum by Id Variables, with Missing Timepoints
How to Get Rows, by Group, of Data Frame with Earliest Timestamp
How to Specify (Non-R) Library Path for Dynamic Library Loading in R