R中的联合()函数
Conjoint() function in R
我对联合函数的输出有疑问。
我有 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)
我把取值为“0 1 and 2”的5个问题的每个答案(一共59个答案)编码为:
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, 0L, 0L, 0L, 2L, 2L, 2L, 0L, 0L, 0L, 0L,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, 0L, 0L, 2L, 2L, 2L, 2L, 0L, 2L, 0L, 0L, 0L, 2L, 2L
)), class = "data.frame", row.names = c(NA, 59L))
Data
更新代码后:
After Update
输出联合函数:
您的原始代码在连续两行中调用了 caFactorialDesign
函数两次。然而根据 conjoint
vignette,caEncodedDesign
是
the function [that] encodes the factorial design obtained using
caFactorialDesign function for the needs of conjoint module
functioning
基于此,我认为一个问题似乎是您的第二个 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))
请注意,通过指定 "full" 设计,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
对象中的数据少于 运行 "full" 析因设计所需的数据。看来您可能需要提供更多 response
数据,或者重新考虑您的设计,特别注意 caFactorialDesign
的 type
参数。 "fractional" 设计可能是替代方案。
我对联合函数的输出有疑问。 我有 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)
我把取值为“0 1 and 2”的5个问题的每个答案(一共59个答案)编码为:
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, 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), `Profil 2` = 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), `Profil 3` = 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), `Profil 4` = 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
), `Profil 5` = 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))
Data
更新代码后:
After Update
输出联合函数:
您的原始代码在连续两行中调用了 caFactorialDesign
函数两次。然而根据 conjoint
vignette,caEncodedDesign
是
the function [that] encodes the factorial design obtained using caFactorialDesign function for the needs of conjoint module functioning
基于此,我认为一个问题似乎是您的第二个 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))
请注意,通过指定 "full" 设计,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
对象中的数据少于 运行 "full" 析因设计所需的数据。看来您可能需要提供更多 response
数据,或者重新考虑您的设计,特别注意 caFactorialDesign
的 type
参数。 "fractional" 设计可能是替代方案。