You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用R语言Likert包抑制或移除因子水平以优化分析

Hey there! I’ve worked with the likert package extensively on survey data full of messy responses like NAs, "不适用", and "不知道"—so let’s break down exactly how to clean this up and get the clear summaries and visuals you need.

Step 1: Standardize Invalid Responses to NA

First, we need to turn all those non-standard "invalid" values into proper R NAs, since the likert package is built to handle these (and we can choose whether to include or exclude them later). We’ll also make sure our Likert columns are formatted as factors with the correct order (critical for accurate plotting):

library(likert)

# Assume your survey data is in a data frame called `survey_data`
# Replace 2:10 with the column indices/names of your Likert scale questions
likert_questions <- 2:10

# Convert all non-valid responses to NA and set factor levels
survey_data[likert_questions] <- lapply(survey_data[likert_questions], function(col) {
  # List all values you want to treat as missing
  invalid_vals <- c("不适用", "不知道", "", "NA")
  col[col %in% invalid_vals] <- NA
  
  # Set factor levels in the correct order (adjust to match your scale!)
  factor(col, levels = c("非常不同意", "不同意", "中立", "同意", "非常同意"))
})

Step 2: Subset Data (Optional)

If you want to remove rows with too many missing responses (rather than just excluding NAs from calculations), you can filter based on how many valid answers a respondent gave:

# Keep only rows where respondents answered at least 70% of Likert questions
min_valid_responses <- round(length(likert_questions) * 0.7)
clean_data <- survey_data[rowSums(!is.na(survey_data[likert_questions])) >= min_valid_responses, ]

Step 3: Analyze and Visualize with NA Control

The likert() function gives you two key options for handling NAs:

Option A: Include Missing Responses as a Separate Category

This is great if you want to show how many people skipped each question:

# Create likert object with missing responses included
likert_with_missing <- likert(clean_data[likert_questions])

# View summary (includes missing percentages)
summary(likert_with_missing)

# Plot with missing responses shown
plot(likert_with_missing) +
  ggtitle("Likert Responses (Including Missing/Skipped Answers)")

Option B: Exclude NAs Entirely (Calculate Proportions on Valid Responses Only)

If you want to focus solely on respondents who answered the question, use na.rm = TRUE:

# Create likert object excluding NAs
likert_no_missing <- likert(clean_data[likert_questions], na.rm = TRUE)

# Summary shows proportions based only on valid answers
summary(likert_no_missing)

# Plot without missing responses
plot(likert_no_missing) +
  ggtitle("Likert Responses (Excluding Missing/Skipped Answers)")

Step 4: Customize Plots to Hide Missing (If Needed)

If you already generated a likert object with missing responses but want to exclude them from the plot, you can tweak the underlying data:

# Extract the plot data from the likert object
plot_data <- likert_with_missing$results

# Filter out the "Missing" category
plot_data_filtered <- plot_data[plot_data$response != "Missing", ]

# Build a custom ggplot (matches the default likert plot style)
ggplot(plot_data_filtered, aes(x = Item, y = value, fill = response)) +
  geom_bar(stat = "identity", position = "fill") +
  coord_flip() +
  scale_y_continuous(labels = scales::percent) +
  scale_fill_brewer(palette = "RdBu") +
  labs(title = "Likert Responses (Missing Removed)", x = "Survey Questions", y = "Percentage") +
  theme_minimal()

Bonus: Handle "不适用" as a Meaningful Group

If "不适用" isn’t just a skipped answer (e.g., some questions only apply to specific respondents), consider splitting your data into groups instead of converting to NA:

# Example: Assume you have a column indicating if the question applies to the respondent
applicable_respondents <- subset(survey_data, question_applicable == "Yes")
likert_applicable <- likert(applicable_respondents[likert_questions], na.rm = TRUE)
plot(likert_applicable)

内容的提问来源于stack exchange,提问作者paramagical

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.28 07:29:12