【问题标题】:Conjoint() function in RR中的联合()函数
【发布时间】:2020-06-28 22:20:13
【问题描述】:

联合函数的输出有问题。 我有 5 个属性,每个属性有 3 个级别,但是在输出中,从第三级的第二个属性开始,结果是 NA,我不知道我能做些什么来解决这个问题。 这是我的代码和联合函数的结果(img.输出联合函数)。

你能帮帮我吗?

alimentatie<-c("mancare gatita","fast food","amandoua in mod alternativ") 

activitati<-c("regulat","de cateva ori pe luna","foarte rar")

bauturi<-c("evit cat de mult pot","oricat de des simt nevoia","ocazional")

odihna<-c("7-8 ore","4-6 ore","mai mult de 8 ore")

Recreere<-c("3-6","mai mult de 6","mai putin de 3")

factor_levels<-as.data.frame(c(alimentatie,activitati,bauturi,odihna,recreere))

colnames(factor_levels)<-"levels"

data<-expand.grid(alimentatie,activitati,bauturi,odihna,recreere)

colnames(data)<-c("Alimentatie","Activitati","Bauturi","Odihna","Recreere")


facdesign_full<-caFactorialDesign(data = data, type = "full")

encdesign_full<-caFactorialDesign(facdesign_full)


response<-read.table("cc.txt", sep="\t",header = T)
response<-as.data.frame(response)


conjoint_analysis<-Conjoint(response,encdesign_full,factor_levels)

我将5个问题的每个答案(一共59个答案)编码为“0 1和2”的值为:

Alimentatie: "mancare gatita" code 0, "fast food" code 1 ,"amandoua in mod alternativ" code 2; 
Activitati: "regulat" code 0,"de cateva ori pe luna" code 1,"foarte rar" code 2; 
Bauturi: "evit cat de mult pot" code 0,"oricat de des simt nevoia" code 1 ,"ocazional" code 2; 
Odihna: "7-8 ore" code 0,"4-6 ore" code 1,"mai mult de 8 ore" code 2;
Recreere: "3-6" code 0,"mai mult de 6" code 1,"mai putin de 3" code  2

我附上了一张照片(数据),其中包含 cc.csv 文件的一部分(因为 txt 文件出现偏移)。

数据: Data Link

dput(rasps)
structure(list(`Profil 1` = c(0L, 0L, 0L, 0L, 0L, 0L, 1L, 0L, 
0L, 0L, 2L, 0L, 2L, 0L, 1L, 2L, 0L, 2L, 0L, 2L, 0L, 0L, 2L, 0L, 
2L, 0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L, 2L, 0L, 0L, 2L, 2L, 1L, 1L, 1L, 0L, 0L, 
2L, 0L, 1L, 0L, 0L, 2L, 2L, 2L, 2L, 0L, 2L, 0L, 0L, 0L, 2L, 2L
)), class = "data.frame", row.names = c(NA, 59L))

Data

更新代码后:

After Update

输出联合函数:

【问题讨论】:

  • 嗨,欢迎来到 SO!目前很难解决您的问题,因为我们无权访问您的cc.txt 文件。如果您可以在问题正文中添加该文件或一些模拟数据,事情会变得容易得多。请查看此帮助中心(页面)[stackoverflow.com/help/minimal-reproducible-example],以更好地了解我的建议。
  • 另外,根据您的代码的呈现方式,我在factor_levels 行出现错误,因为Recreere 对象使用大写的R,而因子级别行使用recreere 较低案子。请编辑您的问题以更正此错字。
  • 我更正了代码,我用小写的那个对象写了。此外,我添加了一部分数据,这些数据代表了调查答案的编纂。我希望现在更好。谢谢!

标签: r data-mining data-analysis


【解决方案1】:

您的原始代码在两个连续的行中两次调用caFactorialDesign 函数。然而根据conjointvignettecaEncodedDesign

[that] 对使用获得的因子设计进行编码的函数 caFactorialDesign 函数用于联合模块的需要 功能正常

基于此,我认为一个问题似乎是您的第二个caFactorialDesign 调用不正确。您可能应该改为调用caEncodedDesign 来生成encdesign_full 对象。因此,尝试将您的代码调整为:

    # replace the line encdesign_full <- caFactorialDesign(facdesign_full) with
    encdesign_full <- caEncodedDesign(facdesign_full)

另外,你的代码重写是一种更紧凑和可重现的方式:

    # load library
    library(conjoint)

    # combinations of levels
    data <- expand.grid(
      alimentatie = c("mancare gatita","fast food","amandoua in mod alternativ"), 
      activitati = c("regulat","de cateva ori pe luna","foarte rar"),
      bauturi = c("evit cat de mult pot","oricat de des simt nevoia","ocazional"),
      odihna = c("7-8 ore","4-6 ore","mai mult de 8 ore"),
      recreere = c("3-6","mai mult de 6","mai putin de 3"))

    # profile
    facdesign_full <- caFactorialDesign(data = data, type = "full")
    profile <- caEncodedDesign(facdesign_full)

    # level names
    levelnames <- data.frame(
      levels = c("mancare gatita","fast food","amandoua in mod alternativ",
      "regulat","de cateva ori pe luna","foarte rar","evit cat de mult pot",
      "oricat de des simt nevoia","ocazional", "7-8 ore","4-6 ore",
      "mai mult de 8 ore","3-6","mai mult de 6","mai putin de 3")
  )
