在R中编码NLS拟合温雨关系时遇维度匹配错误求助
Hey there! Let's walk through solving your nonlinear regression problems step by step—you're already on the right track with your logistic function, just a couple of small missteps in setting up nls().
First: Understand Your Model's Parameters
Your func_rain(x,b) = 1/(1+exp(-b*x)) is a basic logistic curve, and it only has one parameter to estimate: b. The x (temperature) is your independent variable, and vec_rain/dp.w2$rain is your dependent variable—neither of these belong in the start argument! That's the root cause of both your errors.
Step 1: Fix the Vector-Based NLS Code
Your first attempt threw a dimension mismatch error because you included vec_temp_num=2.6 in the start argument. Here's the corrected version:
# Define your function (unchanged) func_rain <- function(x,b){(1/(1+exp(-b*x)))} # Corrected vector-based fit fit_vec <- nls(vec_rain ~ func_rain(vec_temp_num, b), start = list(b = 1)) # Only include the parameter b!
Step 2: Fix the Data Frame-Based NLS Code
Your second attempt had two issues: you added non-parameter variables (temperature, rain) to start, and you didn't use the data argument to simplify the formula. Here's the fixed code:
# Corrected data frame-based fit Rain_fit <- nls(rain ~ func_rain(temperature, b), data = dp.w2, # Tell nls where to find your columns start = list(b = 1)) # Again, only parameter b
Optional (But Recommended): Add an Inflection Point Parameter
Your initial attempt included 2.6—I suspect you wanted to set the inflection point (where rain proportion hits 50%) at 2.6 degrees. Your current function has its inflection point at x=0, which probably doesn't match your data. Let's adjust the function to include an inflection point parameter x0:
# Updated logistic function with inflection point func_rain_inflect <- function(x, b, x0) { 1 / (1 + exp(-b * (x - x0))) } # Fit with both parameters (b = slope, x0 = inflection temp) Rain_fit_better <- nls(rain ~ func_rain_inflect(temperature, b, x0), data = dp.w2, start = list(b = 1, x0 = 2.6)) # Now we need both initial values
This will give you a model that's far more aligned with real-world temperature-precipitation relationships.
Step 3: Verify Your Fit
Once you run the corrected code, check the results with:
summary(Rain_fit_better) # See parameter estimates and model stats
You can also plot the fit to visualize how well it matches your data:
# Generate predicted values dp.w2$pred_rain <- predict(Rain_fit_better, newdata = dp.w2) # Plot raw data + fitted curve plot(dp.w2$temperature, dp.w2$rain, main = "Temperature vs Rain Proportion", xlab = "Temperature (°C)", ylab = "Rain Proportion") lines(dp.w2$temperature, dp.w2$pred_rain, col = "darkred", lwd = 2)
Why Your Original Code Failed
- First error: You included the independent variable (
vec_temp_num) instart—nls()only expects parameters to estimate here, not data columns. This caused a dimension mismatch. - Second error: You added both independent and dependent variables to
start, and didn't use thedataargument. This confused R's formula parser, leading to the "unexpected end of input" error.
内容的提问来源于stack exchange,提问作者Sanda

