简体   繁体   English

如何修复 R 中模拟中的“colnames”错误?

[英]How to Fix 'colnames" error in a simulation in R?

Using a simulation in R, I am trying to test consistency of selection criterion, but I keep getting the following error:在 R 中使用模拟,我试图测试选择标准的一致性,但我不断收到以下错误:

"Error in colnames<- ( *tmp* , value = make.names(np)): attempt to set 'colnames' on an object with less than two dimensions" colnames<- ( *tmp* , value = make.names(np)) 中的错误:尝试在小于二维的对象上设置‘colnames’”

How do I fix this error?我该如何解决这个错误?

set.seed(1)
X <- rnorm(100)
noise <- rnorm(100)

Y <- 3 + 1*X + 4*X + 6*X + noise

require(leaps)
df <- data.frame(Y, X)
fit <- regsubsets(Y~X, data = df, nvmax = 10)

fit_summary <- summary(fit)

require(tidyverse);require(ggplot2);require(ggthemes);

data_frame(Cp = fit_summary$cp,
           BIC = fit_summary$bic,
           AdjR2 = fit_summary$adjr2) %>%
    mutate(id = row_number()) %>%
    gather(value_type, value, -id) %>%
    ggplot(aes(id, value, col = value_type)) +
    geom_line() + geom_point() + ylab('') + xlab('Number of Variables Used') +
    facet_wrap(~ value_type, scales = 'free') +
    theme_tufte() + scale_x_continuous(breaks = 1:10)

You have a design matrix with only one column, selection doesn't really make sense in that case, does it?你有一个只有一列的设计矩阵,在这种情况下选择没有意义,是吗? If you add another column, the code starts working.如果您添加另一列,代码将开始工作。

set.seed(1)
X <- rnorm(100)
X2 <- rnorm(100)
noise <- rnorm(100)
Y <- 3 + 1*X + 4*X2 + noise

df <- data.frame(Y, X, X2)
fit <- leaps::regsubsets(Y~., data = df, nvmax = 10)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM