<?xml version="1.0" encoding="UTF-8"?>
<rss  xmlns:atom="http://www.w3.org/2005/Atom" 
      xmlns:media="http://search.yahoo.com/mrss/" 
      xmlns:content="http://purl.org/rss/1.0/modules/content/" 
      xmlns:dc="http://purl.org/dc/elements/1.1/" 
      version="2.0">
<channel>
<title>统计学原理</title>
<link>https://coursest.netlify.app/course-topics.html</link>
<atom:link href="https://coursest.netlify.app/course-topics.xml" rel="self" type="application/rss+xml"/>
<description></description>
<generator>quarto-1.10.18</generator>
<lastBuildDate>Fri, 26 Apr 2024 16:00:00 GMT</lastBuildDate>
<item>
  <title>尺度变换与方差计算</title>
  <dc:creator>胡华平 </dc:creator>
  <link>https://coursest.netlify.app/topics/2024-04-27-variance-scales/</link>
  <description><![CDATA[ 





<section id="尺度变换" class="level2">
<h2 class="anchored" data-anchor-id="尺度变换">尺度变换</h2>
<p>考虑如下的数据尺度变换</p>
<ul>
<li><p>给定变量<img src="https://latex.codecogs.com/png.latex?X_i">以及权重<img src="https://latex.codecogs.com/png.latex?f_i"></p></li>
<li><p>分别给出尺度因子<img src="https://latex.codecogs.com/png.latex?w">和<img src="https://latex.codecogs.com/png.latex?v">（大于0），也即<img src="https://latex.codecogs.com/png.latex?X%5E%5Cast_i=%20w*X_i"> ，以及<img src="https://latex.codecogs.com/png.latex?f_i%5E%5Cast%20=%20v*f_i">。</p></li>
</ul>
<p>现在考虑尺度变换前后，均值和方差计算的关系。</p>
<p>（1）变换前：</p>
<p><img src="https://latex.codecogs.com/png.latex?%0A%5Cbegin%7Baligned%7D%0A%5Coverline%7BX%7D%20&amp;=%20%5Cfrac%7B%5Csum%7B(X_i%5Ccdot%20f_i)%7D%7D%7B%5Csum%7Bf_i%7D%7D%20%5C%5C%0AS%5E2_%7BX%7D%20&amp;=%20%5Cfrac%7B%5Csum%7B%5Cleft((X_i%20-%20%5Coverline%7BX%7D)%5E2f_i%5Cright)%7D%7D%7B(%5Csum%7Bf_i%7D)%20-%201%7D%0A%5Cend%7Baligned%7D%0A"></p>
<p>（2）变换后：</p>
<p><img src="https://latex.codecogs.com/png.latex?%0A%5Cbegin%7Baligned%7D%0A%5Coverline%7BX%7D%5E%7B%5Cast%7D%20&amp;=%20%5Cfrac%7B%5Csum%7B(X%5E%7B%5Cast%7D_i%5Ccdot%20f%5E%7B%5Cast%7D_i)%7D%7D%7B%5Csum%7Bf%5E%7B%5Cast%7D_i%7D%7D%0A=%20%5Cfrac%7B%5Csum%7B(wX_i%5Ccdot%20vf_i)%7D%7D%7B%5Csum%7Bvf_i%7D%7D%0A=%20w%5Ccdot%20%5Cfrac%7B%5Csum%7B(X_i%5Ccdot%20f_i)%7D%7D%7B%5Csum%7Bf_i%7D%7D%20=%20w%20%5Coverline%7BX%7D%5C%5C%0AS%5E2_%7BX%5E%7B%5Cast%7D%7D%20&amp;=%20%5Cfrac%7B%5Csum%7B%5Cleft((X%5E%7B%5Cast%7D_i%20-%20%5Coverline%7BX%7D%5E%7B%5Cast%7D)%5E2f%5E%7B%5Cast%7D_i%5Cright)%7D%7D%7B(%5Csum%7Bf%5E%7B%5Cast%7D_i%7D)%20-%201%7D%0A=%20%5Cfrac%7B%5Csum%7B%5Cleft((wX_i%20-%20w%5Coverline%7BX%7D)%5E2vf_i%5Cright)%7D%7D%7B(%5Csum%7Bvf_i%7D)%20-%201%7D%20%5C%5C%0A&amp;=%20%5Cfrac%7Bw%5E2v%20%5Ccdot%20%5Csum%7B%5Cleft((X_i%20-%20%5Coverline%7BX%7D)%5E2f_i%5Cright)%7D%7D%7B(%5Csum%7Bvf_i%7D)%20-%201%7D%20%5C%5C%0A&amp;%20=%20%5Cfrac%7Bw%5E2%20%5Ccdot%20%5Csum%7B%5Cleft((X_i%20-%20%5Coverline%7BX%7D)%5E2f_i%5Cright)%7D%7D%7B(%5Csum%7Bf_i%7D)%20-%201/v%7D%20%20%5Cquad%20%5Ctext%7B%20(if%20%7D%20v%20%5Capprox%201%5Ctext%7B%20)%7D%20%5C%5C%0A&amp;%20%5Capprox%20w%5E2%20%5Ccdot%20S%5E2_X%0A%5Cend%7Baligned%7D%0A"></p>
<div class="callout callout-style-default callout-note callout-titled">
<div class="callout-header d-flex align-content-center">
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-title-container flex-fill">
注记
</div>
</div>
<div class="callout-body-container callout-body">
<p>尺度变换下，方差计算可能会有数值近似差异。理论上，方差公式有两种形式：</p>
<p><span id="eq-s2-calc"><img src="https://latex.codecogs.com/png.latex?%0A%5Cbegin%7Baligned%7D%0AS%5E2_%7BX%7D%20&amp;=%20%5Cfrac%7B%5Csum%7B%5Cleft((X_i%20-%20%5Coverline%7BX%7D)%5E2f_i%5Cright)%7D%7D%7B(%5Csum%7Bf_i%7D)%20-%201%7D%20&amp;&amp;%20%5Ctext%7B(calcuation)%7D%0A%5Cend%7Baligned%7D%0A%5Ctag%7B1%7D"></span></p>
<p><span id="eq-s2-theory"><img src="https://latex.codecogs.com/png.latex?%0A%5Cbegin%7Baligned%7D%0AS%5E2_%7BX%7D%20&amp;=%20%5Cfrac%7B%5Csum%7B%5Cleft((X_i%20-%20%5Coverline%7BX%7D)%5E2f_i%5Cright)%7D%7D%7B%5Csum%7Bf_i%7D%7D%20&amp;&amp;%20%5Ctext%7B(theory)%7D%0A%5Cend%7Baligned%7D%0A%5Ctag%7B2%7D"></span></p>
<ul>
<li><p>如果使用 式&nbsp;2 ，那么因子变换的方差结果严格等于<img src="https://latex.codecogs.com/png.latex?S%5E2_%7BX%5E%7B%5Cast%7D%7D%20=%20w%5E2%20%5Ccdot%20S%5E2_X">。</p></li>
<li><p>如果使用 式&nbsp;1 ，那么因子变换的方差结果可能会与上述情况非常不同<img src="https://latex.codecogs.com/png.latex?S%5E2_%7BX%5E%7B%5Cast%7D%7D%20%5Capprox%20w%5E2%20%5Ccdot%20S%5E2_X">。这取决于<img src="https://latex.codecogs.com/png.latex?%5Csum%7Bf_i%7D">与<img src="https://latex.codecogs.com/png.latex?1/v">的大小关系。如果<img src="https://latex.codecogs.com/png.latex?%5Csum%7Bf_i%7D%20%5Cggg%201/v">，则理论方差和数值方差基本接近；否则，理论方差和数值方差的差异会比较明显。</p></li>
</ul>
</div>
</div>
</section>
<section id="r示例说明" class="level2">
<h2 class="anchored" data-anchor-id="r示例说明">R示例说明</h2>
<section id="数据准备" class="level3">
<h3 class="anchored" data-anchor-id="数据准备">数据准备</h3>
<div class="cell">
<details open="" class="code-fold">
<summary>Show the code</summary>
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb1" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb1-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(tidyverse)</span>
<span id="cb1-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(magrittr)</span>
<span id="cb1-3"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#library(openxlsx)</span></span>
<span id="cb1-4"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#library(here)</span></span>
<span id="cb1-5"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(janitor)</span>
<span id="cb1-6"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(Hmisc)</span>
<span id="cb1-7"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(scales)</span></code></pre></div></div>
</details>
</div>
<div class="cell">
<details open="" class="code-fold">
<summary>Show the code</summary>
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb2" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb2-1">w <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10</span></span>
<span id="cb2-2">v <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10</span></span>
<span id="cb2-3"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">set.seed</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1254</span>)</span>
<span id="cb2-4">X0 <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sample</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">20</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">replace =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">FALSE</span>)</span>
<span id="cb2-5">X1 <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> w<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span>X0</span>
<span id="cb2-6"></span>
<span id="cb2-7">f0 <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">30</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">20</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">20</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">20</span>) </span>
<span id="cb2-8">f1 <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> v<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span>f0</span>
<span id="cb2-9"></span>
<span id="cb2-10">df <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">data.frame</span>(</span>
<span id="cb2-11">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">X0 =</span> X0, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">f0 =</span> f0,</span>
<span id="cb2-12">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">X1 =</span> X1, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">f1 =</span> f1</span>
<span id="cb2-13">)</span></code></pre></div></div>
</details>
</div>
</section>
<section id="手动计算结果" class="level3">
<h3 class="anchored" data-anchor-id="手动计算结果">手动计算结果</h3>
<div class="cell">
<details open="" class="code-fold">
<summary>Show the code</summary>
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb3" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb3-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># calculate step-by step</span></span>
<span id="cb3-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">source</span>(here<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">here</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Rscript/calc-var.R"</span>))</span>
<span id="cb3-3"></span>
<span id="cb3-4">result_a <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">calc_weighted_var</span>(</span>
<span id="cb3-5">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">dt =</span> df, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"X0"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">w =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"f0"</span>)</span>
<span id="cb3-6"></span>
<span id="cb3-7">result_b <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">calc_weighted_var</span>(</span>
<span id="cb3-8">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">dt =</span> df, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"X1"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">w =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"f1"</span>)</span></code></pre></div></div>
</details>
</div>
<div class="cell">
<details open="" class="code-fold">
<summary>Show the code</summary>
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb4" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb4-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">bind_rows</span>(</span>
<span id="cb4-2">  result_a<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>tbl <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span> </span>
<span id="cb4-3">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mutate</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">trans =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"before"</span>,</span>
<span id="cb4-4">           <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">w =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>,</span>
<span id="cb4-5">           <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">v =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">select</span>(trans, w, v, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">everything</span>()),</span>
<span id="cb4-6">  result_b<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>tbl <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span> </span>
<span id="cb4-7">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mutate</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">trans =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"after"</span>,</span>
<span id="cb4-8">           <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">w =</span> w,</span>
<span id="cb4-9">           <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">v =</span> v) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">select</span>(trans, w, v, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">everything</span>())</span>
<span id="cb4-10">  </span>
<span id="cb4-11">) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb4-12">  knitr<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">kable</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">caption =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"计算表"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">align =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"c"</span>)</span></code></pre></div></div>
</details>
<div class="cell-output-display">
<table class="caption-top table table-sm table-striped small">
<caption>计算表</caption>
<colgroup>
<col style="width: 12%">
<col style="width: 6%">
<col style="width: 7%">
<col style="width: 9%">
<col style="width: 7%">
<col style="width: 9%">
<col style="width: 11%">
<col style="width: 15%">
<col style="width: 19%">
</colgroup>
<thead>
<tr class="header">
<th style="text-align: center;">trans</th>
<th style="text-align: center;">w</th>
<th style="text-align: center;">v</th>
<th style="text-align: center;">x</th>
<th style="text-align: center;">f</th>
<th style="text-align: center;">xf</th>
<th style="text-align: center;">x_dm</th>
<th style="text-align: center;">x_dm_sqr</th>
<th style="text-align: center;">x_dm_sqr_f</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td style="text-align: center;">before</td>
<td style="text-align: center;">1</td>
<td style="text-align: center;">1.0</td>
<td style="text-align: center;">10</td>
<td style="text-align: center;">30</td>
<td style="text-align: center;">300</td>
<td style="text-align: center;">-5.3</td>
<td style="text-align: center;">28.09</td>
<td style="text-align: center;">842.7</td>
</tr>
<tr class="even">
<td style="text-align: center;">before</td>
<td style="text-align: center;">1</td>
<td style="text-align: center;">1.0</td>
<td style="text-align: center;">19</td>
<td style="text-align: center;">20</td>
<td style="text-align: center;">380</td>
<td style="text-align: center;">3.7</td>
<td style="text-align: center;">13.69</td>
<td style="text-align: center;">273.8</td>
</tr>
<tr class="odd">
<td style="text-align: center;">before</td>
<td style="text-align: center;">1</td>
<td style="text-align: center;">1.0</td>
<td style="text-align: center;">13</td>
<td style="text-align: center;">10</td>
<td style="text-align: center;">130</td>
<td style="text-align: center;">-2.3</td>
<td style="text-align: center;">5.29</td>
<td style="text-align: center;">52.9</td>
</tr>
<tr class="even">
<td style="text-align: center;">before</td>
<td style="text-align: center;">1</td>
<td style="text-align: center;">1.0</td>
<td style="text-align: center;">20</td>
<td style="text-align: center;">20</td>
<td style="text-align: center;">400</td>
<td style="text-align: center;">4.7</td>
<td style="text-align: center;">22.09</td>
<td style="text-align: center;">441.8</td>
</tr>
<tr class="odd">
<td style="text-align: center;">before</td>
<td style="text-align: center;">1</td>
<td style="text-align: center;">1.0</td>
<td style="text-align: center;">16</td>
<td style="text-align: center;">20</td>
<td style="text-align: center;">320</td>
<td style="text-align: center;">0.7</td>
<td style="text-align: center;">0.49</td>
<td style="text-align: center;">9.8</td>
</tr>
<tr class="even">
<td style="text-align: center;">before</td>
<td style="text-align: center;">1</td>
<td style="text-align: center;">1.0</td>
<td style="text-align: center;">合计</td>
<td style="text-align: center;">100</td>
<td style="text-align: center;">1530</td>
<td style="text-align: center;">1.5</td>
<td style="text-align: center;">69.65</td>
<td style="text-align: center;">1621.0</td>
</tr>
<tr class="odd">
<td style="text-align: center;">after</td>
<td style="text-align: center;">10</td>
<td style="text-align: center;">0.1</td>
<td style="text-align: center;">100</td>
<td style="text-align: center;">3</td>
<td style="text-align: center;">300</td>
<td style="text-align: center;">-53.0</td>
<td style="text-align: center;">2809.00</td>
<td style="text-align: center;">8427.0</td>
</tr>
<tr class="even">
<td style="text-align: center;">after</td>
<td style="text-align: center;">10</td>
<td style="text-align: center;">0.1</td>
<td style="text-align: center;">190</td>
<td style="text-align: center;">2</td>
<td style="text-align: center;">380</td>
<td style="text-align: center;">37.0</td>
<td style="text-align: center;">1369.00</td>
<td style="text-align: center;">2738.0</td>
</tr>
<tr class="odd">
<td style="text-align: center;">after</td>
<td style="text-align: center;">10</td>
<td style="text-align: center;">0.1</td>
<td style="text-align: center;">130</td>
<td style="text-align: center;">1</td>
<td style="text-align: center;">130</td>
<td style="text-align: center;">-23.0</td>
<td style="text-align: center;">529.00</td>
<td style="text-align: center;">529.0</td>
</tr>
<tr class="even">
<td style="text-align: center;">after</td>
<td style="text-align: center;">10</td>
<td style="text-align: center;">0.1</td>
<td style="text-align: center;">200</td>
<td style="text-align: center;">2</td>
<td style="text-align: center;">400</td>
<td style="text-align: center;">47.0</td>
<td style="text-align: center;">2209.00</td>
<td style="text-align: center;">4418.0</td>
</tr>
<tr class="odd">
<td style="text-align: center;">after</td>
<td style="text-align: center;">10</td>
<td style="text-align: center;">0.1</td>
<td style="text-align: center;">160</td>
<td style="text-align: center;">2</td>
<td style="text-align: center;">320</td>
<td style="text-align: center;">7.0</td>
<td style="text-align: center;">49.00</td>
<td style="text-align: center;">98.0</td>
</tr>
<tr class="even">
<td style="text-align: center;">after</td>
<td style="text-align: center;">10</td>
<td style="text-align: center;">0.1</td>
<td style="text-align: center;">合计</td>
<td style="text-align: center;">10</td>
<td style="text-align: center;">1530</td>
<td style="text-align: center;">15.0</td>
<td style="text-align: center;">6965.00</td>
<td style="text-align: center;">16210.0</td>
</tr>
</tbody>
</table>
</div>
</div>
<p>我们来验证两类方差公式的计算结果：</p>
<div class="cell">
<details open="" class="code-fold">
<summary>Show the code</summary>
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb5" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb5-1">var1 <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> result_a<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>sum<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>x_dm_sqr_f <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span>(result_a<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>sum<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>f <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>)</span>
<span id="cb5-2">var1_alt <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> result_a<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>sum<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>x_dm_sqr_f <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span>(result_a<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>sum<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>f)</span>
<span id="cb5-3">se1 <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sqrt</span>(var1)</span>
<span id="cb5-4"></span>
<span id="cb5-5">var2 <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> result_b<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>sum<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>x_dm_sqr_f <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span>(result_b<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>sum<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>f <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>)</span>
<span id="cb5-6">var2_alt <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> result_b<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>sum<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>x_dm_sqr_f <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span>(result_b<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>sum<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>f)</span>
<span id="cb5-7">se2 <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sqrt</span>(var2)</span></code></pre></div></div>
</details>
</div>
<p>（1）采用经典方差计算公式 （式&nbsp;1）</p>
<div class="cell">
<details open="" class="code-fold">
<summary>Show the code</summary>
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb6" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb6-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">list</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">w=</span>w, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">var1 =</span> var1, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">var2 =</span> var2)</span></code></pre></div></div>
</details>
<div class="cell-output cell-output-stdout">
<pre><code>$w
[1] 10

$var1
[1] 16.37374

$var2
[1] 1801.111</code></pre>
</div>
<details open="" class="code-fold">
<summary>Show the code</summary>
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb8" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb8-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">identical</span>(var2, w<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">^</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span>var1)</span></code></pre></div></div>
</details>
<div class="cell-output cell-output-stdout">
<pre><code>[1] FALSE</code></pre>
</div>
<details open="" class="code-fold">
<summary>Show the code</summary>
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb10" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb10-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">number</span>(var2, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.000000001</span>)</span></code></pre></div></div>
</details>
<div class="cell-output cell-output-stdout">
<pre><code>[1] "1 801.111111111"</code></pre>
</div>
<details open="" class="code-fold">
<summary>Show the code</summary>
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb12" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb12-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">number</span>(w<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">^</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span>var1, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.000000001</span>)</span></code></pre></div></div>
</details>
<div class="cell-output cell-output-stdout">
<pre><code>[1] "1 637.373737374"</code></pre>
</div>
</div>
<p>（2）采用其他理论方差计算公式 （式&nbsp;2）</p>
<div class="cell">
<details open="" class="code-fold">
<summary>Show the code</summary>
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb14" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb14-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">list</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">w=</span>w, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">var1_alt =</span> var1_alt, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">var2_alt =</span> var2_alt)</span></code></pre></div></div>
</details>
<div class="cell-output cell-output-stdout">
<pre><code>$w
[1] 10

$var1_alt
[1] 16.21

$var2_alt
[1] 1621</code></pre>
</div>
<details open="" class="code-fold">
<summary>Show the code</summary>
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb16" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb16-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">identical</span>(var2_alt, w<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">^</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span>var1_alt)</span></code></pre></div></div>
</details>
<div class="cell-output cell-output-stdout">
<pre><code>[1] TRUE</code></pre>
</div>
<details open="" class="code-fold">
<summary>Show the code</summary>
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb18" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb18-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">number</span>(var2_alt, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.000000001</span>)</span></code></pre></div></div>
</details>
<div class="cell-output cell-output-stdout">
<pre><code>[1] "1 621.000000000"</code></pre>
</div>
<details open="" class="code-fold">
<summary>Show the code</summary>
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb20" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb20-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">number</span>(w<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">^</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span>var1_alt, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.000000001</span>)</span></code></pre></div></div>
</details>
<div class="cell-output cell-output-stdout">
<pre><code>[1] "1 621.000000000"</code></pre>
</div>
</div>
</section>
<section id="计算机结果" class="level3">
<h3 class="anchored" data-anchor-id="计算机结果">计算机结果</h3>
<p>实际上，计算机程序默认采用经典方差计算公式 （式&nbsp;1）</p>
<div class="cell">
<details open="" class="code-fold">
<summary>Show the code</summary>
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb22" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb22-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># ==== quick calculation : weighted====</span></span>
<span id="cb22-2">avr1 <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">weighted.mean</span>(df<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>X0, df<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>f0)</span>
<span id="cb22-3">avr2 <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">weighted.mean</span>(df<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>X1, df<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>f1)</span>
<span id="cb22-4"></span>
<span id="cb22-5">v1 <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> Hmisc<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">wtd.var</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> df<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>X0, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">weights =</span> df<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>f0)</span>
<span id="cb22-6">sd1 <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sqrt</span>(v1)</span>
<span id="cb22-7"></span>
<span id="cb22-8">v2 <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> Hmisc<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">wtd.var</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> df<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>X1, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">weights =</span> df<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>f1)</span>
<span id="cb22-9">sd2 <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sqrt</span>(v2)</span></code></pre></div></div>
</details>
</div>
<div class="cell">
<details open="" class="code-fold">
<summary>Show the code</summary>
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb23" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb23-1">w</span></code></pre></div></div>
</details>
<div class="cell-output cell-output-stdout">
<pre><code>[1] 10</code></pre>
</div>
<details open="" class="code-fold">
<summary>Show the code</summary>
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb25" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb25-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">identical</span>(v2, w<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">^</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span>v1)</span></code></pre></div></div>
</details>
<div class="cell-output cell-output-stdout">
<pre><code>[1] FALSE</code></pre>
</div>
<details open="" class="code-fold">
<summary>Show the code</summary>
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb27" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb27-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">number</span>(v2, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.000000001</span>)</span></code></pre></div></div>
</details>
<div class="cell-output cell-output-stdout">
<pre><code>[1] "1 801.111111111"</code></pre>
</div>
<details open="" class="code-fold">
<summary>Show the code</summary>
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb29" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb29-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">number</span>(w<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">^</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span>v1, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.000000001</span>)</span></code></pre></div></div>
</details>
<div class="cell-output cell-output-stdout">
<pre><code>[1] "1 637.373737374"</code></pre>
</div>
</div>


</section>
</section>

 ]]></description>
  <category>统计</category>
  <category>方差</category>
  <guid>https://coursest.netlify.app/topics/2024-04-27-variance-scales/</guid>
  <pubDate>Fri, 26 Apr 2024 16:00:00 GMT</pubDate>
</item>
</channel>
</rss>
