处理UCI fertility_diagnosis数据集遇“object not found”问题求助
Hey there! Let's work through your issues with the UCI fertility_diagnosis dataset step by step—both the "object not found" error during type conversion and setting up your regression analysis.
1. Fixing the "object not found" error when converting variables to numeric
This error almost always means R can't locate the variables you're referencing. Here are the most common fixes:
First, confirm your data setup
The fertility_diagnosis dataset doesn't come with built-in headers, so if you loaded it without naming columns, R might not recognize V1-V10 as valid variable names. Start by properly loading and naming your data:
# Load the dataset (adjust the file path to match your local setup) fertility_data <- read.csv("fertility_diagnosis.csv", header = FALSE) # Assign the V1-V10 variable names explicitly colnames(fertility_data) <- paste0("V", 1:10)
Convert variables correctly (no more missing objects!)
Avoid using attach() (it’s prone to naming conflicts) and instead reference variables directly from your data frame. Here are two reliable methods to convert V3-V6 and V9 to numeric:
Base R approach:
# Target the specific columns and convert them fertility_data[, c("V3", "V4", "V5", "V6", "V9")] <- lapply( fertility_data[, c("V3", "V4", "V5", "V6", "V9")], as.numeric )
dplyr approach (cleaner syntax):
library(dplyr) fertility_data <- fertility_data %>% mutate(across(c(V3, V4, V5, V6, V9), as.numeric))
Verify the conversion worked
Run str(fertility_data) to check that the targeted columns now show as num (numeric) instead of int or chr.
2. Setting up regression analysis for this dataset
First, note that the final variable V10 is a categorical diagnosis (N = normal, O = abnormal). Depending on your goal, you’ll use either logistic regression (for predicting the diagnosis) or linear regression (for predicting a numeric outcome like V1 or V2).
Example 1: Logistic regression (predict V10 diagnosis)
Since V10 is binary, logistic regression is the right fit:
# Convert V10 to a factor first (required for glm) fertility_data$V10 <- factor(fertility_data$V10, levels = c("N", "O")) # Fit the logistic regression model logit_model <- glm( V10 ~ V1 + V2 + V3 + V4 + V5 + V6 + V7 + V8 + V9, data = fertility_data, family = binomial ) # View model summary summary(logit_model)
Example 2: Linear regression (predict a numeric variable)
If you want to predict a numeric outcome like V1 (age), use linear regression:
lm_model <- lm( V1 ~ V3 + V4 + V5 + V6 + V9 + V2 + V7 + V8 + V10, data = fertility_data ) # View model summary summary(lm_model)
Quick checks to avoid regression issues
- Check for missing values: Run
sum(is.na(fertility_data))—if there are missing rows, usena.omit(fertility_data)to clean the dataset before modeling. - Confirm variable types: Make sure categorical variables (like
V10) are converted to factors, otherwise R will treat them as numeric values and skew your results.
内容的提问来源于stack exchange,提问作者hareen tej

