{"spec_id":"coefficient-confidence","library":"muix","language":"javascript","code":"// anyplot.ai\n// coefficient-confidence: Coefficient Plot with Confidence Intervals\n// Library: muix 9.16.0 | JavaScript 22.23.3\n// Quality: 86/100 | Updated: 2026-10-24\n\nimport Box from \"@mui/material/Box\";\nimport Typography from \"@mui/material/Typography\";\nimport { ChartsContainer } from \"@mui/x-charts/ChartsContainer\";\nimport { ScatterPlot } from \"@mui/x-charts/ScatterChart\";\nimport { ChartsXAxis } from \"@mui/x-charts/ChartsXAxis\";\nimport { ChartsYAxis } from \"@mui/x-charts/ChartsYAxis\";\nimport { ChartsGrid } from \"@mui/x-charts/ChartsGrid\";\nimport { ChartsReferenceLine } from \"@mui/x-charts/ChartsReferenceLine\";\nimport { useXScale, useYScale } from \"@mui/x-charts/hooks\";\n\nconst t = window.ANYPLOT_TOKENS;\n\n// Constants\nconst TITLE = \"coefficient-confidence · javascript · muix · anyplot.ai\";\nconst FONT = \"-apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Helvetica, Arial, sans-serif\";\nconst SIG_COLOR = t.palette[0];\nconst NON_SIG_COLOR = t.inkSoft;\nconst X_PAD_SHARE = 0.14;\nconst WHISKER_CAP = 9;\n\n// Columns: set these to your data's column names\nconst VARIABLE = \"Predictor variable\"; // role: variable\nconst COEFFICIENT = \"Coefficient estimate (standardized β)\"; // role: coefficient\nconst CI_LOWER = \"95% CI lower\"; // role: ci_lower\nconst CI_UPPER = \"95% CI upper\"; // role: ci_upper\n\n// Data: replace this block with your own records\nconst data = [\n  { [VARIABLE]: \"Proximity to Highway\", [COEFFICIENT]: -0.04, [CI_LOWER]: -0.19, [CI_UPPER]: 0.11 },\n  { [VARIABLE]: \"Number of Bedrooms\", [COEFFICIENT]: -0.05, [CI_LOWER]: -0.21, [CI_UPPER]: 0.11 },\n  { [VARIABLE]: \"Garage Spaces\", [COEFFICIENT]: 0.08, [CI_LOWER]: -0.09, [CI_UPPER]: 0.25 },\n  { [VARIABLE]: \"Lot Size\", [COEFFICIENT]: 0.11, [CI_LOWER]: -0.06, [CI_UPPER]: 0.28 },\n  { [VARIABLE]: \"Renovated (Yes/No)\", [COEFFICIENT]: 0.15, [CI_LOWER]: -0.02, [CI_UPPER]: 0.32 },\n  { [VARIABLE]: \"Number of Bathrooms\", [COEFFICIENT]: 0.19, [CI_LOWER]: 0.05, [CI_UPPER]: 0.33 },\n  { [VARIABLE]: \"Age of Home\", [COEFFICIENT]: -0.22, [CI_LOWER]: -0.35, [CI_UPPER]: -0.09 },\n  { [VARIABLE]: \"Crime Rate\", [COEFFICIENT]: -0.29, [CI_LOWER]: -0.44, [CI_UPPER]: -0.14 },\n  { [VARIABLE]: \"Distance to City Center\", [COEFFICIENT]: -0.34, [CI_LOWER]: -0.47, [CI_UPPER]: -0.21 },\n  { [VARIABLE]: \"School Rating\", [COEFFICIENT]: 0.38, [CI_LOWER]: 0.24, [CI_UPPER]: 0.52 },\n  { [VARIABLE]: \"Square Footage\", [COEFFICIENT]: 0.52, [CI_LOWER]: 0.41, [CI_UPPER]: 0.63 },\n];\n\n// Plot: reads only data and the constants above\nconst COEFFICIENTS = [...data]\n  .sort((a, b) => Math.abs(Number(a[COEFFICIENT])) - Math.abs(Number(b[COEFFICIENT])))\n  .map((d, i) => ({\n    i,\n    variable: String(d[VARIABLE]),\n    coefficient: Number(d[COEFFICIENT]),\n    ciLower: Number(d[CI_LOWER]),\n    ciUpper: Number(d[CI_UPPER]),\n    significant: Number(d[CI_LOWER]) > 0 || Number(d[CI_UPPER]) < 0,\n  }));\n\nconst VARIABLES = COEFFICIENTS.map((d) => d.variable);\nconst N = COEFFICIENTS.length;\nconst N_SIG = COEFFICIENTS.filter((d) => d.significant).length;\nconst SUBTITLE = `${N} predictors · ${N_SIG} significant (${CI_LOWER} to ${CI_UPPER})`;\n\nconst rawMin = Math.min(0, ...COEFFICIENTS.map((d) => d.ciLower));\nconst rawMax = Math.max(0, ...COEFFICIENTS.map((d) => d.ciUpper));\nconst rawSpan = rawMax - rawMin;\nconst xMin = rawMin - rawSpan * X_PAD_SHARE;\nconst xMax = rawMax + rawSpan * X_PAD_SHARE;\n\nconst significantPoints = COEFFICIENTS.filter((d) => d.significant).map((d) => ({\n  id: d.i,\n  x: d.coefficient,\n  y: d.i,\n}));\nconst notSignificantPoints = COEFFICIENTS.filter((d) => !d.significant).map((d) => ({\n  id: d.i,\n  x: d.coefficient,\n  y: d.i,\n}));\n\nconst LEGEND_ITEMS = [\n  { label: \"Significant (CI excludes 0)\", color: SIG_COLOR },\n  { label: \"Not significant (CI includes 0)\", color: NON_SIG_COLOR },\n];\n\nfunction ConfidenceWhiskers() {\n  const xScale = useXScale(\"x\") as any;\n  const yScale = useYScale(\"y\") as any;\n\n  return (\n    <g>\n      {COEFFICIENTS.map((d) => {\n        const x1 = xScale(d.ciLower);\n        const x2 = xScale(d.ciUpper);\n        const y = yScale(d.i);\n        const color = d.significant ? SIG_COLOR : NON_SIG_COLOR;\n        return (\n          <g key={d.variable}>\n            <line\n              x1={x1}\n              y1={y}\n              x2={x2}\n              y2={y}\n              stroke={color}\n              strokeWidth={3}\n              strokeLinecap=\"round\"\n            />\n            <line\n              x1={x1}\n              y1={y - WHISKER_CAP}\n              x2={x1}\n              y2={y + WHISKER_CAP}\n              stroke={color}\n              strokeWidth={3}\n              strokeLinecap=\"round\"\n            />\n            <line\n              x1={x2}\n              y1={y - WHISKER_CAP}\n              x2={x2}\n              y2={y + WHISKER_CAP}\n              stroke={color}\n              strokeWidth={3}\n              strokeLinecap=\"round\"\n            />\n          </g>\n        );\n      })}\n    </g>\n  );\n}\n\nexport default function Chart() {\n  const W = window.ANYPLOT_SIZE.width;\n  const H = window.ANYPLOT_SIZE.height;\n  const chartW = W - 48;\n  const chartH = H - 150;\n\n  return (\n    <Box\n      sx={{\n        width: W,\n        height: H,\n        bgcolor: t.pageBg,\n        color: t.ink,\n        fontFamily: FONT,\n        display: \"flex\",\n        flexDirection: \"column\",\n        alignItems: \"center\",\n        pt: \"18px\",\n        px: \"24px\",\n        boxSizing: \"border-box\",\n      }}\n    >\n      <Typography\n        sx={{\n          fontSize: 22,\n          fontWeight: 700,\n          color: t.ink,\n          fontFamily: FONT,\n          textAlign: \"center\",\n          lineHeight: 1.2,\n        }}\n      >\n        {TITLE}\n      </Typography>\n      <Typography\n        sx={{\n          fontSize: 15,\n          color: t.inkSoft,\n          fontFamily: FONT,\n          textAlign: \"center\",\n          mt: \"4px\",\n          mb: \"4px\",\n        }}\n      >\n        {SUBTITLE}\n      </Typography>\n\n      <ChartsContainer\n        width={chartW}\n        height={chartH}\n        skipAnimation\n        series={[\n          {\n            type: \"scatter\",\n            id: \"significant\",\n            label: LEGEND_ITEMS[0].label,\n            data: significantPoints,\n            color: SIG_COLOR,\n            markerSize: 8,\n            xAxisId: \"x\",\n            yAxisId: \"y\",\n          },\n          {\n            type: \"scatter\",\n            id: \"notSignificant\",\n            label: LEGEND_ITEMS[1].label,\n            data: notSignificantPoints,\n            color: NON_SIG_COLOR,\n            markerSize: 8,\n            xAxisId: \"x\",\n            yAxisId: \"y\",\n          },\n        ]}\n        xAxis={[\n          {\n            id: \"x\",\n            min: xMin,\n            max: xMax,\n            height: 60,\n            label: COEFFICIENT,\n            labelStyle: { fontSize: 16, fill: t.ink, fontFamily: FONT },\n            tickLabelStyle: { fontSize: 14, fill: t.inkSoft, fontFamily: FONT },\n          },\n        ]}\n        yAxis={[\n          {\n            id: \"y\",\n            min: -0.5,\n            max: N - 0.5,\n            width: 230,\n            tickInterval: COEFFICIENTS.map((d) => d.i),\n            disableLine: true,\n            disableTicks: true,\n            valueFormatter: (v: number) => VARIABLES[Math.round(v)] ?? \"\",\n            tickLabelStyle: { fontSize: 15, fill: t.inkSoft, fontFamily: FONT },\n          },\n        ]}\n        margin={{ top: 28, bottom: 16, left: 20, right: 40 }}\n        sx={{\n          \"& .MuiChartsAxis-line\": { stroke: t.inkSoft },\n          \"& .MuiChartsAxis-tick\": { stroke: t.inkSoft },\n          \"& .MuiChartsGrid-line\": { stroke: t.grid, strokeDasharray: \"4 4\" },\n        }}\n      >\n        <ChartsGrid vertical />\n        <ChartsReferenceLine\n          x={0}\n          axisId=\"x\"\n          label=\"No effect (0)\"\n          labelAlign=\"start\"\n          lineStyle={{ stroke: t.inkSoft, strokeDasharray: \"6 4\", strokeWidth: 1.5 }}\n          labelStyle={{ fontSize: 14, fill: t.inkSoft, fontStyle: \"italic\", fontFamily: FONT }}\n        />\n        <ConfidenceWhiskers />\n        <ScatterPlot />\n        <ChartsXAxis axisId=\"x\" />\n        <ChartsYAxis axisId=\"y\" />\n      </ChartsContainer>\n\n      <Box\n        sx={{\n          display: \"flex\",\n          gap: \"28px\",\n          alignItems: \"center\",\n          justifyContent: \"center\",\n          mt: \"4px\",\n        }}\n      >\n        {LEGEND_ITEMS.map((item) => (\n          <Box key={item.label} sx={{ display: \"flex\", alignItems: \"center\", gap: \"8px\" }}>\n            <svg width={24} height={14}>\n              <line x1={2} y1={7} x2={22} y2={7} stroke={item.color} strokeWidth={3} strokeLinecap=\"round\" />\n              <circle cx={12} cy={7} r={5.5} fill={item.color} />\n            </svg>\n            <Typography sx={{ fontSize: 14, color: t.ink, fontFamily: FONT }}>{item.label}</Typography>\n          </Box>\n        ))}\n      </Box>\n    </Box>\n  );\n}\n"}