generate_python_code
Generate Python code for statistical analysis using statsmodels, sklearn, or linearmodels libraries to perform regression and quantitative research tasks.
Instructions
Python 코드 생성 (statsmodels, sklearn)
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| analysis_type | Yes | 분석 유형 | |
| library | No | 라이브러리 | |
| variables | No | 변수 정보 |
Implementation Reference
- src/tools/index.ts:1598-1643 (handler)The handler function implementing the tool logic. It generates Python code templates for statistical analyses (e.g., OLS, panel fixed effects) using libraries like statsmodels and linearmodels based on the analysis_type parameter.function handleGeneratePythonCode(args: Record<string, unknown>) { const analysisType = args.analysis_type as string; const templates: Record<string, string> = { ols: ` import pandas as pd import statsmodels.api as sm import statsmodels.formula.api as smf # OLS model = smf.ols('y ~ x1 + x2 + x3', data=df).fit() print(model.summary()) # Robust SE model_robust = smf.ols('y ~ x1 + x2 + x3', data=df).fit(cov_type='HC3') # VIF from statsmodels.stats.outliers_influence import variance_inflation_factor X = df[['x1', 'x2', 'x3']] vif = pd.DataFrame({ 'Variable': X.columns, 'VIF': [variance_inflation_factor(X.values, i) for i in range(X.shape[1])] }) `, panel_fe: ` import pandas as pd from linearmodels import PanelOLS # Set index df = df.set_index(['id', 'time']) # Entity FE model_fe = PanelOLS(df['y'], df[['x1', 'x2']], entity_effects=True) result_fe = model_fe.fit(cov_type='clustered', cluster_entity=True) # Two-way FE model_twfe = PanelOLS(df['y'], df[['x1', 'x2']], entity_effects=True, time_effects=True) result_twfe = model_twfe.fit(cov_type='clustered', cluster_entity=True) ` }; return { analysis_type: analysisType, python_code: templates[analysisType] || "# Analysis template not found" }; }
- src/tools/index.ts:378-386 (schema)The input schema defining the parameters for the tool: analysis_type (required), library (statsmodels/sklearn/linearmodels), and variables.inputSchema: { type: "object", properties: { analysis_type: { type: "string", description: "분석 유형" }, library: { type: "string", enum: ["statsmodels", "sklearn", "linearmodels"], description: "라이브러리" }, variables: { type: "object", description: "변수 정보" }, }, required: ["analysis_type"], },
- src/tools/index.ts:375-387 (registration)Tool object registration in the exported tools array.{ name: "generate_python_code", description: "Python 코드 생성 (statsmodels, sklearn)", inputSchema: { type: "object", properties: { analysis_type: { type: "string", description: "분석 유형" }, library: { type: "string", enum: ["statsmodels", "sklearn", "linearmodels"], description: "라이브러리" }, variables: { type: "object", description: "변수 정보" }, }, required: ["analysis_type"], }, },
- src/tools/index.ts:836-837 (registration)Switch case in handleToolCall function that dispatches calls to the specific handler.case "generate_python_code": return handleGeneratePythonCode(args);