jbryer / likert

Package to analyze likert based items.
305 stars 124 forks source link

does the package work for levels = 7 #95

Closed jimegon closed 5 years ago

jimegon commented 5 years ago

I would love to plot my likert questions, but I am running into issues following your code. I have 7 levels: mylevels<-c("Strongly agree", "Agree", "Somewhat agree", "Neither agree nor disagree", "Somewhat disagree", "Disagree", "Strongly disagree") Here is the structure of my data.frame str(itemsCN)

Classes ‘tbl_df’, ‘tbl’ and 'data.frame': 114 obs. of 2 variables: $ CN_2: Factor w/ 7 levels "Strongly agree",..: 4 6 1 4 1 2 3 2 1 6 ... $ CN_4: Factor w/ 7 levels "Strongly agree",..: 6 3 4 3 1 1 2 2 2 3 ...

I have followed the code provided in other posts or tutorials:

`sapply(itemsCN,class) #to check that they are factors sapply(itemsCN, function(x) {length(levels(x))}) # to check number of levels

to recode each factor and explicitly set the levels

for (i in seq_along(itemsCN)){ itemsCN[,i]<-factor(itemsCN[,i], levels = mylevels) } lgood<-likert(itemsCN)`

When I run the likert function, I get the following error: Error in likert(itemsCN) : All items (columns) must have the same number of levels

I know others have posted about this error, and I have tried following the recommendations under their posts. Yet, I still get the same error.

Most of the examples have 5 levels, and I wonder if I have issues because my likert has 7 levels.

Looking forward to your feedback.

jbryer commented 5 years ago

You need to convert tibbles to data.frame before calling likert. Use the as.data.frame function.

jimegon commented 5 years ago

Thank you for the easy solution!

jimegon commented 5 years ago

I have noticed that this error comes up after I re-run the code:

likert(itemsT)
Error in dimnames(x) <- `*vtmp*` : 
  length of 'dimnames' [2] not equal to array extent

Here is the structure of my data.frame:

str(itemsT)
'data.frame':   114 obs. of  4 variables:
 $ Government Institutions: Factor w/ 7 levels "Strongly \n disagree",..: 3 4 1 5 5 1 1 5 3 6 ...
 $ Police                 : Factor w/ 7 levels "Strongly \n disagree",..: 4 4 2 6 5 6 6 5 3 5 ...
 $ Politicians            : Factor w/ 7 levels "Strongly \n disagree",..: 3 4 1 3 5 1 4 5 3 5 ...
 $ Legal System           : Factor w/ 7 levels "Strongly \n disagree",..: 4 5 1 5 5 2 4 6 3 5 ...

the dimnames are:

dimnames(itemsT)[2]
[[1]]
[1] "Government Institutions" "Police"                  "Politicians"             "Legal System"           

Here is a head of my dataset

head(itemsT)
  Government Institutions            Police          Politicians         Legal System
1    Somewhat \n disagree           Neither Somewhat \n disagree              Neither
2                 Neither           Neither              Neither    Somewhat \n agree
3    Strongly \n disagree          Disagree Strongly \n disagree Strongly \n disagree
4       Somewhat \n agree             Agree Somewhat \n disagree    Somewhat \n agree
5       Somewhat \n agree Somewhat \n agree    Somewhat \n agree    Somewhat \n agree
6    Strongly \n disagree             Agree Strongly \n disagree             Disagree
jbryer commented 5 years ago

Can you provide a small reproducible example?

On Wed, Sep 26, 2018 at 3:29 PM jimegon notifications@github.com wrote:

I have noticed that this error comes up after I re-run the code: likert(itemsT) Error in dimnames(x) <- vtmp : length of 'dimnames' [2] not equal to array extent

Here is the structure of my data.frame: str(itemsT) 'data.frame': 114 obs. of 4 variables: $ Government Institutions: Factor w/ 7 levels "Strongly \n disagree",..: 3 4 1 5 5 1 1 5 3 6 ... $ Police : Factor w/ 7 levels "Strongly \n disagree",..: 4 4 2 6 5 6 6 5 3 5 ... $ Politicians : Factor w/ 7 levels "Strongly \n disagree",..: 3 4 1 3 5 1 4 5 3 5 ... $ Legal System : Factor w/ 7 levels "Strongly \n disagree",..: 4 5 1 5 5 2 4 6 3 5 ...

the dimnames are: dimnames(itemsT)[2] [[1]] [1] "Government Institutions" "Police" "Politicians" "Legal System"

Here is a head of my dataset

head(itemsT) Government Institutions Police Politicians Legal System 1 Somewhat \n disagree Neither Somewhat \n disagree Neither 2 Neither Neither Neither Somewhat \n agree 3 Strongly \n disagree Disagree Strongly \n disagree Strongly \n disagree 4 Somewhat \n agree Agree Somewhat \n disagree Somewhat \n agree 5 Somewhat \n agree Somewhat \n agree Somewhat \n agree Somewhat \n agree 6 Strongly \n disagree Agree Strongly \n disagree Disagree

— You are receiving this because you modified the open/close state. Reply to this email directly, view it on GitHub https://github.com/jbryer/likert/issues/95#issuecomment-424840838, or mute the thread https://github.com/notifications/unsubscribe-auth/AAmEj-Pn6T0vuNrLVJr3wvtYbykA1_EVks5ue9WMgaJpZM4W1SyB .

jimegon commented 5 years ago
itemsCN<-as.data.frame(data2[,c("CN_1","CN_2", "CN_3","CN_4","CN_5", "CN_6")])
save(itemsCN, file="itemsCN.Rda")
load("itemsCN.Rda"

#to change levels of factors 
[itemsCN.zip](https://github.com/jbryer/likert/files/2422018/itemsCN.zip)

mylevels<-c("Strongly disagree", "Disagree","Somewhat disagree",
            "Neither agree, nor disagree",
            "Somewhat agree", "Agree","Strongly agree")

str(itemsCN)
sapply(itemsCN,class) #to check that they are factors
sapply(itemsCN, function(x) {length(levels(x))}) # to check number of levels

#to recode each factor and explicitly set the levels
for (i in seq_along(itemsCN)){
  itemsCN[,i]<-factor(itemsCN[,i], levels = mylevels,
                      labels = c("Strongly \n disagree", "Disagree","Somewhat \n disagree",
                                 "Neither",
                                 "Somewhat \n agree", "Agree","Strongly \n agree")
  )
}

lCN<-likert(itemsCN)
l.CN<-likert(summary=lCN$results)
[itemsCN.zip](https://github.com/jbryer/likert/files/2422021/itemsCN.zip)
Goozpak commented 10 months ago

Hi, did you solve this problem by any chance?

@jbryer @jimegon