generate_r_code
Generate R code for statistical analysis, diagnostics, and visualization to support quantitative research workflows.
Instructions
R 코드 생성 (분석, 진단, 시각화)
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| analysis_type | Yes | 분석 유형 | |
| variables | No | 변수 정보 | |
| options | No | 추가 옵션 |
Implementation Reference
- src/tools/index.ts:1483-1542 (handler)The main handler function for the 'generate_r_code' tool. It generates R code templates for various analysis types (OLS, panel FE, DID, etc.) based on the input 'analysis_type'.function handleGenerateRCode(args: Record<string, unknown>) { const analysisType = args.analysis_type as string; const templates: Record<string, string> = { ols: ` # OLS Regression library(tidyverse) library(fixest) library(modelsummary) # Estimate model <- lm(y ~ x1 + x2 + x3, data = df) # Robust SE model_robust <- feols(y ~ x1 + x2 + x3, data = df, vcov = "hetero") # Diagnostics car::vif(model) # Multicollinearity lmtest::bptest(model) # Heteroscedasticity # Results table modelsummary(list("OLS" = model, "Robust" = model_robust)) `, panel_fe: ` # Panel Fixed Effects library(fixest) library(modelsummary) # Entity FE model_fe <- feols(y ~ x1 + x2 | id, data = panel_df, vcov = ~id) # Entity + Time FE model_twfe <- feols(y ~ x1 + x2 | id + year, data = panel_df, vcov = ~id) # Results modelsummary(list("Entity FE" = model_fe, "TWFE" = model_twfe)) `, did: ` # Difference-in-Differences library(fixest) library(did) # Basic DID did_model <- feols(y ~ treat:post | id + time, data = df, vcov = ~id) # Event Study es_model <- feols(y ~ i(time, treat, ref = -1) | id + time, data = df, vcov = ~id) iplot(es_model) # Callaway-Sant'Anna (staggered) cs_did <- att_gt(yname = "y", tname = "time", idname = "id", gname = "first_treat", data = df) aggte(cs_did, type = "dynamic") |> ggdid() ` }; return { analysis_type: analysisType, r_code: templates[analysisType] || "# Analysis template not found\n# Use search_stats_knowledge for guidance" }; }
- src/tools/index.ts:350-361 (registration)The tool registration in the exported tools array, including name, description, and input schema for validation.name: "generate_r_code", description: "R 코드 생성 (분석, 진단, 시각화)", inputSchema: { type: "object", properties: { analysis_type: { type: "string", description: "분석 유형" }, variables: { type: "object", description: "변수 정보" }, options: { type: "object", description: "추가 옵션" }, }, required: ["analysis_type"], }, },
- src/tools/index.ts:832-833 (handler)The switch case in handleToolCall that routes calls to 'generate_r_code' to the specific handler function.case "generate_r_code": return handleGenerateRCode(args);