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

R语言PCA中创建svydesign对象报错的技术问询

Fixing svydesign Errors for Your PCA Analysis

First off, the errors you’re seeing (object 'psu' not found, etc.) are happening because the variables psu, ststr, and cntywt don’t exist in your dataset—looking at your sample data, those columns aren’t present!

Since you mentioned you used Simple Random Sampling (SRS), you don’t need to specify clustering (psu) or stratification (strata) unless those variables actually exist in your data. Here’s how to adjust your code to match your actual sampling design:

Step 1: Correct the Survey Design Object

For SRS with no weights, clustering, or stratification, set up your design like this:

options(survey.lonely.psu = "adjust")
scwb <- scwb[complete.cases(scwb),]
# For SRS without weights, clustering, or strata
des <- svydesign(ids = ~1, strata = NULL, weights = ~1, data = scwb)

If you do have survey weights (maybe cntywt is a weight variable you didn’t include in your sample), replace ~1 with your weight column name:

des <- svydesign(ids = ~1, strata = NULL, weights = ~cntywt, data = scwb)

Step 2: Run PCA with Survey Data

Once your design is set up correctly, use the survey package’s svyprcomp() function instead of base R’s prcomp()—this accounts for your survey design in the PCA results:

# Select the variables you want to include in PCA (exclude IDs, demographics, etc.)
pca_vars <- c("Jobsat_1", "Jobsat_2", "Jobsat_3", "Member_1", "Member_2", "Member_4", 
              "LingInt", "Belong_2", "Belong_3", "Grpor_14", "Trust_G1", "Trust_G2")

# Run PCA (scale variables to avoid bias from different scales)
survey_pca <- svyprcomp(~., data = scwb[pca_vars], design = des, scale. = TRUE)

# View summary of PCA results
summary(survey_pca)

Key Notes

  • ids = ~1 tells R there’s no clustering (each observation is its own primary sampling unit).
  • strata = NULL skips stratification since you’re using SRS.
  • Always set scale. = TRUE in PCA to ensure variables with larger scales don’t dominate the results.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.07 22:07:31