R语言绘图:test_mse曲线无法显示且无报错问题咨询
Here are practical fixes for your issue where test_mse doesn’t appear on the plot despite no error messages:
Verify vector length consistency
Ensurelength(test_mse)equalsp=10. If the vector is shorter or longer, the points won’t align with the x-axis range1:pand may fail to render. Run this check:length(test_mse) == pAdjust y-axis range to include test_mse values
The initialplot()call sets the y-axis bounds based ontrain_msevalues. Iftest_mseis significantly higher or lower, it’ll be plotted outside the visible area. Update your code to explicitly set a y-axis range that covers both datasets:# Calculate combined range of both MSE vectors mse_range <- range(c(train_mse, test_mse)) plot(1:p, train_mse, type = "b", pch = 19, col = "blue", xlab = "Number of Components (M)", ylab = "Mean Squared Error (MSE)", main = "PCR: Training and Test MSEs vs. Number of Components", ylim = mse_range) points(1:p, test_mse, type = "b", pch = 19, col = "red") legend("topright", legend = c("Training MSE", "Test MSE"), col = c("blue", "red"), pch = 19, cex = 0.8)Check for missing values
Iftest_msecontains NA values, those points won’t render. Confirm with:any(is.na(test_mse))If the result is
TRUE, handle the missing values (e.g., remove or impute them) before plotting.Isolate the issue with simplified code
To rule out conflicts with the initial plot, try plottingtest_msealone first:plot(1:p, test_mse, type="b", col="red")If this works, the problem was almost certainly the y-axis range in your original plot.
内容的提问来源于stack exchange,提问作者khali-blinks

