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

使用rpy2在Python中执行nls回归遇RRuntimeError求助

Troubleshooting RRuntimeError with rpy2 + nls Non-Linear Regression

Let's break down why you're hitting that RRuntimeError and fix it step by step. The error message parameters without starting value in 'data': rates, count tells us R's nls can't find the variables rates and count you referenced in your formula—here's how to resolve this:

Root Cause

Your original code extracts count and rates as separate R vectors, but you didn't tell nls where to find these variables. Additionally, your formula uses these Python-assigned aliases instead of the actual column names from your data frame, and you didn't pass the data frame to nls's data parameter (which nls relies on to locate variables).

Fixed Code

Here's the revised code that should work correctly:

import pandas as pd
import rpy2.robjects as ro
from rpy2.robjects.packages import importr
from rpy2.robjects import pandas2ri

pandas2ri.activate()

# Load your data (use your local file path)
dfData = pd.read_csv('C:\\Users\\nick\\Desktop\\ratedata.csv')
rdf = pandas2ri.py2ri(dfData)

# Define initial parameter values for the model
start_params = ro.ListVector({'a': 0.5, 'b': 1.1})

# Import required R packages with name translations
base = importr('base', robject_translations={'with': '_with'})
stats = importr('stats', robject_translations={'format_perc': '_format_perc'})

# Update formula to use actual column names from your data frame
my_formula = stats.as_formula('Successes ~ 1 - (1 / (10^(a * Trials ^ (b - 1))))')

# Run nls with explicit data parameter, weights, and start values
fit = stats.nls(
    formula=my_formula,
    data=rdf,
    weights=rdf.rx(True, 'Trials'),
    start=start_params
)

# Extract fitted parameters into Python variables
fit_params = dict(zip(fit.names, list(fit)))
a_fitted = fit_params['a']
b_fitted = fit_params['b']

print(f"Fitted parameter a: {a_fitted}")
print(f"Fitted parameter b: {b_fitted}")

Key Changes Explained

  1. Formula Alignment: We replaced rates and count with your actual data frame column names (Successes and Trials). This ensures nls can locate the variables in the provided data frame.
  2. Data Parameter: Adding data=rdf tells nls to look for all formula variables within your R data frame, eliminating the "variable not found" error.
  3. Explicit Weights: We directly reference the Trials column from rdf for weights, keeping all data references consistent within the R data frame context.
  4. Parameter Extraction: Converting the fitted R object to a Python dictionary makes it easy to pull out the estimated a and b values for your confidence interval calculations.

Once you run this code, you'll get the fitted parameter values you need to proceed with building your confidence intervals in Python.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.15 04:30:33