在R中定义线性模型时,对比度出错 [英] Error in contrasts when defining a linear model in R

查看:6622
本文介绍了在R中定义线性模型时,对比度出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我试图在R中定义我的线性模型如下:

When I try to define my linear model in R as follows:

lm1 <- lm(predictorvariable ~ x1+x2+x3, data=dataframe.df)

我收到以下错误信息:

Error in `contrasts<-`(`*tmp*`, value = contr.funs[1 + isOF[nn]]) : 
contrasts can be applied only to factors with 2 or more levels 

这个还是修复呢?

推荐答案

如果你的自变量(RHS变量)是一个因子或字符只取一个值,则发生该类型的错误。

If your independent variable (RHS variable) is a factor or a character taking only one value then that type of error occurs.

示例:R中的iris数据

Example: iris data in R

model1<-lm(Sepal.Length~Sepal.Width+Species,data=iris)
> model1

Call:
lm(formula = Sepal.Length ~ Sepal.Width + Species, data = iris)

Coefficients:
      (Intercept)        Sepal.Width  Speciesversicolor   Speciesvirginica  
           2.2514             0.8036             1.4587             1.9468  

现在,如果您的数据只包含一种:

Now, if your data consists of only one species:

model1<-lm(Sepal.Length~Sepal.Width+Species,data=iris[iris$Species=="setosa",])
Error in `contrasts<-`(`*tmp*`, value = contr.funs[1 + isOF[nn]]) : 
  contrasts can be applied only to factors with 2 or more levels

如果变量是数字(Sepal.Width)值说3,那么模型运行,但你将得到NA作为该变量的系数,如下:

If the variable is numeric (Sepal.Width) but taking only a single value say 3, then the model runs but you will get NA as coefficient of that variable as follows:

model2<-lm(Sepal.Length~Sepal.Width+Species,data=iris[iris$Sepal.Width==3,])
> model2

Call:
lm(formula = Sepal.Length ~ Sepal.Width + Species, data = iris[iris$Sepal.Width == 
    3, ])

Coefficients:
      (Intercept)        Sepal.Width  Speciesversicolor   Speciesvirginica  
            4.700                 NA              1.250              2.017

解决方案:只有一个值的因变量没有足够的变化。所以,你需要删除该变量,不管是数字还是字符或因子变量。

Solution: There is no enough variation in dependent variable with only one value. So, you need to drop that variable, irrespective of whether that is numeric or character or factor variable.

根据评论进行更新:由于您知道错误只会发生在factor / character,因此您只能关注这些错误,这些因子变量的级别长度为1(DROP)或大于1(NODROP)。

Updated as per comments: Since you know that the error will only occur with factor/character, you can focus only on those and see whether the length of levels of those factor variables is 1 (DROP) or greater than 1 (NODROP).

要查看该变量是否是因子,请使用以下代码:

To see, whether the variable is a factor or not, use the following code:

l<-sapply(iris,function(x)is.factor(x))
>l
Sepal.Length  Sepal.Width Petal.Length  Petal.Width      Species 
       FALSE        FALSE        FALSE        FALSE         TRUE 

那么你只能得到因子变量的数据框

Then you can get the data frame of factor variables only

m<-iris[,names(which(l=="TRUE"))]

现在,找到因子变量的级别数,如果这是您需要删除的级别。

Now, find the number of levels of factor variables, if this is one you need to drop that

ifelse(n<-sapply(m,function(x)length(levels(x)))==1,"DROP","NODROP")

注意:如果因子变量的级别只有一个,那么就是变量,你必须删除。

Note: If the levels of factor variable is only one then that is the variable, you have to drop.

这篇关于在R中定义线性模型时,对比度出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