当前位置:网站首页>R language mice package error in terms Formula (TMP, simplify = true): the model formula in extractvars is incorrect

R language mice package error in terms Formula (TMP, simplify = true): the model formula in extractvars is incorrect

2022-06-11 17:32:00 High cold charging

ExtractVars The model formula in is wrong

mice

mice It can perform multiple imputation for packets containing missing values , And this so-called multiple interpolation method , You need to keep fitting , That is to say, we need ——formula, This error is due to formula There's a problem in

Error reason

Look at an example , I thought it was outrageous

library(mice)
A <- as.data.frame(matrix(0, 6, 7))
A[1, 2] <- NA
mice(A, m=5)

There is no problem with the operation at this time , But if you continue to run

colnames(A) <- c(1:7)
mice(A, m=5)

This will be the result

iter imp variable
  1   1  1Error in terms.formula(tmp, simplify = TRUE) : 
  ExtractVars The model formula in is wrong 

Yes , Listing is not good , My guess is that the column names composed of numbers are recognized as numeric Type, not character type . But if you are character The column name of the type , It may not run successfully , The reason is —— Column name is too long !!
This is my mistake in doing a big homework , Because I'm doing biometrics , So the list is very long , Among them the first 3 The column name of the column is like this :[1] “cna_ADAMTS19-AS1”, Results run mice The times mistakenly said that they couldn't find cna_ADAMTS19, In the formation of formula I cut off the list of names , I was shocked .

Fast solutions

If there is one now 1000*100 Data packets of data, And unfortunately , Its column names are not mice() Accept , The processing is as follows :

temp <- matrix(0, 1, dim(data)[2])
temp <- data.frame(temp)
#  utilize matrix turn data.frame Default column name for : "V1","V2",···
temp <- colnames(temp)
#  Keep the original column name , You can take it back after processing 
origin_col <- colnames(data)
colnames(data) <- temp
#  At this point, we can do mice 了 
mice(data, m=5)
原网站

版权声明
本文为[High cold charging]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/162/202206111724003172.html