为什么lm()在R中不显示某些输出?

我想知道为什么lm()5 coefs not defined because of singularities,然后在摘要输出中为5个系数给出所有NA

请注意,我所有的预测变量都是绝对的。

我在这5个系数或代码上的数据有问题吗?我该如何解决?

d <- read.csv("https://raw.githubusercontent.com/rnorouzian/m/master/v.csv",h = T) # Data

nms <- c("Age","genre","Length","cf.training","error.type","cf.scope","cf.type","cf.revision")

d[nms] <- lapply(d[nms],as.factor) # make factor

vv <- lm(dint~Age+genre+Length+cf.training+error.type+cf.scope+cf.type+cf.revision,data = d)

summary(vv) 

输出的前6行:

     Coefficients: (5 not defined because of singularities)
              Estimate Std. Error t value Pr(>|t|)    
(Intercept)    0.17835    0.63573   0.281 0.779330    
Age1          -0.04576    0.86803  -0.053 0.958010    
Age2           0.46431    0.87686   0.530 0.596990    
Age99         -1.64099    1.04830  -1.565 0.118949    
genre2         1.57015    0.55699   2.819 0.005263 ** 
genre4              NA         NA      NA       NA    ## For example here is all `NA`s? there are 4 more !
zhry0203203 回答:为什么lm()在R中不显示某些输出?

在这种情况下,您可以在R中使用“ olsrr”软件包进行逐步回归分析。我为您提供了一个示例代码,以在R中进行逐步回归分析

library("olsrr")

#Load the data
d <- read.csv("https://raw.githubusercontent.com/rnorouzian/m/master/v.csv",h = T)

# stepwise regression 
vv <- lm(dint ~ Age + genre + Length + cf.training + error.type + cf.scope + cf.type + cf.revision,data = d)

summary(vv)  

k <- ols_step_both_p(vv,pent = 0.05,prem = 0.1)

# stepwise regression plot 
plot(k)

# final model 
k$model

It will provide you exactly same output as that of SPSS.
,

正如其他人指出的那样,问题在于您似乎具有多重共线性。另一个是您的数据集中缺少值。缺少的值可能应该被删除。至于相关变量,您应该检查数据以识别此共线性,然后将其删除。确定要删除和保留哪些变量是一个非常特定于域的主题。但是,如果您希望决定使用regularisation并拟合模型,同时保留所有变量,则可以。当n(样本数)小于p(预测数)时,这也使您可以拟合模型。

我在下面显示了代码,该代码演示了如何检查数据中的相关结构以及如何确定哪些变量之间的相关性最高(感谢this answer。我已经提供了一个拟合此类模型的示例,使用L2正则化(通常称为岭回归)。

d <- read.csv("https://raw.githubusercontent.com/rnorouzian/m/master/v.csv",h = T) # Data

nms <- c("Age","genre","Length","cf.training","error.type","cf.scope","cf.type","cf.revision")

d[nms] <- lapply(d[nms],as.factor) # make factor

vv <- lm(dint~Age+genre+Length+cf.training+error.type+cf.scope+cf.type+cf.revision,data = d)


df <- d
df[] <- lapply(df,as.numeric)
cor_mat <- cor(as.matrix(df),use = "complete.obs")

library("gplots")
heatmap.2(cor_mat,trace = "none")

## https://stackoverflow.com/questions/22282531/how-to-compute-correlations-between-all-columns-in-r-and-detect-highly-correlate
library("tibble")
library("dplyr")
library("tidyr")

d2 <- df %>% 
  as.matrix() %>%
  cor(use = "complete.obs") %>%
  ## Set diag (a vs a) to NA,then remove
  (function(x) {
    diag(x) <- NA
    x
  }) %>%
  as.data.frame %>%
  rownames_to_column(var = 'var1') %>%
  gather(var2,value,-var1) %>%
  filter(!is.na(value)) %>%
  ## Sort by decreasing absolute correlation
  arrange(-abs(value))

## 2 pairs of variables are almost exactly correlated!
head(d2)
#>         var1       var2     value
#> 1         id study.name 0.9999430
#> 2 study.name         id 0.9999430
#> 3   Location      timed 0.9994082
#> 4      timed   Location 0.9994082
#> 5        Age   ed.level 0.7425026
#> 6   ed.level        Age 0.7425026
## Remove some variables here,or maybe try regularized regression (see below)
library("glmnet")

## glmnet requires matrix input
X <- d[,c("Age","cf.revision")]
X[] <- lapply(X,as.numeric)
X <- as.matrix(X)
ind_na <- apply(X,1,function(row) any(is.na(row)))
X <- X[!ind_na,]
y <- d[!ind_na,"dint"]
glmnet <- glmnet(
    x = X,y = y,## alpha = 0 is ridge regression
    alpha = 0)

plot(glmnet)

reprex package(v0.3.0)于2019-11-08创建

本文链接:https://www.f2er.com/3136589.html

大家都在问