2015-11-07 51 views
2

我的示例数据集:线性disriminant功能错误 - 参数必须是相同的长度

year <- c("2002","2002","2002","2004","2005","2005","2005","2006", "2006") 
FA1 <- c(0.7975030, 1.5032768, 0.8805000, 1.0505961, 1.1379715, 1.1334510, 1.1359434, 0.9614926, 1.2631387) 
FA2 <- c(0.7930153, 1.2862355, 0.5633592, 1.0396431, 0.9446277, 1.1944455, 1.086171, 0.767955, 1.2385361) 
FA3 <- c(-0.7825210, 0.56415672, -0.9294417, 0.21485071, -0.447953,0.037978, 0.038363, -0.495383, 0.509704) 
FA4 <- c(0.38829957,0.34638035,-0.06783007, 0.505020, 0.3158221,0.55505411, 0.42822783, 0.36399347, 0.51352115) 
df <- data.frame(year,FA1,FA2,FA3,FA4) 

我再选择我要使用的数据和运行一个DFA

library(magrittr) 
library(DiscriMiner) 
yeardf <- df[df$year %in% c(2002, 2005, 2006),] 
yeardfd <- linDA(yeardf[,2:4],yeardf$year, validation = "crossval") 

但现在我得到一个错误告诉我参数是不同的长度。

"Error in table(original = y[test], predicted = pred_class) : 
all arguments must have the same length" 

我看着

length(yeardf$year) 
dim(yeardf) 

而且看起来他们是相同的。 我也检查拼写错误,因为这有时会导致此错误。

  • 以下是答案。
    建议的答案适用于我的示例数据(这确实给了我同样的错误),但我不能完全使它在我的真实代码上工作。

我第一次转变应用于选定列在我的data.frame。然后,我结合了我想作为团体使用的变量转化列在我的DFA

library(robCompositions) 
tFA19 <- cenLR(fadata.PIZ[names(FA19)])[1] 
tFA19 <- cbind(fadata.PIZ[1:16],tFA19) 

所以,我认为我创建这个data.frame方式必须导致我的错误。我试图在我的cbind声明中插入stringsAsFactors,但没有运气。

+0

看看'STR(tFA19)'。如果你看到那里的因素,它将无法工作。 –

+0

啊!谢谢!!我已将因素转换为字符,现在一切都很好。 – heatherr

回答

1

你需要,stringsAsFactors = FALSEdata.frame

year <- c("2002","2002","2002","2004","2005","2005","2005","2006", "2006") 
FA1 <- c(0.7975030, 1.5032768, 0.8805000, 1.0505961, 1.1379715, 1.1334510, 1.1359434, 0.9614926, 1.2631387) 
FA2 <- c(0.7930153, 1.2862355, 0.5633592, 1.0396431, 0.9446277, 1.1944455, 1.086171, 0.767955, 1.2385361) 
FA3 <- c(-0.7825210, 0.56415672, -0.9294417, 0.21485071, -0.447953,0.037978, 0.038363, -0.495383, 0.509704) 
FA4 <- c(0.38829957,0.34638035,-0.06783007, 0.505020, 0.3158221,0.55505411, 0.42822783, 0.36399347, 0.51352115) 
df <- data.frame(year,FA1,FA2,FA3,FA4,stringsAsFactors = FALSE) 


library(magrittr) 
library(DiscriMiner) 
yeardf <- df[df$year %in% c(2002, 2005, 2006),] 
yeardfd <- linDA(yeardf[,2:4],yeardf$year, validation = "crossval") 
yeardfd 

Linear Discriminant Analysis 
------------------------------------------- 
$functions  discrimination functions 
$confusion  confusion matrix 
$scores   discriminant scores 
$classification assigned class 
$error_rate  error rate 
------------------------------------------- 

$functions 
      2002 2005 2006 
constant -345 -371 -305 
FA1  228 231 213 
... 
+0

谢谢!我的真实数据有点不同,虽然我可以用上面的示例数据重现错误,但修复程序对我来说并不适用。我会在我的原始文本中添加一些注释,以便显示我正在使用的代码 – heatherr

相关问题