mirror of
https://github.com/hanzoai/skills.git
synced 2026-11-19 00:47:36 +00:00
Every page now loads /webmcp.js, scripts/webmcp.ts with its types stripped (npm run webmcp). Where the browser has document.modelContext it registers two read-only tools: search_skills over the published skills index, and get_skill, which returns a skill's markdown checked against the index digest. The markdown alternate link moves from build.py to site.py, so CI holds every page to it. The API catalog gains the site itself (llms.txt and the skills index) and hanzo.ai/agents; the live discovery check now fetches every catalog link. tests/site.mjs drives the tools through a recording modelContext, and checks a browser without one loads clean.
676 lines
32 KiB
HTML
676 lines
32 KiB
HTML
<!doctype html>
|
|
<html lang="en"><head>
|
|
<meta charset="utf-8"><meta name="viewport" content="width=device-width,initial-scale=1">
|
|
<title>research-quantitative-methods</title>
|
|
<meta name="color-scheme" content="dark">
|
|
<style>
|
|
/* Committed to one visual world: a dark instrument panel. No light variant —
|
|
the dot field and the hairline rules only read on black, and a naive
|
|
inversion would wash both out. Every colour is painted explicitly so the page
|
|
never borrows a host background. */
|
|
@font-face{font-family:'Zen';src:url('/fonts/Zen-Variable.woff2') format('woff2');font-weight:100 900;font-style:normal;font-display:swap}
|
|
@font-face{font-family:'Zen';src:url('/fonts/Zen-Italic-Variable.woff2') format('woff2');font-weight:100 900;font-style:italic;font-display:swap}
|
|
@font-face{font-family:'Zen Mono';src:url('/fonts/ZenMono-Variable.woff2') format('woff2');font-weight:100 900;font-style:normal;font-display:swap}
|
|
@font-face{font-family:'Zen Mono';src:url('/fonts/ZenMono-Italic-Variable.woff2') format('woff2');font-weight:100 900;font-style:italic;font-display:swap}
|
|
@font-face{font-family:'Zen Pixel Square';src:url('/fonts/ZenPixel-Square.woff2') format('woff2');font-style:normal;font-display:swap}
|
|
|
|
:root{
|
|
--ink:#f4f4f2; --soft:#9c9c98; --faint:#6a6a66; --ground:#000; --panel:#0a0a0a;
|
|
--line:#232323; --line-hi:#3a3a3a; --accent:#f4f4f2;
|
|
--font-zen-sans:'Zen',-apple-system,BlinkMacSystemFont,"Segoe UI",Helvetica,Arial,sans-serif;
|
|
--font-zen-mono:'Zen Mono',ui-monospace,"SF Mono",SFMono-Regular,Menlo,Consolas,monospace;
|
|
--font-zen-pixel-square:'Zen Pixel Square',monospace;
|
|
--mono:var(--font-zen-mono);
|
|
--sans:var(--font-zen-sans);
|
|
/* Controls are pills and surfaces are soft, as on hanzo.ai. */
|
|
--r:999px; --r-soft:14px;
|
|
}
|
|
*{box-sizing:border-box}
|
|
html{-webkit-text-size-adjust:100%}
|
|
body{margin:0;background:var(--ground);color:var(--ink);font-family:var(--sans);
|
|
font-size:16px;line-height:1.65;-webkit-font-smoothing:antialiased}
|
|
.wrap{max-width:60rem;margin:0 auto;padding:0 1.75rem}
|
|
|
|
/* The dot field. A halftone made of one repeating radial-gradient, faded out
|
|
along the diagonal by a mask so it reads as a wave rather than wallpaper. */
|
|
/* No overflow clip here: the dot field is inset to the box already, and a clip
|
|
would cut off the copy-prompt menu. */
|
|
.hero{position:relative;border-bottom:1px solid var(--line)}
|
|
.hero::before{content:"";position:absolute;inset:0;
|
|
background-image:radial-gradient(var(--line-hi) 1px, transparent 1px);
|
|
background-size:7px 7px;
|
|
-webkit-mask-image:linear-gradient(115deg,#000 0%,rgba(0,0,0,.55) 38%,transparent 72%);
|
|
mask-image:linear-gradient(115deg,#000 0%,rgba(0,0,0,.55) 38%,transparent 72%);
|
|
pointer-events:none}
|
|
.hero .wrap{position:relative;padding-top:5.5rem;padding-bottom:4rem}
|
|
|
|
nav{border-bottom:1px solid var(--line)}
|
|
nav .in{max-width:60rem;margin:0 auto;padding:1.1rem 1.75rem;display:flex;
|
|
justify-content:space-between;align-items:center;gap:1rem}
|
|
.brand{font-family:var(--mono);font-size:.85rem;letter-spacing:.14em;
|
|
text-transform:uppercase;color:var(--ink);text-decoration:none;
|
|
display:inline-flex;align-items:center;gap:.6rem}
|
|
.brand i{font-style:normal;color:var(--faint)}
|
|
.mark{width:1.15em;height:1.15em;display:block;flex:0 0 auto}
|
|
nav a.up{font-family:var(--mono);font-size:.72rem;letter-spacing:.14em;
|
|
text-transform:uppercase;color:var(--soft);text-decoration:none}
|
|
nav a.up:hover{color:var(--ink)}
|
|
|
|
h1{font-family:var(--mono);font-size:clamp(2rem,5.4vw,3.4rem);line-height:1.12;
|
|
letter-spacing:-.02em;margin:0 0 1.1rem;font-weight:500;text-wrap:balance}
|
|
h1 em{font-style:normal;color:var(--faint)}
|
|
.lede{color:var(--soft);font-size:1.05rem;max-width:52ch;margin:0 0 2.25rem}
|
|
|
|
.btns{display:flex;flex-wrap:wrap;gap:.7rem;margin:0 0 .5rem;align-items:center}
|
|
.btn{display:inline-flex;align-items:center;gap:.5rem;font-family:var(--mono);
|
|
font-size:.72rem;letter-spacing:.14em;text-transform:uppercase;
|
|
padding:.8rem 1.4rem;border:1px solid var(--line-hi);border-radius:var(--r);
|
|
color:var(--ink);text-decoration:none;background:transparent;cursor:pointer;
|
|
line-height:1.2;font-weight:400}
|
|
.btn:hover{background:#141414;text-decoration:none;border-color:var(--soft)}
|
|
.btn.solid{background:var(--ink);color:#000;border-color:var(--ink)}
|
|
.btn.solid:hover{background:#d8d8d4}
|
|
.btn .chev{width:.7em;height:.7em;flex:0 0 auto;transition:transform .15s ease}
|
|
.btn[aria-expanded="true"] .chev{transform:rotate(180deg)}
|
|
|
|
/* Copy-prompt selector. Each row copies the prompt for its own target. */
|
|
.copy{position:relative;display:inline-flex}
|
|
.menu{position:absolute;top:calc(100% + .5rem);left:0;z-index:20;min-width:21rem;
|
|
background:var(--panel);border:1px solid var(--line-hi);border-radius:var(--r-soft);
|
|
padding:.4rem;display:flex;flex-direction:column;
|
|
box-shadow:0 12px 32px rgba(0,0,0,.75)}
|
|
.menu[hidden]{display:none}
|
|
.mi{display:flex;align-items:center;gap:.7rem;width:100%;text-align:left;
|
|
padding:.62rem .8rem;border:0;border-radius:calc(var(--r-soft) - .35rem);
|
|
background:transparent;color:var(--ink);font-family:var(--mono);font-size:.76rem;
|
|
letter-spacing:.04em;text-decoration:none;cursor:pointer;line-height:1.3;
|
|
white-space:nowrap}
|
|
.mi:hover,.mi:focus-visible{background:#181818;outline:none}
|
|
.mi .mark{width:1.05rem;height:1.05rem;color:var(--soft)}
|
|
.mi:hover .mark{color:var(--ink)}
|
|
.mi u{margin-left:auto;color:var(--faint);text-decoration:none;font-size:.68rem;
|
|
letter-spacing:.1em;text-transform:uppercase}
|
|
|
|
/* Works-with bar: one scrolling row, name and mark per agent. */
|
|
.strip{border-bottom:1px solid var(--line);background:var(--panel)}
|
|
.strip .in{max-width:60rem;margin:0 auto;padding:.9rem 1.75rem;display:flex;
|
|
align-items:center;gap:1.25rem;font-family:var(--mono);font-size:.72rem;
|
|
letter-spacing:.1em;text-transform:uppercase;color:var(--faint)}
|
|
.strip b{color:var(--soft);font-weight:400;flex:0 0 auto;
|
|
border-right:1px solid var(--line);padding-right:1.25rem;align-self:stretch;
|
|
display:flex;align-items:center}
|
|
.bar{display:flex;align-items:center;gap:.5rem;flex:1 1 auto;min-width:0;
|
|
overflow-x:auto;scrollbar-width:none;scroll-snap-type:x proximity;
|
|
-webkit-overflow-scrolling:touch;
|
|
-webkit-mask-image:linear-gradient(90deg,#000 calc(100% - 3rem),transparent);
|
|
mask-image:linear-gradient(90deg,#000 calc(100% - 3rem),transparent)}
|
|
.bar::-webkit-scrollbar{display:none}
|
|
.agent{display:inline-flex;align-items:center;gap:.45rem;flex:0 0 auto;
|
|
padding:.4rem .85rem;border:1px solid var(--line);border-radius:var(--r);
|
|
color:var(--soft);white-space:nowrap;scroll-snap-align:start;
|
|
text-decoration:none;background:var(--ground)}
|
|
.agent:hover{color:var(--ink);border-color:var(--line-hi);text-decoration:none}
|
|
.agent .mark{width:.95rem;height:.95rem}
|
|
|
|
h2{font-family:var(--mono);font-size:1.1rem;letter-spacing:-.01em;margin:2.75rem 0 .75rem;font-weight:500}
|
|
h3{font-size:1rem;margin:2rem 0 .5rem;font-weight:600}
|
|
h4,h5,h6{font-size:.94rem;margin:1.5rem 0 .5rem;font-weight:600}
|
|
p{margin:0 0 1rem;max-width:70ch}
|
|
ul,ol{margin:0 0 1rem;padding-left:1.35rem;max-width:70ch}
|
|
li{margin:.22rem 0}
|
|
a{color:var(--ink);text-decoration:underline;text-underline-offset:3px;
|
|
text-decoration-color:var(--line-hi)}
|
|
a:hover{text-decoration-color:var(--ink)}
|
|
a:focus-visible{outline:1px solid var(--ink);outline-offset:3px}
|
|
code{font-family:var(--mono);font-size:.85em;background:var(--panel);
|
|
border:1px solid var(--line);border-radius:5px;padding:.1em .36em}
|
|
pre{background:var(--panel);border:1px solid var(--line);padding:1rem 1.1rem;
|
|
border-radius:var(--r-soft);overflow-x:auto;margin:0 0 1.25rem}
|
|
pre code{background:none;border:0;padding:0;font-size:.82rem;line-height:1.7}
|
|
blockquote{margin:0 0 1rem;padding-left:1rem;border-left:1px solid var(--line-hi);color:var(--soft)}
|
|
hr{border:0;border-top:1px solid var(--line);margin:2.5rem 0}
|
|
table{border-collapse:collapse;width:100%;margin:0 0 1.25rem;font-size:.92rem;
|
|
display:block;overflow-x:auto}
|
|
th,td{border:1px solid var(--line);padding:.5rem .7rem;text-align:left}
|
|
|
|
.curl{display:block;background:var(--panel);border:1px solid var(--line);
|
|
border-radius:var(--r-soft);padding:.95rem 1.1rem;font-family:var(--mono);
|
|
font-size:.8rem;color:var(--soft);overflow-x:auto;margin:0 0 2.5rem;
|
|
white-space:pre;line-height:1.9}
|
|
.curl b{color:var(--ink);font-weight:400}
|
|
.curl i{font-style:normal;color:var(--faint)}
|
|
|
|
/* index */
|
|
#q{width:100%;padding:.85rem 1.2rem;font-family:var(--mono);font-size:.85rem;color:var(--ink);
|
|
background:var(--panel);border:1px solid var(--line);border-radius:var(--r);margin:0 0 .6rem}
|
|
#q::placeholder{color:var(--faint);text-transform:uppercase;letter-spacing:.1em;font-size:.72rem}
|
|
#q:focus{outline:none;border-color:var(--soft)}
|
|
#count{color:var(--faint);font-family:var(--mono);font-size:.72rem;letter-spacing:.1em;
|
|
text-transform:uppercase;margin:0 0 2rem}
|
|
.cat{margin:2.5rem 0 .5rem;font-family:var(--mono);font-size:.7rem;letter-spacing:.16em;
|
|
text-transform:uppercase;color:var(--faint);border-bottom:1px solid var(--line);
|
|
padding-bottom:.45rem}
|
|
.row{display:block;padding:.6rem .9rem;margin:0 -.9rem;border-bottom:1px solid var(--line);
|
|
border-radius:var(--r-soft);text-decoration:none}
|
|
.row:hover{background:#0a0a0a}
|
|
.nm{font-family:var(--mono);font-size:.84rem;color:var(--ink);display:block}
|
|
.ds{color:var(--soft);font-size:.9rem;display:block;margin-top:.12rem}
|
|
footer{margin-top:4rem;padding:2rem 0 4rem;border-top:1px solid var(--line);
|
|
color:var(--faint);font-size:.85rem}
|
|
footer a{color:var(--soft)}
|
|
.fmap{display:grid;gap:1.75rem;grid-template-columns:repeat(auto-fit,minmax(11rem,1fr));
|
|
margin:0 0 2rem;font-size:.85rem}
|
|
.fmap b{display:block;font-family:var(--mono);font-size:.68rem;letter-spacing:.16em;
|
|
text-transform:uppercase;color:var(--faint);font-weight:400;margin:0 0 .6rem;
|
|
border-bottom:1px solid var(--line);padding-bottom:.4rem}
|
|
.fmap a{display:inline-block;color:var(--ink);text-decoration:none;margin:.3rem .35rem 0 0}
|
|
.fmap a:hover{text-decoration:underline}
|
|
.fmap div{color:var(--faint);line-height:1.5}
|
|
.fend{border-top:1px solid var(--line);padding-top:1.25rem;font-size:.82rem}
|
|
|
|
@media (max-width:640px){
|
|
.hero .wrap{padding-top:3.25rem;padding-bottom:2.75rem}
|
|
.btns{gap:.5rem}
|
|
.btn{padding:.75rem 1.15rem;font-size:.68rem}
|
|
.copy,.copy .btn{width:100%;justify-content:center}
|
|
.menu{left:0;right:0;min-width:0}
|
|
.strip .in{flex-direction:column;align-items:flex-start;gap:.6rem;padding:.9rem 1.75rem}
|
|
.strip b{border-right:0;padding-right:0}
|
|
.bar{width:100%}
|
|
}
|
|
</style>
|
|
<link rel="icon" href="/favicon.svg">
|
|
<link rel="canonical" href="https://hanzoskills.com/research-quantitative-methods">
|
|
<link rel="alternate" type="text/markdown" href="/research-quantitative-methods.md">
|
|
<script type="module" src="/webmcp.js"></script>
|
|
</head>
|
|
<!-- Reading this with a machine? Every page here is also plain markdown.
|
|
This one: https://hanzoskills.com/research-quantitative-methods.md
|
|
Index: https://hanzoskills.com/llms.txt
|
|
Corpus: https://hanzoskills.com/all.md
|
|
Append .md to any path for the markdown twin. -->
|
|
<body>
|
|
<nav><div class="in">
|
|
<a class="brand" href="/"><svg class="mark" viewBox="0 0 67 67" fill="currentColor" aria-hidden="true"><path d="M22.21 67V44.6369H0V67H22.21Z"/><path d="M66.7038 22.3184H22.2534L0.0878906 44.6367H44.4634L66.7038 22.3184Z"/><path d="M22.21 0H0V22.3184H22.21V0Z"/><path d="M66.7198 0H44.5098V22.3184H66.7198V0Z"/><path d="M66.7198 67V44.6369H44.5098V67H66.7198Z"/></svg> HANZO<i>SKILLS</i></a>
|
|
<a class="up" href="/">All skills</a>
|
|
</div></nav>
|
|
|
|
<div class="wrap">
|
|
<h1>research-quantitative-methods</h1><p class="lede">Master quantitative research methods including statistical analysis, surveys, experiments, and hypothesis testing</p><h1>Quantitative Research Methods Skill</h1>
|
|
<h2>When to Use This Skill</h2>
|
|
<p>Use this skill when you need to:</p>
|
|
<ul><li>Design quantitative research studies</li><li>Conduct statistical hypothesis testing</li><li>Analyze survey data</li><li>Run experiments and measure effects</li><li>Perform correlation and regression analyses</li><li>Test causal relationships</li><li>Validate measurement instruments</li><li>Report quantitative findings</li></ul>
|
|
<h2>Core Quantitative Approaches</h2>
|
|
<h3>1. Experimental Design</h3>
|
|
<p><strong>Purpose</strong>: Test causal relationships through controlled manipulation</p>
|
|
<p><strong>Design Types</strong>:</p>
|
|
<pre><code class="lang-python">from dataclasses import dataclass
|
|
from typing import List, Optional
|
|
import pandas as pd
|
|
import numpy as np
|
|
from scipy import stats
|
|
|
|
@dataclass
|
|
class ExperimentDesign:
|
|
"""Define experimental design parameters"""
|
|
design_type: str # 'between', 'within', 'mixed', 'factorial'
|
|
independent_vars: List[str]
|
|
dependent_vars: List[str]
|
|
n_per_group: int
|
|
random_assignment: bool = True
|
|
blocking_var: Optional[str] = None
|
|
|
|
def calculate_power(self, effect_size, alpha=0.05):
|
|
"""Calculate statistical power"""
|
|
from statsmodels.stats.power import TTestIndPower
|
|
|
|
if self.design_type == 'between':
|
|
analysis = TTestIndPower()
|
|
power = analysis.power(effect_size=effect_size,
|
|
nobs1=self.n_per_group,
|
|
alpha=alpha,
|
|
alternative='two-sided')
|
|
return power
|
|
else:
|
|
raise NotImplementedError(f"{self.design_type} power calculation")
|
|
|
|
def required_n(self, effect_size, power=0.80, alpha=0.05):
|
|
"""Calculate required sample size"""
|
|
from statsmodels.stats.power import TTestIndPower
|
|
|
|
if self.design_type == 'between':
|
|
analysis = TTestIndPower()
|
|
n = analysis.solve_power(effect_size=effect_size,
|
|
power=power,
|
|
alpha=alpha,
|
|
alternative='two-sided')
|
|
return int(np.ceil(n))
|
|
else:
|
|
raise NotImplementedError(f"{self.design_type} sample size")
|
|
|
|
# Example: Between-subjects experiment
|
|
design = ExperimentDesign(
|
|
design_type='between',
|
|
independent_vars=['treatment_condition'],
|
|
dependent_vars=['outcome_score'],
|
|
n_per_group=50
|
|
)
|
|
|
|
print(f"Power for d=0.5: {design.calculate_power(0.5):.3f}")
|
|
print(f"Required N for 80% power: {design.required_n(0.5)}")</code></pre>
|
|
<h3>2. Survey Methods</h3>
|
|
<p><strong>Purpose</strong>: Collect self-report data from samples or populations</p>
|
|
<p><strong>Survey Design Implementation</strong>:</p>
|
|
<pre><code class="lang-python">import pandas as pd
|
|
import numpy as np
|
|
from scipy import stats
|
|
|
|
class SurveyAnalysis:
|
|
"""Analyze survey data with proper weighting and missing data"""
|
|
|
|
def __init__(self, data: pd.DataFrame):
|
|
self.data = data
|
|
self.weights = None
|
|
|
|
def set_weights(self, weight_col: str):
|
|
"""Set sampling weights"""
|
|
self.weights = self.data[weight_col]
|
|
|
|
def weighted_mean(self, var: str):
|
|
"""Calculate weighted mean"""
|
|
if self.weights is None:
|
|
return self.data[var].mean()
|
|
|
|
weighted_sum = (self.data[var] * self.weights).sum()
|
|
return weighted_sum / self.weights.sum()
|
|
|
|
def weighted_proportion(self, var: str, value):
|
|
"""Calculate weighted proportion"""
|
|
if self.weights is None:
|
|
return (self.data[var] == value).mean()
|
|
|
|
matches = (self.data[var] == value).astype(int)
|
|
weighted_sum = (matches * self.weights).sum()
|
|
return weighted_sum / self.weights.sum()
|
|
|
|
def cronbach_alpha(self, items: List[str]):
|
|
"""Calculate reliability (internal consistency)"""
|
|
item_data = self.data[items].dropna()
|
|
n_items = len(items)
|
|
|
|
# Variance of each item
|
|
item_vars = item_data.var(axis=0, ddof=1)
|
|
|
|
# Variance of total score
|
|
total_var = item_data.sum(axis=1).var(ddof=1)
|
|
|
|
# Cronbach's alpha
|
|
alpha = (n_items / (n_items - 1)) * (1 - item_vars.sum() / total_var)
|
|
return alpha
|
|
|
|
def response_rate_bias(self, response_indicator: str,
|
|
demographic_vars: List[str]):
|
|
"""Test for response bias"""
|
|
results = {}
|
|
|
|
for var in demographic_vars:
|
|
# Compare respondents vs non-respondents
|
|
respondents = self.data[self.data[response_indicator] == 1][var]
|
|
non_respondents = self.data[self.data[response_indicator] == 0][var]
|
|
|
|
# T-test or chi-square depending on variable type
|
|
if self.data[var].dtype in ['int64', 'float64']:
|
|
stat, p = stats.ttest_ind(respondents, non_respondents,
|
|
nan_policy='omit')
|
|
test = 't-test'
|
|
else:
|
|
# Chi-square for categorical
|
|
contingency = pd.crosstab(self.data[response_indicator],
|
|
self.data[var])
|
|
stat, p, _, _ = stats.chi2_contingency(contingency)
|
|
test = 'chi-square'
|
|
|
|
results[var] = {
|
|
'test': test,
|
|
'statistic': stat,
|
|
'p_value': p,
|
|
'biased': p < 0.05
|
|
}
|
|
|
|
return results
|
|
|
|
# Example usage
|
|
survey = SurveyAnalysis(pd.DataFrame({
|
|
'satisfaction': [4, 5, 3, 4, 5, 2, 4, 5, 3, 4],
|
|
'loyalty': [5, 5, 4, 4, 5, 3, 4, 5, 4, 4],
|
|
'recommend': [4, 5, 3, 5, 5, 2, 4, 5, 3, 5],
|
|
'weight': [1.2, 0.8, 1.0, 1.1, 0.9, 1.3, 1.0, 0.9, 1.1, 1.0]
|
|
}))
|
|
|
|
survey.set_weights('weight')
|
|
print(f"Weighted mean satisfaction: {survey.weighted_mean('satisfaction'):.2f}")
|
|
|
|
# Calculate scale reliability
|
|
alpha = survey.cronbach_alpha(['satisfaction', 'loyalty', 'recommend'])
|
|
print(f"Cronbach's alpha: {alpha:.3f}")</code></pre>
|
|
<h3>3. Hypothesis Testing</h3>
|
|
<p><strong>Purpose</strong>: Make statistical inferences about populations</p>
|
|
<p><strong>Common Tests Implementation</strong>:</p>
|
|
<pre><code class="lang-python">import pandas as pd
|
|
import numpy as np
|
|
from scipy import stats
|
|
from typing import Tuple
|
|
|
|
class HypothesisTests:
|
|
"""Common hypothesis tests with effect sizes"""
|
|
|
|
@staticmethod
|
|
def t_test_independent(group1, group2, alpha=0.05):
|
|
"""Independent samples t-test with Cohen's d"""
|
|
# Remove NaN values
|
|
g1 = np.array(group1)[~np.isnan(group1)]
|
|
g2 = np.array(group2)[~np.isnan(group2)]
|
|
|
|
# T-test
|
|
t_stat, p_value = stats.ttest_ind(g1, g2)
|
|
|
|
# Cohen's d
|
|
pooled_std = np.sqrt(((len(g1)-1)*np.var(g1, ddof=1) +
|
|
(len(g2)-1)*np.var(g2, ddof=1)) /
|
|
(len(g1) + len(g2) - 2))
|
|
cohens_d = (np.mean(g1) - np.mean(g2)) / pooled_std
|
|
|
|
# Confidence interval
|
|
se_diff = pooled_std * np.sqrt(1/len(g1) + 1/len(g2))
|
|
df = len(g1) + len(g2) - 2
|
|
t_crit = stats.t.ppf(1 - alpha/2, df)
|
|
ci = (np.mean(g1) - np.mean(g2) - t_crit * se_diff,
|
|
np.mean(g1) - np.mean(g2) + t_crit * se_diff)
|
|
|
|
return {
|
|
't_statistic': t_stat,
|
|
'p_value': p_value,
|
|
'cohens_d': cohens_d,
|
|
'mean_diff': np.mean(g1) - np.mean(g2),
|
|
'ci_95': ci,
|
|
'significant': p_value < alpha
|
|
}
|
|
|
|
@staticmethod
|
|
def t_test_paired(before, after, alpha=0.05):
|
|
"""Paired samples t-test with Cohen's d"""
|
|
differences = np.array(after) - np.array(before)
|
|
differences = differences[~np.isnan(differences)]
|
|
|
|
# T-test
|
|
t_stat, p_value = stats.ttest_1samp(differences, 0)
|
|
|
|
# Cohen's d for paired samples
|
|
cohens_d = np.mean(differences) / np.std(differences, ddof=1)
|
|
|
|
# Confidence interval
|
|
se = stats.sem(differences)
|
|
df = len(differences) - 1
|
|
t_crit = stats.t.ppf(1 - alpha/2, df)
|
|
ci = (np.mean(differences) - t_crit * se,
|
|
np.mean(differences) + t_crit * se)
|
|
|
|
return {
|
|
't_statistic': t_stat,
|
|
'p_value': p_value,
|
|
'cohens_d': cohens_d,
|
|
'mean_diff': np.mean(differences),
|
|
'ci_95': ci,
|
|
'significant': p_value < alpha
|
|
}
|
|
|
|
@staticmethod
|
|
def anova_oneway(groups, alpha=0.05):
|
|
"""One-way ANOVA with eta-squared"""
|
|
# F-test
|
|
f_stat, p_value = stats.f_oneway(*groups)
|
|
|
|
# Effect size (eta-squared)
|
|
grand_mean = np.mean([x for group in groups for x in group])
|
|
ss_between = sum(len(g) * (np.mean(g) - grand_mean)**2
|
|
for g in groups)
|
|
ss_total = sum((x - grand_mean)**2
|
|
for group in groups for x in group)
|
|
eta_squared = ss_between / ss_total
|
|
|
|
return {
|
|
'f_statistic': f_stat,
|
|
'p_value': p_value,
|
|
'eta_squared': eta_squared,
|
|
'significant': p_value < alpha
|
|
}
|
|
|
|
@staticmethod
|
|
def chi_square_test(observed_freq, expected_freq=None, alpha=0.05):
|
|
"""Chi-square goodness of fit or independence"""
|
|
if expected_freq is None:
|
|
# Equal expected frequencies
|
|
expected_freq = np.ones_like(observed_freq) * np.mean(observed_freq)
|
|
|
|
chi2_stat, p_value = stats.chisquare(observed_freq, expected_freq)
|
|
|
|
# Effect size (Cramer's V for 2x2 table)
|
|
n = np.sum(observed_freq)
|
|
cramers_v = np.sqrt(chi2_stat / n)
|
|
|
|
return {
|
|
'chi2_statistic': chi2_stat,
|
|
'p_value': p_value,
|
|
'cramers_v': cramers_v,
|
|
'significant': p_value < alpha
|
|
}
|
|
|
|
# Example usage
|
|
control = [23, 25, 21, 24, 22, 20, 23, 24, 22, 21]
|
|
treatment = [28, 30, 26, 29, 31, 27, 30, 28, 29, 30]
|
|
|
|
result = HypothesisTests.t_test_independent(control, treatment)
|
|
print(f"t({len(control)+len(treatment)-2}) = {result['t_statistic']:.3f}, "
|
|
f"p = {result['p_value']:.3f}, d = {result['cohens_d']:.3f}")</code></pre>
|
|
<h3>4. Regression Analysis</h3>
|
|
<p><strong>Purpose</strong>: Model relationships between variables</p>
|
|
<p><strong>Implementation</strong>:</p>
|
|
<pre><code class="lang-python">import pandas as pd
|
|
import numpy as np
|
|
from sklearn.linear_model import LinearRegression
|
|
from sklearn.metrics import r2_score, mean_squared_error
|
|
from scipy import stats
|
|
|
|
class RegressionAnalysis:
|
|
"""Multiple regression with diagnostics"""
|
|
|
|
def __init__(self, data: pd.DataFrame, outcome: str, predictors: List[str]):
|
|
self.data = data.dropna(subset=[outcome] + predictors)
|
|
self.outcome = outcome
|
|
self.predictors = predictors
|
|
self.model = None
|
|
self.results = {}
|
|
|
|
def fit(self):
|
|
"""Fit regression model"""
|
|
X = self.data[self.predictors]
|
|
y = self.data[self.outcome]
|
|
|
|
self.model = LinearRegression()
|
|
self.model.fit(X, y)
|
|
|
|
# Predictions
|
|
y_pred = self.model.predict(X)
|
|
residuals = y - y_pred
|
|
|
|
# Model fit statistics
|
|
self.results['r_squared'] = r2_score(y, y_pred)
|
|
self.results['adj_r_squared'] = 1 - (1 - self.results['r_squared']) * \
|
|
(len(y) - 1) / (len(y) - len(self.predictors) - 1)
|
|
self.results['rmse'] = np.sqrt(mean_squared_error(y, y_pred))
|
|
|
|
# Coefficients with significance tests
|
|
n = len(y)
|
|
k = len(self.predictors)
|
|
mse = np.sum(residuals**2) / (n - k - 1)
|
|
|
|
X_with_intercept = np.column_stack([np.ones(n), X])
|
|
var_coef = mse * np.linalg.inv(X_with_intercept.T @ X_with_intercept).diagonal()
|
|
se_coef = np.sqrt(var_coef)
|
|
|
|
t_stats = np.concatenate([[self.model.intercept_], self.model.coef_]) / se_coef
|
|
p_values = 2 * (1 - stats.t.cdf(np.abs(t_stats), n - k - 1))
|
|
|
|
self.results['coefficients'] = pd.DataFrame({
|
|
'Variable': ['Intercept'] + self.predictors,
|
|
'B': np.concatenate([[self.model.intercept_], self.model.coef_]),
|
|
'SE': se_coef,
|
|
't': t_stats,
|
|
'p': p_values
|
|
})
|
|
|
|
return self
|
|
|
|
def standardized_coefficients(self):
|
|
"""Calculate standardized (beta) coefficients"""
|
|
X = self.data[self.predictors]
|
|
y = self.data[self.outcome]
|
|
|
|
# Standardize variables
|
|
X_std = (X - X.mean()) / X.std()
|
|
y_std = (y - y.mean()) / y.std()
|
|
|
|
model_std = LinearRegression()
|
|
model_std.fit(X_std, y_std)
|
|
|
|
return pd.DataFrame({
|
|
'Variable': self.predictors,
|
|
'Beta': model_std.coef_
|
|
})
|
|
|
|
def vif(self):
|
|
"""Calculate variance inflation factors (multicollinearity)"""
|
|
from statsmodels.stats.outliers_influence import variance_inflation_factor
|
|
|
|
X = self.data[self.predictors]
|
|
vif_data = pd.DataFrame({
|
|
'Variable': self.predictors,
|
|
'VIF': [variance_inflation_factor(X.values, i)
|
|
for i in range(len(self.predictors))]
|
|
})
|
|
return vif_data
|
|
|
|
def diagnostics(self):
|
|
"""Run regression diagnostics"""
|
|
X = self.data[self.predictors]
|
|
y = self.data[self.outcome]
|
|
y_pred = self.model.predict(X)
|
|
residuals = y - y_pred
|
|
|
|
diagnostics = {
|
|
'normality': stats.shapiro(residuals)[1], # p-value
|
|
'durbin_watson': self._durbin_watson(residuals),
|
|
'homoscedasticity': self._breusch_pagan(residuals, y_pred)
|
|
}
|
|
|
|
return diagnostics
|
|
|
|
@staticmethod
|
|
def _durbin_watson(residuals):
|
|
"""Durbin-Watson test for autocorrelation"""
|
|
diff_resid = np.diff(residuals)
|
|
return np.sum(diff_resid**2) / np.sum(residuals**2)
|
|
|
|
@staticmethod
|
|
def _breusch_pagan(residuals, fitted):
|
|
"""Breusch-Pagan test for heteroscedasticity"""
|
|
# Simplified version
|
|
aux_model = LinearRegression()
|
|
aux_model.fit(fitted.reshape(-1, 1), residuals**2)
|
|
r2 = r2_score(residuals**2, aux_model.predict(fitted.reshape(-1, 1)))
|
|
lm = len(residuals) * r2
|
|
p_value = 1 - stats.chi2.cdf(lm, 1)
|
|
return p_value
|
|
|
|
# Example usage
|
|
data = pd.DataFrame({
|
|
'sales': [100, 150, 120, 180, 200, 160, 140, 190, 170, 155],
|
|
'advertising': [10, 15, 12, 18, 20, 16, 14, 19, 17, 15],
|
|
'price': [50, 45, 48, 42, 40, 44, 46, 41, 43, 45],
|
|
'competition': [5, 3, 4, 2, 1, 3, 4, 2, 3, 3]
|
|
})
|
|
|
|
regression = RegressionAnalysis(
|
|
data=data,
|
|
outcome='sales',
|
|
predictors=['advertising', 'price', 'competition']
|
|
).fit()
|
|
|
|
print(f"R² = {regression.results['r_squared']:.3f}")
|
|
print(regression.results['coefficients'])</code></pre>
|
|
<h2>Quantitative Patterns</h2>
|
|
<h3>Effective Quantitative Research</h3>
|
|
<pre><code>✓ Clear hypotheses stated a priori
|
|
✓ Appropriate sample size (power analysis)
|
|
✓ Random sampling or assignment when possible
|
|
✓ Valid and reliable measures
|
|
✓ Assumptions checked before analysis
|
|
✓ Effect sizes reported alongside p-values
|
|
✓ Multiple testing corrections when needed
|
|
✓ Confidence intervals provided</code></pre>
|
|
<h3>Ineffective Quantitative Research</h3>
|
|
<pre><code>✗ P-hacking (trying tests until significant)
|
|
✗ HARKing (hypothesizing after results known)
|
|
✗ Ignoring violated assumptions
|
|
✗ Confusing significance with importance
|
|
✗ Cherry-picking analyses to report
|
|
✗ Insufficient sample size
|
|
✗ Measurement without validity evidence</code></pre>
|
|
<h2>Best Practices</h2>
|
|
<h3>1. Study Design</h3>
|
|
<ul><li>Conduct power analysis before data collection</li><li>Use random assignment for causal inference</li><li>Control for confounding variables</li><li>Pilot test instruments</li><li>Pre-register analysis plan</li></ul>
|
|
<h3>2. Measurement</h3>
|
|
<ul><li>Use validated instruments when available</li><li>Report reliability (Cronbach's alpha, test-retest)</li><li>Assess validity (construct, criterion, content)</li><li>Check for measurement invariance across groups</li><li>Consider multiple methods to measure constructs</li></ul>
|
|
<h3>3. Analysis</h3>
|
|
<ul><li>Check assumptions before running tests</li><li>Report descriptive statistics first</li><li>Use appropriate tests for data type and design</li><li>Report effect sizes, not just p-values</li><li>Provide confidence intervals</li><li>Correct for multiple comparisons</li><li>Conduct sensitivity analyses</li></ul>
|
|
<h3>4. Reporting</h3>
|
|
<ul><li>Follow APA or journal guidelines</li><li>Report all tested hypotheses</li><li>Provide sufficient detail for replication</li><li>Share data and code when possible</li><li>Discuss practical significance</li><li>Acknowledge limitations</li></ul>
|
|
<h2>Related Skills</h2>
|
|
<ul><li><strong>research-design</strong>: Planning quantitative studies</li><li><strong>data-collection</strong>: Survey and experiment implementation</li><li><strong>data-analysis</strong>: Advanced statistical techniques</li><li><strong>research-synthesis</strong>: Meta-analysis of quantitative studies</li><li><strong>research-writing</strong>: Reporting quantitative results</li></ul>
|
|
<h2>Quick Reference</h2>
|
|
<h3>Sample Size Rules of Thumb</h3>
|
|
<pre><code>Correlation: N ≥ 30 for detection, N ≥ 85 for stable estimates
|
|
T-test: N ≥ 50 per group for d=0.5, 80% power
|
|
ANOVA: N ≥ 30 per group minimum
|
|
Regression: N ≥ 104 + k (k = predictors) for R²
|
|
N ≥ 50 + 8k for individual coefficients</code></pre>
|
|
<h3>Effect Size Interpretation (Cohen's benchmarks)</h3>
|
|
<pre><code>Cohen's d: Small = 0.2, Medium = 0.5, Large = 0.8
|
|
Correlation r: Small = 0.1, Medium = 0.3, Large = 0.5
|
|
Eta-squared: Small = 0.01, Medium = 0.06, Large = 0.14</code></pre>
|
|
<h3>Assumption Checks</h3>
|
|
<pre><code>Normality: Shapiro-Wilk test, Q-Q plot
|
|
Homogeneity: Levene's test, residual plots
|
|
Independence: Durbin-Watson statistic
|
|
Linearity: Scatterplots, residual plots
|
|
Multicollinearity: VIF < 10, tolerance > 0.1</code></pre>
|
|
<footer>
|
|
<div class="fmap">
|
|
<div><b>Build</b>
|
|
<a href="https://hanzo.app">hanzo.app</a> app builder
|
|
<a href="https://hanzo.codes">hanzo.codes</a> code with an agent
|
|
<a href="https://hanzoskills.com">hanzoskills.com</a> what an agent reads</div>
|
|
<div><b>Run</b>
|
|
<a href="https://console.hanzo.ai">console.hanzo.ai</a> cloud console
|
|
<a href="https://api.hanzo.ai">api.hanzo.ai</a> the API</div>
|
|
<div><b>Account</b>
|
|
<a href="https://hanzo.id">hanzo.id</a> sign in
|
|
<a href="https://pay.hanzo.ai">pay.hanzo.ai</a> top up
|
|
<a href="https://billing.hanzo.ai">billing.hanzo.ai</a> invoices and usage</div>
|
|
<div><b>Company</b>
|
|
<a href="https://hanzo.ai">hanzo.ai</a> canonical
|
|
<a href="https://hanzo.ventures">hanzo.ventures</a> ventures</div>
|
|
</div>
|
|
<div class="fend"><a href="/research-quantitative-methods.md">This page as plain text</a> · <a href="/llms.txt">Index for agents</a></div>
|
|
</footer>
|
|
</div></body></html> |