# preferences
    response <- structure(list(alimentatie = c(0L, 0L, 0L, 0L, 0L, 0L, 1L, 0L, 
      0L, 0L, 2L, 0L, 2L, 0L, 1L, 2L, 0L, 2L, 0L, 2L, 0L, 0L, 2L, 0L, 
      2L, 0L, 0L, 0L, 2L, 2L, 2L, 0L, 0L, 0L, 0L, 2L, 0L, 2L, 0L, 0L, 
      2L, 2L, 0L, 0L, 2L, 0L, 2L, 2L, 0L, 2L, 2L, 1L, 0L, 2L, 0L, 2L, 
      2L, 0L, 1L), activitati = c(1L, 1L, 0L, 2L, 1L, 1L, 2L, 0L, 0L, 
      0L, 1L, 1L, 1L, 1L, 1L, 1L, 0L, 2L, 2L, 0L, 0L, 1L, 1L, 2L, 1L, 
      2L, 0L, 0L, 2L, 1L, 1L, 0L, 0L, 1L, 0L, 2L, 1L, 2L, 2L, 1L, 1L, 
      2L, 2L, 0L, 2L, 0L, 1L, 2L, 0L, 2L, 2L, 2L, 0L, 2L, 1L, 1L, 1L, 
      2L, 2L), bauturi = c(2L, 0L, 0L, 2L, 0L, 2L, 1L, 0L, 0L, 2L, 
      1L, 1L, 0L, 0L, 1L, 2L, 2L, 2L, 0L, 2L, 0L, 0L, 2L, 2L, 1L, 2L, 
      0L, 0L, 0L, 2L, 1L, 2L, 1L, 2L, 2L, 2L, 0L, 1L, 2L, 1L, 0L, 1L, 
      2L, 0L, 1L, 0L, 2L, 2L, 1L, 1L, 2L, 1L, 0L, 2L, 2L, 1L, 1L, 0L, 
      2L), odihna = c(0L, 0L, 0L, 0L, 0L, 0L, 1L, 0L, 0L, 0L, 0L, 0L, 
      0L, 0L, 0L, 2L, 0L, 1L, 2L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 1L, 0L, 
      0L, 1L, 2L, 0L, 0L, 0L, 0L, 0L, 1L, 0L, 1L, 2L, 0L, 1L, 0L, 0L, 
      0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 1L, 2L, 0L, 1L), 
      recreere = c(0L, 1L, 2L, 0L, 0L, 2L, 0L, 2L, 2L, 2L, 2L, 
      0L, 0L, 0L, 1L, 2L, 0L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 0L, 
      2L, 0L, 2L, 1L, 0L, 0L, 2L, 2L, 0L, 0L, 2L, 2L, 1L, 1L, 1L, 
      0L, 0L, 2L, 0L, 1L, 0L, 0L, 2L, 2L, 2L, 2L, 0L, 2L, 0L, 0L, 
      0L, 2L, 2L)), class = "data.frame", row.names = c(NA, -59L))

请注意,通过指定“完整”设计,profile 对象有 243 行,而您的 response 对象只有 59:

dim(profile)
[1] 243 5
dim(response)
[1] 59 5

这似乎混淆了Conjoint 函数,我猜是因为它在response 对象中查找243 行数据。

实际上,如果将 184 行模拟数据附加到 response 对象,Conjoint 似乎会产生预期的结果:

    # simulated response data
    set.seed(1)
    response_sim <- data.frame(alimentatie = sample(0:2, 184, replace = T),
      activitati = sample(0:2, 184, replace = T),
      bauturi = sample(0:2, 184, replace = T),
      odihna = sample(0:2, 184, replace = T),
      recreere = sample(0:2, 184, replace = T))

    # append simulated preferences
    response_n243 <- rbind(response, response_sim)

    # run 
    Conjoint(response_n243,profile, levelnames)

结论

您的问题似乎是由于您的 response 对象中的数据少于运行“完整”因子设计所需的数据。似乎您可能需要提供更多 response 数据,或者重新考虑您的设计,特别注意 caFactorialDesigntype 参数。 “分数”设计可能是一种替代方案。

【讨论】:

  • 我尝试应用您所说的,但问题仍然存在。我在原始消息中附加了一个打印屏幕(更新后 img。)
  • 能否提供response 对象(或rasps 对象)的完整数据?这可以通过将代码dput(response)dput(rasps) 的控制台输出复制粘贴到您的问题正文中来实现。
  • 完成。另外,我在 google 驱动器上添加了两种格式的文件 .csv 和 .txt(cc1.csv 和 cc.txt)。 link
  • 非常感谢!你是对的,问题在于配置文件的数量高于响应的数量,所以我使用了 59 个配置文件的分数设计。我设法做了联合分析。facdesign_fractional&lt;-caFactorialDesign(data = data, type = "fractional",cards = 59) profile&lt;-caEncodedDesign(facdesign_fractional)
猜你喜欢
  • 1970-01-01
  • 2021-07-17
  • 2021-12-31
  • 1970-01-01
  • 2022-01-05
  • 1970-01-01
  • 1970-01-01
  • 2017-10-05
  • 2012-08-04
相关资源
最近更新 更多