首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >SensoMineR面板错误在对比中,对比只能适用于2级或2级以上的因素

SensoMineR面板错误在对比中,对比只能适用于2级或2级以上的因素
EN

Stack Overflow用户
提问于 2022-10-08 17:58:34
回答 1查看 21关注 0票数 0

我试图使用SensoMineR包中的panelperf函数运行以下代码:

panelperf<-panelperf(data,formul="~Product+Panelist+rep+Product:Panelist+Product:rep+Panelist:rep", firstvar=4)

我的数据框架包括:第1栏:小组成员,充满了小组成员的名字,第2栏:产品:填充了产品的名称,第3栏: rep,产品的复制,第4栏,直到被测量的变量(轻水果,黑果,酒精,酸味等)--我所有的变量都是dbl。

但是,在运行该函数时会出现以下错误:Error in `contrasts<-`(`*tmp*`, value = contr.funs[1 + isOF[nn]]) : contrasts can be applied only to factors with 2 or more levels In addition: Warning messages: 1: In xtfrm.data.frame(x) : cannot xtfrm data frames 2: In xtfrm.data.frame(x) : cannot xtfrm data frames 3: In xtfrm.data.frame(x) : cannot xtfrm data frames

sapply(lapply(data, unique), length)

代码语言:javascript
运行
复制
   Product     Panelist          rep   Lightfruit    Darkfruit    Applepear       Citrus     DryFruit        Nutty   Vegetables       Earthy       Floral 
        13           12            2           87           83           72           67           76           69           76           67           66 
     Woddy          hgt     Chemical    Chocolate        Honey       Cheesy      Alcohol Overallaroma   Astringent         Sour          Hot    Viscocity 
        62           64           80           57           65           69           86           85           88           85           85           83 
     Sweet       Bitter 
        87           86 
So none of the variables has only 2 levels as the error suggests
I have been reading answers about this error, but either the answers dont apply to my case or, as a non very experienced R user, I do not follow what they suggest. 

I would appreciate your help a lot!
Thank you!

And let me know if you need more information!
EN

回答 1

Stack Overflow用户

发布于 2022-10-08 18:01:33

检查因子级别的代码可以是

代码语言:javascript
运行
复制
i1 <- sapply(data, \(x) is.factor(x) && nlevels(x) > 1)
i2 <- !sapply(data, is.factor)
data1 <- data[i1|i2]

下面是一个例子,说明了这个问题??当数据的nlevel的因子大于1时,它可以工作。

代码语言:javascript
运行
复制
library(SensoMineR)
data(chocolates)
res <- panelperf(sensochoc, firstvar = 5, formul = "~Product+Panelist+
    Session+Product:Panelist+Session:Product+Panelist:Session")
> str(res)
List of 4
 $ p.value    : num [1:14, 1:6] 8.85e-14 6.44e-08 1.75e-28 3.74e-40 1.18e-22 ...
  ..- attr(*, "dimnames")=List of 2
  .. ..$ : chr [1:14] "CocoaA" "MilkA" "CocoaF" "MilkF" ...
  .. ..$ : chr [1:6] "Product         " "Panelist        " "Session         " "Product:Panelist" ...
 $ variability: num [1:14, 1:6] 0.139 0.103 0.397 0.532 0.267 ...
  ..- attr(*, "dimnames")=List of 2
  .. ..$ : chr [1:14] "CocoaA" "MilkA" "CocoaF" "MilkF" ...
  .. ..$ : chr [1:6] "Product         " "Panelist        " "Session         " "Product:Panelist" ...
 $ res        : num [1:14, 1] 1.87 1.89 1.41 1.47 1.63 ...
  ..- attr(*, "dimnames")=List of 2
  .. ..$ : chr [1:14] "CocoaA" "MilkA" "CocoaF" "MilkF" ...
  .. ..$ : chr "stdev residual"
 $ r2         : num [1:14, 1] 0.673 0.761 0.846 0.882 0.862 ...
  ..- attr(*, "dimnames")=List of 2
  .. ..$ : chr [1:14] "CocoaA" "MilkA" "CocoaF" "MilkF" ...
  .. ..$ : chr "r2"
 > str(sensochoc)
    'data.frame':   348 obs. of  18 variables:
     $ Panelist   : Factor w/ 29 levels "1","2","3","4",..: 1 1 1 1 1 1 2 2 2 2 ...
     $ Session    : Factor w/ 2 levels "1","2": 1 1 1 1 1 1 1 1 1 1 ...
     $ Rank       : Factor w/ 6 levels "1","2","3","4",..: 1 6 3 5 2 4 1 4 3 5 ...
     $ Product    : Factor w/ 6 levels "choc1","choc2",..: 6 3 2 1 4 5 4 3 6 2 ...
     $ CocoaA     : int  7 6 8 7 8 7 6 4 5 5 ...
     $ MilkA      : int  6 7 6 8 5 5 1 2 1 2 ...
     $ CocoaF     : int  6 2 5 8 4 3 8 3 8 8 ...
     $ MilkF      : int  5 7 4 3 4 5 1 4 1 1 ...
     $ Caramel    : int  5 8 7 3 4 6 0 0 0 0 ...
     $ Vanilla    : int  3 4 4 2 4 2 0 0 0 0 ...
     $ Sweetness  : int  7 7 5 4 5 5 1 5 1 0 ...
     $ Acidity    : int  2 2 5 7 6 4 0 0 0 0 ...
     $ Bitterness : int  4 2 6 8 6 7 3 0 3 6 ...
     $ Astringency: int  5 3 6 6 4 4 0 0 0 0 ...
     $ Crunchy    : int  8 3 7 3 6 6 8 4 6 8 ...
     $ Melting    : int  3 8 5 2 3 6 5 8 2 2 ...
     $ Sticky     : int  4 6 4 3 7 4 0 3 1 4 ...
     $ Granular   : int  3 5 3 5 3 7 0 1 1 0 ...

如果我们将“会话”级别2更改为NA (只有2个级别),则会显示错误。

代码语言:javascript
运行
复制
levels(sensochoc$Session)[2] <- NA

res1 <- panelperf(sensochoc, firstvar = 5, formul = "~Product+Panelist+
        Session+Product:Panelist+Session:Product+Panelist:Session")
 Error in `contrasts<-`(`*tmp*`, value = contr.funs[1 + isOF[nn]]) : 
   contrasts can be applied only to factors with 2 or more levels   

使用OP的代码,它仍然显示数据具有大于1的级别,因为如果存在,unique也会返回NA,因此length将包含NA元素,这里总共是2

代码语言:javascript
运行
复制
> sapply(lapply(sensochoc, unique), length)
   Panelist     Session        Rank     Product      CocoaA       MilkA      CocoaF       MilkF     Caramel     Vanilla   Sweetness     Acidity 
         29           2           6           6          11          11          11          11          11          10          11          11 
 Bitterness Astringency     Crunchy     Melting      Sticky    Granular 
         11          11          11          11          11          11 

与本文中的特定代码一样,nlevels只删除非NA级别的计数返回NA

代码语言:javascript
运行
复制
i1 <- sapply(sensochoc, \(x) is.factor(x) && nlevels(x) > 1)
i2 <- !sapply(sensochoc, is.factor)
names(sensochoc)[i1]
[1] "Panelist" "Rank"     "Product" 
names(sensochoc)[sapply(sensochoc, is.factor)]
[1] "Panelist" "Session"  "Rank"     "Product" 

省略了Session。我们可能需要修改公式,以省略包含Session的条件

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/73999342

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档