sjp.likert sjplot 中的命令,factor 或 numeric?,为什么两个不同的结果?
sjp.likert command in sjplot, factor or numeric?, why two different results?
我使用 sjp.likert 命令从我的数据中获得了 2 个不同的结果,这是我的代码:
library(sjPlot)
l <- c(1,2,2,2,3,3,4,4,5,5,5,4,3,2,2)
lab <- c("strongly not agree",
"not agree",
"Neutral",
"Agree",
"Strongly agree")
sjp.likert(items = l,
cat.neutral = 3,
catcount = 4,
legend.labels = lab)
请注意,我使用的是数字变量而不是因子,此时一切看起来都很好,但有时我更喜欢使用因子来省略 legend.labels 参数。所以我用这个
l.factor <- factor(x = l,labels = lab)
sjp.likert(items = l.factor,
cat.neutral = 3,
catcount = 4)
但这就是我遇到问题的地方,例如:"neutral" 响应不再是 20%,现在是 6.7%。据我所知,包裹将 "neutral" 响应读取为中性,因为右侧为灰色。
你可以看到正确的数字是 20% 使用这个
prop.table(table(l.factor))
prop.table(table(l))
我做错了什么?
这是一个错误吗?
问题是 sjp-likert()
函数内的值重新排序(如果您有一个中性类别)是基于数字索引 - 这不适用于具有字符级别的因子。所以你必须在调用函数之前重新排序你的因子水平:
l.factor <- factor(x = l,labels = lab[c(1,2,4,5,3)])
sjp.likert(items = l.factor,
cat.neutral = 5,
catcount = 4)
另一种方法是将因子转化为数值,并将因子水平设置为label-attribute。您可以使用带有参数 keep.labels = TRUE
的 sjmisc::to_value()
来执行此操作。以你的例子为例,稍微修改一下:
l <- c(1,2,2,2,3,3,4,4,5,5,5,4,3,2,2)
lab <- c("strongly not agree",
"not agree",
"Neutral",
"Agree",
"Strongly agree")
l.factor <- factor(x = l,labels = lab)
l.factor <- to_value(l.factor, keep.labels = T)
sjp.likert(items = l.factor,
cat.neutral = 3,
catcount = 4)
to_value()
适用于矢量和数据框,因此您可以轻松地将数据框中的因子转换为数字,并保留值标签:
to_value(my_data_frame, keep.labels = T)
我使用 sjp.likert 命令从我的数据中获得了 2 个不同的结果,这是我的代码:
library(sjPlot)
l <- c(1,2,2,2,3,3,4,4,5,5,5,4,3,2,2)
lab <- c("strongly not agree",
"not agree",
"Neutral",
"Agree",
"Strongly agree")
sjp.likert(items = l,
cat.neutral = 3,
catcount = 4,
legend.labels = lab)
请注意,我使用的是数字变量而不是因子,此时一切看起来都很好,但有时我更喜欢使用因子来省略 legend.labels 参数。所以我用这个
l.factor <- factor(x = l,labels = lab)
sjp.likert(items = l.factor,
cat.neutral = 3,
catcount = 4)
但这就是我遇到问题的地方,例如:"neutral" 响应不再是 20%,现在是 6.7%。据我所知,包裹将 "neutral" 响应读取为中性,因为右侧为灰色。
你可以看到正确的数字是 20% 使用这个
prop.table(table(l.factor))
prop.table(table(l))
我做错了什么? 这是一个错误吗?
问题是 sjp-likert()
函数内的值重新排序(如果您有一个中性类别)是基于数字索引 - 这不适用于具有字符级别的因子。所以你必须在调用函数之前重新排序你的因子水平:
l.factor <- factor(x = l,labels = lab[c(1,2,4,5,3)])
sjp.likert(items = l.factor,
cat.neutral = 5,
catcount = 4)
另一种方法是将因子转化为数值,并将因子水平设置为label-attribute。您可以使用带有参数 keep.labels = TRUE
的 sjmisc::to_value()
来执行此操作。以你的例子为例,稍微修改一下:
l <- c(1,2,2,2,3,3,4,4,5,5,5,4,3,2,2)
lab <- c("strongly not agree",
"not agree",
"Neutral",
"Agree",
"Strongly agree")
l.factor <- factor(x = l,labels = lab)
l.factor <- to_value(l.factor, keep.labels = T)
sjp.likert(items = l.factor,
cat.neutral = 3,
catcount = 4)
to_value()
适用于矢量和数据框,因此您可以轻松地将数据框中的因子转换为数字,并保留值标签:
to_value(my_data_frame, keep.labels = T)