# Load necessary libraries
if (!require(forcats)) {
install.packages("forcats", dependencies = TRUE)
library(forcats)
}
# Set the working directory to where your data and process.R file are located
setwd("MYPATH")
# Check the current working directory to verify the change
cat("Current working directory:", getwd(), "\n")
# Load the data
political_position_data <- read.csv("MYDATA.csv")
# Subset the data to only include entries where CountryCode is 1
us_data <- subset(political_position_data, CountryCode == 1)
# Convert Rec_Cond and PO3_Ex to factors
us_data$Rec_Cond <- as.factor(us_data$Rec_Cond)
us_data$PO3_Ex <- as.factor(us_data$PO3_Ex)
# Convert other covariates to numeric
us_data$BiGender <- as.numeric(as.character(us_data$BiGender))
us_data$BiEducation <- as.numeric(as.character(us_data$BiEducation))
us_data$Bi_Age <- as.numeric(as.character(us_data$Bi_Age))
# Verify the levels of categorical variables
cat("Levels of Rec_Cond:", levels(us_data$Rec_Cond), "\n")
cat("Levels of PO3_Ex:", levels(us_data$PO3_Ex), "\n")
# Print a summary to verify the data preparation
cat("Summary of us_data:\n")
print(summary(us_data))
# Load the PROCESS macro (make sure the file exists in the specified path)
source("process.R")
# Run the PROCESS macro with mcx and mcw options for multicategorical variables
results <- process(data = us_data,
y = "Conf_gov",
x = "Rec_Cond",
m = "STAXI",
w = "PO3_Ex",
cov = c("BiGender", "BiEducation", "Bi_Age"),
model = 58,
mcx = list(Rec_Cond = levels(us_data$Rec_Cond)),
mcw = list(PO3_Ex = levels(us_data$PO3_Ex)))
# Print the results
print(results)
我认为这个问题很简单,因为将非数字值传递给MCW变量:
mcw= list(po3_ex = latver(us_data $ po3_ex)))检查列表的输出(po3_ex = laste(us_data $ po3_ex)))为),并确保其数据类型进程期望它是数字数据类型,因为它试图使用trunc()函数.