Welcome to TiddlyWiki created by Jeremy Ruston, Copyright © 2007 UnaMesa Association
>Ridge Regression is a variant of ordinary Multiple Linear Regression whose goal is to circumvent the problem of predictors collinearity. It gives-up the Least Squares (LS) as a method for estimating the parameters of the model, and focusses instead of the X'X matrix. This matrix will be artificially modified so as to make its determinant appreciably different from 0.
>
>By doing so, it makes the new model parameters somewhat biased (whereas the parameters as calculated by the LS method are unbiased estimators of the true parameters). But the variances of these new parameters are smaller than that of the LS parameters and in fact, so much smaller than their Mean Square Errors (MSE) may also be smaller than that of the parameters of the LS model. This is an illustration of the fact that a biased estimator may outperform an unbiased estimator provided its variance is small enough.
>
>Moreover, the predictions errors of the Ridge Model will also turn out to be more accurate than that of the LS regression model when predictors exhibit near collinearity. Therefore, the idea behind of Ridge Regression is at the heart of the "bias-variance tradeoff" issue.
!Ridge parameter
These improvements do not come free.
* An extra parameter has to be introduced in the model, the "ridge parameter". Its value is assigned by the analyst, and determines how much Ridge Regression departs from LS Regression. If this value is too small, Ridge Regression cannot fight collinearity efficiently. If it is too large, the bias of the parameters become too large, and so do the parameters and predictions MSEs. There is therefore an optimal value for the ridge parameter, that theory alone cannot calculate accurately from the data only. It has therefore to be estimated by a series of trial and errors, usually resorting to cross-validation.
* The nice results about confidence intervals and tests in LS regression are lost, and have to be replaced by complex and approximate results.
Yet, Ridge Regression is more than a "last resort" attempt to salvage LS linear regression in case of near or full collinearity of the predictors. It is to be considered a major linear regression technique of its own that proves its usefulness when collinearity is a problem, an all-too-common circumstance.
----
The recipe as given by Hoerl and Jennard was as follows for model of eqn.
$$
Y = X \theta + \epsilon
$$
# Centre the x-variables in eqn. $\sum_i x_{ij} = 0, j = 1, \dots, p$, leading to model $Y = \mu 1_n + X \beta + \epsilon$.
# Scale the $p$ x-variables so that the diagonal elements of $X^TX$ are $n$.
# Plot the components of $\hat \beta_{RR}(k)$, ++++[where]
$$
\hat \beta_{RR}(k) = (X^TX + k I_p)^{-1} X^T y.
$$
===
This plot is often called the "ridge trace". The estimator of $\mu$ is traditionally taken to be $\bar y$ irrespective of $k$.
# Plot the residual sum of squares as a function of $k$. In drawing this on the same graph as the ridge trace, a different (right-hand vertical axis) scale may be needed.
# Choose a value of $\hat k$ of $k$ which stabilizes the coefficient trace and at the same time does not penalize the sum of squares too much.
----
# 一致性
## 在網路上找到課本練習題2.10的數據,把它讀進R的「表格物件」並且命名為「Table2.10」。++++[(請回答相關的R指令。)]
{{{
利用關鍵字【hadi】跟【regression】找到數據放在
http://www.ilr.cornell.edu/~hadi/RABE4/Data4/P049.txt
所以
我用指令
> Table2.10 = read.table("http://www.ilr.cornell.edu/~hadi/RABE4/Data4/P049.txt", header=T)
}}}
===
## 這一筆數據的變數叫做什麼名字?++++[(請回答相關的「R指令」以及「結果」。)]
{{{
> names(Table2.10)
[1] "Husband" "Wife"
}}}
===
## 是一個幾乘幾的表格。++++[(請回答相關的「R指令」以及「結果」。)]
{{{
> dim(Table2.10)
[1] 96 2
}}}
===
## 兩變數之間的相關係數為何?++++[(請回答相關的「R指令」以及「結果」。)]
{{{
> attach(Table2.10)
> cor(Husband, Wife)
[1] 0.7633864
}}}
===
## 檢定兩變數之間的相關係數等於0。++++[(請回答相關的「R指令」以及「結果」。)]
{{{
> cor.test(Husband, Wife)
Pearson's product-moment correlation
data: Husband and Wife
t = 11.4582, df = 94, p-value < 2.2e-16
alternative hypothesis: true correlation is not equal to 0
95 percent confidence interval:
0.6646193 0.8359341
sample estimates:
cor
0.7633864
}}}
===
## 繪製一張圖輔助你觀察兩變數之間是否存在「一致性(agreement, reproducibility)」。++++[(請回答相關的「R指令」以及「結果」。)]
{{{
> plot(Husband, Wife)
> abline(0,1)
}}}
===
## 為了檢定是否存在「一致性」,有人建議以下的++++[模型]
>Modelling men's heights as $m_i = \mu_m + \epsilon_{m, i}$ and women's heights as $w_i = \mu_w + \epsilon_{w, i}$, we get $w_i - m_i = (\mu_w - \mu_m) + (\epsilon_{w, i} - \epsilon_{m, i})$. If tall men marry tall women and short men marry short women, then $\epsilon_{w, i} - \epsilon_{m, i}$ will be close to zero.
===
# 利用lm()估計$\mu_w - \mu_m$。++++[(請回答相關的「R指令」以及「結果」。)]
{{{
> lm(I(Wife - Husband)~., data = Table2.10)
Call:
lm(formula = I(Wife - Husband) ~ ., data = Table2.10)
Coefficients:
(Intercept)
-10.43
}}}
===
# 利用linear.hypothesis()檢定$H_1: \mu_m > \mu_w$,報告p值並且下結論。++++[(請回答相關的「R指令」以及「結果」。)]
{{{
> agree2.10 = lm(I(Wife - Husband)~., data = Table2.10)
> library(car)
> linear.hypothesis(agree2.10, c("(Intercept) = 0"))
Linear hypothesis test
Hypothesis:
(Intercept) = 0
Model 1: I(Wife - Husband) ~ .
Model 2: restricted model
Res.Df RSS Df Sum of Sq F Pr(>F)
1 95 4153.5
2 96 14591.0 -1 -10438 238.73 < 2.2e-16 ***
---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
}}}
===
# 請設計你關於「一致性」的虛無與對立假設,並且利用linear.hypothesis()報告p值並且得到檢定的結論。++++[(請回答相關的「R指令」以及「結果」。)]
{{{
OPEN QUESTION.
}}}
===
# boot()
## 利用boot()以及標準常態分配的亂數(樣本數等於30),驗證「樣本平均」的抽樣分配也是常態的($B = 99$)。++++[(請回答相關的「R指令」以及「結果」。)]
{{{
> library(boot)
> Mean = function(x, d) mean(x[d])
> x = rnorm(30, mean=0, sd=1)
> xBoot = boot(x, Mean, R = 99)
> plot(xBoot)
}}}
===
## (繼續前一題)根據你的數據,常態理論下母體平均(0)的信賴區間為何?++++[(請回答相關的「R指令」以及「結果」。)]
{{{
> mean(x)+qt(0.975, 29)*sd(x)/sqrt(30)
[1] 0.3225576
> mean(x)-qt(0.975, 29)*sd(x)/sqrt(30)
[1] -0.5171387
}}}
===
## 利用boot.ci()得到的「normal interval(常態區間)」為何?++++[(請回答相關的「R指令」以及「結果」。)]
{{{
> boot.ci(xBoot)
BOOTSTRAP CONFIDENCE INTERVAL CALCULATIONS
Based on 99 bootstrap replicates
CALL :
boot.ci(boot.out = xBoot)
Intervals :
Level Normal Basic
95% (-0.5076, 0.3414 ) (-0.4670, 0.3371 )
Level Percentile BCa
95% (-0.5317, 0.2724 ) (-0.5392, 0.2557 )
Calculations and Intervals on Original Scale
Some basic intervals may be unstable
Some percentile intervals may be unstable
Some BCa intervals may be unstable
Warning message:
In boot.ci(xBoot) : bootstrap variances needed for studentized intervals
}}}
===
## 寫一段程式比較【常態理論之信賴區間】與【拔靴常態區間】之長度,孰長孰短?++++[(請回答相關的「R指令」以及「結果」。)]
{{{
理論:
> mean(x)+qt(0.975, 29)*sd(x)/sqrt(30)
[1] 0.3225576
> mean(x)-qt(0.975, 29)*sd(x)/sqrt(30)
[1] -0.5171387
> 2*qt(0.975, 29)*sd(x)/sqrt(30)
[1] 0.8396963
拔靴法:
Level Normal Basic
95% (-0.5076, 0.3414 ) (-0.4670, 0.3371 )
Level Percentile BCa
95% (-0.5317, 0.2724 ) (-0.5392, 0.2557 )
結論:
孰長孰短?好像比較短。
}}}
===
!技術
# [[相關分析]](Correlation Analysis)
# [[簡單線性迴歸分析]](Simple Linear Regression)
# [[複迴歸分析]](Multiple Regression)
# [[Ridge Regression]]
# [[迴歸診斷]](Diagnostics)
!R
網頁上提到過的R函示
!拔靴法
# [[拔靴迴歸分析]](Bootstrapping Regression Models)
# [[拔靴信賴區間]](Bootstrapping Confidence Interval)
# [[拔靴假設檢定]](Bootstrapping Hypothesis Testing)
!總論
# [[隨堂考]](10%):採外加方式。
## [[第一次隨堂考|]]
## [[第二次隨堂考|]]
## (10%)[[12/03小考]]
# [[家庭作業]](30%)(請注意[[家庭作業基本要求]]以及[[家庭作業評分方式]]。)
## (10%)[[第一次家庭作業|恰弟與哈弟第47頁練習題2-10]]
## (10%)[[第二次家庭作業|體驗拔靴法]]
## (10%)[[第三次家庭作業|更多拔靴法的體驗]]
# [[期中考(上機考)|2009年期中考]](10%)
# [[期末考(上機考)|2009年期末考]](15%)
# [[期末報告]](45%):兩人一組。
!期末報告之成份以及比例
{{{
網頁上每一種R函示都必須至少使用一次。
}}}
# (20%)[[使用說明]]
## (15%)同儕互評
### (2%)BB初評(幾顆星 *2%)
### (5%)單項初評(加總單項得分 *5%)
### (8%)複評(幾顆星 *8%)
## (5%)學術審查
# (80%)決戰期間的++++[得分消長]
# (10%)第一場得分被等分隔為四等分,第一等分4分,第二等分5分,第三等分7分,第四等分10分。
# (10%)第二場得分被等分隔為四等分,第一等分4分,第二等分5分,第三等分7分,第四等分10分。
# (10%)第三場得分被等分隔為四等分,第一等分4分,第二等分5分,第三等分7分,第四等分10分。
# (10%)第四場得分被等分隔為四等分,第一等分4分,第二等分5分,第三等分7分,第四等分10分。
# (10%)第五場得分被等分隔為四等分,第一等分4分,第二等分5分,第三等分7分,第四等分10分。
# (10%)第六場得分被等分隔為四等分,第一等分4分,第二等分5分,第三等分7分,第四等分10分。
# (10%)第七場得分被等分隔為四等分,第一等分4分,第二等分5分,第三等分7分,第四等分10分。
# (10%)第八場得分被等分隔為四等分,第一等分4分,第二等分5分,第三等分7分,第四等分10分。
# (加分部份)得分之總積分
## 達36分者,加10分。
## 達30 ~ 35分者,加7分。
## 達27 ~ 29分者,加5分。
===
# (20%)海報。@@採公開展示的方式,接受同學、學長姐、老師們的評頭論足。@@
## (5%)同學與學長姐的【同儕互評】(幾顆星 *5%)
## (15%)【授課老師】的【學術審查】(幾顆星 *15%)
!R使用說明評分辦法
!!說明文稿之必要內容
# BACKGROUND (Using 5w1h logic to write this section)
# Data and its format,
# Required Input(s) and its (their) format(s),
# Useful output(s) and its (their) name(s) and format(s),
# Example(s) and the corresponding explanation(s).
# Reference(s)
!!第一階段評分規則
# 先在BB上打分數。
# 針對以下項目進行單項【評分、1 ~ 10分】以及【評論】。為了避免同儕之間不必要的困擾,維護互評機制的公正與客觀,此項評分以紙本報告的方式繳交。
## 架構(是否按照任課老師的要求)
## 文筆清晰(文筆是否可以讓讀者迅速了解使用說明的訴求)
## 案例豐富(是否提供與[[任務|學期任務]]有關的案例說明)
## 合作態度(是否以讓他人最容易上手的調調編寫使用說明)
!!第二階段評分規則
# 再一次打分數。但是不再進行紙本攻防。
!【NBA勝負分析與預測】比賽規則
# 各分區(東區跟西區)挑選一隊。採BB登記制。先註冊先贏。【@@請提前註冊你的隊伍。@@】
# 試跑期間必須練習10場,東西區各5場(不分主場與客場)。老師也不管各組私底下的模擬測試。【@@請提供你的試跑日期、台灣比賽時間、客隊名稱。@@】
# 試跑期間,用[[試跑戰績報告方式]]透過BB提報成果。
# 決戰期間預測每一場的每一種玩法(如下所示)。決戰期間必須預測東西區各4場,而且必須是主場2場、客場2場。【@@請提供你的決戰日子、台灣比賽時間、客隊名稱。@@】
# 決戰期間,用[[決戰戰績報告方式]]透過BB提報成果。
# 不論試跑或是決戰期間,比賽@@前12個小時@@用[[預測報告方式]]透過BB提報預測結果。
# 試跑期間,比賽結束後12個小時內,透過BB提報戰果。
# @@決戰期間,比賽結束後12個小時內,透過BB提報戰果,否則不計分(以BB上的時間為準)@@。
# 不投注。課程期間,絕不運用任何本課程分析結果簽賭。一旦發現將依校規處分。
# 每一場都要參加[[北富銀運彩|http://www.i-win.com.tw/]]提供的方式:
## [[讓分|http://www.i-win.com.tw/tsl/sites/cms_admin/cms/tsl_guide/approved/guide1_1208323621359.html]]
## [[不讓分|http://www.i-win.com.tw/tsl/sites/cms_admin/cms/tsl_guide/approved/guide1_1235697123094.html]]
## [[勝分差|http://www.i-win.com.tw/tsl/sites/cms_admin/cms/tsl_guide/approved/guide1_1208323944609.html]]
# 用以下的權數加權上述的玩法。
## 正確預測【不讓分】一次加1分
## 正確預測【讓分】一次加2分
## 正確預測【勝分差】一次加3分
# ++++[模擬比賽計分方式:]
$$
\mbox{讓分正確預測一次} \times \mbox{2分} + \mbox{不讓分正確預測一次} \times \mbox{1分} + \mbox{勝分差正確預測一次} \times \mbox{3分}
$$
===
# (暫定)++++[正式比賽計分方式:]
$$
\mbox{讓分正確預測一次} \times \mbox{2分} + \mbox{不讓分正確預測一次} \times \mbox{1分} + \mbox{勝分差正確預測一次} \times \mbox{3分}
$$
===
!預測報告方式(PDF檔案)
根據你的模型與北富銀運彩官網的投注內容,@@針對各組註冊的兩隊@@,分別針對【不讓分】、【讓分】、【勝分差】三種玩法提供
# 三種【預測比賽輸贏的模型】以及R報表(在試跑期間基於各組隱私,這一個部份不上BB。請把每一次的預測模型郵寄到我的信箱jungpinwu@gmail.com以及cwu@fcu.edu.tw)、
# 【不讓分勝負預測】、
# 【讓分勝負預測】、
# 【勝分差勝負預測】。
!試跑戰績報告方式(PDF檔案)
用以下格式把過程與結果放在BB:@@針對各組註冊的兩隊@@,分別針對【不讓分】、【讓分】、【勝分差】三種玩法提供
# 【不讓分】成功預測與否的結果、
# 【讓分】成功預測與否的結果、
# 【勝分差】成功預測與否的結果、
# 提供一個類似北富銀運彩的投注表格。
# 根據[[比賽規則]]的計分公式得多少分數。
!決戰戰績報告方式(PDF檔案)
用以下格式把過程與結果放在BB:@@針對各組註冊的兩隊@@,分別針對【不讓分】、【讓分】、【勝分差】三種玩法提供
# 【不讓分】成功預測與否的結果、
# 【讓分】成功預測與否的結果、
# 【勝分差】成功預測與否的結果、
# 提供一個類似北富銀運彩的投注表格、
# 根據[[比賽規則]]的計分公式得多少分數。
並且
{{{
把預測報告的內容放在決戰戰績報告的附錄。
}}}
也就是說,把
* 三種【預測比賽輸贏的模型】以及R報表、
* 【不讓分勝負預測】、
* 【讓分勝負預測】、
* 【勝分差勝負預測】
放在附錄。所以你的【決戰戰績報告】會有
{{{
五節加一節附錄。
}}}
!海報
{{{
美編部份請發揮你的創意!
}}}
!!格式
# 標題、作者、指導教授、單位、課程。
# 摘要你的建模八大步驟。
# 摘要模擬比賽的起承轉合。(改變模型前後要有一段銜接文字說明故事發展的脈絡!)
# 造表摘要你正式比賽八次預測的模型與戰績。
# 總論:評論你的模型與整體建議。
# 警語:論述此份海報的內容純屬研究,任何引用與簽賭行為,恕不負責之類的文字。
# 致謝詞(如果有的話!)
# 參考文獻:列舉你曾經參考過、最重要的文獻與網路資料三筆。
!!製作海報的最主要指導資料
{{{
十次的試跑戰績報告、八次的決戰戰績報告
}}}
!!評分重點
{{{
學術正確性!
}}}
[[who]]
[[what]]
[[where]]
[[when]]
[[why]]
[[how]]
{{{
data(longley) # not the same as the S-PLUS dataset
names(longley)[1] <- "y"
lm.ridge(y ~ ., longley)
plot(lm.ridge(y ~ ., longley, lambda = seq(0,0.1,0.001)))
> select(lm.ridge(y ~ ., longley, lambda = seq(0,0.1,0.0001)))
modified HKB estimator is 0.006836982
modified L-W estimator is 0.05267247
smallest value of GCV at 0.0057
> data(longley) # not the same as the S-PLUS dataset
names(longley)[1] <- "y"
lm.ridge(y ~ ., longley, lambda = seq(0,0.1,0.001)))
plot(lm.ridge(y ~ ., longley, lambda = seq(0,0.1,0.001)))
select(lm.ridge(y ~ ., longley, lambda = seq(0,0.1,0.0001)))
}}}
{{{
CC = read.table("http://www.ilr.cornell.edu/~hadi/RABE4/Data4/P081.txt", header=T)
> CC
State Age HS Income Black Female Price Sales
1 AL 27.0 41.3 2948 26.2 51.7 42.7 89.8
2 AK 22.9 66.7 4644 3.0 45.7 41.8 121.3
3 AZ 26.3 58.1 3665 3.0 50.8 38.5 115.2
4 AR 29.1 39.9 2878 18.3 51.5 38.8 100.3
5 CA 28.1 62.6 4493 7.0 50.8 39.7 123.0
6 CO 26.2 63.9 3855 3.0 50.7 31.1 124.8
7 CT 29.1 56.0 4917 6.0 51.5 45.5 120.0
8 DE 26.8 54.6 4524 14.3 51.3 41.3 155.0
9 DC 28.4 55.2 5079 71.1 53.5 32.6 200.4
10 FL 32.3 52.6 3738 15.3 51.8 43.8 123.6
11 GA 25.9 40.6 3354 25.9 51.4 35.8 109.9
12 HI 25.0 61.9 4623 1.0 48.0 36.7 82.1
13 ID 26.4 59.5 3290 0.3 50.1 33.6 102.4
14 IL 28.6 52.6 4507 12.8 51.5 41.4 124.8
15 IN 27.2 52.9 3772 6.9 51.3 32.2 134.6
16 IO 28.8 59.0 3751 1.2 51.4 38.5 108.5
17 KA 28.7 59.9 3853 4.8 51.0 38.9 114.0
18 KY 27.5 38.5 3112 7.2 50.9 30.1 155.8
19 LA 24.8 42.2 3090 29.8 51.4 39.3 115.9
20 ME 28.0 54.7 3302 0.3 51.3 38.8 128.5
21 MD 27.1 52.3 4309 17.8 51.1 34.2 123.5
22 MA 29.0 58.5 4340 3.1 52.2 41.0 124.3
23 MI 26.3 52.8 4180 11.2 51.0 39.2 128.6
24 MN 26.8 57.6 3859 0.9 51.0 40.1 104.3
25 MS 25.1 41.0 2626 36.8 51.6 37.5 93.4
26 MO 29.4 48.8 3781 10.3 51.8 36.8 121.3
27 MT 27.1 59.2 3500 0.3 50.0 34.7 111.2
28 NB 28.6 59.3 3789 2.7 51.2 34.7 108.1
29 NV 27.8 65.2 4563 5.7 49.3 44.0 189.5
30 NH 28.0 57.6 3737 0.3 51.1 34.1 265.7
31 NJ 30.1 52.5 4701 10.8 51.6 41.7 120.7
32 NM 23.9 55.2 3077 1.9 50.7 41.7 90.0
33 NY 30.3 52.7 4712 11.9 52.2 41.7 119.0
34 NC 26.5 38.5 3252 22.2 51.0 29.4 172.4
35 ND 26.4 50.3 3086 0.4 49.5 38.9 93.8
36 OH 27.7 53.2 4020 9.1 51.5 38.1 121.6
37 OK 29.4 51.6 3387 6.7 51.3 39.8 108.4
38 OR 29.0 60.0 3719 1.3 51.0 29.0 157.0
39 PA 30.7 50.2 3971 8.0 52.0 44.7 107.3
40 RI 29.2 46.4 3959 2.7 50.9 40.2 123.9
41 SC 24.8 37.8 2990 30.5 50.9 34.3 103.6
42 SD 27.4 53.3 3123 0.3 50.3 38.5 92.7
43 TN 28.1 41.8 3119 15.8 51.6 41.6 99.8
44 TX 26.4 47.4 3606 12.5 51.0 42.0 106.4
45 UT 23.1 67.3 3227 0.6 50.6 36.6 65.5
46 VT 26.8 57.1 3468 0.2 51.1 39.5 122.6
47 VA 26.8 47.8 3712 18.5 50.6 30.2 124.3
48 WA 27.5 63.5 4053 2.1 50.3 40.3 96.7
49 WV 30.0 41.6 3061 3.9 51.6 41.6 114.5
50 WI 27.2 54.5 3812 2.9 50.9 40.2 106.4
51 WY 27.2 62.9 3815 0.8 50.0 34.4 132.2
> dim(CC)
[1] 51 8
> names(CC)
[1] "State" "Age" "HS" "Income" "Black" "Female" "Price" "Sales"
> attach(CC)
> CC.lm = lm(Sales ~ ., data=CC)
> CC.lm
Call:
lm(formula = Sales ~ ., data = CC)
Coefficients:
(Intercept) StateAL StateAR StateAZ StateCA StateCO StateCT
1.213e+02 -3.150e+01 -2.100e+01 -6.100e+00 1.700e+00 3.500e+00 -1.300e+00
StateDC StateDE StateFL StateGA StateHI StateID StateIL
7.910e+01 3.370e+01 2.300e+00 -1.140e+01 -3.920e+01 -1.890e+01 3.500e+00
StateIN StateIO StateKA StateKY StateLA StateMA StateMD
1.330e+01 -1.280e+01 -7.300e+00 3.450e+01 -5.400e+00 3.000e+00 2.200e+00
StateME StateMI StateMN StateMO StateMS StateMT StateNB
7.200e+00 7.300e+00 -1.700e+01 5.269e-14 -2.790e+01 -1.010e+01 -1.320e+01
StateNC StateND StateNH StateNJ StateNM StateNV StateNY
5.110e+01 -2.750e+01 1.444e+02 -6.000e-01 -3.130e+01 6.820e+01 -2.300e+00
StateOH StateOK StateOR StatePA StateRI StateSC StateSD
3.000e-01 -1.290e+01 3.570e+01 -1.400e+01 2.600e+00 -1.770e+01 -2.860e+01
StateTN StateTX StateUT StateVA StateVT StateWA StateWI
-2.150e+01 -1.490e+01 -5.580e+01 3.000e+00 1.300e+00 -2.460e+01 -1.490e+01
StateWV StateWY Age HS Income Black Female
-6.800e+00 1.090e+01 NA NA NA NA NA
Price
NA
> names(CC)
[1] "State" "Age" "HS" "Income" "Black" "Female" "Price" "Sales"
> CC[1,]
State Age HS Income Black Female Price Sales
1 AL 27 41.3 2948 26.2 51.7 42.7 89.8
> CC[1:4,]
State Age HS Income Black Female Price Sales
1 AL 27.0 41.3 2948 26.2 51.7 42.7 89.8
2 AK 22.9 66.7 4644 3.0 45.7 41.8 121.3
3 AZ 26.3 58.1 3665 3.0 50.8 38.5 115.2
4 AR 29.1 39.9 2878 18.3 51.5 38.8 100.3
> CC.lm = lm(Sales ~ HS + Income + Black + Female + Price, data=CC)
> CC.lm
Call:
lm(formula = Sales ~ HS + Income + Black + Female + Price, data = CC)
Coefficients:
(Intercept) HS Income Black Female Price
-44.69004 -0.40641 0.02564 0.01876 4.10472 -3.09913
> step(CC.lm)
Start: AIC=347.22
Sales ~ HS + Income + Black + Female + Price
Df Sum of Sq RSS AIC
- Black 1 2 36492 345
- HS 1 217 36708 346
- Female 1 767 37258 346
<none> 36491 347
- Income 1 6393 42883 353
- Price 1 7251 43741 354
Step: AIC=345.22
Sales ~ HS + Income + Female + Price
Df Sum of Sq RSS AIC
- HS 1 327 36819 344
- Female 1 845 37337 344
<none> 36492 345
- Income 1 7712 44204 353
- Price 1 7829 44322 353
Step: AIC=343.68
Sales ~ Income + Female + Price
Df Sum of Sq RSS AIC
<none> 36819 344
- Female 1 1735 38555 344
- Price 1 7669 44488 351
- Income 1 8755 45574 353
Call:
lm(formula = Sales ~ Income + Female + Price, data = CC)
Coefficients:
(Intercept) Income Female Price
-118.32739 0.02285 5.31699 -3.07344
> summary(step(CC.lm))
Start: AIC=347.22
Sales ~ HS + Income + Black + Female + Price
Df Sum of Sq RSS AIC
- Black 1 2 36492 345
- HS 1 217 36708 346
- Female 1 767 37258 346
<none> 36491 347
- Income 1 6393 42883 353
- Price 1 7251 43741 354
Step: AIC=345.22
Sales ~ HS + Income + Female + Price
Df Sum of Sq RSS AIC
- HS 1 327 36819 344
- Female 1 845 37337 344
<none> 36492 345
- Income 1 7712 44204 353
- Price 1 7829 44322 353
Step: AIC=343.68
Sales ~ Income + Female + Price
Df Sum of Sq RSS AIC
<none> 36819 344
- Female 1 1735 38555 344
- Price 1 7669 44488 351
- Income 1 8755 45574 353
Call:
lm(formula = Sales ~ Income + Female + Price, data = CC)
Residuals:
Min 1Q Median 3Q Max
-47.606 -11.627 -6.420 6.515 131.761
Coefficients:
Estimate Std. Error t value Pr(>|t|)
(Intercept) -1.183e+02 1.870e+02 -0.633 0.52999
Income 2.285e-02 6.834e-03 3.343 0.00163 **
Female 5.317e+00 3.572e+00 1.488 0.14334
Price -3.073e+00 9.823e-01 -3.129 0.00301 **
---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
Residual standard error: 27.99 on 47 degrees of freedom
Multiple R-squared: 0.284, Adjusted R-squared: 0.2383
F-statistic: 6.215 on 3 and 47 DF, p-value: 0.001209
}}}
{{{
cr = read.table("http://www.ilr.cornell.edu/~hadi/RABE4/Data4/P027.txt", header=T)
cr
plot(cr)
attach(cr)
lm(Minutes ~ Units, data=cr)
abline(lm(Minutes ~ Units, data=cr))
cr.lm = lm(Minutes ~ Units, data=cr)
abline(cr.lm)
confint(cr.lm, "Units", 0.95)
}}}
{{{
cr = read.table("http://www.ilr.cornell.edu/~hadi/RABE4/Data4/P027.txt", header=T)
cr
plot(cr)
attach(cr)
lm(Minutes ~ Units, data=cr)
abline(lm(Minutes ~ Units, data=cr))
cr.lm = lm(Minutes ~ Units, data=cr)
abline(cr.lm)
> linear.hypothesis(cr.lm, c("Units = 0"))
Linear hypothesis test
Hypothesis:
Units = 0
Model 1: Minutes ~ Units
Model 2: restricted model
Res.Df RSS Df Sum of Sq F Pr(>F)
1 12 348.8
2 13 27768.4 -1 -27419.5 943.2 8.916e-13 ***
---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
}}}
!大綱
[[簡單線性迴歸模型]]、
[[估計簡單線性迴歸模型]]
[[最新消息]]
[[迴歸分析的重要日子(Due Day)]]
[[有誰跟你一起?]]
!檢定
$$
H_0: \beta_1 = \beta_2
$$
!用法
{{{
mod.duncan <- lm(prestige ~ income + education, data=Duncan)
## the following are all equivalent:
linear.hypothesis(mod.duncan, "1*income - 1*education = 0")
linear.hypothesis(mod.duncan, "income = education")
linear.hypothesis(mod.duncan, "income - education")
linear.hypothesis(mod.duncan, "1income - 1education = 0")
linear.hypothesis(mod.duncan, "0 = 1*income - 1*education")
linear.hypothesis(mod.duncan, "income-education=0")
linear.hypothesis(mod.duncan, "1*income - 1*education + 1 = 1")
linear.hypothesis(mod.duncan, "2income = 2*education")
}}}
{{{
library(car)
attach(Duncan)
duncan.model = lm(prestige ~ income + education)
duncan.model
sDuncan <- step(duncan.model)
summary(sDuncan)
sDuncan$anova
}}}
{{{
library(car)
attach(Duncan)
duncan.model = lm(prestige ~ income + education)
duncan.model
summary(duncan.model)
confint(duncan.model, "income", 0.95)
confint(duncan.model, "education", 0.95)
}}}
{{{
library(car)
attach(Duncan)
duncan.model = lm(prestige ~ income + education)
duncan.model
summary(duncan.model)
> summary(duncan.model)
Call:
lm(formula = prestige ~ income + education)
Residuals:
Min 1Q Median 3Q Max
-29.5380 -6.4174 0.6546 6.6051 34.6412
Coefficients:
Estimate Std. Error t value Pr(>|t|)
(Intercept) -6.06466 4.27194 -1.420 0.163
income 0.59873 0.11967 5.003 1.05e-05 ***
education 0.54583 0.09825 5.555 1.73e-06 ***
---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
Residual standard error: 13.37 on 42 degrees of freedom
Multiple R-squared: 0.8282, Adjusted R-squared: 0.82
F-statistic: 101.2 on 2 and 42 DF, p-value: < 2.2e-16
> linear.hypothesis(duncan.model, c("(Intercept) = 0", "income = 1"))
Linear hypothesis test
Hypothesis:
(Intercept) = 0
income = 1
Model 1: prestige ~ income + education
Model 2: restricted model
Res.Df RSS Df Sum of Sq F Pr(>F)
1 42 7506.7
2 44 10660.2 -2 -3153.5 8.8219 0.0006329 ***
---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
}}}
++++[第一階段]
{{{
hist(prestige)
2*sqrt(length(prestige))
hist(prestige, nclass = 12)
pairs(cbind(prestige, income, education),
panel = function(x, y){
points(x, y)
abline(lm(y~x), lty = 2)
lines(lowess(x, y))
},
diag.panel = function(x){
par(new = T)
hist(x, main = "", axes = F, nclass = 12)
}
)
}}}
===
+++[把最後一段包裝起來!]
{{{
scatterplot = function(..., nclass = NULL){
pairs(cbind(...),
panel = function(x, y){
points(x, y)
abline(lm(y~x), lty = 2)
lines(lowess(x, y))
},
diag.panel = function(x){
par(new = T)
hist(x, main = "", axes = F, nclass = 12)
}
)
}
}}}
===
+++[繼續]
{{{
plot(income, education)
identify(income, education, row.names(Duncan)) # Use the mouse to identify points, ...
row.names(Duncan)[c(6,16,27)]
}}}
===
[[FieldGoals2003to2006.csv|http://www.stat.tamu.edu/~sheather/book/docs/datasets/FieldGoals2003to2006.csv]]
!大綱
[[圖1-1]]、
[[如何分析圖1-1?]]、
[[對圖1-1的相關係數分析有沒有瑕疵?]]
!大綱
[[圖1-2]]、
[[圖1-1的個別簡單線性迴歸分析]]
!大綱
[[檢定圖1-1的相關係數是否等於0?]]、
[[拔靴法初體驗]]
<html><div align="center"><iframe src ="http://www.moztw.org/" width="100%" align="center" height=400"></iframe></div></html>
/***
|Name|FontSizePlugin|
|Created by|SaqImtiaz|
|Location|http://tw.lewcid.org/#FontSizePlugin|
|Version|1.0|
|Requires|~TW2.x|
!Description:
Resize tiddler text on the fly. The text size is remembered between sessions by use of a cookie.
You can customize the maximum and minimum allowed sizes.
(only affects tiddler content text, not any other text)
Also, you can load a TW file with a font-size specified in the url.
Eg: http://tw.lewcid.org/#font:110
!Demo:
Try using the font-size buttons in the sidebar, or in the MainMenu above.
!Installation:
Copy the contents of this tiddler to your TW, tag with systemConfig, save and reload your TW.
Then put {{{<<fontSize "font-size:">>}}} in your SideBarOptions tiddler, or anywhere else that you might like.
!Usage
{{{<<fontSize>>}}} results in <<fontSize>>
{{{<<fontSize font-size: >>}}} results in <<fontSize font-size:>>
!Customizing:
The buttons and prefix text are wrapped in a span with class fontResizer, for easy css styling.
To change the default font-size, and the maximum and minimum font-size allowed, edit the config.fontSize.settings section of the code below.
!Notes:
This plugin assumes that the initial font-size is 100% and then increases or decreases the size by 10%. This stepsize of 10% can also be customized.
!History:
*27-07-06, version 1.0 : prevented double clicks from triggering editing of containing tiddler.
*25-07-06, version 0.9
!Code
***/
//{{{
config.fontSize={};
//configuration settings
config.fontSize.settings =
{
defaultSize : 100, // all sizes in %
maxSize : 200,
minSize : 40,
stepSize : 10
};
//startup code
var fontSettings = config.fontSize.settings;
if (!config.options.txtFontSize)
{config.options.txtFontSize = fontSettings.defaultSize;
saveOptionCookie("txtFontSize");}
setStylesheet(".tiddler .viewer {font-size:"+config.options.txtFontSize+"%;}\n","fontResizerStyles");
setStylesheet("#contentWrapper .fontResizer .button {display:inline;font-size:105%; font-weight:bold; margin:0 1px; padding: 0 3px; text-align:center !important;}\n .fontResizer {margin:0 0.5em;}","fontResizerButtonStyles");
//macro
config.macros.fontSize={};
config.macros.fontSize.handler = function (place,macroName,params,wikifier,paramString,tiddler)
{
var sp = createTiddlyElement(place,"span",null,"fontResizer");
sp.ondblclick=this.onDblClick;
if (params[0])
createTiddlyText(sp,params[0]);
createTiddlyButton(sp,"+","increase font-size",this.incFont);
createTiddlyButton(sp,"=","reset font-size",this.resetFont);
createTiddlyButton(sp,"–","decrease font-size",this.decFont);
}
config.macros.fontSize.onDblClick = function (e)
{
if (!e) var e = window.event;
e.cancelBubble = true;
if (e.stopPropagation) e.stopPropagation();
return false;
}
config.macros.fontSize.setFont = function ()
{
saveOptionCookie("txtFontSize");
setStylesheet(".tiddler .viewer {font-size:"+config.options.txtFontSize+"%;}\n","fontResizerStyles");
}
config.macros.fontSize.incFont=function()
{
if (config.options.txtFontSize < fontSettings.maxSize)
config.options.txtFontSize = (config.options.txtFontSize*1)+fontSettings.stepSize;
config.macros.fontSize.setFont();
}
config.macros.fontSize.decFont=function()
{
if (config.options.txtFontSize > fontSettings.minSize)
config.options.txtFontSize = (config.options.txtFontSize*1) - fontSettings.stepSize;
config.macros.fontSize.setFont();
}
config.macros.fontSize.resetFont=function()
{
config.options.txtFontSize=fontSettings.defaultSize;
config.macros.fontSize.setFont();
}
config.paramifiers.font =
{
onstart: function(v)
{
config.options.txtFontSize = v;
config.macros.fontSize.setFont();
}
};
//}}}
/***
|Name|HoverMenuPlugin|
|Created by|SaqImtiaz|
|Location|http://tw.lewcid.org/#HoverMenuPlugin|
|Version|1.11|
|Requires|~TW2.x|
!Description:
Provides a hovering menu on the edge of the screen for commonly used commands, that scrolls with the page.
!Demo:
Observe the hovering menu on the right edge of the screen.
!Installation:
Copy the contents of this tiddler to your TW, tag with systemConfig, save and reload your TW.
To customize your HoverMenu, edit the HoverMenu shadow tiddler.
To customize whether the menu sticks to the right or left edge of the screen, and its start position, edit the HoverMenu configuration settings part of the code below. It's well documented, so don't be scared!
The menu has an id of hoverMenu, in case you want to style the buttons in it using css.
!Notes:
Since the default HoverMenu contains buttons for toggling the side bar and jumping to the top of the screen and to open tiddlers, the ToggleSideBarMacro, JumpMacro and the JumpToTopMacro are included in this tiddler, so you dont need to install them separately. Having them installed separately as well could lead to complications.
If you dont intend to use these three macros at all, feel free to remove those sections of code in this tiddler.
!To Do:
* rework code to allow multiple hovering menus in different positions, horizontal etc.
* incorporate code for keyboard shortcuts that correspond to the buttons in the hovermenu
!History:
*03-08-06, ver 1.1.2: compatibility fix with SelectThemePlugin
*03-08-06, ver 1.11: fixed error with button tooltips
*27-07-06, ver 1.1 : added JumpMacro to hoverMenu
*23-07-06
!Code
***/
/***
start HoverMenu plugin code
***/
//{{{
config.hoverMenu={};
//}}}
/***
HoverMenu configuration settings
***/
//{{{
config.hoverMenu.settings={
align: 'right', //align menu to right or left side of screen, possible values are 'right' and 'left'
x: 1, // horizontal distance of menu from side of screen, increase to your liking.
y: 158 //vertical distance of menu from top of screen at start, increase or decrease to your liking
};
//}}}
//{{{
//continue HoverMenu plugin code
config.hoverMenu.handler=function()
{
if (!document.getElementById("hoverMenu"))
{
var theMenu = createTiddlyElement(document.getElementById("contentWrapper"), "div","hoverMenu");
theMenu.setAttribute("refresh","content");
theMenu.setAttribute("tiddler","HoverMenu");
var menuContent = store.getTiddlerText("HoverMenu");
wikify(menuContent,theMenu);
}
var Xloc = this.settings.x;
Yloc =this.settings.y;
var ns = (navigator.appName.indexOf("Netscape") != -1);
function SetMenu(id)
{
var GetElements=document.getElementById?document.getElementById(id):document.all?document.all[id]:document.layers[id];
if(document.layers)GetElements.style=GetElements;
GetElements.sP=function(x,y){this.style[config.hoverMenu.settings.align]=x +"px";this.style.top=y +"px";};
GetElements.x = Xloc;
GetElements.y = findScrollY();
GetElements.y += Yloc;
return GetElements;
}
window.LoCate_XY=function()
{
var pY = findScrollY();
ftlObj.y += (pY + Yloc - ftlObj.y)/15;
ftlObj.sP(ftlObj.x, ftlObj.y);
setTimeout("LoCate_XY()", 10);
}
ftlObj = SetMenu("hoverMenu");
LoCate_XY();
};
window.old_lewcid_hovermenu_restart = restart;
restart = function()
{
window.old_lewcid_hovermenu_restart();
config.hoverMenu.handler();
};
setStylesheet(
"#hoverMenu .imgLink, #hoverMenu .imgLink:hover {border:none; padding:0px; float:right; margin-bottom:2px; margin-top:0px;}\n"+
"#hoverMenu .button, #hoverMenu .tiddlyLink {border:none; font-weight:bold; background:#18f; color:#FFF; padding:0 5px; float:right; margin-bottom:4px;}\n"+
"#hoverMenu .button:hover, #hoverMenu .tiddlyLink:hover {font-weight:bold; border:none; color:#fff; background:#000; padding:0 5px; float:right; margin-bottom:4px;}\n"+
"#hoverMenu .button {width:100%; text-align:center}"+
"#hoverMenu { position:absolute; width:7px;}\n"+
"\n","hoverMenuStyles");
config.macros.renameButton={};
config.macros.renameButton.handler = function(place,macroName,params,wikifier,paramString,tiddler)
{
if (place.lastChild.tagName!="BR")
{
place.lastChild.firstChild.data = params[0];
if (params[1]) {place.lastChild.title = params[1];}
}
};
config.shadowTiddlers["HoverMenu"]="<<top>>\n<<toggleSideBar>><<renameButton '>' >>\n<<jump j '' top>>\n<<saveChanges>><<renameButton s 'Save TiddlyWiki'>>\n<<newTiddler>><<renameButton n>>\n";
//}}}
//end HoverMenu plugin code
//Start ToggleSideBarMacro code
//{{{
config.macros.toggleSideBar={};
config.macros.toggleSideBar.settings={
styleHide : "#sidebar { display: none;}\n"+"#contentWrapper #displayArea { margin-right: 1em;}\n"+"",
styleShow : " ",
arrow1: "«",
arrow2: "»"
};
config.macros.toggleSideBar.handler=function (place,macroName,params,wikifier,paramString,tiddler)
{
var tooltip= params[1]||'toggle sidebar';
var mode = (params[2] && params[2]=="hide")? "hide":"show";
var arrow = (mode == "hide")? this.settings.arrow1:this.settings.arrow2;
var label= (params[0]&¶ms[0]!='.')?params[0]+" "+arrow:arrow;
var theBtn = createTiddlyButton(place,label,tooltip,this.onToggleSideBar,"button HideSideBarButton");
if (mode == "hide")
{
(document.getElementById("sidebar")).setAttribute("toggle","hide");
setStylesheet(this.settings.styleHide,"ToggleSideBarStyles");
}
};
config.macros.toggleSideBar.onToggleSideBar = function(){
var sidebar = document.getElementById("sidebar");
var settings = config.macros.toggleSideBar.settings;
if (sidebar.getAttribute("toggle")=='hide')
{
setStylesheet(settings.styleShow,"ToggleSideBarStyles");
sidebar.setAttribute("toggle","show");
this.firstChild.data= (this.firstChild.data).replace(settings.arrow1,settings.arrow2);
}
else
{
setStylesheet(settings.styleHide,"ToggleSideBarStyles");
sidebar.setAttribute("toggle","hide");
this.firstChild.data= (this.firstChild.data).replace(settings.arrow2,settings.arrow1);
}
return false;
}
setStylesheet(".HideSideBarButton .button {font-weight:bold; padding: 0 5px;}\n","ToggleSideBarButtonStyles");
//}}}
//end ToggleSideBarMacro code
//start JumpToTopMacro code
//{{{
config.macros.top={};
config.macros.top.handler=function(place,macroName)
{
createTiddlyButton(place,"^","jump to top",this.onclick);
}
config.macros.top.onclick=function()
{
window.scrollTo(0,0);
};
config.commands.top =
{
text:" ^ ",
tooltip:"jump to top"
};
config.commands.top.handler = function(event,src,title)
{
window.scrollTo(0,0);
}
//}}}
//end JumpToStartMacro code
//start JumpMacro code
//{{{
config.macros.jump= {};
config.macros.jump.handler = function (place,macroName,params,wikifier,paramString,tiddler)
{
var label = (params[0] && params[0]!=".")? params[0]: 'jump';
var tooltip = (params[1] && params[1]!=".")? params[1]: 'jump to an open tiddler';
var top = (params[2] && params[2]=='top') ? true: false;
var btn =createTiddlyButton(place,label,tooltip,this.onclick);
if (top==true)
btn.setAttribute("top","true")
}
config.macros.jump.onclick = function(e)
{
if (!e) var e = window.event;
var theTarget = resolveTarget(e);
var top = theTarget.getAttribute("top");
var popup = Popup.create(this);
if(popup)
{
if(top=="true")
{createTiddlyButton(createTiddlyElement(popup,"li"),'Top ↑','Top of TW',config.macros.jump.top);
createTiddlyElement(popup,"hr");}
story.forEachTiddler(function(title,element) {
createTiddlyLink(createTiddlyElement(popup,"li"),title,true);
});
}
Popup.show(popup,false);
e.cancelBubble = true;
if (e.stopPropagation) e.stopPropagation();
return false;
}
config.macros.jump.top = function()
{
window.scrollTo(0,0);
}
//}}}
//end JumpMacro code
//utility functions
//{{{
Popup.show = function(unused,slowly)
{
var curr = Popup.stack[Popup.stack.length-1];
var rootLeft = findPosX(curr.root);
var rootTop = findPosY(curr.root);
var rootHeight = curr.root.offsetHeight;
var popupLeft = rootLeft;
var popupTop = rootTop + rootHeight;
var popupWidth = curr.popup.offsetWidth;
var winWidth = findWindowWidth();
if (isChild(curr.root,'hoverMenu'))
var x = config.hoverMenu.settings.x;
else
var x = 0;
if(popupLeft + popupWidth+x > winWidth)
popupLeft = winWidth - popupWidth -x;
if (isChild(curr.root,'hoverMenu'))
{curr.popup.style.right = x + "px";}
else
curr.popup.style.left = popupLeft + "px";
curr.popup.style.top = popupTop + "px";
curr.popup.style.display = "block";
addClass(curr.root,"highlight");
if(config.options.chkAnimate)
anim.startAnimating(new Scroller(curr.popup,slowly));
else
window.scrollTo(0,ensureVisible(curr.popup));
}
window.isChild = function(e,parentId) {
while (e != null) {
var parent = document.getElementById(parentId);
if (parent == e) return true;
e = e.parentNode;
}
return false;
};
//}}}
/***
|''Name:''|LoadRemoteFileThroughProxy (previous LoadRemoteFileHijack)|
|''Description:''|When the TiddlyWiki file is located on the web (view over http) the content of [[SiteProxy]] tiddler is added in front of the file url. If [[SiteProxy]] does not exist "/proxy/" is added. |
|''Version:''|1.1.0|
|''Date:''|mar 17, 2007|
|''Source:''|http://tiddlywiki.bidix.info/#LoadRemoteFileHijack|
|''Author:''|BidiX (BidiX (at) bidix (dot) info)|
|''License:''|[[BSD open source license|http://tiddlywiki.bidix.info/#%5B%5BBSD%20open%20source%20license%5D%5D ]]|
|''~CoreVersion:''|2.2.0|
***/
//{{{
version.extensions.LoadRemoteFileThroughProxy = {
major: 1, minor: 1, revision: 0,
date: new Date("mar 17, 2007"),
source: "http://tiddlywiki.bidix.info/#LoadRemoteFileThroughProxy"};
if (!window.bidix) window.bidix = {}; // bidix namespace
if (!bidix.core) bidix.core = {};
bidix.core.loadRemoteFile = loadRemoteFile;
loadRemoteFile = function(url,callback,params)
{
if ((document.location.toString().substr(0,4) == "http") && (url.substr(0,4) == "http")){
url = store.getTiddlerText("SiteProxy", "/proxy/") + url;
}
return bidix.core.loadRemoteFile(url,callback,params);
}
//}}}
$y = \beta_0+\beta_1x+\epsilon$
----
[[迴歸分析預定進度]]
[[評比計畫|2009迴歸分析評比計畫]]
[[期末報告]]
[[比賽規則]]
----
迴歸分析議題
<<tag 表列網頁>>
----
R
[[全解析data.frame()]]
[[全解析lm()]]
[[我要跟R交朋友]]
[[迴歸分析的相關函示]]
----
指導
[[數據分析初論]]
[[拔靴法]]
[[估計統計量的變異數]]
----
作業答案區
[[第一次作業的參考答案|恰弟與哈弟第47頁練習題2-10]]
[[第二次作業的參考答案|更多拔靴法的體驗]]
[[第三次作業的參考答案|我要比較各式各樣的拔靴信賴區間]]
----
作業名人堂
<<tag 徐瑜彬>>
<<tag 林辰昇>>
----
考古題
[[期中考|2009期中考]]
----
Tools
[[R|R本店]]
[[jsMath]]
[[FireFox]]
[[TiddlyWiki]]
[[統計方法決策樹]]
----
<html><div align="center"><object width="425" height="344"><param name="movie" value="http://www.youtube.com/v/tfyyGQxZrAY&hl=zh_TW&fs=1&"></param><param name="allowFullScreen" value="true"></param><param name="allowscriptaccess" value="always"></param><embed src="http://www.youtube.com/v/tfyyGQxZrAY&hl=zh_TW&fs=1&" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="425" height="344"></embed></object></div></html>
/***
|Name|NestedSlidersPlugin|
|Source|http://www.TiddlyTools.com/#NestedSlidersPlugin|
|Documentation|http://www.TiddlyTools.com/#NestedSlidersPluginInfo|
|Version|2.3.4|
|Author|Eric Shulman - ELS Design Studios|
|License|http://www.TiddlyTools.com/#LegalStatements <br>and [[Creative Commons Attribution-ShareAlike 2.5 License|http://creativecommons.org/licenses/by-sa/2.5/]]|
|~CoreVersion|2.1|
|Type|plugin|
|Requires||
|Overrides||
|Description|show content in nest-able sliding/floating panels, without creating separate tiddlers for each panel's content|
This plugin adds new wiki syntax for embedding 'slider' panels directly into tiddler content.
!!!!!Documentation
>see [[NestedSlidersPluginInfo]]
!!!!!Configuration
<<<
Enable animation for slider panels
<<option chkFloatingSlidersAnimate>> allow sliders to animate when opening/closing
>(note: This setting is in //addition// to the general option for enabling/disabling animation effects:
><<option chkAnimate>> enable animations (entire document)
>For slider animation to occur, you must also allow animation in general.
Debugging messages for 'lazy sliders' deferred rendering:
<<option chkDebugLazySliderDefer>> show debugging alert when deferring slider rendering
<<option chkDebugLazySliderRender>> show debugging alert when deferred slider is actually rendered
<<<
!!!!!Revisions
<<<
2008.01.08 - [*.*.*] plugin size reduction: documentation moved to ...Info tiddler
2007.12.28 - 2.3.4 added hijack for Animator.prototype.startAnimating(). Previously, the plugin code simply set the overflow to "visible" after animation. This code tweak corrects handling of elements that were styled with overflow=hidden/auto/scroll before animation by saving the overflow style and then restoring it after animation has completed.
|please see [[NestedSlidersPluginInfo]] for additional revision details|
2005.11.03 - 1.0.0 initial public release. Thanks to RodneyGomes, GeoffSlocock, and PaulPetterson for suggestions and experiments.
<<<
!!!!!Code
***/
//{{{
version.extensions.nestedSliders = {major: 2, minor: 3, revision: 4, date: new Date(2007,12,28)};
//}}}
//{{{
// options for deferred rendering of sliders that are not initially displayed
if (config.options.chkDebugLazySliderDefer==undefined) config.options.chkDebugLazySliderDefer=false;
if (config.options.chkDebugLazySliderRender==undefined) config.options.chkDebugLazySliderRender=false;
if (config.options.chkFloatingSlidersAnimate==undefined) config.options.chkFloatingSlidersAnimate=false;
// default styles for 'floating' class
setStylesheet(".floatingPanel { position:absolute; z-index:10; padding:0.5em; margin:0em; \
background-color:#eee; color:#000; border:1px solid #000; text-align:left; }","floatingPanelStylesheet");
//}}}
//{{{
config.formatters.push( {
name: "nestedSliders",
match: "\\n?\\+{3}",
terminator: "\\s*\\={3}\\n?",
lookahead: "\\n?\\+{3}(\\+)?(\\([^\\)]*\\))?(\\!*)?(\\^(?:[^\\^\\*\\[\\>]*\\^)?)?(\\*)?(?:\\{\\{([\\w]+[\\s\\w]*)\\{)?(\\[[^\\]]*\\])?(\\[[^\\]]*\\])?(?:\\}{3})?(\\#[^:]*\\:)?(\\>)?(\\.\\.\\.)?\\s*",
handler: function(w)
{
lookaheadRegExp = new RegExp(this.lookahead,"mg");
lookaheadRegExp.lastIndex = w.matchStart;
var lookaheadMatch = lookaheadRegExp.exec(w.source)
if(lookaheadMatch && lookaheadMatch.index == w.matchStart)
{
// var defopen=lookaheadMatch[1]
// var cookiename=lookaheadMatch[2]
// var header=lookaheadMatch[3]
// var panelwidth=lookaheadMatch[4]
// var transient=lookaheadMatch[5]
// var class=lookaheadMatch[6]
// var label=lookaheadMatch[7]
// var openlabel=lookaheadMatch[8]
// var panelID=lookaheadMatch[9]
// var blockquote=lookaheadMatch[10]
// var deferred=lookaheadMatch[11]
// location for rendering button and panel
var place=w.output;
// default to closed, no cookie, no accesskey, no alternate text/tip
var show="none"; var cookie=""; var key="";
var closedtext=">"; var closedtip="";
var openedtext="<"; var openedtip="";
// extra "+", default to open
if (lookaheadMatch[1]) show="block";
// cookie, use saved open/closed state
if (lookaheadMatch[2]) {
cookie=lookaheadMatch[2].trim().slice(1,-1);
cookie="chkSlider"+cookie;
if (config.options[cookie]==undefined)
{ config.options[cookie] = (show=="block") }
show=config.options[cookie]?"block":"none";
}
// parse label/tooltip/accesskey: [label=X|tooltip]
if (lookaheadMatch[7]) {
var parts=lookaheadMatch[7].trim().slice(1,-1).split("|");
closedtext=parts.shift();
if (closedtext.substr(closedtext.length-2,1)=="=")
{ key=closedtext.substr(closedtext.length-1,1); closedtext=closedtext.slice(0,-2); }
openedtext=closedtext;
if (parts.length) closedtip=openedtip=parts.join("|");
else { closedtip="show "+closedtext; openedtip="hide "+closedtext; }
}
// parse alternate label/tooltip: [label|tooltip]
if (lookaheadMatch[8]) {
var parts=lookaheadMatch[8].trim().slice(1,-1).split("|");
openedtext=parts.shift();
if (parts.length) openedtip=parts.join("|");
else openedtip="hide "+openedtext;
}
var title=show=='block'?openedtext:closedtext;
var tooltip=show=='block'?openedtip:closedtip;
// create the button
if (lookaheadMatch[3]) { // use "Hn" header format instead of button/link
var lvl=(lookaheadMatch[3].length>6)?6:lookaheadMatch[3].length;
var btn = createTiddlyElement(createTiddlyElement(place,"h"+lvl,null,null,null),"a",null,lookaheadMatch[6],title);
btn.onclick=onClickNestedSlider;
btn.setAttribute("href","javascript:;");
btn.setAttribute("title",tooltip);
}
else
var btn = createTiddlyButton(place,title,tooltip,onClickNestedSlider,lookaheadMatch[6]);
btn.innerHTML=title; // enables use of HTML entities in label
// set extra button attributes
btn.setAttribute("closedtext",closedtext);
btn.setAttribute("closedtip",closedtip);
btn.setAttribute("openedtext",openedtext);
btn.setAttribute("openedtip",openedtip);
btn.sliderCookie = cookie; // save the cookiename (if any) in the button object
btn.defOpen=lookaheadMatch[1]!=null; // save default open/closed state (boolean)
btn.keyparam=key; // save the access key letter ("" if none)
if (key.length) {
btn.setAttribute("accessKey",key); // init access key
btn.onfocus=function(){this.setAttribute("accessKey",this.keyparam);}; // **reclaim** access key on focus
}
btn.onmouseover=function(event) // mouseover on button aligns floater position with button
{ if (window.adjustSliderPos) window.adjustSliderPos(this.parentNode,this,this.sliderPanel); }
// create slider panel
var panelClass=lookaheadMatch[4]?"floatingPanel":"sliderPanel";
var panelID=lookaheadMatch[9]; if (panelID) panelID=panelID.slice(1,-1); // trim off delimiters
var panel=createTiddlyElement(place,"div",panelID,panelClass,null);
panel.button = btn; // so the slider panel know which button it belongs to
btn.sliderPanel=panel; // so the button knows which slider panel it belongs to
panel.defaultPanelWidth=(lookaheadMatch[4] && lookaheadMatch[4].length>2)?lookaheadMatch[4].slice(1,-1):"";
panel.setAttribute("transient",lookaheadMatch[5]=="*"?"true":"false");
panel.style.display = show;
panel.style.width=panel.defaultPanelWidth;
panel.onmouseover=function(event) // mouseover on panel aligns floater position with button
{ if (window.adjustSliderPos) window.adjustSliderPos(this.parentNode,this.button,this); }
// render slider (or defer until shown)
w.nextMatch = lookaheadMatch.index + lookaheadMatch[0].length;
if ((show=="block")||!lookaheadMatch[11]) {
// render now if panel is supposed to be shown or NOT deferred rendering
w.subWikify(lookaheadMatch[10]?createTiddlyElement(panel,"blockquote"):panel,this.terminator);
// align floater position with button
if (window.adjustSliderPos) window.adjustSliderPos(place,btn,panel);
}
else {
var src = w.source.substr(w.nextMatch);
var endpos=findMatchingDelimiter(src,"+++","===");
panel.setAttribute("raw",src.substr(0,endpos));
panel.setAttribute("blockquote",lookaheadMatch[10]?"true":"false");
panel.setAttribute("rendered","false");
w.nextMatch += endpos+3;
if (w.source.substr(w.nextMatch,1)=="\n") w.nextMatch++;
if (config.options.chkDebugLazySliderDefer) alert("deferred '"+title+"':\n\n"+panel.getAttribute("raw"));
}
}
}
}
)
// TBD: ignore 'quoted' delimiters (e.g., "{{{+++foo===}}}" isn't really a slider)
function findMatchingDelimiter(src,starttext,endtext) {
var startpos = 0;
var endpos = src.indexOf(endtext);
// check for nested delimiters
while (src.substring(startpos,endpos-1).indexOf(starttext)!=-1) {
// count number of nested 'starts'
var startcount=0;
var temp = src.substring(startpos,endpos-1);
var pos=temp.indexOf(starttext);
while (pos!=-1) { startcount++; pos=temp.indexOf(starttext,pos+starttext.length); }
// set up to check for additional 'starts' after adjusting endpos
startpos=endpos+endtext.length;
// find endpos for corresponding number of matching 'ends'
while (startcount && endpos!=-1) {
endpos = src.indexOf(endtext,endpos+endtext.length);
startcount--;
}
}
return (endpos==-1)?src.length:endpos;
}
//}}}
//{{{
window.onClickNestedSlider=function(e)
{
if (!e) var e = window.event;
var theTarget = resolveTarget(e);
var theLabel = theTarget.firstChild.data;
var theSlider = theTarget.sliderPanel
var isOpen = theSlider.style.display!="none";
// toggle label
theTarget.innerHTML=isOpen?theTarget.getAttribute("closedText"):theTarget.getAttribute("openedText");
// toggle tooltip
theTarget.setAttribute("title",isOpen?theTarget.getAttribute("closedTip"):theTarget.getAttribute("openedTip"));
// deferred rendering (if needed)
if (theSlider.getAttribute("rendered")=="false") {
if (config.options.chkDebugLazySliderRender)
alert("rendering '"+theLabel+"':\n\n"+theSlider.getAttribute("raw"));
var place=theSlider;
if (theSlider.getAttribute("blockquote")=="true")
place=createTiddlyElement(place,"blockquote");
wikify(theSlider.getAttribute("raw"),place);
theSlider.setAttribute("rendered","true");
}
// show/hide the slider
if(config.options.chkAnimate && (!hasClass(theSlider,'floatingPanel') || config.options.chkFloatingSlidersAnimate))
anim.startAnimating(new Slider(theSlider,!isOpen,e.shiftKey || e.altKey,"none"));
else
theSlider.style.display = isOpen ? "none" : "block";
// reset to default width (might have been changed via plugin code)
theSlider.style.width=theSlider.defaultPanelWidth;
// align floater panel position with target button
if (!isOpen && window.adjustSliderPos) window.adjustSliderPos(theSlider.parentNode,theTarget,theSlider);
// if showing panel, set focus to first 'focus-able' element in panel
if (theSlider.style.display!="none") {
var ctrls=theSlider.getElementsByTagName("*");
for (var c=0; c<ctrls.length; c++) {
var t=ctrls[c].tagName.toLowerCase();
if ((t=="input" && ctrls[c].type!="hidden") || t=="textarea" || t=="select")
{ ctrls[c].focus(); break; }
}
}
var cookie=theTarget.sliderCookie;
if (cookie && cookie.length) {
config.options[cookie]=!isOpen;
if (config.options[cookie]!=theTarget.defOpen)
saveOptionCookie(cookie);
else { // remove cookie if slider is in default display state
var ex=new Date(); ex.setTime(ex.getTime()-1000);
document.cookie = cookie+"=novalue; path=/; expires="+ex.toGMTString();
}
}
// prevent SHIFT-CLICK from being processed by browser (opens blank window... yuck!)
// but allow plain click to bubble up to page background (to dismiss open popup, if any)
if (e.shiftKey) { e.cancelBubble=true; if (e.stopPropagation) e.stopPropagation(); }
return false;
}
//}}}
//{{{
// click in document background closes transient panels
document.nestedSliders_savedOnClick=document.onclick;
document.onclick=function(ev) { if (!ev) var ev=window.event; var target=resolveTarget(ev);
// call original click handler
if (document.nestedSliders_savedOnClick)
var retval=document.nestedSliders_savedOnClick.apply(this,arguments);
// if click was inside transient panel (or something contained by a transient panel)... leave it alone
var p=target;
while (p)
if ((hasClass(p,"floatingPanel")||hasClass(p,"sliderPanel"))&&p.getAttribute("transient")=="true") break;
else p=p.parentNode;
if (p) return retval;
// otherwise, find and close all transient panels...
var all=document.all?document.all:document.getElementsByTagName("DIV");
for (var i=0; i<all.length; i++) {
// if it is not a transient panel, or the click was on the button that opened this panel, don't close it.
if (all[i].getAttribute("transient")!="true" || all[i].button==target) continue;
// otherwise, if the panel is currently visible, close it by clicking it's button
if (all[i].style.display!="none") window.onClickNestedSlider({target:all[i].button})
}
return retval;
};
//}}}
//{{{
// adjust floating panel position based on button position
if (window.adjustSliderPos==undefined) window.adjustSliderPos=function(place,btn,panel) {
if (hasClass(panel,"floatingPanel")) {
var left=0;
var top=btn.offsetHeight;
if (place.style.position!="relative") {
var left=findPosX(btn);
var top=findPosY(btn)+btn.offsetHeight;
var p=place; while (p && !hasClass(p,'floatingPanel')) p=p.parentNode;
if (p) { left-=findPosX(p); top-=findPosY(p); }
}
if (findPosX(btn)+panel.offsetWidth > getWindowWidth()) // adjust position to stay inside right window edge
left-=findPosX(btn)+panel.offsetWidth-getWindowWidth()+15; // add extra 15px 'fudge factor'
panel.style.left=left+"px"; panel.style.top=top+"px";
}
}
function getWindowWidth() {
if(document.width!=undefined)
return document.width; // moz (FF)
if(document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) )
return document.documentElement.clientWidth; // IE6
if(document.body && ( document.body.clientWidth || document.body.clientHeight ) )
return document.body.clientWidth; // IE4
if(window.innerWidth!=undefined)
return window.innerWidth; // IE - general
return 0; // unknown
}
//}}}
//{{{
// TW2.1 and earlier:
// hijack Slider animation handler 'stop' handler so overflow is visible after animation has completed
Slider.prototype.coreStop = Slider.prototype.stop;
Slider.prototype.stop = function()
{ this.coreStop.apply(this,arguments); this.element.style.overflow = "visible"; }
// TW2.2+
// hijack start/stop handlers so overflow style is saved and restored after animation has completed
if (version.major+.1*version.minor+.01*version.revision>=2.2) {
/**
Animator.prototype.core_startAnimating = Animator.prototype.startAnimating;
Animator.prototype.startAnimating = function() {
for(var t=0; t<arguments.length; t++)
arguments[t].element.save_overflow=arguments[t].element.style.overflow;
return this.core_startAnimating.apply(this,arguments);
};
**/
Morpher.prototype.coreStop = Morpher.prototype.stop;
Morpher.prototype.stop = function() {
this.coreStop.apply(this,arguments);
this.element.style.overflow = this.element.save_overflow||"visible";
};
}
//}}}
/***
|''Name:''|PasswordOptionPlugin|
|''Description:''|Extends TiddlyWiki options with non encrypted password option.|
|''Version:''|1.0.2|
|''Date:''|Apr 19, 2007|
|''Source:''|http://tiddlywiki.bidix.info/#PasswordOptionPlugin|
|''Author:''|BidiX (BidiX (at) bidix (dot) info)|
|''License:''|[[BSD open source license|http://tiddlywiki.bidix.info/#%5B%5BBSD%20open%20source%20license%5D%5D ]]|
|''~CoreVersion:''|2.2.0 (Beta 5)|
***/
//{{{
version.extensions.PasswordOptionPlugin = {
major: 1, minor: 0, revision: 2,
date: new Date("Apr 19, 2007"),
source: 'http://tiddlywiki.bidix.info/#PasswordOptionPlugin',
author: 'BidiX (BidiX (at) bidix (dot) info',
license: '[[BSD open source license|http://tiddlywiki.bidix.info/#%5B%5BBSD%20open%20source%20license%5D%5D]]',
coreVersion: '2.2.0 (Beta 5)'
};
config.macros.option.passwordCheckboxLabel = "Save this password on this computer";
config.macros.option.passwordInputType = "password"; // password | text
setStylesheet(".pasOptionInput {width: 11em;}\n","passwordInputTypeStyle");
merge(config.macros.option.types, {
'pas': {
elementType: "input",
valueField: "value",
eventName: "onkeyup",
className: "pasOptionInput",
typeValue: config.macros.option.passwordInputType,
create: function(place,type,opt,className,desc) {
// password field
config.macros.option.genericCreate(place,'pas',opt,className,desc);
// checkbox linked with this password "save this password on this computer"
config.macros.option.genericCreate(place,'chk','chk'+opt,className,desc);
// text savePasswordCheckboxLabel
place.appendChild(document.createTextNode(config.macros.option.passwordCheckboxLabel));
},
onChange: config.macros.option.genericOnChange
}
});
merge(config.optionHandlers['chk'], {
get: function(name) {
// is there an option linked with this chk ?
var opt = name.substr(3);
if (config.options[opt])
saveOptionCookie(opt);
return config.options[name] ? "true" : "false";
}
});
merge(config.optionHandlers, {
'pas': {
get: function(name) {
if (config.options["chk"+name]) {
return encodeCookie(config.options[name].toString());
} else {
return "";
}
},
set: function(name,value) {config.options[name] = decodeCookie(value);}
}
});
// need to reload options to load passwordOptions
loadOptionsCookie();
/*
if (!config.options['pasPassword'])
config.options['pasPassword'] = '';
merge(config.optionsDesc,{
pasPassword: "Test password"
});
*/
//}}}
// AJAX code adapted from http://timmorgan.org/mini
// This is already loaded by ziddlywiki...
if(typeof(window["ajax"]) == "undefined") {
ajax = {
x: function(){try{return new ActiveXObject('Msxml2.XMLHTTP')}catch(e){try{return new ActiveXObject('Microsoft.XMLHTTP')}catch(e){return new XMLHttpRequest()}}},
gets: function(url){var x=ajax.x();x.open('GET',url,false);x.send(null);return x.responseText}
}
}
// Load jsMath
jsMath = {
Setup: {inited: 1}, // don't run jsMath.Setup.Body() yet
Autoload: {root: new String(document.location).replace(/[^\/]*$/,'jsMath/')} // URL to jsMath directory, change if necessary
};
var jsMathstr;
try {
jsMathstr = ajax.gets(jsMath.Autoload.root+"jsMath.js");
} catch(e) {
alert("jsMath was not found: you must place the 'jsMath' directory in the same place as this file. "
+"The error was:\n"+e.name+": "+e.message);
throw(e); // abort eval
}
try {
window.eval(jsMathstr);
} catch(e) {
alert("jsMath failed to load. The error was:\n"+e.name + ": " + e.message + " on line " + e.lineNumber);
}
jsMath.Setup.inited=0; // allow jsMath.Setup.Body() to run again
// Define wikifers for latex
config.formatterHelpers.mathFormatHelper = function(w) {
var e = document.createElement(this.element);
e.className = this.className;
var endRegExp = new RegExp(this.terminator, "mg");
endRegExp.lastIndex = w.matchStart+w.matchLength;
var matched = endRegExp.exec(w.source);
if(matched) {
var txt = w.source.substr(w.matchStart+w.matchLength,
matched.index-w.matchStart-w.matchLength);
if(this.keepdelim) {
txt = w.source.substr(w.matchStart, matched.index+matched[0].length-w.matchStart);
}
e.appendChild(document.createTextNode(txt));
w.output.appendChild(e);
w.nextMatch = endRegExp.lastIndex;
}
}
config.formatters.push({
name: "displayMath1",
match: "\\\$\\\$",
terminator: "\\\$\\\$\\n?", // 2.0 compatability
termRegExp: "\\\$\\\$\\n?",
element: "div",
className: "math",
handler: config.formatterHelpers.mathFormatHelper
});
config.formatters.push({
name: "inlineMath1",
match: "\\\$",
terminator: "\\\$", // 2.0 compatability
termRegExp: "\\\$",
element: "span",
className: "math",
handler: config.formatterHelpers.mathFormatHelper
});
var backslashformatters = new Array(0);
backslashformatters.push({
name: "inlineMath2",
match: "\\\\\\\(",
terminator: "\\\\\\\)", // 2.0 compatability
termRegExp: "\\\\\\\)",
element: "span",
className: "math",
handler: config.formatterHelpers.mathFormatHelper
});
backslashformatters.push({
name: "displayMath2",
match: "\\\\\\\[",
terminator: "\\\\\\\]\\n?", // 2.0 compatability
termRegExp: "\\\\\\\]\\n?",
element: "div",
className: "math",
handler: config.formatterHelpers.mathFormatHelper
});
backslashformatters.push({
name: "displayMath3",
match: "\\\\begin\\{equation\\}",
terminator: "\\\\end\\{equation\\}\\n?", // 2.0 compatability
termRegExp: "\\\\end\\{equation\\}\\n?",
element: "div",
className: "math",
handler: config.formatterHelpers.mathFormatHelper
});
// These can be nested. e.g. \begin{equation} \begin{array}{ccc} \begin{array}{ccc} ...
backslashformatters.push({
name: "displayMath4",
match: "\\\\begin\\{eqnarray\\}",
terminator: "\\\\end\\{eqnarray\\}\\n?", // 2.0 compatability
termRegExp: "\\\\end\\{eqnarray\\}\\n?",
element: "div",
className: "math",
keepdelim: true,
handler: config.formatterHelpers.mathFormatHelper
});
// The escape must come between backslash formatters and regular ones.
// So any latex-like \commands must be added to the beginning of
// backslashformatters here.
backslashformatters.push({
name: "escape",
match: "\\\\.",
handler: function(w) {
w.output.appendChild(document.createTextNode(w.source.substr(w.matchStart+1,1)));
w.nextMatch = w.matchStart+2;
}
});
config.formatters=backslashformatters.concat(config.formatters);
window.wikify = function(source,output,highlightRegExp,tiddler)
{
if(source && source != "") {
if(version.major == 2 && version.minor > 0) {
var wikifier = new Wikifier(source,getParser(tiddler),highlightRegExp,tiddler);
wikifier.subWikifyUnterm(output);
} else {
var wikifier = new Wikifier(source,formatter,highlightRegExp,tiddler);
wikifier.subWikify(output,null);
}
jsMath.ProcessBeforeShowing();
}
}
!數據:Duncan{car}
+++[它的data.frame]
{{{
> Duncan
type income education prestige
accountant prof 62 86 82
pilot prof 72 76 83
architect prof 75 92 90
author prof 55 90 76
chemist prof 64 86 90
minister prof 21 84 87
professor prof 64 93 93
dentist prof 80 100 90
reporter wc 67 87 52
engineer prof 72 86 88
undertaker prof 42 74 57
lawyer prof 76 98 89
physician prof 76 97 97
welfare.worker prof 41 84 59
teacher prof 48 91 73
conductor wc 76 34 38
contractor prof 53 45 76
factory.owner prof 60 56 81
store.manager prof 42 44 45
banker prof 78 82 92
bookkeeper wc 29 72 39
mail.carrier wc 48 55 34
insurance.agent wc 55 71 41
store.clerk wc 29 50 16
carpenter bc 21 23 33
electrician bc 47 39 53
RR.engineer bc 81 28 67
machinist bc 36 32 57
auto.repairman bc 22 22 26
plumber bc 44 25 29
gas.stn.attendant bc 15 29 10
coal.miner bc 7 7 15
streetcar.motorman bc 42 26 19
taxi.driver bc 9 19 10
truck.driver bc 21 15 13
machine.operator bc 21 20 24
barber bc 16 26 20
bartender bc 16 28 7
shoe.shiner bc 9 17 3
cook bc 14 22 16
soda.clerk bc 12 30 6
watchman bc 17 25 11
janitor bc 7 20 8
policeman bc 34 47 41
waiter bc 8 32 10
}}}
===
!四大步驟
# [[Reading the Data]]
# [[Examining the Data]]
# [[Regression Analysis]]
# [[Regression Diagnostics]]
!背景(background)
!!Goal
{{{
一種估計統計量之抽樣分配的電腦程式技術。
}}}
!!手邊有什麼?(What did you have at hand?)
{{{
一個大表格(data.frame)。橫向表觀察值(observation)、縱向表變數(variable)。
}}}
!!演算法(algorithm)
# sampling $n$ observations with replacement
# computing your statistic
# repeat steps 1 and 2, $B$ times
# plot your $B$ statistics using hist() and qqnorm() to see the resulting bootstrapping distribution
!R
!!自己寫(write it by yourself)
{{{
coef.Duncan = numeric(100)
for(i in 1:100)
{
Data = Duncan[sample(45, 45, T),]
coef.Duncan[i] = coef(lm(prestige ~ income, data=Data))[2]
}
coef.Duncan
hist(coef.Duncan)
}}}
!!boot
{{{
Duncan.b = function(data, indices)
{
Data = data[indices,]
coef(lm(prestige ~ income, data=Data))[2]
}
library(boot)
coef.Duncan = boot(Duncan, Duncan.b, 99)
plot(coef.Duncan)
}}}
!!bootstrap
!Regression analysis for the computer repair data
{{{
cr = read.table("http://www.ilr.cornell.edu/~hadi/RABE4/Data4/P027.txt", header=T)
cr
plot(cr)
attach(cr)
names(cr)
plot(Units, Minutes)
help(text)
text(Units, Minutes)
plot(Units, Minutes, pch="")
text(Units, Minutes)
plot(Units, Minutes, pch="")
text(4, 20, "吳榮彬")
text(4, 30, "吳榮彬")
plot(Units, Minutes, pch="")
text(4, 70, "吳榮彬")
plot(Units, Minutes, pch="")
text(Units, Minutes)
lm(Minutes ~ Units, data=cr)
abline(lm(Minutes ~ Units, data=cr))
plot(Units, Minutes, pch=".")
abline(lm(Minutes ~ Units, data=cr))
line(c(Units[1], Units[1]), c(Minutes[1], ?))
yhat = fitted(lm(Minutes ~ Units, data=cr))
yhat
line(c(Units[1], Units[1]), c(Minutes[1], yhat[1]))
names(cr)
help(line)
Units[1]
Minutes[1]
yhat[1]
line(c(Units[1], Units[1]), c(Minutes[1], yhat[1]))
c(Units[1], Units[1])
c(Minutes[1], yhat[1])
line(c(Units[1], Units[1]), c(Minutes[1], as.numeric(yhat[1])))
as.numeric(yhat[1])
y = as.numeric(yhat[1])
c(Minutes[1], y)
line(c(Units[1], Units[1]), c(Minutes[1], y))
line(c(1,1), c(23, 30))
line(c(1,23), c(1, 30))
lines(c(1,1), c(23, 30))
lines(c(Units[1], Units[1]), c(Minutes[1], yhat[1]))
plot(Units, Minutes, pch=".")
abline(lm(Minutes ~ Units, data=cr))
lines(c(Units[1], Units[1]), c(Minutes[1], as.numeric(yhat[1])))
lines(c(Units[10], Units[10]), c(Minutes[10], as.numeric(yhat[10])))
}}}
!Regression analysis of the Duncan data set
{{{
library(car)
Duncan
summary(Duncan)
attach(Duncan)
prestige
utils:::menuInstallPkgs()
library(car)
Duncan
summary(Duncan)
attach(Duncan)
prestige
Duncan$prestige
hist(prestige)
2*sqrt(length(prestige))
hist(prestige, nclass = 12)
pairs(cbind(prestige, income, education),
panel = function(x, y){
points(x, y)
abline(lm(y~x), lty = 2)
lines(lowess(x, y))
},
diag.panel = function(x){
par(new = T)
hist(x, main = "", axes = F, nclass = 12)
}
)
duncan.model = lm(prestige ~ income + education)
duncan.model
summary(duncan.model)
library(car)
hist(rstudent(duncan.model), nclass = 12)
qq.plot(duncan.model, labels = row.names(Duncan), simulate = T)
plot(hatvalues(duncan.model))
abline(h = c(2,3)*3/45)
identify(1:45, hatvalue(duncan.model), row.names(Duncan))
av.plot(duncan.model, labels = row.names(Duncan))
cr.plots(duncan.model)
save.image("D:\\My Documents\\test.RData")
lm(prestige ~ income, data=Duncan)
library(car)
dim(Duncan)
sample(45, 45, T)
Duncan[sample(45, 45, T),]
Data = Duncan[sample(45, 45, T),]
lm(prestige ~ income, data=Data)
coef.Duncan[1] = coef(lm(prestige ~ income, data=Data))
coef.Duncan = vector(100)
coef.Duncan = numeric(100)
coef.Duncan
coef.Duncan[1] = coef(lm(prestige ~ income, data=Data))[2]
coef.Duncan
lm(prestige ~ income, data=Data)
Data = Duncan[sample(45, 45, T),]
coef.Duncan[2] = coef(lm(prestige ~ income, data=Data))
coef.Duncan[2] = coef(lm(prestige ~ income, data=Data))[2]
coef.Duncan[1:2]
for(i in 1:100)
{
Data = Duncan[sample(45, 45, T),]
coef.Duncan[i] = coef(lm(prestige ~ income, data=Data))[2]
}
coef.Duncan
hist(coef.Duncan)
utils:::menuInstallPkgs()
coef.Duncan
coef.Duncan.old = coef.Duncan
coef.Duncan.old
Duncan.b = function(data, indices)
{
Data = data[indices,]
coef(lm(prestige ~ income, data=Data))[2]
}
library(boot)
coef.Duncan = boot(Duncan, Duncan.b, 99)
par(mfrow=c(1,2))
hist(coef.Duncan)
plot(coef.Duncan)
coef.Duncan
help(device)
device
device()
dev.new()
hist(coef.Duncan.old)
}}}
!To bootstrap the estimated distribution of the $\hat \beta_1$
{{{
Duncan.b = function(data, indices)
{
Data = data[indices,]
coef(lm(prestige ~ income, data=Data))[2]
}
library(boot)
coef.Duncan = boot(Duncan, Duncan.b, 99)
plot(coef.Duncan)
}}}
[[讀取圖1-1的數據]]、
[[圖1-1的散佈圖]]、
[[相關係數分析估計篇]]
[[用R發現圖1-1的個別簡單線性迴歸分析]]、
[[利用R的輸出繪製圖1-2]]
[[相關係數分析檢定篇]]、
[[相關係數的拔靴分配]]
{{{
library(car)
Duncan
summary(Duncan)
attach(Duncan)
prestige
}}}
{{{
duncan.model = lm(prestige ~ income + education)
duncan.model
summary(duncan.model)
options(show.signif.stars = F)
summary(duncan.model)
}}}
+++[R輸出]
{{{
> duncan.model = lm(prestige ~ income + education)
>
> duncan.model
Call:
lm(formula = prestige ~ income + education)
Coefficients:
(Intercept) income education
-6.0647 0.5987 0.5458
>
> summary(duncan.model)
Call:
lm(formula = prestige ~ income + education)
Residuals:
Min 1Q Median 3Q Max
-29.5380 -6.4174 0.6546 6.6051 34.6412
Coefficients:
Estimate Std. Error t value Pr(>|t|)
(Intercept) -6.06466 4.27194 -1.420 0.163
income 0.59873 0.11967 5.003 1.05e-05 ***
education 0.54583 0.09825 5.555 1.73e-06 ***
---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
Residual standard error: 13.37 on 42 degrees of freedom
Multiple R-squared: 0.8282, Adjusted R-squared: 0.82
F-statistic: 101.2 on 2 and 42 DF, p-value: < 2.2e-16
>
> options(show.signif.stars = F)
> summary(duncan.model)
Call:
lm(formula = prestige ~ income + education)
Residuals:
Min 1Q Median 3Q Max
-29.5380 -6.4174 0.6546 6.6051 34.6412
Coefficients:
Estimate Std. Error t value Pr(>|t|)
(Intercept) -6.06466 4.27194 -1.420 0.163
income 0.59873 0.11967 5.003 1.05e-05
education 0.54583 0.09825 5.555 1.73e-06
Residual standard error: 13.37 on 42 degrees of freedom
Multiple R-squared: 0.8282, Adjusted R-squared: 0.82
F-statistic: 101.2 on 2 and 42 DF, p-value: < 2.2e-16
}}}
===
{{{
library(car)
hist(rstudent(duncan.model), nclass = 12)
qq.plot(duncan.model, labels = row.names(Duncan), simulate = T)
plot(hatvalues(duncan.model))
abline(h = c(2,3)*3/45)
identify(1:45, hatvalue(duncan.model), row.names(Duncan))
av.plot(duncan.model, labels = row.names(Duncan))
cr.plots(duncan.model)
spread.level.plot(duncan.model)
ncv.test(duncan.model)
ncv.test(duncan.model, var.formula = ~ income + education)
remove = which.names(c('minister', 'conductor'), Duncan)
remove
duncan.model2 = update(duncan.model, subset = -remove)
summary(duncan.model2)
}}}
{{{
require(graphics)
## Analysis of the life-cycle savings data
## given in Belsley, Kuh and Welsch.
lm.SR <- lm(sr ~ pop15 + pop75 + dpi + ddpi, data = LifeCycleSavings)
inflm.SR <- influence.measures(lm.SR)
which(apply(inflm.SR$is.inf, 1, any))
# which observations 'are' influential
summary(inflm.SR) # only these
inflm.SR # all
plot(rstudent(lm.SR) ~ hatvalues(lm.SR)) # recommended by some
## The 'infl' argument is not needed, but avoids recomputation:
rs <- rstandard(lm.SR)
iflSR <- influence(lm.SR)
identical(rs, rstandard(lm.SR, infl = iflSR))
## to "see" the larger values:
1000 * round(dfbetas(lm.SR, infl = iflSR), 3)
## Huber's data [Atkinson 1985]
xh <- c(-4:0, 10)
yh <- c(2.48, .73, -.04, -1.44, -1.32, 0)
summary(lmH <- lm(yh ~ xh))
(im <- influence.measures(lmH))
plot(xh,yh, main = "Huber's data: L.S. line and influential obs.")
abline(lmH); points(xh[im$is.inf], yh[im$is.inf], pch=20, col=2)
## Irwin's data [Williams 1987]
xi <- 1:5
yi <- c(0,2,14,19,30) # number of mice responding to does xi
mi <- rep(40, 5) # number of mice exposed
summary(lmI <- glm(cbind(yi, mi -yi) ~ xi, family = binomial))
signif(cooks.distance(lmI), 3)# ~= Ci in Table 3, p.184
(imI <- influence.measures(lmI))
stopifnot(all.equal(imI$infmat[,"cook.d"],
cooks.distance(lmI)))
}}}
{{{
influence.measures(model)
rstandard(model, ...)
rstudent(model, ...)
dffits(model, infl = , res = )
dfbeta(model, ...)
dfbetas(model, ...)
covratio(model, infl = lm.influence(model, do.coef = FALSE),
res = weighted.residuals(model))
cooks.distance(model, ...)
hatvalues(model, ...)
hat(x, intercept = TRUE)
}}}
<html><div align="center"><iframe src ="http://www.r-project.org/doc/bib/R-books.html" width="100%" align="center" height=400"></iframe></div></html>
<html><div align="center"><iframe src ="http://www.r-project.org/" width="100%" align="center" height=400"></iframe></div></html>
建模是王道, (<<version>>), <<fontSize "">>
<html><div align="center"><iframe src ="http://www.tiddlywiki.com/" width="100%" align="center" height=400"></iframe></div></html>
/***
Description: Contains the stuff you need to use Tiddlyspot
Note, you also need UploadPlugin, PasswordOptionPlugin and LoadRemoteFileThroughProxy
from http://tiddlywiki.bidix.info for a complete working Tiddlyspot site.
***/
//{{{
// edit this if you are migrating sites or retrofitting an existing TW
config.tiddlyspotSiteId = 'appliedregression';
// make it so you can by default see edit controls via http
config.options.chkHttpReadOnly = false;
window.readOnly = false; // make sure of it (for tw 2.2)
window.showBackstage = true; // show backstage too
// disable autosave in d3
if (window.location.protocol != "file:")
config.options.chkGTDLazyAutoSave = false;
// tweak shadow tiddlers to add upload button, password entry box etc
with (config.shadowTiddlers) {
SiteUrl = 'http://'+config.tiddlyspotSiteId+'.tiddlyspot.com';
SideBarOptions = SideBarOptions.replace(/(<<saveChanges>>)/,"$1<<tiddler TspotSidebar>>");
OptionsPanel = OptionsPanel.replace(/^/,"<<tiddler TspotOptions>>");
DefaultTiddlers = DefaultTiddlers.replace(/^/,"[[WelcomeToTiddlyspot]] ");
MainMenu = MainMenu.replace(/^/,"[[WelcomeToTiddlyspot]] ");
}
// create some shadow tiddler content
merge(config.shadowTiddlers,{
'WelcomeToTiddlyspot':[
"This document is a ~TiddlyWiki from tiddlyspot.com. A ~TiddlyWiki is an electronic notebook that is great for managing todo lists, personal information, and all sorts of things.",
"",
"@@font-weight:bold;font-size:1.3em;color:#444; //What now?// @@ Before you can save any changes, you need to enter your password in the form below. Then configure privacy and other site settings at your [[control panel|http://" + config.tiddlyspotSiteId + ".tiddlyspot.com/controlpanel]] (your control panel username is //" + config.tiddlyspotSiteId + "//).",
"<<tiddler TspotControls>>",
"See also GettingStarted.",
"",
"@@font-weight:bold;font-size:1.3em;color:#444; //Working online// @@ You can edit this ~TiddlyWiki right now, and save your changes using the \"save to web\" button in the column on the right.",
"",
"@@font-weight:bold;font-size:1.3em;color:#444; //Working offline// @@ A fully functioning copy of this ~TiddlyWiki can be saved onto your hard drive or USB stick. You can make changes and save them locally without being connected to the Internet. When you're ready to sync up again, just click \"upload\" and your ~TiddlyWiki will be saved back to tiddlyspot.com.",
"",
"@@font-weight:bold;font-size:1.3em;color:#444; //Help!// @@ Find out more about ~TiddlyWiki at [[TiddlyWiki.com|http://tiddlywiki.com]]. Also visit [[TiddlyWiki.org|http://tiddlywiki.org]] for documentation on learning and using ~TiddlyWiki. New users are especially welcome on the [[TiddlyWiki mailing list|http://groups.google.com/group/TiddlyWiki]], which is an excellent place to ask questions and get help. If you have a tiddlyspot related problem email [[tiddlyspot support|mailto:support@tiddlyspot.com]].",
"",
"@@font-weight:bold;font-size:1.3em;color:#444; //Enjoy :)// @@ We hope you like using your tiddlyspot.com site. Please email [[feedback@tiddlyspot.com|mailto:feedback@tiddlyspot.com]] with any comments or suggestions."
].join("\n"),
'TspotControls':[
"| tiddlyspot password:|<<option pasUploadPassword>>|",
"| site management:|<<upload http://" + config.tiddlyspotSiteId + ".tiddlyspot.com/store.cgi index.html . . " + config.tiddlyspotSiteId + ">>//(requires tiddlyspot password)//<br>[[control panel|http://" + config.tiddlyspotSiteId + ".tiddlyspot.com/controlpanel]], [[download (go offline)|http://" + config.tiddlyspotSiteId + ".tiddlyspot.com/download]]|",
"| links:|[[tiddlyspot.com|http://tiddlyspot.com/]], [[FAQs|http://faq.tiddlyspot.com/]], [[blog|http://tiddlyspot.blogspot.com/]], email [[support|mailto:support@tiddlyspot.com]] & [[feedback|mailto:feedback@tiddlyspot.com]], [[donate|http://tiddlyspot.com/?page=donate]]|"
].join("\n"),
'TspotSidebar':[
"<<upload http://" + config.tiddlyspotSiteId + ".tiddlyspot.com/store.cgi index.html . . " + config.tiddlyspotSiteId + ">><html><a href='http://" + config.tiddlyspotSiteId + ".tiddlyspot.com/download' class='button'>download</a></html>"
].join("\n"),
'TspotOptions':[
"tiddlyspot password:",
"<<option pasUploadPassword>>",
""
].join("\n")
});
//}}}
| !date | !user | !location | !storeUrl | !uploadDir | !toFilename | !backupdir | !origin |
| 02/12/2009 09:51:06 | 吳榮彬 | [[appliedregression.html|file:///Users/wujungpin/MyNotes/%E6%88%91%E7%9A%84%E8%AA%B2%E7%A8%8B/regression/wiki/appliedregression.html]] | [[store.cgi|http://appliedregression.tiddlyspot.com/store.cgi]] | . | [[index.html | http://appliedregression.tiddlyspot.com/index.html]] | . | failed |
| 02/12/2009 09:51:32 | 吳榮彬 | [[appliedregression.html|file:///Users/wujungpin/MyNotes/%E6%88%91%E7%9A%84%E8%AA%B2%E7%A8%8B/regression/wiki/appliedregression.html]] | [[store.cgi|http://appliedregression.tiddlyspot.com/store.cgi]] | . | [[index.html | http://appliedregression.tiddlyspot.com/index.html]] | . | ok |
| 02/12/2009 14:39:23 | 吳榮彬 | [[appliedregression.html|file:///Users/wujungpin/MyNotes/%E6%88%91%E7%9A%84%E8%AA%B2%E7%A8%8B/regression/wiki/appliedregression.html]] | [[store.cgi|http://appliedregression.tiddlyspot.com/store.cgi]] | . | [[index.html | http://appliedregression.tiddlyspot.com/index.html]] | . | ok |
| 03/12/2009 16:21:37 | 吳榮彬 | [[appliedregression.html|file:///Users/wujungpin/MyNotes/%E6%88%91%E7%9A%84%E8%AA%B2%E7%A8%8B/regression/wiki/appliedregression.html]] | [[store.cgi|http://appliedregression.tiddlyspot.com/store.cgi]] | . | [[index.html | http://appliedregression.tiddlyspot.com/index.html]] | . | ok |
| 03/12/2009 17:21:30 | 吳榮彬 | [[appliedregression.html|file:///Users/wujungpin/MyNotes/%E6%88%91%E7%9A%84%E8%AA%B2%E7%A8%8B/regression/wiki/appliedregression.html]] | [[store.cgi|http://appliedregression.tiddlyspot.com/store.cgi]] | . | [[index.html | http://appliedregression.tiddlyspot.com/index.html]] | . | ok |
| 16/12/2009 10:49:59 | 吳榮彬 | [[appliedregression.html|file:///Users/wujungpin/MyNotes/%E6%88%91%E7%9A%84%E8%AA%B2%E7%A8%8B/regression/wiki/appliedregression.html]] | [[store.cgi|http://appliedregression.tiddlyspot.com/store.cgi]] | . | [[index.html | http://appliedregression.tiddlyspot.com/index.html]] | . |
| 16/12/2009 10:50:17 | 吳榮彬 | [[appliedregression.html|file:///Users/wujungpin/MyNotes/%E6%88%91%E7%9A%84%E8%AA%B2%E7%A8%8B/regression/wiki/appliedregression.html]] | [[store.cgi|http://appliedregression.tiddlyspot.com/store.cgi]] | . | [[index.html | http://appliedregression.tiddlyspot.com/index.html]] | . | failed | failed |
| 23/12/2009 19:52:38 | 吳榮彬 | [[appliedregression.html|file:///Users/wujungpin/MyNotes/%E6%88%91%E7%9A%84%E8%AA%B2%E7%A8%8B/regression/wiki/appliedregression.html]] | [[store.cgi|http://appliedregression.tiddlyspot.com/store.cgi]] | . | [[index.html | http://appliedregression.tiddlyspot.com/index.html]] | . | failed |
| 23/12/2009 19:54:19 | 吳榮彬 | [[appliedregression.html|file:///Users/wujungpin/MyNotes/%E6%88%91%E7%9A%84%E8%AA%B2%E7%A8%8B/regression/wiki/appliedregression.html]] | [[store.cgi|http://appliedregression.tiddlyspot.com/store.cgi]] | . | [[index.html | http://appliedregression.tiddlyspot.com/index.html]] | . | ok |
| 23/12/2009 22:23:45 | 吳榮彬 | [[appliedregression.html|file:///Users/wujungpin/MyNotes/%E6%88%91%E7%9A%84%E8%AA%B2%E7%A8%8B/regression/wiki/appliedregression.html]] | [[store.cgi|http://appliedregression.tiddlyspot.com/store.cgi]] | . | [[index.html | http://appliedregression.tiddlyspot.com/index.html]] | . |
/***
|''Name:''|UploadPlugin|
|''Description:''|Save to web a TiddlyWiki|
|''Version:''|4.1.3|
|''Date:''|Feb 24, 2008|
|''Source:''|http://tiddlywiki.bidix.info/#UploadPlugin|
|''Documentation:''|http://tiddlywiki.bidix.info/#UploadPluginDoc|
|''Author:''|BidiX (BidiX (at) bidix (dot) info)|
|''License:''|[[BSD open source license|http://tiddlywiki.bidix.info/#%5B%5BBSD%20open%20source%20license%5D%5D ]]|
|''~CoreVersion:''|2.2.0|
|''Requires:''|PasswordOptionPlugin|
***/
//{{{
version.extensions.UploadPlugin = {
major: 4, minor: 1, revision: 3,
date: new Date("Feb 24, 2008"),
source: 'http://tiddlywiki.bidix.info/#UploadPlugin',
author: 'BidiX (BidiX (at) bidix (dot) info',
coreVersion: '2.2.0'
};
//
// Environment
//
if (!window.bidix) window.bidix = {}; // bidix namespace
bidix.debugMode = false; // true to activate both in Plugin and UploadService
//
// Upload Macro
//
config.macros.upload = {
// default values
defaultBackupDir: '', //no backup
defaultStoreScript: "store.php",
defaultToFilename: "index.html",
defaultUploadDir: ".",
authenticateUser: true // UploadService Authenticate User
};
config.macros.upload.label = {
promptOption: "Save and Upload this TiddlyWiki with UploadOptions",
promptParamMacro: "Save and Upload this TiddlyWiki in %0",
saveLabel: "save to web",
saveToDisk: "save to disk",
uploadLabel: "upload"
};
config.macros.upload.messages = {
noStoreUrl: "No store URL in parmeters or options",
usernameOrPasswordMissing: "Username or password missing"
};
config.macros.upload.handler = function(place,macroName,params) {
if (readOnly)
return;
var label;
if (document.location.toString().substr(0,4) == "http")
label = this.label.saveLabel;
else
label = this.label.uploadLabel;
var prompt;
if (params[0]) {
prompt = this.label.promptParamMacro.toString().format([this.destFile(params[0],
(params[1] ? params[1]:bidix.basename(window.location.toString())), params[3])]);
} else {
prompt = this.label.promptOption;
}
createTiddlyButton(place, label, prompt, function() {config.macros.upload.action(params);}, null, null, this.accessKey);
};
config.macros.upload.action = function(params)
{
// for missing macro parameter set value from options
if (!params) params = {};
var storeUrl = params[0] ? params[0] : config.options.txtUploadStoreUrl;
var toFilename = params[1] ? params[1] : config.options.txtUploadFilename;
var backupDir = params[2] ? params[2] : config.options.txtUploadBackupDir;
var uploadDir = params[3] ? params[3] : config.options.txtUploadDir;
var username = params[4] ? params[4] : config.options.txtUploadUserName;
var password = config.options.pasUploadPassword; // for security reason no password as macro parameter
// for still missing parameter set default value
if ((!storeUrl) && (document.location.toString().substr(0,4) == "http"))
storeUrl = bidix.dirname(document.location.toString())+'/'+config.macros.upload.defaultStoreScript;
if (storeUrl.substr(0,4) != "http")
storeUrl = bidix.dirname(document.location.toString()) +'/'+ storeUrl;
if (!toFilename)
toFilename = bidix.basename(window.location.toString());
if (!toFilename)
toFilename = config.macros.upload.defaultToFilename;
if (!uploadDir)
uploadDir = config.macros.upload.defaultUploadDir;
if (!backupDir)
backupDir = config.macros.upload.defaultBackupDir;
// report error if still missing
if (!storeUrl) {
alert(config.macros.upload.messages.noStoreUrl);
clearMessage();
return false;
}
if (config.macros.upload.authenticateUser && (!username || !password)) {
alert(config.macros.upload.messages.usernameOrPasswordMissing);
clearMessage();
return false;
}
bidix.upload.uploadChanges(false,null,storeUrl, toFilename, uploadDir, backupDir, username, password);
return false;
};
config.macros.upload.destFile = function(storeUrl, toFilename, uploadDir)
{
if (!storeUrl)
return null;
var dest = bidix.dirname(storeUrl);
if (uploadDir && uploadDir != '.')
dest = dest + '/' + uploadDir;
dest = dest + '/' + toFilename;
return dest;
};
//
// uploadOptions Macro
//
config.macros.uploadOptions = {
handler: function(place,macroName,params) {
var wizard = new Wizard();
wizard.createWizard(place,this.wizardTitle);
wizard.addStep(this.step1Title,this.step1Html);
var markList = wizard.getElement("markList");
var listWrapper = document.createElement("div");
markList.parentNode.insertBefore(listWrapper,markList);
wizard.setValue("listWrapper",listWrapper);
this.refreshOptions(listWrapper,false);
var uploadCaption;
if (document.location.toString().substr(0,4) == "http")
uploadCaption = config.macros.upload.label.saveLabel;
else
uploadCaption = config.macros.upload.label.uploadLabel;
wizard.setButtons([
{caption: uploadCaption, tooltip: config.macros.upload.label.promptOption,
onClick: config.macros.upload.action},
{caption: this.cancelButton, tooltip: this.cancelButtonPrompt, onClick: this.onCancel}
]);
},
options: [
"txtUploadUserName",
"pasUploadPassword",
"txtUploadStoreUrl",
"txtUploadDir",
"txtUploadFilename",
"txtUploadBackupDir",
"chkUploadLog",
"txtUploadLogMaxLine"
],
refreshOptions: function(listWrapper) {
var opts = [];
for(i=0; i<this.options.length; i++) {
var opt = {};
opts.push();
opt.option = "";
n = this.options[i];
opt.name = n;
opt.lowlight = !config.optionsDesc[n];
opt.description = opt.lowlight ? this.unknownDescription : config.optionsDesc[n];
opts.push(opt);
}
var listview = ListView.create(listWrapper,opts,this.listViewTemplate);
for(n=0; n<opts.length; n++) {
var type = opts[n].name.substr(0,3);
var h = config.macros.option.types[type];
if (h && h.create) {
h.create(opts[n].colElements['option'],type,opts[n].name,opts[n].name,"no");
}
}
},
onCancel: function(e)
{
backstage.switchTab(null);
return false;
},
wizardTitle: "Upload with options",
step1Title: "These options are saved in cookies in your browser",
step1Html: "<input type='hidden' name='markList'></input><br>",
cancelButton: "Cancel",
cancelButtonPrompt: "Cancel prompt",
listViewTemplate: {
columns: [
{name: 'Description', field: 'description', title: "Description", type: 'WikiText'},
{name: 'Option', field: 'option', title: "Option", type: 'String'},
{name: 'Name', field: 'name', title: "Name", type: 'String'}
],
rowClasses: [
{className: 'lowlight', field: 'lowlight'}
]}
};
//
// upload functions
//
if (!bidix.upload) bidix.upload = {};
if (!bidix.upload.messages) bidix.upload.messages = {
//from saving
invalidFileError: "The original file '%0' does not appear to be a valid TiddlyWiki",
backupSaved: "Backup saved",
backupFailed: "Failed to upload backup file",
rssSaved: "RSS feed uploaded",
rssFailed: "Failed to upload RSS feed file",
emptySaved: "Empty template uploaded",
emptyFailed: "Failed to upload empty template file",
mainSaved: "Main TiddlyWiki file uploaded",
mainFailed: "Failed to upload main TiddlyWiki file. Your changes have not been saved",
//specific upload
loadOriginalHttpPostError: "Can't get original file",
aboutToSaveOnHttpPost: 'About to upload on %0 ...',
storePhpNotFound: "The store script '%0' was not found."
};
bidix.upload.uploadChanges = function(onlyIfDirty,tiddlers,storeUrl,toFilename,uploadDir,backupDir,username,password)
{
var callback = function(status,uploadParams,original,url,xhr) {
if (!status) {
displayMessage(bidix.upload.messages.loadOriginalHttpPostError);
return;
}
if (bidix.debugMode)
alert(original.substr(0,500)+"\n...");
// Locate the storeArea div's
var posDiv = locateStoreArea(original);
if((posDiv[0] == -1) || (posDiv[1] == -1)) {
alert(config.messages.invalidFileError.format([localPath]));
return;
}
bidix.upload.uploadRss(uploadParams,original,posDiv);
};
if(onlyIfDirty && !store.isDirty())
return;
clearMessage();
// save on localdisk ?
if (document.location.toString().substr(0,4) == "file") {
var path = document.location.toString();
var localPath = getLocalPath(path);
saveChanges();
}
// get original
var uploadParams = new Array(storeUrl,toFilename,uploadDir,backupDir,username,password);
var originalPath = document.location.toString();
// If url is a directory : add index.html
if (originalPath.charAt(originalPath.length-1) == "/")
originalPath = originalPath + "index.html";
var dest = config.macros.upload.destFile(storeUrl,toFilename,uploadDir);
var log = new bidix.UploadLog();
log.startUpload(storeUrl, dest, uploadDir, backupDir);
displayMessage(bidix.upload.messages.aboutToSaveOnHttpPost.format([dest]));
if (bidix.debugMode)
alert("about to execute Http - GET on "+originalPath);
var r = doHttp("GET",originalPath,null,null,username,password,callback,uploadParams,null);
if (typeof r == "string")
displayMessage(r);
return r;
};
bidix.upload.uploadRss = function(uploadParams,original,posDiv)
{
var callback = function(status,params,responseText,url,xhr) {
if(status) {
var destfile = responseText.substring(responseText.indexOf("destfile:")+9,responseText.indexOf("\n", responseText.indexOf("destfile:")));
displayMessage(bidix.upload.messages.rssSaved,bidix.dirname(url)+'/'+destfile);
bidix.upload.uploadMain(params[0],params[1],params[2]);
} else {
displayMessage(bidix.upload.messages.rssFailed);
}
};
// do uploadRss
if(config.options.chkGenerateAnRssFeed) {
var rssPath = uploadParams[1].substr(0,uploadParams[1].lastIndexOf(".")) + ".xml";
var rssUploadParams = new Array(uploadParams[0],rssPath,uploadParams[2],'',uploadParams[4],uploadParams[5]);
var rssString = generateRss();
// no UnicodeToUTF8 conversion needed when location is "file" !!!
if (document.location.toString().substr(0,4) != "file")
rssString = convertUnicodeToUTF8(rssString);
bidix.upload.httpUpload(rssUploadParams,rssString,callback,Array(uploadParams,original,posDiv));
} else {
bidix.upload.uploadMain(uploadParams,original,posDiv);
}
};
bidix.upload.uploadMain = function(uploadParams,original,posDiv)
{
var callback = function(status,params,responseText,url,xhr) {
var log = new bidix.UploadLog();
if(status) {
// if backupDir specified
if ((params[3]) && (responseText.indexOf("backupfile:") > -1)) {
var backupfile = responseText.substring(responseText.indexOf("backupfile:")+11,responseText.indexOf("\n", responseText.indexOf("backupfile:")));
displayMessage(bidix.upload.messages.backupSaved,bidix.dirname(url)+'/'+backupfile);
}
var destfile = responseText.substring(responseText.indexOf("destfile:")+9,responseText.indexOf("\n", responseText.indexOf("destfile:")));
displayMessage(bidix.upload.messages.mainSaved,bidix.dirname(url)+'/'+destfile);
store.setDirty(false);
log.endUpload("ok");
} else {
alert(bidix.upload.messages.mainFailed);
displayMessage(bidix.upload.messages.mainFailed);
log.endUpload("failed");
}
};
// do uploadMain
var revised = bidix.upload.updateOriginal(original,posDiv);
bidix.upload.httpUpload(uploadParams,revised,callback,uploadParams);
};
bidix.upload.httpUpload = function(uploadParams,data,callback,params)
{
var localCallback = function(status,params,responseText,url,xhr) {
url = (url.indexOf("nocache=") < 0 ? url : url.substring(0,url.indexOf("nocache=")-1));
if (xhr.status == 404)
alert(bidix.upload.messages.storePhpNotFound.format([url]));
if ((bidix.debugMode) || (responseText.indexOf("Debug mode") >= 0 )) {
alert(responseText);
if (responseText.indexOf("Debug mode") >= 0 )
responseText = responseText.substring(responseText.indexOf("\n\n")+2);
} else if (responseText.charAt(0) != '0')
alert(responseText);
if (responseText.charAt(0) != '0')
status = null;
callback(status,params,responseText,url,xhr);
};
// do httpUpload
var boundary = "---------------------------"+"AaB03x";
var uploadFormName = "UploadPlugin";
// compose headers data
var sheader = "";
sheader += "--" + boundary + "\r\nContent-disposition: form-data; name=\"";
sheader += uploadFormName +"\"\r\n\r\n";
sheader += "backupDir="+uploadParams[3] +
";user=" + uploadParams[4] +
";password=" + uploadParams[5] +
";uploaddir=" + uploadParams[2];
if (bidix.debugMode)
sheader += ";debug=1";
sheader += ";;\r\n";
sheader += "\r\n" + "--" + boundary + "\r\n";
sheader += "Content-disposition: form-data; name=\"userfile\"; filename=\""+uploadParams[1]+"\"\r\n";
sheader += "Content-Type: text/html;charset=UTF-8" + "\r\n";
sheader += "Content-Length: " + data.length + "\r\n\r\n";
// compose trailer data
var strailer = new String();
strailer = "\r\n--" + boundary + "--\r\n";
data = sheader + data + strailer;
if (bidix.debugMode) alert("about to execute Http - POST on "+uploadParams[0]+"\n with \n"+data.substr(0,500)+ " ... ");
var r = doHttp("POST",uploadParams[0],data,"multipart/form-data; ;charset=UTF-8; boundary="+boundary,uploadParams[4],uploadParams[5],localCallback,params,null);
if (typeof r == "string")
displayMessage(r);
return r;
};
// same as Saving's updateOriginal but without convertUnicodeToUTF8 calls
bidix.upload.updateOriginal = function(original, posDiv)
{
if (!posDiv)
posDiv = locateStoreArea(original);
if((posDiv[0] == -1) || (posDiv[1] == -1)) {
alert(config.messages.invalidFileError.format([localPath]));
return;
}
var revised = original.substr(0,posDiv[0] + startSaveArea.length) + "\n" +
store.allTiddlersAsHtml() + "\n" +
original.substr(posDiv[1]);
var newSiteTitle = getPageTitle().htmlEncode();
revised = revised.replaceChunk("<title"+">","</title"+">"," " + newSiteTitle + " ");
revised = updateMarkupBlock(revised,"PRE-HEAD","MarkupPreHead");
revised = updateMarkupBlock(revised,"POST-HEAD","MarkupPostHead");
revised = updateMarkupBlock(revised,"PRE-BODY","MarkupPreBody");
revised = updateMarkupBlock(revised,"POST-SCRIPT","MarkupPostBody");
return revised;
};
//
// UploadLog
//
// config.options.chkUploadLog :
// false : no logging
// true : logging
// config.options.txtUploadLogMaxLine :
// -1 : no limit
// 0 : no Log lines but UploadLog is still in place
// n : the last n lines are only kept
// NaN : no limit (-1)
bidix.UploadLog = function() {
if (!config.options.chkUploadLog)
return; // this.tiddler = null
this.tiddler = store.getTiddler("UploadLog");
if (!this.tiddler) {
this.tiddler = new Tiddler();
this.tiddler.title = "UploadLog";
this.tiddler.text = "| !date | !user | !location | !storeUrl | !uploadDir | !toFilename | !backupdir | !origin |";
this.tiddler.created = new Date();
this.tiddler.modifier = config.options.txtUserName;
this.tiddler.modified = new Date();
store.addTiddler(this.tiddler);
}
return this;
};
bidix.UploadLog.prototype.addText = function(text) {
if (!this.tiddler)
return;
// retrieve maxLine when we need it
var maxLine = parseInt(config.options.txtUploadLogMaxLine,10);
if (isNaN(maxLine))
maxLine = -1;
// add text
if (maxLine != 0)
this.tiddler.text = this.tiddler.text + text;
// Trunck to maxLine
if (maxLine >= 0) {
var textArray = this.tiddler.text.split('\n');
if (textArray.length > maxLine + 1)
textArray.splice(1,textArray.length-1-maxLine);
this.tiddler.text = textArray.join('\n');
}
// update tiddler fields
this.tiddler.modifier = config.options.txtUserName;
this.tiddler.modified = new Date();
store.addTiddler(this.tiddler);
// refresh and notifiy for immediate update
story.refreshTiddler(this.tiddler.title);
store.notify(this.tiddler.title, true);
};
bidix.UploadLog.prototype.startUpload = function(storeUrl, toFilename, uploadDir, backupDir) {
if (!this.tiddler)
return;
var now = new Date();
var text = "\n| ";
var filename = bidix.basename(document.location.toString());
if (!filename) filename = '/';
text += now.formatString("0DD/0MM/YYYY 0hh:0mm:0ss") +" | ";
text += config.options.txtUserName + " | ";
text += "[["+filename+"|"+location + "]] |";
text += " [[" + bidix.basename(storeUrl) + "|" + storeUrl + "]] | ";
text += uploadDir + " | ";
text += "[[" + bidix.basename(toFilename) + " | " +toFilename + "]] | ";
text += backupDir + " |";
this.addText(text);
};
bidix.UploadLog.prototype.endUpload = function(status) {
if (!this.tiddler)
return;
this.addText(" "+status+" |");
};
//
// Utilities
//
bidix.checkPlugin = function(plugin, major, minor, revision) {
var ext = version.extensions[plugin];
if (!
(ext &&
((ext.major > major) ||
((ext.major == major) && (ext.minor > minor)) ||
((ext.major == major) && (ext.minor == minor) && (ext.revision >= revision))))) {
// write error in PluginManager
if (pluginInfo)
pluginInfo.log.push("Requires " + plugin + " " + major + "." + minor + "." + revision);
eval(plugin); // generate an error : "Error: ReferenceError: xxxx is not defined"
}
};
bidix.dirname = function(filePath) {
if (!filePath)
return;
var lastpos;
if ((lastpos = filePath.lastIndexOf("/")) != -1) {
return filePath.substring(0, lastpos);
} else {
return filePath.substring(0, filePath.lastIndexOf("\\"));
}
};
bidix.basename = function(filePath) {
if (!filePath)
return;
var lastpos;
if ((lastpos = filePath.lastIndexOf("#")) != -1)
filePath = filePath.substring(0, lastpos);
if ((lastpos = filePath.lastIndexOf("/")) != -1) {
return filePath.substring(lastpos + 1);
} else
return filePath.substring(filePath.lastIndexOf("\\")+1);
};
bidix.initOption = function(name,value) {
if (!config.options[name])
config.options[name] = value;
};
//
// Initializations
//
// require PasswordOptionPlugin 1.0.1 or better
bidix.checkPlugin("PasswordOptionPlugin", 1, 0, 1);
// styleSheet
setStylesheet('.txtUploadStoreUrl, .txtUploadBackupDir, .txtUploadDir {width: 22em;}',"uploadPluginStyles");
//optionsDesc
merge(config.optionsDesc,{
txtUploadStoreUrl: "Url of the UploadService script (default: store.php)",
txtUploadFilename: "Filename of the uploaded file (default: in index.html)",
txtUploadDir: "Relative Directory where to store the file (default: . (downloadService directory))",
txtUploadBackupDir: "Relative Directory where to backup the file. If empty no backup. (default: ''(empty))",
txtUploadUserName: "Upload Username",
pasUploadPassword: "Upload Password",
chkUploadLog: "do Logging in UploadLog (default: true)",
txtUploadLogMaxLine: "Maximum of lines in UploadLog (default: 10)"
});
// Options Initializations
bidix.initOption('txtUploadStoreUrl','');
bidix.initOption('txtUploadFilename','');
bidix.initOption('txtUploadDir','');
bidix.initOption('txtUploadBackupDir','');
bidix.initOption('txtUploadUserName','');
bidix.initOption('pasUploadPassword','');
bidix.initOption('chkUploadLog',true);
bidix.initOption('txtUploadLogMaxLine','10');
// Backstage
merge(config.tasks,{
uploadOptions: {text: "upload", tooltip: "Change UploadOptions and Upload", content: '<<uploadOptions>>'}
});
config.backstageTasks.push("uploadOptions");
//}}}
[[官網|http://www.math.csi.cuny.edu/UsingR]]
<html><div align="center"><iframe src ="http://www.math.union.edu/~dpvc/jsmath/" width="100%" align="center" height=400"></iframe></div></html>
{{{
kappa(x1 <- cbind(1,1:10))# 15.71
kappa(x1, exact = TRUE) # 13.68
kappa(x2 <- cbind(x1,2:11))# high! [x2 is singular!]
hilbert <- function(n) { i <- 1:n; 1 / outer(i - 1, i, "+") }
sv9 <- svd(h9 <- hilbert(9))$ d
kappa(h9)# pretty high!
kappa(h9, exact = TRUE) == max(sv9) / min(sv9)
kappa(h9, exact = TRUE) / kappa(h9) # .677 (i.e., rel.error = 32%)
}}}
{{{
library(car)
help(linear.hypothesis)
}}}
++++[細節]
!來源
{{{
linear.hypothesis {car}
}}}
!目標
{{{
Test Linear Hypothesis
}}}
!功能描述
Generic function for testing a linear hypothesis, and methods for linear models, generalized linear models, multivariate linear models, and other models that have methods for coef and vcov.
!語法
{{{
linear.hypothesis(model, ...)
lht(model, ...)
## Default S3 method:
linear.hypothesis(model, hypothesis.matrix, rhs=NULL,
test=c("Chisq", "F"), vcov.=NULL, verbose=FALSE, ...)
## S3 method for class 'lm':
linear.hypothesis(model, hypothesis.matrix, rhs=NULL,
test=c("F", "Chisq"), vcov.=NULL, white.adjust=FALSE, ...)
## S3 method for class 'glm':
linear.hypothesis(model, ...)
## S3 method for class 'mlm':
linear.hypothesis(model, hypothesis.matrix, rhs=NULL, SSPE, V,
test, idata, icontrasts=c("contr.sum", "contr.poly"), idesign, iterms,
P=NULL, title="", verbose=FALSE, ...)
## S3 method for class 'linear.hypothesis.mlm':
print(x, SSP=TRUE, SSPE=SSP,
digits=unlist(options("digits")), ...)
}}}
!輸入
| model |fitted model object. The default method works for models for which the estimated parameters can be retrieved by coef and the corresponding estimated covariance matrix by vcov. See the Details for more information. |
| hypothesis.matrix |matrix (or vector) giving linear combinations of coefficients by rows, or a character vector giving the hypothesis in symbolic form (see Details). |
| rhs |right-hand-side vector for hypothesis, with as many entries as rows in the hypothesis matrix; can be omitted, in which case it defaults to a vector of zeroes. For a multivariate linear model, rhs is a matrix, defaulting to 0. |
| idata |an optional data frame giving a factor or factors defining the intra-subject model for multivariate repeated-measures data. See Details for an explanation of the intra-subject design and for further explanation of the other arguments relating to intra-subject factors. |
| icontrasts |names of contrast-generating functions to be applied by default to factors and ordered factors, respectively, in the within-subject ``data''; the contrasts must produce an intra-subject model matrix in which different terms are orthogonal. |
| idesign |a one-sided model formula using the ``data'' in idata and specifying the intra-subject design. |
| iterms |the quoted name of a term, or a vector of quoted names of terms, in the intra-subject design to be tested. |
| P |transformation matrix to be applied to the repeated measures in multivariate repeated-measures data; if NULL and no intra-subject model is specified, no response-transformation is applied; if an intra-subject model is specified via the idata, idesign, and (optionally) icontrasts arguments, then P is generated automatically from the iterms argument. |
| SSPE |in linear.hypothesis method for mlm objects: optional error sum-of-squares-and-products matrix; if missing, it is computed from the model. In print method for linear.hypothesis.mlm objects: if TRUE, print the sum-of-squares and cross-products matrix for error. |
| test |character string, "F" or "Chisq", specifying whether to compute the finite-sample F statistic (with approximate F distribution) or the large-sample Chi-squared statistic (with asymptotic Chi-squared distribution). For a multivariate linear model, the multivariate test statistic to report — one of "Pillai", "Wilks", "Hotelling-Lawley", or "Roy", with "Pillai" as the default. |
| title |an optional character string to label the output. |
| V |inverse of sum of squares and products of the model matrix; if missing it is computed from the model. |
| vcov |a function for estimating the covariance matrix of the regression coefficients, e.g., hccm, or an estimated covariance matrix for model. See also white.adjust. |
| white.adjust |logical or character. Convenience interface to hccm (instead of using the argument vcov). Can be set either to a character specifying the type argument of hccm or TRUE, in which case "hc3" is used implicitly. For backwards compatibility. |
| verbose |If TRUE, the hypothesis matrix, right-hand-side vector (or matrix), and estimated value of the hypothesis are printed to standard output; if FALSE (the default), the hypothesis is only printed in symbolic form and the value of the hypothesis is not printed. |
| x |an object produced by linear.hypothesis.mlm. |
| SSP |if TRUE (the default), print the sum-of-squares and cross-products matrix for the hypothesis and the response-transformation matrix. |
| digits |minimum number of signficiant digits to print. |
| ... |aruments to pass down. |
!細節
Computes either a finite sample F statistic or asymptotic Chi-squared statistic for carrying out a Wald-test-based comparison between a model and a linearly restricted model. The default method will work with any model object for which the coefficient vector can be retrieved by coef and the coefficient-covariance matrix by vcov (otherwise the argument vcov. has to be set explicitely). For computing the F statistic (but not the Chi-squared statistic) a df.residual method needs to be available. If a formula method exists, it is used for pretty printing.
The method for "lm" objects calls the default method, but it changes the default test to "F", supports the convenience argument white.adjust (for backwards compatibility), and enhances the output by residual sums of squares. For "glm" objects just the default method is called (bypassing the "lm" method).
The function lht also dispatches to linear.hypothesis.
The hypothesis matrix can be supplied as a numeric matrix (or vector), the rows of which specify linear combinations of the model coefficients, which are tested equal to the corresponding entries in the righ-hand-side vector, which defaults to a vector of zeroes.
Alternatively, the hypothesis can be specified symbolically as a character vector with one or more elements, each of which gives either a linear combination of coefficients, or a linear equation in the coefficients (i.e., with both a left and right side separated by an equals sign). Components of a linear expression or linear equation can consist of numeric constants, or numeric constants multiplying coefficient names (in which case the number precedes the coefficient, and may be separated from it by spaces or an asterisk); constants of 1 or -1 may be omitted. Spaces are always optional. Components are separated by plus or minus signs. See the examples below.
A linear hypothesis for a multivariate linear model (i.e., an object of class "mlm") can optionally include an intra-subject transformation matrix for a repeated-measures design. If the intra-subject transformation is absent (the default), the multivariate test concerns all of the corresponding coefficients for the response variables. There are two ways to specify the transformation matrix for the repeated meaures:
The transformation matrix can be specified directly via the P argument.
A data frame can be provided defining the repeated-measures factor or factors via idata, with default contrasts given by the icontrasts argument. An intra-subject model-matrix is generated from the one-sided formula specified by the idesign argument; columns of the model matrix corresponding to different terms in the intra-subject model must be orthogonal (as is insured by the default contrasts). Note that the contrasts given in icontrasts can be overridden by assigning specific contrasts to the factors in idata. The repeated-measures transformation matrix consists of the columns of the intra-subject model matrix corresponding to the term or terms in iterms. In most instances, this will be the simpler approach, and indeed, most tests of interests can be generated automatically via the Anova function.
!輸出
For a univariate model, an object of class "anova" which contains the residual degrees of freedom in the model, the difference in degrees of freedom, Wald statistic (either "F" or "Chisq") and corresponding p value.
For a multivariate linear model, an object of class "linear.hypothesis.mlm", which contains sums-of-squares-and-product matrices for the hypothesis and for error, degrees of freedom for the hypothesis and error, and some other information.
The returned object normally would be printed.
!作者
Achim Zeileis and John Fox jfox@mcmaster.ca
!參考文獻
# Fox, J. (1997) Applied Regression, Linear Models, and Related Methods. Sage.
# Hand, D. J., and Taylor, C. C. (1987) Multivariate Analysis of Variance and Repeated Measures: A Practical Approach for Behavioural Scientists. Chapman and Hall.
# O'Brien, R. G., and Kaiser, M. K. (1985) MANOVA method for analyzing repeated measures designs: An extensive primer. Psychological Bulletin 97, 316–333.
!請參考其他函示
{{{
anova, Anova, waldtest, hccm, vcovHC, vcovHAC, coef, vcov
}}}
!範例
{{{
mod.davis <- lm(weight ~ repwt, data=Davis)
## the following are equivalent:
linear.hypothesis(mod.davis, diag(2), c(0,1))
linear.hypothesis(mod.davis, c("(Intercept) = 0", "repwt = 1"))
linear.hypothesis(mod.davis, c("(Intercept)", "repwt"), c(0,1))
linear.hypothesis(mod.davis, c("(Intercept)", "repwt = 1"))
## use asymptotic Chi-squared statistic
linear.hypothesis(mod.davis, c("(Intercept) = 0", "repwt = 1"), test = "Chisq")
## the following are equivalent:
## use HC3 standard errors via white.adjust option
linear.hypothesis(mod.davis, c("(Intercept) = 0", "repwt = 1"),
white.adjust = TRUE)
## covariance matrix *function*
linear.hypothesis(mod.davis, c("(Intercept) = 0", "repwt = 1"), vcov = hccm)
## covariance matrix *estimate*
linear.hypothesis(mod.davis, c("(Intercept) = 0", "repwt = 1"),
vcov = hccm(mod.davis, type = "hc3"))
mod.duncan <- lm(prestige ~ income + education, data=Duncan)
## the following are all equivalent:
linear.hypothesis(mod.duncan, "1*income - 1*education = 0")
linear.hypothesis(mod.duncan, "income = education")
linear.hypothesis(mod.duncan, "income - education")
linear.hypothesis(mod.duncan, "1income - 1education = 0")
linear.hypothesis(mod.duncan, "0 = 1*income - 1*education")
linear.hypothesis(mod.duncan, "income-education=0")
linear.hypothesis(mod.duncan, "1*income - 1*education + 1 = 1")
linear.hypothesis(mod.duncan, "2income = 2*education")
mod.duncan.2 <- lm(prestige ~ type*(income + education), data=Duncan)
coefs <- names(coef(mod.duncan.2))
## test against the null model (i.e., only the intercept is not set to 0)
linear.hypothesis(mod.duncan.2, coefs[-1])
## test all interaction coefficients equal to 0
linear.hypothesis(mod.duncan.2, coefs[grep(":", coefs)], verbose=TRUE)
## a multivariate linear model for repeated-measures data
## see ?OBrienKaiser for a description of the data set used in this example.
mod.ok <- lm(cbind(pre.1, pre.2, pre.3, pre.4, pre.5,
post.1, post.2, post.3, post.4, post.5,
fup.1, fup.2, fup.3, fup.4, fup.5) ~ treatment*gender,
data=OBrienKaiser)
coef(mod.ok)
## specify the model for the repeated measures:
phase <- factor(rep(c("pretest", "posttest", "followup"), c(5, 5, 5)),
levels=c("pretest", "posttest", "followup"))
hour <- ordered(rep(1:5, 3))
idata <- data.frame(phase, hour)
idata
## test the four-way interaction among the between-subject factors
## treatment and gender, and the intra-subject factors
## phase and hour
linear.hypothesis(mod.ok, c("treatment1:gender1", "treatment2:gender1"),
title="treatment:gender:phase:hour", idata=idata, idesign=~phase*hour,
iterms="phase:hour")
}}}
===
!
{{{
lm.ridge {MASS}
}}}
!
{{{
Ridge Regression
}}}
!
Fit a linear model by ridge regression.
!語法
{{{
lm.ridge(formula, data, subset, na.action, lambda = 0, model = FALSE,
x = FALSE, y = FALSE, contrasts = NULL, ...)
}}}
!輸入
| formula |a formula expression as for regression models, of the form response ~ predictors. See the documentation of formula for other details. offset terms are allowed. |
| data |an optional data frame in which to interpret the variables occurring in formula. |
| subset |expression saying which subset of the rows of the data should be used in the fit. All observations are included by default. |
| na.action |a function to filter missing data. |
| lambda |A scalar or vector of ridge constants. |
| model |should the model frame be returned? |
| x |should the design matrix be returned? |
| y |should the response be returned? |
| contrasts |a list of contrasts to be used for some or all of factor terms in the formula. See the contrasts.arg of model.matrix.default. |
| ... |additional arguments to lm.fit. |
!細節
If an intercept is present in the model, its coefficient is not penalized. (If you want to penalized an intercept, put in your own constant term and remove the intercept.)
!輸出
| coef |matrix of coefficients, one row for each value of lambda. Note that these are not on the original scale and are for use by the coef method. |
| scales |scalings used on the X matrix. |
| Inter |was intercept included? |
| lambda |vector of lambda values |
| ym |mean of y |
| xm |column means of x matrix |
| GCV |vector of GCV values |
| kHKB |HKB estimate of the ridge constant |
| kLW |L-W estimate of the ridge constant |
!參考文獻
# Brown, P. J. (1994) Measurement, Regression and Calibration Oxford.
!同時參考
{{{
lm
}}}
!範例
{{{
data(longley) # not the same as the S-PLUS dataset
names(longley)[1] <- "y"
lm.ridge(y ~ ., longley)
plot(lm.ridge(y ~ ., longley,
lambda = seq(0,0.1,0.001)))
select(lm.ridge(y ~ ., longley,
lambda = seq(0,0.1,0.0001)))
}}}
<html><div align="center"><iframe src ="http://www.statgraphics.com/regression_analysis.htm" width="100%" align="center" height=400"></iframe></div></html>
<html><div align="center"><iframe src ="http://www.cyclismo.org/tutorial/R/" width="100%" align="center" height=400"></iframe></div></html>
!主題
預測NBA比賽結果。
!第一件事
{{{
課程期間,絕不運用任何分析結果簽賭。一旦發現將依校規處分。
}}}
!Q&A
# 問如何從網際網路把數據讀入R?
# 繪製散佈圖?
# 知道那一點對應哪一筆數據?
!開始三點決定一條直線的遊戲
{{{
利用前測的結果。
}}}
<html><div align="center"><iframe src ="http://www.biosino.org/pages/newhtm/r/tchtml/index.html#Top" width="100%" align="center" height=400"></iframe></div></html>
[[參數估計式的拔靴抽樣分配]]、
[[參數的拔靴信賴區間]]
!以下這一些樣本來的統計量,你知道如何估計它的標準差嗎?
$$
\bar x
$$
----
$$
\bar y / \bar x
$$
----
$$
S_X / \bar x
$$
----
!如果你的數理統計夠強,...
{{{
}}}
!如果你的R能力夠強,...,加上你又懂拔靴法,...
{{{
}}}
$$
\hat \beta = (X^TX)^{-1} X^T y
$$
!問題
$$
(X^TX)^{-1}
$$
!Suggestion
{{{
Use GOOGLE to find out the 'best' introduction paper to begin your MANUAL writing!
}}}
!It must contain
# BACKGROUND (Using 5w1h logic to write this section)
# Data and its format,
# Required Input(s) and its (their) format(s),
# Useful output(s) and its (their) name(s) and format(s),
# Example(s) and the corresponding explanation(s).
# Reference(s)
{{{
data.frame()
is.data.frame()
as.data.frame()
row.names()
plot.data.frame()
print.data.frame()
}}}
{{{
cbind()
rbind()
}}}
{{{
numeric()
character()
logic()
factor()
}}}
{{{
class()
mode()
}}}
{{{
summary()
}}}
!來源
{{{
lm {stats}
}}}
!目的
{{{
Fitting Linear Models
}}}
!功能描述
lm is used to fit linear models. It can be used to carry out regression, single stratum analysis of variance and analysis of covariance (although aov may provide a more convenient interface for these).
!用法
{{{
lm(formula, data, subset, weights, na.action,
method = "qr", model = TRUE, x = FALSE, y = FALSE, qr = TRUE,
singular.ok = TRUE, contrasts = NULL, offset, ...)
}}}
!輸入
| formula | an object of class "formula" (or one that can be coerced to that class): a symbolic description of the model to be fitted. The details of model specification are given under ‘Details’. |
| data |an optional data frame, list or environment (or object coercible by as.data.frame to a data frame) containing the variables in the model. If not found in data, the variables are taken from environment(formula), typically the environment from which lm is called. |
| subset |an optional vector specifying a subset of observations to be used in the fitting process. |
| weights |an optional vector of weights to be used in the fitting process. Should be NULL or a numeric vector. If non-NULL, weighted least squares is used with weights weights (that is, minimizing sum(w*e^2)); otherwise ordinary least squares is used. See also ‘Details’. |
| na.action |a function which indicates what should happen when the data contain NAs. The default is set by the na.action setting of options, and is na.fail if that is unset. The ‘factory-fresh’ default is na.omit. Another possible value is NULL, no action. Value na.exclude can be useful. |
| method |the method to be used; for fitting, currently only method = "qr" is supported; method = "model.frame" returns the model frame (the same as with model = TRUE, see below). |
| model, x, y, qr |logicals. If TRUE the corresponding components of the fit (the model frame, the model matrix, the response, the QR decomposition) are returned. |
| singular.ok |logical. If FALSE (the default in S but not in R) a singular fit is an error. |
| contrasts |an optional list. See the contrasts.arg of model.matrix.default. |
| offset |this can be used to specify an a priori known component to be included in the linear predictor during fitting. This should be NULL or a numeric vector of length equal to the number of cases. One or more offset terms can be included in the formula instead or as well, and if more than one are specified their sum is used. See model.offset. |
| ... |additional arguments to be passed to the low level regression fitting functions (see below). |
!輸出
lm returns an object of class "lm" or for multiple responses of class c("mlm", "lm").
The functions summary and anova are used to obtain and print a summary and analysis of variance table of the results. The generic accessor functions coefficients, effects, fitted.values and residuals extract various useful features of the value returned by lm.
An object of class "lm" is a list containing at least the following components:
| coefficients |a named vector of coefficients |
| residuals |the residuals, that is response minus fitted values. |
| fitted.values |the fitted mean values. |
| rank |the numeric rank of the fitted linear model. |
| weights |(only for weighted fits) the specified weights. |
| df.residual |the residual degrees of freedom. |
| call |the matched call. |
| terms |the terms object used. |
| contrasts |(only where relevant) the contrasts used. |
| xlevels |(only where relevant) a record of the levels of the factors used in fitting. |
| offset |the offset used (missing if none were used). |
| y |if requested, the response used. |
| x |if requested, the model matrix used. |
| model |if requested (the default), the model frame used. |
| na.action |(where relevant) information returned by model.frame on the special handling of NAs. |
!作者
The design was inspired by the S function of the same name described in Chambers (1992). The implementation of model formula by Ross Ihaka was based on Wilkinson & Rogers (1973).
!參考文獻
# Chambers, J. M. (1992) Linear models. Chapter 4 of Statistical Models in S eds J. M. Chambers and T. J. Hastie, Wadsworth & Brooks/Cole.
# Wilkinson, G. N. and Rogers, C. E. (1973) Symbolic descriptions of factorial models for analysis of variance. Applied Statistics, 22, 392–9.
!一起參考
[[summary.lm]] for summaries and [[anova.lm]] for the ANOVA table; [[aov]] for a different interface.
The generic functions [[coef]], [[effects]], [[residuals]], [[fitted]], [[vcov]].
[[predict.lm]] (via [[predict]]) for prediction, including confidence and prediction intervals; [[confint]] for confidence intervals of parameters.
[[lm.influence]] for regression diagnostics, and [[glm]] for generalized linear models.
The underlying low level functions, [[lm.fit]] for plain, and [[lm.wfit]] for weighted regression fitting.
More lm() examples are available e.g., in [[anscombe]], [[attitude]], [[freeny]], [[LifeCycleSavings]], [[longley]], [[stackloss]], [[swiss]].
[[biglm]] in package biglm for an alternative way to fit linear models to large datasets (especially those with many cases).
!例子
{{{
require(graphics)
## Annette Dobson (1990) "An Introduction to Generalized Linear Models".
## Page 9: Plant Weight Data.
ctl <- c(4.17,5.58,5.18,6.11,4.50,4.61,5.17,4.53,5.33,5.14)
trt <- c(4.81,4.17,4.41,3.59,5.87,3.83,6.03,4.89,4.32,4.69)
group <- gl(2,10,20, labels=c("Ctl","Trt"))
weight <- c(ctl, trt)
anova(lm.D9 <- lm(weight ~ group))
summary(lm.D90 <- lm(weight ~ group - 1))# omitting intercept
summary(resid(lm.D9) - resid(lm.D90)) #- residuals almost identical
opar <- par(mfrow = c(2,2), oma = c(0, 0, 1.1, 0))
plot(lm.D9, las = 1) # Residuals, Fitted, ...
par(opar)
## model frame :
stopifnot(identical(lm(weight ~ group, method = "model.frame"),
model.frame(lm.D9)))
### less simple examples in "See Also" above
}}}
>Multiple Linear Regression is very sensitive to predictors being in a configuration of near-collinearity : when this happens, the model parameters become unstable (large variances) and can therefore no longer be interpreted. From a mathematical standpoint, near-collinearity makes the X'X matrix ill-conditioned (with X the data matrix) : the value of its determinant is nearly 0, and attempts to calculate the inverse of the matrix run into numerical snags with uncertain final values.
>
>Exact collinearity occurs when at least one of the predictors is a linear combination of other predictors. X is not a full rank matrix anymore, the determinant of X is exactly 0, and inverting X'X is not just difficult, it is downright impossible because the inverse matrix simply does not exist.
----
>The same happens when there are fewer observations than there are parameters to be estimated, a not uncommon situation. For example, a spectrum may be described by the list of light intensity measured at a few hundred different wavelengths (the predictors), whereas only a few tens of spectra (the observations) describing some phenomenon are available.
----
>For the analyst, quasi-collinearity of some predictors causes a large variance (uncertainty) of the model predictions, therefore making these predictions highly unreliable.
!11.1.2
{{{
y = rnorm(20)
y
y = rgamma(n = 10, shape = 2)
y = rgamma(n = 10, shape = c(1:10))
sample(10)
sample(10, replace = T)
set = c(11,22,33,44,55)
sample(set)
sample(set, size = 10, replace = T)
sample(set, size = 10, replace = T, prob = c(0.1, 0.1, 0.1, 0.1, 0.6))
city
city$u
city$x
city$x[1]
city$x[c(1:4)]
city$x[c(1, 5, 10)]
city[c(1, 5, 10), 2]
city$x[-1]
city[c(1:3),]
i = sample(10, replace = T)
city[i,]
}}}
!11.2.1
{{{
city.fun = function(data, i)
{
d = data[i,]
mean(d$x) / mean(d$u)
}
city.boot = boot(data = city, statistic = city.fun, R = 49)
city.boot
plot(city.boot)
names(city.boot)
system.time(city.boot = boot(data = city, statistic = city.fun, R = 49))
# speed up
mat = as.matrix(city)
mat.fun = function(data, i)
{
d = data[i,]
mean(d[,2]) / mean(d[,1])
}
system.time(boot(data = mat, statistic = mat.fun, R = 49))
f = boot.array(city.boot)
f[1:20,]
# Type of statistic
city.w = function(data, w = rep(1, nrow(data)) / nrow(data))
{
w = w / sum(w)
sum(w * data$x) / sum(w * data$u)
}
city.boot = boot(city, city.w, R = 49, stype = "w")
city.f = function(data, f)
{
sum(f * data$x) / sum(f * data$u)
}
city.boot = boot(city, city.f, R = 49, stype = "f")
# function "statistic"
air.fun = function(data, i)
{
d = data[i,]
c(mean(d), var(d)/nrow(data))
}
air.boot = boot(data = aircondit, statistic = air.fun, R = 199)
city.subset = function(data, i, n = 10)
{
d = data[i[1:n],]
mean(d[,2]) / mean(d[,1])
}
city.boot = boot(data = city, statistic = city.subset, R = 199, n = 5)
boot.array(city.boot, indices = T)[,1:5]
}}}
!11.2.2
{{{
aircondit.fun = function(data) mean(data$hours)
aircondit.sim = function(data, mle)
{
d = data
d$hours = rexp(n = nrow(data), rate = mle)
d
}
aircondit.mle = 1 / mean(aircondit$hours)
aircondit.para = boot(data = aircondit, statistic = aircondit.fun,
R = 19, sim = "parametric", ran.gen = aircondit.sim,
mle = aircondit.mle)
# A more complicates example
l.city = log(city)
city.mle = c(apply(l.city, 2, mean), sqrt(apply(l.city, 2, var)), corr(l.city))
city.sim = function(data, mle)
{
n = nrow(data)
d = matrix(rnorm(2*n), n, 2)
d[,2] = mle[2] + mle[4] * (mle[5]*d[,2]+sqrt(1 - mle[5]^2)*d[,1])
d[,1] = mle[1] + mle[3]*d[,1]
data$x = exp(d[,2])
data$u = exp(d[,1])
data
}
city.f = function(data) mean(data[,2]) / mean(data[,1])
city.para = boot(city, city.f, R = 199, sim = "parametric",
ran.gen = city.sim, mle = city.mle)
# Nonparametric version
city.boot = boot(city, statistic = function(data, i) city.f(data[i,]), R = 199)
# Empirical influence values
L.diff = empinf(data = city, statistic = city.w, stype = "w")
cbind(L.diff, (city$x - city.w(city) * city$u) / mean(city$u))
city.boot = boot(city, city.fun, R = 999)
L.reg = empinf(city.boot)
L.reg
J = empinf(data = city, statistic = city.fun, stype= "i", type = "jack")
# Variance approximations
var.linear(L.diff)
var.linear(L.reg)
# Linear approximation
city.tL.reg = linear.approx(city.boot)
city.tL.diff = linear.approx(city.boot, L = L.diff)
split.screen(c(1,2))
screen(1)
plot(city.tL.reg, city.boot$t)
abline(0, 1, lty = 2)
screen(2)
plot(city.tL.diff, city.boot$t)
abline(0, 1, lty = 2)
}}}
!11.3.1
{{{
gravity
grav = gravity[as.numeric(gravity$series) >= 7,]
grav
grav.fun = function(data, i, trim = 0.125)
{
d = data[i,]
m = tapply(d$g, d$series, mean, trim = trim)
m[7] - m[8]
}
grav.boot = boot(grav, grav.fun, R = 199, strata = grav$series)
grav.L = empinf(grav.boot)
grav.tL = linear.approx(grav.boot)
var.linear(grav.L, strata = grav$series)
}}}
!11.3.2
{{{
y = rnorm(99)
h = 0.5
y.gen = function(data, mle)
{
n = length(data)
i = sample(n, n, replace = T)
data[i] + mle * rnorm(n)
}
y.boot = boot(y, median, R = 199, sim = "parametric",
ran.gen = y.gen, mle = h)
}}}
!11.3.4
{{{
city.fun = function(data, i)
{
d = data[i,]
rat = mean(d$x) / mean(d$u)
L = (d$x - rat*d$u) / mean(d$u)
c(rat, sum(L^2) / nrow(d)^2, L)
}
city.boot = boot(city, city.fun, R = 999)
city.L = city.boot$t0[3:12]
split.screen(c(1,2))
screen(1)
split.screen(c(2,1))
screen(4)
plot(city$u, city$x, type = "n", xlim = c(0, 300), ylim = c(0, 300))
text(city$u, city$x, round(city.L, 2))
screen(3)
plot(city$u, city$x, type = "n", xlim = c(0, 300), ylim = c(0, 300))
text(city$u, city$x, c(1:10))
abline(0, city.boot$t0[1], lty = 2)
screen(2)
jack.after.boot(boot.out = city.boot, useJ = F, stinf = F, L = city.L)
close.screen(all = T)
jack.after.boot(boot.out = city.boot, useJ = F, stinf = F, index = 2)
jack.after.boot(boot.out = city.boot, useJ = F, stinf = F, t = city.boot$t[,2])
}}}
{{{
Martin, please write down anything you see in this paper.
}}}
----
{{{
中文作答!謝謝你願意跟我一起學習,如果你覺得被我虐待了,對不起,請原諒我。
}}}
----
# 藍球在哪一年++++[發明的?]
{{{
}}}
===
# 誰是公認最受歡迎的++++[NBA球員?]
{{{
}}}
===
# Michael Jordan在++++[哪一支球隊退休的?]
{{{
}}}
===
# 這一篇文章(1998)想要分析++++[哪三種關於籃球的數據?]
{{{
}}}
===
# SMH提供了哪三種分析球隊季後賽輸贏的++++[模型?]
{{{
}}}
===
# SSH展延SMH的模型,總共提出了幾種++++[模型?]
{{{
}}}
===
# SSH提出了哪幾種++++[模型?]
{{{
}}}
===
# 根據本篇文章,請說明有幾種++++[球員(原文作答)?]
{{{
}}}
===
# 根據本篇文章的分類,1987-88總共有多少++++[NBA球員?]
{{{
}}}
===
# 根據本篇文章,哪幾本書提供分數++++[檢視]
{{{
To measure an individual player's contribution to his team's success?
}}}
{{{
}}}
===
# 根據Bellotti的結果,Michael Jordan在1992年排++++[第幾名?]
{{{
}}}
===
# 哪些作者首先分析藍球【射手】的++++[數據?]
{{{
}}}
===
# 為了找到【The hot hand】,GVT跟~TG1分析++++[哪三種數據?]
{{{
}}}
===
# 關於~TG1問過的一道++++[問題]
{{{
When shooting free throws, does a player have a better chance of
making his second shot after making his first shot than after missing
his first shot?
}}}
{{{
}}}
===
當時有多少比例的人回答【No】?
# 本篇文章的作者認為哪一種議題,對球迷而言是++++[最重要的?]
{{{
}}}
===
# 有一位射手10次出手,++++[得到]
{{{
F,F,S,S,S,S,S,S,F,F,S
}}}
{{{
}}}
===
統計有過什麼樣的統計量可以檢定這一位射手的表現?
# 如果你正在找尋藍球統計研究的議題,本篇文章的作者建議從++++[哪裡出發?]
{{{
}}}
===
# 本篇文章的作者是誰++++[(原文作答)?]
{{{
}}}
===
# 作者發表文章的當時在哪一所學校++++[(原文作答)?]
{{{
}}}
===
# SMH用哪一種檢定評估他們提出來的++++[三種模型?]
{{{
}}}
===
{{{
中文作答!謝謝你願意跟我一起學習,如果你覺得被我虐待了,對不起,請原諒我。
}}}
{{{
plot(kicker$FGtM1,kicker$FGt,
main="Slope of each line = -0.504",
xlab="Field Goal Percentage in Year t-1",
ylab="Field Goal Percentage in Year t")
tt <- seq(60,100,length=1001)
slope.piece <- summary(fit.2)$coef[20]*tt
lines(tt,summary(fit.2)$coef[1]+slope.piece,lty=2)
for (i in 2:19)
{
lines(tt,summary(fit.2)$coef[1]+summary(fit.2)$coef[i]+slope.piece,lty=2)
}
}}}
+++[-0.504怎麼來的?]===
+++[60, 100怎麼來的?]===
+++[為什麼用1001?]===
+++[為什麼是2:19?]===
證明
$$
\begin{eqnarray}
S^2 & = & \frac{\sum (x_i - \bar x)^2}{n - 1} \\
& = & \frac{1}{2} \frac{\sum (x_i - x_j)^2}{C^n_2} \\
\end{eqnarray}
$$
[img[http://i299.photobucket.com/albums/mm318/jungpinwu/9cf4dcf9.jpg]]
!第一階段
# 先在BB上打分數。
# 針對以下項目進行單項【評分、1 ~ 10分】以及【評論】。為了避免同儕之間不必要的困擾,維護互評機制的公正與客觀,此項評分以紙本報告的方式繳交。
## 架構(是否按照任課老師的要求)
## 文筆清晰(文筆是否可以讓讀者迅速了解使用說明的訴求)
## 案例豐富(是否提供與[[任務|學期任務]]有關的案例說明)
## 合作態度(是否以讓他人最容易上手的調調編寫使用說明)
!第二階段
# 再一次打分數。但是不再進行紙本攻防。
{{{
plot(kicker$FGtM1,kicker$FGt,
main="Unadjusted Correlation = -0.139",
xlab="Field Goal Percentage in Year t-1",
ylab="Field Goal Percentage in Year t")
}}}
+++[-0.139怎麼來的?]
{{{
> cor(FGt,FGtM1)
[1] -0.1391935
}}}
===
!目標
整合各項迴歸模型的基本建構技巧以及對應R的計算技巧,循序漸進地建立NBA運動彩劵預測模型。
!任務內容
# 撰寫一份迴歸模型R函示的簡易使用說明。採[[同儕互評機制]]審查【合作態度】的分數。
# 收集相關NBA運動彩劵的數據、新聞、以及各種有利預測比賽結果的消息與資料。
# 從BB各組提供的使用說明,收集建構各種迴歸模型的機制,不論是【參數型模型】、【無母數型模型】、還是【半參數型模型】。
# 學習利用R函示建構各種迴歸模型的語法。
# 學習運用結果的迴歸模型預測比賽結果。按[[這一個公式]]排名比賽結果。
# 把整個建模過程寫成一份海報,並且公開展示。一樣採[[同儕互評機制]]審查海報的分數。
先請各位參考[[迴歸分析的相關函示]]。你會看到迴歸分析的[[招式]]這麼多!為了在學期內完成【完全攻陷】與迴歸分析有關的R函示,只有一條路,[[眾志成城]]。計畫如下:
# 1-12項認養一項。
# 2-24項認養一項。
# 兩個月內為各認養項目完成一份簡易的使用說明,並且把成果放在BB的討論區。
# 第三個月期間各組上BB閱讀他組提供的使用說明,並且在BB上進行互評。
# 第四個月期間,各組可針對他組的評價改進、改善,或者是強化先前撰寫的使用說明。
# 學期結束前,會再為各組提供的【使用說明】進行一次互評。
# 不要抄襲。
# 寫出自己的創意。
# 全部用R計算,但是不准直接剪貼R的答案回答問題。
# 用正式的格式撰寫:
## 題意(背景)
## 問題(逐題回答)
## 解答
## 參考文獻
## 附錄:我的R程式與R報表
# 於規定期限之前,繳交紙本到我的信箱以及該紙本的PDF電子檔到我的cwu@fcu.edu.tw的信箱(以電子檔的繳交時間為憑)。
# 抓到抄襲,會請[[當事人]]前來我的辦公室說明。
# 確定抄襲,該次作業不計分。
# 對於分數的疑義,請當事人在拿到成績後的一週內,前來我的辦公室當面討論。
# 請確實按照[[家庭作業基本要求]]撰寫你的家庭作業。
# 被評定為優秀的作業,會在當事人同意之下,於老師的教學網站與學校的BB教室公佈該作業的部份內容,以資參考。
# 讀取手邊有的數據
# @@探索@@++++[:]
{{{
探索樣本出現的現象。(波士頓公寓數據的經驗!)
}}}
===
# 試跑
# 診斷
# @@選模@@
# 試跑
# 診斷
# @@換模@@
# 試跑
# 診斷
# @@換模@@
# 試跑
# 診斷
...
{{{
kicker<-read.csv("http://www.stat.tamu.edu/~sheather/book/docs/datasets/FieldGoals2003to2006.csv",header=T)
attach(kicker)
Reg <-lm(FGt~Name+FGtM1,data=kicker)
plot(kicker$FGtM1,kicker$FGt,main="Slope of each line = -0.504",xlab="Field Goal Percentage in Year t-1",ylab="Field Goal Percentage in Year t")
tt<-seq(60,100,length=1001)
slope.piece<-summary(Reg)$coef[20]*tt
lines(tt,summary(Reg)$coef[1]+slope.piece,lty=2)
lines(tt,summary(Reg)$coef[1]+slope.piece,lty=2)
for(i in 2:19)
{
lines(tt,summary(Reg)$coef[1]+summary(Reg)$coef[i]+slope.piece,lty=2)
text((100-65)/18*(i-1)+65,summary(Reg)$coef[1]+summary(Reg)$coef[i]+summary(Reg)$coef[20]*((100-65)/18*(i-1)+65),row.names(summary(Reg)$coef)[i],col=1)
}
}}}
!家庭作業的基本要求
# 不要抄襲。
# 寫出自己的創意。
# 全部用R計算,但是不准直接剪貼R的答案回答問題。
# 用正式的格式撰寫:
## 題意(背景)
## 問題(逐題回答)
## 解答
## 參考文獻
## 附錄:我的R程式與R報表
!課本提供的題意
One may wonder if people of similar heights tend to marry each other. For this purpose, a sample of newly married couples was selected. Let $X$ be the height of the husband and $Y$ be the height of the wife. The heights (in cm) of husbands and wives are found in Table 2.11. The data can also be found in the book's Web site.
!課本建議的練習
{{{
利用關鍵字【hadi】跟【regression】找到數據放在
http://www.ilr.cornell.edu/~hadi/RABE4/Data4/P049.txt
所以
我用指令
> Table2.10 = read.table("http://www.ilr.cornell.edu/~hadi/RABE4/Data4/P049.txt", header=T)
}}}
# ++++[Compute the covariance between the heights of the husbands and wives.]
{{{
> attach(Table2.10)
> cov(Husband, Wife)
[1] 69.41294
}}}
===
# ++++[What would the covariance be if heights were measured in inches rather than in centimeters?]
{{{
> cov(Husband*2.54, Wife*2.54)
[1] 447.8245
}}}
===
# ++++[Compute the correlation coefficient between the heights of the husband and wife.]
{{{
> cor(Husband, Wife)
[1] 0.7633864
}}}
===
# ++++[What would the correlation be if heights were measured in inches rather than in centimeters?]
{{{
> cor(Husband*2.54, Wife*2.54)
[1] 0.7633864
}}}
===
# ++++[What would the correlation be if every man married a woman exactly 5 centimeters shorter than him?]
{{{
> wife = Husband + 5/2.54
> cor(Husband, wife)
[1] 1
}}}
===
# ++++[We wish to fit a regression model relating the heights of husbands and wives. Which one of the two variables would you choose as the response variable? Justify your answer.]
{{{
> lm(Wife~Husband, data = Table2.10)
Call:
lm(formula = Wife ~ Husband, data = Table2.10)
Coefficients:
(Intercept) Husband
41.9302 0.6997
> lm(Husband~Wife, data = Table2.10)
Call:
lm(formula = Husband ~ Wife, data = Table2.10)
Coefficients:
(Intercept) Wife
37.810 0.833
>
}}}
===
# ++++[Using your choice of the response variable in (6), test the null hypothesis that the slope is zero.]
{{{
> summary(lm(Husband~Wife, data = Table2.10))
Call:
lm(formula = Husband ~ Wife, data = Table2.10)
Residuals:
Min 1Q Median 3Q Max
-16.7438 -4.2838 -0.1615 4.2562 17.7500
Coefficients:
Estimate Std. Error t value Pr(>|t|)
(Intercept) 37.81005 11.93231 3.169 0.00207 **
Wife 0.83292 0.07269 11.458 < 2e-16 ***
---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
Residual standard error: 6.468 on 94 degrees of freedom
Multiple R-squared: 0.5828, Adjusted R-squared: 0.5783
F-statistic: 131.3 on 1 and 94 DF, p-value: < 2.2e-16
>
}}}
===
# ++++[Using your choice of the response variable in (6), test the null hypothesis that the intercept is zero.]
{{{
Same as above.
}}}
===
# ++++[Using your choice of the response variable in (6), test the null hypothesis that both the intercept and the slope are zero.]
{{{
> library(car)
> HonW = lm(Husband~Wife, data = Table2.10)
> linear.hypothesis(HonW, c("(Intercept) = 0", "Wife = 0"))
Linear hypothesis test
Hypothesis:
(Intercept) = 0
Wife = 0
Model 1: Husband ~ Wife
Model 2: restricted model
Res.Df RSS Df Sum of Sq F Pr(>F)
1 94 3932
2 96 2926719 -2 -2922787 34932 < 2.2e-16 ***
---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
>
}}}
===
# ++++[Which of the above hypotheses and tests would you choose to test whether people of similar heights tend to marry each other? What is your conclusion?]
{{{
None.
}}}
===
# ++++[If none of the above tests is appropriate for testing the hypothesis that people of similar heights tend to marry each other, which test would you use? What is your conclusion based on this test?]
{{{
請參考期中考解答。
}}}
===
!我要求的練習
>根據圖1-1與圖1-2的討論與你的創意,試著為[[相關分析]]與[[簡單線性迴歸分析]]搭個橋梁!所以
{{{
自己訂題目(自問),自己試著用R得到類似圖1-1跟1-2的答案(自答)。
}}}
{{{
利用身高把數據分成幾群,比如說,[min, Q1]、[Q1, Q2]、[Q2, Q3]、[Q3, max],
然後再運用FG時的經驗。
}}}
用微軟的PPT報告。請
{{{
在有所依據的前提下,各自發揮創意!
}}}
!關於相關係數這一個非常有用的參數,曾經有過這麼一段...
>The bootstrap is a nonparametric technique for estimating standard errors and approximate confidence intervals. Rasmussen has used a simulation experiment to suggest that bootstrap confidence intervals perform very poorly in the estimation of a correlation coefficient. Part of Rasmussen's simulation is repeated. A careful look at the results shows the bootstrap intervals performing quite well. Some remarks are made concerning the virtues and defects of bootstrap intervals in general.
!第一部份(50%)
!!題意
發揮你的創意,比較網站上提供的五種(可以只是boot.ci()提供的四種)的表現。
!!問題
試著利用以下分配的假數據,針對參數$\sigma/\mu$並且用$S/\bar X$估計它,請試着比較各種拔靴信賴區間:
{{{
boot.CV = function(x, index)
{
sd(x[index]) / mean(x[index])
}
library(boot)
}}}
# (10%)++++[常態分配]
{{{
n = 100
x = rnorm(n, mean = 0, sd = 1)
B = 9999
CV.boot = boot(x, boot.CV, R = B)
plot(CV.boot)
boot.ci(CV.boot)
}}}
===
# (20%)$t$++++[分配]
{{{
df = 100
x = rt(n, df = df)
B = 9999
CV.boot = boot(x, boot.CV, R = B)
plot(CV.boot)
boot.ci(CV.boot)
}}}
===
# (30%)++++[對數常態分配]
{{{
n = 100
x = rlnorm(n, meanlog = 0, sdlog = 1)
B = 9999
CV.boot = boot(x, boot.CV, R = B)
plot(CV.boot)
boot.ci(CV.boot)
}}}
===
# (40%)++++[短尾對稱分配]
{{{
n = 100
d = ?
x = rtikuv(n, d, mean=0, sigma=1, Smallno=1.0e-6)
B = 9999
CV.boot = boot(x, boot.CV, R = B)
plot(CV.boot)
boot.ci(CV.boot)
}}}
===
!第二部份(50%)
!!題意
比較兩種拔靴假設檢定思維的表現。
!!問題
課堂上提過兩種拔靴假設檢定的思維:
# 拔靴虛無假設下的分配。
# J. Fox (2002)的建議。拔靴檢定統計量的分配。
請針對跟第一部份同樣的分配,以及簡單線性迴歸之斜率的虛無假設,比較兩種拔靴假設檢定思維的表現。
!!提示
假設模型如下:
$$
y = \beta_0 + \beta_1 x + \mbox{某一種分配的誤差}
$$
其中$x$來自Uniform(0,100)這一個分配。假設我們的樣本數是20。
{{{
請參考老師發的講義。
}}}
{{{
為什麼挑R?
}}}
理由很簡單:它是免費的。就像老師挑~TiddlyWiki一樣的理由。R是全世界統計專家給這個世界的一份禮物。善用這一份禮物為自己加分,不論你要繼續留在「統計界」,還是你要轉進其他與數據有關的「XX界」。榮彬爸爸盡我最大的腦力與體力,透過這一份~TiddlyWiki跟各位同學、讀者諸君、網友討論R、認識R、使用R、改進R、貢獻R。努力成為R達人。
以下幾個頁面個人覺得你應該會常用到:
<<tabs "" [[挑一個]] "" [[]][[老師開的店]] "" [[老師開的店]][[本店]] "" [[本店]][[維基百科]] "" [[維基百科]][[第一枚搜尋引擎]] "" [[搜尋引擎]][[一位英文R家教]] "" [[一位英文R家教]][[R書不是A書]] "" [[R書]]>>
>Bootstrapping is a general approach to statistical inference based on building a sampling distribution for a statistic by resampling from the data at hand. The term ‘bootstrapping,’ due to Efron (1979), is an allusion to the expression ‘pulling oneself up by one’s bootstraps’ – in this case, using the sample data as a population from which repeated samples are drawn. At first blush, the approach seems circular, but has been shown to be sound.
>There are several forms of the bootstrap, and, additionally, several other resampling methods that are related to it, such as jackknifing, cross-validation, randomization tests, and permutation tests. I will stress the nonparametric bootstrap.
>Now suppose that we are interested in some statistic $T = t(S)$ as an estimate of the corresponding population parameter $\theta = t(P)$. Again, $\theta$ could be a vector of parameters and $T$ the corresponding vector of estimates, but for simplicity assume that $\theta$ is a scalar. A traditional approach to statistical inference is to make assumptions about the structure of the population (e.g., an assumption of normality), and, along with the stipulation of random sampling, to use these assumptions to derive the sampling distribution of $T$, on which classical inference is based. @@In certain instances, the exact distribution of $T$ may be intractable, and so we instead derive its asymptotic distribution.@@ This familiar approach has two potentially important deficiencies:
# If the assumptions about the population are wrong, then the corresponding sampling distribution of the statistic may be seriously inaccurate. On the other hand, if asymptotic results are relied upon, these may not hold to the required level of accuracy in a relatively small sample.
# @@The approach requires sufficient mathematical prowess to derive the sampling distribution of the statistic of interest. In some cases, such a derivation may be prohibitively difficult.@@
>In contrast, the nonparametric bootstrap allows us to estimate the sampling distribution of a statistic empirically without making assumptions about the form of the population, and without deriving the sampling distribution explicitly.
>Then the distribution of $T^∗_b$ around the original estimate $T$ is analogous to the sampling distribution of the estimator $T$ around the population parameter $\theta$. For example, the average of the bootstrapped statistics,
$$
\bar T^* = \hat E(T^*) = \frac{\sum_{b = 1}^B T^*_b}{B}
$$
>estimates the expectation of the bootstrapped statistics; then
$$
\hat B^* = \bar T^* - T
$$
>is an estimate of the bias of $T$, that is, $T − \theta$. Similarly, the estimated bootstrap variance of $T^∗$,
$$
\hat V(T^*) = \frac{\sum_{b = 1}^B (T^* - \bar T^*)^2}{B - 1}
$$
>estimates the sampling variance of T.
用一個大表格(data.frame)當例子,再抽樣只不過是抽樣過後得到一個新的大表格。這一個新的大表格不是跟原來的
# 一樣大
# 小一點
# 小許多
至於,原來大表格能做的事,當然後來這一個新的大表格也都能做。因為【再抽樣】的目的是【估計統計量的抽樣分配】,所以【再抽樣】這一件事當然必須被重複。
!基本概念
{{{
The population is to the sample
as
the sample is to the bootstrap samples.
}}}
!符號
!!樣本
$$
x_1, x_2, \dots, x_n
$$
!!拔靴樣本
$$
\begin{eqnarray}
x_{11}^*, x_{12}^*, \dots, x_{1n}^* \\
x_{21}^*, x_{22}^*, \dots, x_{2n}^* \\
\vdots \\
x_{B1}^*, x_{B2}^*, \dots, x_{Bn}^* \\
\end{eqnarray}
$$
!!注意事項
上述的$x$可以是[[純量]],也可以是[[向量]]。
<html><div align="center"><iframe src ="http://www.dangoldstein.com/search_r.html" width="100%" align="center" height=400"></iframe></div></html>
!(一)用眼睛加上你的創意仔細觀察你的數據
眼睛採礦[[波士頓的公寓數據|波士頓公寓數據]]
!(二)劃個圖幫助你解答迴歸問題
{{{
}}}
!(三)一種建模的過程:
!!!了解問題
!!!建構問題的統計模型
!!!發展收集數據計畫
!!!學習數據分析工具
!!!建立初模
!!!初模學習階段與回饋
!!!改變、改進、改善初模得到新模
!!!來回上述兩階段數回
!!!定調與試跑
!!!最後定調與起跑
!!!定期維護
!!!...
{{{
假設檢定的一般步驟?
}}}
----
{{{
假設檢定的關鍵步驟?
}}}
!王道
{{{
計算p值。
}}}
!p值是什麼?
$$
P(\hat \beta_1 \ge \hat \beta_{1, observed} | H_0: \beta_1 = 0)
$$
!$H_0$之下的模型
$$
y = \beta_0
$$
!拔靴過程
!!再論拔靴迴歸模型
# +++[預測變數是隨機的]
{{{
Duncan.b = function(data, indices)
{
Data = data[indices,]
coef(lm(prestige ~ income, data=Data))[2]
}
library(boot)
coef.Duncan = boot(Duncan, Duncan.b, 99)
plot(coef.Duncan)
}}}
===
# +++[預測變數是固定的]
{{{
Duncan.model = lm(prestige ~ income, data=Duncan)
X.Duncan = model.matrix(Duncan.model)
fitted.Duncan = fitted(Duncan.model)
e.Duncan = residuals(Duncan.model)
boot.Duncan.fixed = function(data, indices){
y = fitted.Duncan + e.Duncan[indices]
mod <- lm(y ~ X.Duncan - 1)
coef(mod)[2]
}
Duncan.fixed.boot = boot(Duncan, boot.Duncan.fixed, 1999)
Duncan.fixed.boot
plot(Duncan.fixed.boot)
}}}
===
!估計虛無假設下的抽樣分配
# +++[預測變數是隨機的]
{{{
cr = read.table("http://www.ilr.cornell.edu/~hadi/RABE4/Data4/P027.txt", header=T)
cr
plot(cr)
attach(cr)
cr.lm = lm(Minutes ~ Units, data=cr)
abline(cr.lm)
beta0 = coef(cr.lm)[1]
beta1 = coef(cr.lm)[2]
yMx = Minutes - beta1 * Units
cr = cbind(cr, yMx)
cr.boot = function(data, indices)
{
Data = data[indices,]
coef(lm(yMx ~ Units, data=Data))[2]
}
library(boot)
coef.cr = boot(cr, cr.boot, 9999)
plot(coef.cr)
}}}
===
# +++[預測變數是固定的]
{{{
cr = read.table("http://www.ilr.cornell.edu/~hadi/RABE4/Data4/P027.txt", header=T)
cr
plot(cr)
attach(cr)
cr.lm = lm(Minutes ~ Units, data=cr)
X.cr = model.matrix(cr.lm)
e.cr = residuals(cr.lm)
cr = cbind(cr, e.cr)
beta = c(coef(cr.lm)[1], 0)
boot.cr.fixed = function(data, indices){
y = X.cr %*% beta + cr$e.cr[indices]
mod <- lm(y ~ X.cr - 1)
coef(mod)[2]
}
library(boot)
B = 1999
cr.fixed.boot = boot(cr, boot.cr.fixed, R = B)
cr.fixed.boot
plot(cr.fixed.boot)
ASL = sum(abs(cr.fixed.boot) >= coef(cr.lm)[2]) / B
}}}
===
!取得檢定統計量的拔靴分配
$$
H_0: \beta_1 = 0
$$
的檢定統計量?
{{{
你會定義檢定統計量嗎?
}}}
這一份作業將延續各位在[[第二次R實習]]的經驗。作業要求如下:【依舊利用[[law school]]跟[[FG]]這兩組數據!】
# ++++[$t_0$到底是不是$t$分配。]
$$
t_0 = \frac{r \times \sqrt{n - 2}}{\sqrt{1 - r^2}}
$$
{{{
cor.t0 = function(data, index)
{
n = dim(data)[1]
r = cor(data[index,1], data[index,2])
r * sqrt(n - 2) / sqrt(1 - r^2)
}
library(bootstrap)
data(law)
library(boot)
boot.t0.law = boot(law, cor.t0, R = 99)
plot(boot.t0.law)
library(car)
names(boot.t0.law)
n = dim(law)[1]
df = n - 2
qq.plot(boot.t0.law$t, distribution="t", df = df)
}}}
===
# +++[$z$到底是不是$Z$分配。]
$$
z = \left(\frac{1}{2} ln(\frac{1 + r}{1 - r}) - \frac{1}{2} ln(\frac{1 + \rho_0}{1 - \rho_0})\right) \times \sqrt{n - 3}
$$
{{{
cor.Z = function(data, index, rho0)
{
n = dim(data)[1]
r = cor(data[index,1], data[index,2])
a = 0.5 * log((1 + r) / (1 - r))
b = 0.5 * log((1 + rho0) / (1 - rho0))
(a - b) * sqrt(n - 3)
}
library(bootstrap)
data(law)
library(boot)
boot.Z.law = boot(law, cor.Z, R = 99, rho0 = 0)
plot(boot.Z.law)
library(car)
names(boot.Z.law)
qq.plot(boot.Z.law$t, distribution="norm")
}}}
===
# +++[不一樣的$B$會有什麼樣的結局?]
{{{
B = c(99, 999, 9999, 99999)
par(mfrow = c(2,2))
for (i in 1:4)
{
boot.t0.law = boot(law, cor.t0, R = B[i])
qqnorm(boot.t0.law$t)
qqline(boot.t0.law$t)
}
for (i in 1:4)
{
boot.Z.law = boot(law, cor.Z, R = B[i], rho0 = 0)
qqnorm(boot.Z.law$t)
qqline(boot.Z.law$t)
}
}}}
===
# +++[如果每一次拔靴抽樣不一定要重抽到$n$(樣本數)個觀察值,那麼重抽幾個的影響如何?]
{{{
B = 99
n = dim(law)[1]
par(mfrow = c(1,2))
for (k in 1:2)
{
less.n = round(n/k, 0)
t0 = numeric(B)
for(i in 1:B)
{
re.law = law[sample(n, less.n, replace = TRUE),]
r = cor(re.law[,1], re.law[,2])
t0[i] = r * sqrt(less.n - 2) / sqrt(1 - r^2)
}
qqnorm(t0)
qqline(t0)
}
}}}
===
# +++[自創一種【再抽樣演算法】,並且為你的演算法提供各種可能的觀察?]
{{{
OPEN QUESTION! 說不定你可以用加權的方式凸顯每一筆數據不一樣的重要性!
}}}
===
# 從書上再找【兩組】數據,看一看是不是跟[[law school]]跟[[FG]]+++[這兩組數據的經驗雷同?]
{{{
第一組數據可以考慮第一次作業的數據。
}}}
===
# ++++[關於模擬比賽的預測以及戰績]
{{{
為了觀察以及了解各組的進度
也為了最後的評分
請各組補寄(沒有寄的組別)每一次預測的詳細報告(有模型的細節)以及最後戰績
到我的
cwu@fcu.edu.tw
跟
jungpinwu@gmail.com
}}}
===
# 關於++++[模擬比賽]
{{{
一定要嘗試一次迴歸模型(不論是簡單還是複迴歸模型)!!!
每一種網頁上的R指令都要用過一次!!!
}}}
===
# 關於++++[賽事]
{{{
各位
我仔細翻閱各組的企劃書
發現
有一組未提供賽事的時間表
有一組只提供模擬比賽的時間表
根據各組目前提供的時間表
我做了以下的更動
1. 模擬比賽的時間縮短了,
從原本的一個月縮短為三週。
從12/01到12/22。
各組請下次上課前提供新的賽程表。
我只需要東西區各5場,老師不管各組私底下的模擬測試。
2. 正式比賽的時間變長。
從12/25到隔年的01/07。
東西區各4場,而且必須是主場2場、客場2場。
一樣請下次上課前提供新的賽程表。
3. 假如這樣新的時間安排,你有任何問題。
比如說,找不到賽事,請立即電郵通知老師,並且告訴我比賽期限應該延至哪一天。
}}}
===
# 關於++++[比賽規則]
{{{
按照舊規則進行模擬比賽。
}}}
===
# [[期末報告]]有修正。
# ++++[期末報告的名稱]
{{{
就是愛Win動
NBA運彩勝負分析與預測
}}}
===
# @@[[第三次作業|我要比較各式各樣的拔靴信賴區間]]已經更新。@@
# [[數據分析初論]]已經更新。
# +++[我要聽嬤嬤歌]
<html><div align="center"><object width="425" height="344"><param name="movie" value="http://www.youtube.com/v/tfyyGQxZrAY&hl=zh_TW&fs=1&"></param><param name="allowFullScreen" value="true"></param><param name="allowscriptaccess" value="always"></param><embed src="http://www.youtube.com/v/tfyyGQxZrAY&hl=zh_TW&fs=1&" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="425" height="344"></embed></object></div></html>
===
# @@一定要收集的數據,請持續收集到比賽結束:[[北富銀運彩http://www.i-win.com.tw/提供的數據]]。(如果我的iPhone給我的資訊沒錯,一天有五場比賽,預計收集70天以上,所以各位收集筆數的目標定在200筆。這一項努力列入[[海報]]之必要考核項目。)@@
# 網站已陸續更新,請各位參考。
# 請注意[[第二次作業|更多拔靴法的體驗]]預定【10/29】繳交。
# 考量期末報告的份量,已經修改本學期的[[評比計畫|迴歸分析評量計畫]]。
# 請[[徐瑜彬]]、[[賴詩婷]]、[[林辰昇]]、[[康君豪]]等人把上週繳交之作業的R程式碼,郵寄到我的信箱。
# [[期末報告]]最新時間表已經上線了。請注意。
# 請密切注意[[第二次作業|更多拔靴法的體驗]]公佈時間。
# [[第一次作業|恰弟與哈弟第47頁練習題2-10]]預定【10/15】繳交。
# 為什麼[[FG]]數據的散佈圖裡,少了一個人的直線?
# 今天交代的功課,利用拔靴法了解[[law school]]跟[[FG]]這兩組數據之$t_0$以及$Z$的抽樣分配?
# 請回答[[相關分析]]的[[5w1h]]?
# 本週(上週)功課:[[FG]]數據的散佈圖裡,每一條直線代表哪一位踢球員?
<html><div align="center"><br><a href="http://www.maploco.com/view.php?id=3561409"><img border=0 src="http://www.maploco.com/vmap/3561409.png" alt="Visitor Map"></a><br><a href="http://www.maploco.com/">Create your own visitor map!</a><br></div></html>
[[用R學迴歸分析之學習大綱]]
[[學習R]]
[[一項小研究]]
----
[[同儕互評機制]]
[[比賽規則]]
[[期末報告評分機制]]
[[NBA官網|http://www.nba.com/]]
!時間表
| 階段 | 期限(週次) | 工作項目 | 工作內容 | 備註 | Done |
| 0 | 10/01 ~ 12/01 | 研擬比賽規則 | 討論一套可行且公平、客觀的[[比賽規則]] || YES |
|~| 10/01 ~ 11/01 | 提企劃書 | 寫一份如何完成[[任務|學期任務]]的計畫書 | 請上[[官網|http://www.nba.com/]]挑隊伍以及決定試跑與決戰的日子,並且把這一份表格放在企劃書的附錄。 ||
|~| !11/01 | 繳交企劃書 | 請把你們那一組的企劃書郵寄到jungpinwu@gmail.com以及cwu@fcu.edu.tw | 每逾期ㄧ天扣(總分45分)一分 ||
|~| 10/08 | 收集資訊 | 吳老師提供一篇文章 | [[12/03小考]] | YES |
| 1 | 10/01 | 學習R | 認養兩個[[迴歸分析的相關函示]]之子項目 || YES |
|~| 11/26 |~| 上傳[[使用說明]]到BB |||
|~| 11/27 ~ 11/30 |~| 互評[[使用說明]] | [[同儕互評機制]] ||
|~| 11/30 |~| 繳交第一階段[[使用說明]]同儕互評的紙本報告 | 郵寄到jungpinwu@gmail.com以及cwu@fcu.edu.tw ||
|~| 12/03 |~| 學術審查[[使用說明]] | [[教師評論]] ||
|~| 12/03 ~ 12/10 |~| 修改BB上的[[使用說明]] |||
|~| 12/10 |~| 報告各組的[[使用說明]] | 上課時間以[[正式方式|我要報告使用說明]]報告 ||
|~| 12/10 ~ 12/11 |~| 第二次互評(再一次打分數。但是不再進行紙本攻防。) | 把結果郵寄到jungpinwu@gmail.com以及cwu@fcu.edu.tw ||
| 1.5 | 12/26 ~ | 寫程式 ||||
| 2 | ~ 10/08 | 收集資料 | 為棒球運彩數據編碼並且建檔 | 上BB互相觀摩 ||
|~||~| !收集[[北富銀運彩http://www.i-win.com.tw/提供的數據]] |||
|~||~| 持續收集有用的數據 |||
|~||~| 各組持續祕密收集對自己有用的數據 |||
|~||~| 收集並且深入了解各種迴歸模型 | 與學習R之互評[[使用說明]]同時進行 ||
| 3 || 模擬比賽 | 根據[[比賽規則]]模擬比賽 |||
|~| 12/01 ~ 12/31 |~| 試跑一個月 | 期間可以不斷修改或是變換預測模型 ||
| 4 || 比賽開始 | 根據[[新的比賽規則]]開始比賽 | 如果沒有出現[[新的比賽規則]],就根據舊的比賽規則論斷得分。 ||
|~| 01/01 ~ 01/07 |~| 一週決勝負 | 決戰期間不能修改預測模型 ||
| 5 | 01/08 ~ 01/14 | 成果展示 | 製作整個建模過程的海報。【[[海報必要內容]]】另訂之。 |||
|~| 01/14 |~| 學術審查海報內容 |||
|~| 01/15 |~| 互評海報內容 |||
|~| 01/15 ~ 01/22 |~| 公開展示海報 | [[展示地點]]另訂之 ||
!成份以及比例
# (20%)[[使用說明]]
## (15%)同儕互評
### (2%)BB初評(幾顆星 *2%)
### (5%)單項初評(加總單項得分 *5%)
### (8%)複評(幾顆星 *8%)
## (5%)學術審查
# (60%)決戰期間的++++[得分消長]
# (10%)第一場得分被等分隔為四等分,第一等分4分,第二等分5分,第三等分7分,第四等分10分。
# (10%)第二場得分被等分隔為四等分,第一等分4分,第二等分5分,第三等分7分,第四等分10分。
# (10%)第三場得分被等分隔為四等分,第一等分4分,第二等分5分,第三等分7分,第四等分10分。
# (10%)第四場得分被等分隔為四等分,第一等分4分,第二等分5分,第三等分7分,第四等分10分。
# (10%)第五場得分被等分隔為四等分,第一等分4分,第二等分5分,第三等分7分,第四等分10分。
# (10%)第六場得分被等分隔為四等分,第一等分4分,第二等分5分,第三等分7分,第四等分10分。
# (加分部份)得分之總積分
## 達36分者,加10分。
## 達30 ~ 35分者,加7分。
## 達27 ~ 29分者,加5分。
===
# (20%)海報。@@採公開展示的方式,接受同學、學長姐、老師們的評頭論足。@@
## (5%)同學與學長姐的【同儕互評】(幾顆星 *5%)
## (15%)【授課老師】的【學術審查】(幾顆星 *15%)
<html><div align="center"><iframe src ="http://www.r-project.org/" width="100%" align="center" height=400"></iframe></div></html>
{{{
kicker <-read.csv("http://www.stat.tamu.edu/~sheather/book/docs/datasets/FieldGoals2003to2006.csv",header=T)
attach(kicker)
X <- kicker$FGtM1
Y <- kicker$FGt
N <- kicker$Name
formula <- lm(Y~N+X,data=kicker)
formula
coef_length <- length(formula$coef)
x_lab <- "Field Goal Percentage in Year t-1"
y_lab <- "Field Goal Percentage in Year t"
main_name <- paste("Slope of each line = ",summary(formula)$coef[coef_length])
plot(X,Y,main=main_name,xlab=x_lab,ylab=y_lab)
fix <- seq(65,95,length=coef_length-2)
slope <- summary(formula)$coef[coef_length]
intercept <- summary(formula)$coef[1]
abline(intercept,slope,col=2)
text(100,100*slope+intercept,"AVG",font=4,col=2,pos=4)
for(i in 2:(coef_length-1)){
intercept <- c(intercept,intercept[1]+summary(formula)$coef[i])
abline(intercept[i],slope,col=1)
text(fix[i-1],fix[i-1]*slope+intercept[i],row.names(summary(formula)$coef)[i],font=2,col=1,pos=4)
}
}}}
| 週次 | 時間 | 實錄 | 備註 |
| 10 | 11/26 |登記每一組模擬比賽的日期與時間、R Session、現場寫比賽用的程式。 ||
| 11 | 12/3 |文章【basketball】小考、更改每一組賽事的台灣時間、模擬比賽的新要求、每一組現場傳只有比數的資料給我、每一組傳他們現有的資料給我、討論建模策略、討論各組的使用說明加上我的註記、到底迴歸模型可以用來預測?、學長報告MLE相關的R函示(15分鐘)、討論如何設定解釋變數(在那裡預測)? ||
| 12 | 12/10|||
| 13 | 12/17 |||
| 14 | 12/24 |||
| 15 | 12/31 |||
| 16 | 01/07 |||
| 17 | 01/14 |||
# 各分區(東區跟西區)挑選一隊。採BB登記制。先註冊先贏。【@@請提前註冊你的隊伍。@@】
# 試跑一個月。試跑期間必須練習10場,東西區各5場。【@@請提供你的試跑日期、台灣比賽時間、客隊名稱。@@】
# 試跑期間,用[[試跑戰績報告方式]]透過BB提報成果。
# 決戰期間預測每一場的每一種玩法(如下所示)。決戰期間必須預測6場,東西區各3場。【@@請提供你的決戰日子、台灣比賽時間、客隊名稱。@@】
# 決戰期間,用[[決戰戰績報告方式]]透過BB提報成果。
# 不論試跑或是決戰期間,比賽@@前12個小時@@用[[預測報告方式]]透過BB提報預測結果。包含@@你的隊伍(註冊成功的投注隊伍)讓分幾分?與勝分差區間?以及根據各種玩法輸贏的結果@@
# 不論試跑或是決戰期間,比賽結束@@後12個小時@@,透過BB提報戰果。
# 不投注。請注意我在[[一項小研究]]所定下的規矩。
# 每一場都要參加[[北富銀運彩|http://www.i-win.com.tw/]]提供的方式:
## [[讓分|http://www.i-win.com.tw/tsl/sites/cms_admin/cms/tsl_guide/approved/guide1_1208323621359.html]](至少3分)
## [[不讓分|http://www.i-win.com.tw/tsl/sites/cms_admin/cms/tsl_guide/approved/guide1_1235697123094.html]]
## [[勝分差|http://www.i-win.com.tw/tsl/sites/cms_admin/cms/tsl_guide/approved/guide1_1208323944609.html]]
# 用以下的權數加權上述的玩法。
## 贏【讓分】一次加2分
## 贏【不讓分】一次加1分
## 贏【勝分差】一次加3分
# 計分方式:
$$
\mbox{讓分贏一次} \times \mbox{2分} + \mbox{不讓分贏一次} \times \mbox{1分} + \mbox{勝分差贏一次} \times \mbox{3分}
$$
用以下格式把過程與結果放在BB:針對各組註冊的兩隊,提供
# 【讓分預測】以及【讓分預測機制】。
# 【勝分差區間預測】以及【勝分差區間預測機制】。
# 三種【預測比賽輸贏的模型】。分別針對【讓分】、【不讓分】、【勝分差】三種玩法。
# 三項預測結果。
# 提供一個類似北富銀運彩的投注表格。
# 根據[[比賽規則]]的計分公式得多少分數。
<<tabs "" [[原始數據]] "" [[波士頓公寓數據(I)]][[認識波士頓公寓數據]] "" [[波士頓公寓數據(II)]][[眼睛採礦]] "" [[波士頓公寓數據(III)]]>>
| 編號 | 要價 | 房間數 | 朝向 | 洗衣機/乾衣機 | 暖氣 |
| 1 | 168,000 | 2 | E | Y | Y |
| 2 | 152,000 | 2 | N | N | Y |
| 3 | 187,000 | 3 | N | Y | Y |
| 4 | 142,500 | 1 | W | N | N |
| 5 | 166,800 | 2 | W | Y | N |
| 編號 | 要價 | 房間數 | 朝向 | 洗衣機/乾衣機 | 暖氣 |
| 1 | 168,000 | 2 | E | Y | Y |
| 2 | 152,000 | 2 | N | N | Y |
| 3 | 187,000 | 3 | N | Y | Y |
| 4 | 142,500 | 1 | W | N | N |
| 5 | 166,800 | 2 | W | Y | N |
請問:
# 有哪些變數?
# 這些變數都是「可量測的」嗎?
# 有哪些屬量變數?
# 有哪些屬性變數?
# 哪些變數是連續的?
# 哪些變數是離散的?
# 有哪些名目尺度的變數?
# 有哪些順序尺度的變數?
# 有哪些區間尺度的變數?
# 有哪些比例尺度的變數?
# 「朝向」這個變數有哪些選項?
# 「要價」這個變數有哪些選項?
!!請用眼睛採礦(挖覺)【波士頓公寓數據】,找出這些數據之間的關係。
| 編號 | 要價 | 房間數 | 朝向 | 洗衣機/乾衣機 | 暖氣 |
| 1 | 168,000 | 2 | E | Y | Y |
| 2 | 152,000 | 2 | N | N | Y |
| 3 | 187,000 | 3 | N | Y | Y |
| 4 | 142,500 | 1 | W | N | N |
| 5 | 166,800 | 2 | W | Y | N |
+++[參考答案]
# 公寓的要價隨著房間數的增加而增高
# 有洗\乾衣機的公寓要價都在160,000以上,而且都比沒有洗\乾衣機的公寓高;即表示有無洗\乾衣機會影響公寓的要價
# 公寓的朝向對於要價並無影響
# 朝向西的公寓均無暖氣,因此推論西向的公寓對於暖氣的需求較低
# 有無洗\乾衣機對於公寓要價的影響大於有無暖氣對於公寓要價的影響
===
{{{
美編不能少。
}}}
# 標題
# 作者群、指導教授
# 數據(部份)
# 建模策略
# 最後定調的預測模式
# 預測成果
# 參考文獻
整個學習只有短短四個月,絕不可能【教你天又教你地】。我們只能學到一些片段,當然你我絕不希望最後的學習成果流於[[瞎子摸象]],但是這也可能【說比做容易】。為了激發各位的學習鬥志與營造健康的學習氣氛,老師決定用比賽的方式進行本學期的迴歸分析課。針對這一項學習活動,本學期的四個月大概被分成以下這四階段:
# [[學習R]]
# 尋找比賽相關的數據
# 模擬比賽
# 比賽開始
最後以各組在【預測NBA賽事】得分多寡論排名,加上他組對各組第一階段([[學習R]])之【合作態度】的評價,引用[[期末報告評分機制]]決定各組的得分(總分45分)。
至於其他學習活動,將被大概分成
# +++[Linear Model]===
# +++[Ridge Regression]===
# +++[Segmented Regression]===
# +++[Generalized Least Squares]===
# +++[Generalized Linear Models]===
# +++[Nonlinear Least Squares]===
# +++[Generalized Nonlinear Least Squares]===
# +++[Loess Regression]===
# +++[Splines Regression]===
# +++[Robust Regression]===
# +++[Structural Equation Models]===
# +++[Simultaneous Equation Estimation]===
# +++[Partial Least Squares Regression]===
# +++[Principal Component Regression]===
# +++[Quantile Regression]===
# +++[Linear and Nonlinear Mixed Effects Models]===
# +++[Generalized Additive Models]===
# +++[Classification and Regression Trees]===
# +++[Beta Regression]===
{{{
fit.2 <- lm(FGt ~ Name + FGtM1,data=kicker)
fit.2
}}}
+++[R輸出]
{{{
> fit.2
Call:
lm(formula = FGt ~ Name + FGtM1, data = kicker)
Coefficients:
(Intercept) NameDavid Akers NameJason Elam
126.6872 -4.6463 -3.0167
NameJason Hanson NameJay Feely NameJeff Reed
2.1172 -10.3737 -8.2955
NameJeff Wilkins NameJohn Carney NameJohn Hall
2.3102 -5.9774 -8.4865
NameKris Brown NameMatt Stover NameMike Vanderjagt
-13.3598 8.7363 4.8955
NameNeil Rackers NameOlindo Mare NamePhil Dawson
-6.6200 -13.0365 3.5524
NameRian Lindell NameRyan Longwell NameSebastian Janikowski
-4.8674 -2.2315 -3.9763
NameShayne Graham FGtM1
2.1350 -0.5037
}}}
===
{{{
par(mfrow = c(1, 4))
plot.lm(cr.lm)
plot(cr.lm, which = 1:4)
par(mfrow = c(3, 2))
plot(cr.lm, which = 1:6)
par(mfrow= c(1, 1))
}}}
!理論
$$
r = \frac{\sum (x - \bar x) \times (y - \bar y)}{\sqrt{\sum (x - \bar x)^2} \times \sqrt{\sum (y - \bar y)^2}}
$$
!R
>cor() compute the correlation of x and y if these are vectors. If x and y are matrices then the correlations between the columns of x and the columns of y are computed.
{{{
var(x, y = NULL, na.rm = FALSE, use)
cov(x, y = NULL, use = "everything", method = c("pearson", "kendall", "spearman"))
cor(x, y = NULL, use = "everything", method = c("pearson", "kendall", "spearman"))
cov2cor(V)
}}}
+++[R如何計算相關係數?]
{{{
function (x, y = NULL, use = "everything", method = c("pearson",
"kendall", "spearman"))
{
na.method <- pmatch(use, c("all.obs", "complete.obs", "pairwise.complete.obs",
"everything", "na.or.complete"))
method <- match.arg(method)
if (is.data.frame(y))
y <- as.matrix(y)
else stopifnot(is.atomic(y))
if (is.data.frame(x))
x <- as.matrix(x)
else {
stopifnot(is.atomic(x))
if (!is.matrix(x)) {
if (is.null(y))
stop("supply both 'x' and 'y' or a matrix-like 'x'")
x <- as.vector(x)
}
}
if (method == "pearson")
.Internal(cor(x, y, na.method, FALSE))
else if (na.method != 3L) {
Rank <- function(u) {
if (length(u) == 0L)
u
else if (is.matrix(u))
apply(u, 2L, rank, na.last = "keep")
else rank(u, na.last = "keep")
}
x <- Rank(x)
if (!is.null(y))
y <- Rank(y)
.Internal(cor(x, y, na.method, method == "kendall"))
}
else {
if (is.null(y)) {
ncy <- ncx <- ncol(x)
if (ncx == 0)
stop("'x' is empty")
r <- matrix(0, nrow = ncx, ncol = ncy)
for (i in seq_len(ncx)) {
for (j in seq_len(i)) {
x2 <- x[, i]
y2 <- x[, j]
ok <- complete.cases(x2, y2)
x2 <- rank(x2[ok])
y2 <- rank(y2[ok])
r[i, j] <- if (any(ok))
.Internal(cor(x2, y2, 1L, method == "kendall"))
else NA
}
}
r <- r + t(r) - diag(diag(r))
rownames(r) <- colnames(x)
colnames(r) <- colnames(x)
r
}
else {
if (length(x) == 0 || length(y) == 0)
stop("both 'x' and 'y' must be non-empty")
matrix_result <- is.matrix(x) || is.matrix(y)
if (!is.matrix(x))
x <- matrix(x, ncol = 1L)
if (!is.matrix(y))
y <- matrix(y, ncol = 1L)
ncx <- ncol(x)
ncy <- ncol(y)
r <- matrix(0, nrow = ncx, ncol = ncy)
for (i in seq_len(ncx)) {
for (j in seq_len(ncy)) {
x2 <- x[, i]
y2 <- y[, j]
ok <- complete.cases(x2, y2)
x2 <- rank(x2[ok])
y2 <- rank(y2[ok])
r[i, j] <- if (any(ok))
.Internal(cor(x2, y2, 1L, method == "kendall"))
else NA
}
}
rownames(r) <- colnames(x)
colnames(r) <- colnames(y)
if (matrix_result)
r
else drop(r)
}
}
}
<environment: namespace:stats>
}}}
===
!第一版
{{{
library(bootstrap)
law
plot(law[,1], law[,2])
boot.law.cor = function(data, indices)
{
data = data[indices,]
cor(data$LSAT, data$GPA)
}
library(boot)
law.boot = boot(law, boot.law.cor, 2999)
plot(law.boot)
}}}
!第二版
{{{
library(boot)
boot.FG.cor = function(data, indices)
{
data = data[indices,]
cor(data$FGt, data$FGtM1)
}
FG.boot = boot(kicker, boot.FG.cor, 1999)
plot(FG.boot)
system.time(boot(kicker, boot.FG.cor, 1999))
}}}
<html><div align="center"><iframe src ="http://www.cyclismo.org/tutorial/R/index.html" width="100%" align="center" height=400"></iframe></div></html>
* Downloading and installing R in Windows (下載與安裝)
* The R graphical user interface(R使用者介面)
* Viewing the graphics demo(示範作圖)
* Vectors and basic stats(向量與基礎統計量)
* Simple plotting(簡單作圖)
+++[用了那些對話]
{{{
demo(graphics)
x=c(1,2,3,4,5,6,7)
y=c(10,14,20,18,16,15,10)
x+y
z=c(x,y)
sum(y)
mean(y)
sd(y)
plot(x,y)
barplot(y,col=”lightgreen”)
}}}
===
----
<html><div align="center"><iframe src ="http://www.dangoldstein.com/flash/Rtutorial1/Rtutorial1.html" width="100%" align="center" height=700"></iframe></div></html>
利用以下幾個函示,要求R把[[踢球員]]的名字打在圖上:
{{{
locator()
text()
}}}
<html><div align="center"><iframe src ="http://mercury.bio.uaf.edu/mercury/R/R.html" width="100%" align="center" height=400"></iframe></div></html>
* Tricking R into starting in your working directory(工作區域)
* Reading in text files(讀入數據)
* Accessing columns in data frames(讀取2維表格的「行」)
* Creating histograms(產生直方圖)
* Side effects and optional parameters of function calls(副作用與選用參數)
* Fitting simple linear models(簡單線性模型)
* Adding least-squares and loess lines to plots(為二維散佈圖補最小平方法跟loess線)
+++[用了那些對話]
{{{
myData=read.table(”pedometer.csv”, header=TRUE, sep=”,”)
x=hist(myData$Steps,col=”lightblue”)
x=hist(myData$Steps,breaks=20,col=”lightblue”)
plot(myData$Steps ~ myData$Observation,col=”blue”)
myModel=lm(myData$Steps ~ myData$Observation)
summary(myModel)
lines(fitted(myModel))
lines(fitted(loess(myModel)),col=”red”)
}}}
===
----
<html><div align="center"><iframe src ="http://www.dangoldstein.com/flash/Rtutorial2/Rtutorial2.html" width="100%" align="center" height=700"></iframe></div></html>
利用拔靴法,為[[law school]]跟[[FG]]這兩組數據,發現以下之$t_0$以及$z$的抽樣分配:
$$
t_0 = \frac{r \times \sqrt{n - 2}}{\sqrt{1 - r^2}}
$$
而
$$
z = \left(\frac{1}{2} ln(\frac{1 + r}{1 - r} - \frac{1}{2} ln(\frac{1 + \rho_0}{1 - \rho_0})\right) \times \sqrt{n - 3}
$$
<html><div align="center"><iframe src ="http://www.microsiris.com/Statistical%20Decision%20Tree/" width="100%" align="center" height=400"></iframe></div></html>
<html><div align="center"><iframe src ="http://en.wikipedia.org/wiki/R_(programming_language)" width="100%" align="center" height=400"></iframe></div></html>
<html><div align="center"><iframe src ="http://rlab.tiddlyspot.com/" width="100%" align="center" height=400"></iframe></div></html>
當我們針對一件事物而產生一個名詞的時候,在使用之初並不會出現太大問題,因為使用這個名詞的人,都是當初產生這個名詞的同一群人,他們都能深刻瞭解這個名詞的意涵為何。但是,當這個名詞逐漸向外擴散傳播,其他部門的成員,甚至公司以外的人士也開始使用這個名詞的時候,由於每經過一次傳播,這個名詞被解讀到的意涵就有可能產生些微偏離,於是問題就漸漸出現了。
一般人經常犯的毛病是「見詞生義」,看到或聽到一句話時,僅僅從字面上單純的意思去認知,並且在未經深思熟慮的情況下,便立即對此做出反應,結果往往導引到錯誤的觀念與結果。這種情形在組織運作,乃至於日常生活或社會現象方面,可謂屢見不鮮。
以公司同仁所熟知的「DIY市場」為例,由於聯強有許多產品是供應到「DIY市場」,所以說這是個同仁所「熟知」的名詞,但到底什麼是「DIY市場」?相信絕大部分同仁的答案會是「消費者自行組裝電腦的市場」,這從字面上來看,似乎沒有錯誤。但從實際的狀況來看,絕大部分的情形是「經銷商幫消費者組裝電腦的市場」,真正是消費者自己組裝電腦的比例,其實非常非常低,即使是極少數玩家級、具有自行組裝能力的使用者,也不盡然是自行組裝。如果我們只是「見詞生義」,看到、聽到一個名詞,以為每一個字都懂就表示真正瞭解這個名詞,那麼,將很容易產生錯誤的判斷與結果,而最可怕的錯誤,便是這種「以為已經瞭解,實際上卻不瞭解」的情形。
在管理方面,也常因為一般人的「見詞生義」而導致許多管理學上的名詞被誤解,導致組織運作上的諸多錯誤與偏差。十多年前,管理學開始在國內成為顯學之際,「人性化管理」便被奉為企業管理的圭臬。從字面上來看,「人性化管理」的意思是「管理要符合人性、要尊重個人」,這樣的說法並沒有錯,但常常出現的情形是,部分員工並未深度瞭解其內涵,反而擴大解釋為「管理上的一切作法均應順著個人的喜好,並以此出發來從事組織管理上的設計」,例如:彈性上下班時間、上班可以不拘形式、不修邊幅,甚至公司的政策要「完全」說服員工、要每一位員工都同意才可以實施等等,因而產生許多態度上的偏差與誤解。
再拿現代人相當重視的「創意」來講,許多人誤以為「創意」便是代表著一個沒有任何限制、可以完全自由發揮的空間。但是落實到企業經營來看,公司的一切作法都是架構在基本政策與市場策略之下,因此,「創意」的發揮應該是座落在公司定位的範疇之內,並且要考慮到公司所處時空環境下所具備的條件與成本因素,絕非任何漫無邊境、天馬行空的想法,都應該去嘗試。
又好比說「授權」,一般人見詞生義的結果,會以為主管將一件事情「授權」給部屬,便表示把這件事情完全交由部屬處理而不再過問。但實際上,「授權」是一種漸進的過程(詳見「授權與受權」主題),如果不能深度地瞭解此一名詞的意涵,則主管很可能授權之後便忽略了還要持續關照一段時間,結果導致運作出了問題;部屬也很容易誤以為獲得主管授權之後,便要一肩扛下所有問題,或是誤以為主管授權之後還不斷前來關照,是一種「不夠授權」的情形。
「見詞生義」的毛病除了針對一個名詞只從字面上去瞭解,而不深究其內涵,導致無法真正地瞭解「什麼是什麼?」之外,進一步延伸擴大,則是一個人對於事物只從表面上去認知,並且因此導致判斷的偏差,甚至決策上的錯誤。
由於一般人習慣用當時絕大多數人共同的認知來解釋所有的現象,往往使得所有問題歸咎出來的原因都一樣。例如,檢討某項業務的業績不佳時,最常被提出來的理由是「淡季」,以此解釋業績不佳是最不容易招致反駁的說法。然而,這樣反射性的思考卻隱藏著不少危險,或許當時的確是市場的淡季,但不同業務領域的淡旺季不見得同步,如果所有業績不好的原因都認為是「淡季」,那麼,真正的問題將因此無法被發覺出來。
上述種種現象其實都因為表面、淺薄的思考所導致,亦即直接從字面上粗略的字義去聯想、揣測一個詞句的意涵,並就此加以認定,於是導引到錯誤的觀念與作法。事實上,一個人聽到一個名詞,看到一個現象,如果能夠多花一點時間思考,便能避免掉這樣的缺失。從另一方面來看,很多人汲汲營營地去學習各種管理理論,但如果對於各種管理學名詞「見詞生義」,在未深入思考領悟的情況下便貿然應用在企業經營上,則不僅浪費資源而難見成效,且很可能導致反效果,未蒙其利、先受其害。
!從公式你看到什麼?
+++[答案]
{{{
樣本變異數是一種歐氏距離的函數。
}}}
===
用以下格式把過程與結果放在BB:針對各組註冊的兩隊,提供
# 【讓分預測】以及【讓分預測機制】。
# 【勝分差區間預測】以及【勝分差區間預測機制】。
# 三種【預測比賽輸贏的模型】。分別針對【讓分】、【不讓分】、【勝分差】三種玩法。
# 以上三項工作成果,在試跑期間基於各組隱私,這一個部份不上BB。請把每一次的預測模型郵寄到我的信箱(jungpinwu@gmail.com、cwu@fcu.edu.tw)。
# 三項預測結果。
# 提供一個類似北富銀運彩的投注表格。
# 根據[[比賽規則]]的計分公式得多少分數。
$$
y_{ij} \sim^{iid} N(\beta_0 + \beta_1 x_i, \sigma^2)
$$
{{{
setwd("C:/Users/sheather.ADSTAT/Documents/docs/AModernApproachToRegression/Data")
kicker <- read.csv("FieldGoals2003to2006.csv",header=T)
attach(kicker)
}}}
+++[另一種選擇]
{{{
kicker <- read.csv("http://www.stat.tamu.edu/~sheather/book/docs/datasets/FieldGoals2003to2006.csv",header=T)
attach(kicker)
}}}
===
{{{
> apropos('.*\\.lm$')
[1] "anova.lm" "anovalist.lm" "CC.lm" "cr.lm" "hatvalues.lm"
[6] "kappa.lm" "model.frame.lm" "model.matrix.lm" "plot.lm" "predict.lm"
[11] "print.lm" "residuals.lm" "rstandard.lm" "rstudent.lm" "summary.lm"
}}}
圖1-1是[[kicker$FGt]]跟[[kicker$FGtM1]]的一種[[散佈圖]]。
手邊有這麼一張[[散佈圖]],你想玩什麼遊戲?讓你可以進一步了解這麼一張[[散佈圖]]。
先了解手邊這一張[[散佈圖]]。我常利用一連串的Q&A,幫助自己了解我的對象:
# 這是一張二維平面上怖滿圓點(實際上是圓圈)的圖。
# [[x軸]]叫做【】。
# [[y軸]]叫做【】。
# [[x軸]]的範圍是【】。
# [[y軸]]的範圍是【】。
# [[x軸]]的意義是什麼?
# [[y軸]]的意義是什麼?
統計學家會問什麼問題?
美式足球專家會問什麼問題?
那你會問什麼問題?
# 去年FG低過平均,今年FG會低過平均嗎?
# 去年FG低過平均,今年FG會高過平均嗎?
# 去年FG高過平均,今年FG會低過平均嗎?
# 去年FG高過平均,今年FG會高過平均嗎?
或是
# 去年哪幾位踢球員(kicker)的FG低過平均,他們今年的FG是不是依舊低過平均?
# 去年哪幾位踢球員(kicker)的FG低過平均,他們今年的FG是不是不再低於平均?
# 去年哪幾位踢球員(kicker)的FG高過平均,他們今年的FG是不是依舊高過平均?
# 去年哪幾位踢球員(kicker)的FG高過平均,他們今年的FG是不是不再高於平均?
或是
# 第一名的踢球員是誰?
# 最後一名的踢球員是誰?
或是
# 如何決定一份踢球員的表現排行榜?
或是
# 去年的FG可以預測今年的FG嗎?
或是
# 我想用去年某一位踢球員的FG預測他今年的FG?
# 我想用去年所有踢球員的FG預測某一位踢球員今年的FG?
# 我想用去年某一位踢球員的FG預測另一位踢球員今年的FG?
或是
# 去年踢球員的FG跟今年踢球員的FG有著線性相關嗎?
!參考資料
# [[R functions for regression analysis|http://www.google.com.tw/url?sa=t&source=web&ct=res&cd=1&ved=0CAkQFjAA&url=http%3A%2F%2Fcran.r-project.org%2Fdoc%2Fcontrib%2FRicci-refcard-regression.pdf&ei=-QrESrWsMYmi6QPuyOHSDQ&usg=AFQjCNFcIuL0sf24qEExClBU0zgGiwSdDQ&sig2=7FPV-LJCJOpBGIlLrUZT2Q]]
----
!分項
# +++[Linear Model]===
# +++[Variables Selection]===
# +++[Diagnostics]===
# +++[Graphics]===
# +++[Tests]===
# +++[Variables Transformations]===
# +++[Ridge Regression]===
# +++[Segmented Regression]===
# +++[Generalized Least Squares]===
# +++[Generalized Linear Models]===
# +++[Nonlinear Least Squares]===
# +++[Generalized Nonlinear Least Squares]===
# +++[Loess Regression]===
# +++[Splines Regression]===
# +++[Robust Regression]===
# +++[Structural Equation Models]===
# +++[Simultaneous Equation Estimation]===
# +++[Partial Least Squares Regression]===
# +++[Principal Component Regression]===
# +++[Quantile Regression]===
# +++[Linear and Nonlinear Mixed Effects Models]===
# +++[Generalized Additive Models]===
# +++[Survival Analysis]===
# +++[Classification and Regression Trees]===
# +++[Beta Regression]===
| 項目 | 日期 | 工作 | 備註 |
| 1 | 10/15 | [[第一次作業|恰弟與哈弟第47頁練習題2-10]] ||
| 2 | 10/29 | [[第二次作業|更多拔靴法的體驗]] ||
| 3 | 11/01 | 繳交[[期末報告]]企劃書 ||
| 4 | 11/05 | [[期中考(上機考R)]] | 請班代借樓下電腦教室 |
|~|~| [[第三次作業|我要比較各式各樣的拔靴信賴區間]] ||
| 5 | 11/26 | 上傳使用說明到BB | 順便寄一份到我的信箱 |
| 6 | 11/27~11/30 | 互評使用說明 | 11/30繳交第一階段使用說明同儕互評的紙本報告(寄到我的信箱) |
| 7 | 12/03 | 學術審查使用說明 | |
# [[隨堂考]](10%):因為這是考驗你課堂上是否專心、聽得懂不懂,所以比例不高(採外加方式)。
## [[第一次隨堂考|]]
## [[第二次隨堂考|]]
## (10%)[[12/03小考]]
# [[家庭作業]](30%)(請注意[[家庭作業基本要求]]以及[[家庭作業評分方式]]。)
## (10%)[[第一次家庭作業|恰弟與哈弟第47頁練習題2-10]]
## (10%)[[第二次家庭作業|體驗拔靴法]]
## (10%)[[第三次家庭作業|更多拔靴法的體驗]]
# [[期中考(上機考)|2009年期中考]](10%)
# [[期末考(上機考)|2009年期末考]](15%):上述這四次考試的比例越來越高,除了表示它們彼此之間的重要性,也希望你慢慢上手,分數可以越來越高!
# [[期末報告]](45%):[[一項小研究]]。兩人一組。
!考試題目可能來自
# 課程用書
# 課程網頁
# [[家庭作業]]
# R實習
# 老師的創意
| 週次 | 日期 | 大綱 | 內容 | 課堂活動 | R實習 | BB | 考試 | 導讀 | 數據 | 家庭作業 |
| 1 | 09/17 | | [[課程簡介|課程簡介(逢甲大學97學年度上學期)]] | [[前測]] || [[電影觀賞感言]] | [[評比計畫|迴歸分析評量計畫]] ||| 再作一遍[[前測]](不列入評分項目) |
|~|~|~|~| 認識大家、購書 |~|~|~|~|~|~|
|~|~|~|~| 深夜加油站遇上蘇格拉底 |~|~|~|~|~|~|
| 2 | 24 | [[相關分析]] | [[Field Goal (I)]] | [[R for the Field Goal (I)]] | [[下載並安裝最新版的R|http://dist.stat.tamu.edu/pub/rvideos/Install2/Install1.html]] | 為棒球運彩數據編碼並且建檔 || [[field goal|http://www.google.com.tw/dictionary?q=field+goal&langpair=en|zh-TW&hl=zh-TW&ei=ib25StigD8yOkQXcm73aBQ&sa=X&oi=translation&ct=result]]、[[踢球員的表現]] | [[FieldGoals2003to2006.csv|http://www.stat.tamu.edu/~sheather/book/docs/datasets/FieldGoals2003to2006.csv]] | [[恰弟與哈弟第47頁練習題2-10]] |
|~|~|~| [[Field Goal (II)]] | [[R for the Field Goal (II)]] | [[觀賞R的第一支指導影片|第一支指導影片]]、[[第一次R實習]] |~|~| [[setwd|http://stat.ethz.ch/R-manual/R-patched/library/base/html/getwd.html]]、[[read.csv|http://stat.ethz.ch/R-manual/R-patched/library/utils/html/read.table.html]]、[[attach|http://stat.ethz.ch/R-manual/R-patched/library/base/html/attach.html]]、[[plot|http://stat.ethz.ch/R-manual/R-patched/library/graphics/html/plot.html]]、[[lm|http://stat.ethz.ch/R-manual/R-patched/library/stats/html/lm.html]]、[[anova|http://stat.ethz.ch/R-manual/R-patched/library/stats/html/anova.html]]、[[seq|http://stat.ethz.ch/R-manual/R-patched/library/base/html/seq.html]]、[[summary|http://stat.ethz.ch/R-manual/R-patched/library/base/html/summary.html]]、[[lines|http://stat.ethz.ch/R-manual/R-patched/library/graphics/html/lines.html]]、[[detach|http://stat.ethz.ch/R-manual/R-patched/library/base/html/detach.html]]、[[cor|http://stat.ethz.ch/R-manual/R-patched/library/stats/html/cor.html]] |~|~|
| 3 | 10/01 |~| [[討論前測]]、[[Field Goal (III)]] | [[R for the Field Goal (III)]] | [[第二次R實習]]、[[一處R的影片庫|http://dist.stat.tamu.edu/pub/rvideos/]] |~|| [[mean|http://stat.ethz.ch/R-manual/R-patched/library/base/html/mean.html]]、[[median|http://stat.ethz.ch/R-manual/R-patched/library/stats/html/median.html]]、[[quantile|http://stat.ethz.ch/R-manual/R-patched/library/stats/html/quantile.html]]、[[range|http://stat.ethz.ch/R-manual/R-patched/library/base/html/range.html]]、[[min|http://stat.ethz.ch/R-manual/R-patched/library/base/html/Extremes.html]]、[[max|http://stat.ethz.ch/R-manual/R-patched/library/base/html/Extremes.html]]、[[var|http://stat.ethz.ch/R-manual/R-patched/library/stats/html/cor.html]]、[[sd|http://stat.ethz.ch/R-manual/R-patched/library/stats/html/sd.html]]、[[sample|http://stat.ethz.ch/R-manual/R-patched/library/base/html/sample.html]]、[[t.test|http://stat.ethz.ch/R-manual/R-patched/library/stats/html/t.test.html]]、[[cor.test|http://stat.ethz.ch/R-manual/R-patched/library/stats/html/cor.test.html]]、[[boot|http://stat.ethz.ch/R-manual/R-patched/library/boot/html/boot.html]] |~|~|
|~|~|~|~| [[討論期末報告的相關規定]] |~|~|~|~|~|~|
| 4 | 10/08 | [[簡單線性迴歸分析]] | [[再論拔靴法|拔靴法(II)]]、[[Computer Repair Data (I)]] | [[三點決定一條直線]] | [[R for bootstrapping]] ||| [[lm|http://stat.ethz.ch/R-manual/R-patched/library/stats/html/lm.html]]、[[summary.lm|http://stat.ethz.ch/R-manual/R-patched/library/stats/html/summary.lm.html]]、[[coef|http://stat.ethz.ch/R-manual/R-patched/library/stats/html/coef.html]]、[[fitted|http://stat.ethz.ch/R-manual/R-patched/library/stats/html/fitted.html]]、[[residuals|http://stat.ethz.ch/R-manual/R-patched/library/stats/html/residuals.html]]、[[predict.lm|http://stat.ethz.ch/R-manual/R-patched/library/stats/html/predict.lm.html]]、[[vcov|http://stat.ethz.ch/R-manual/R-patched/library/stats/html/vcov.html]]、[[boot|http://stat.ethz.ch/R-manual/R-patched/library/boot/html/boot.html]] | [[Computer Repair Data|http://www.ilr.cornell.edu/~hadi/RABE4/Data4/P027.txt]]、Duncan{car} |~|
|~|~|~|~| [[R for the Computer Repair Data (I)]] | [[R Session for Simple Linear Regression of the Duncan data set]] |~|~|~|~| [[第二次作業|更多拔靴法的體驗]] |
|~|~|~| [[迴歸參數的信賴區間]] | [[迴歸參數估計式的理論抽樣分配]]以及[[迴歸參數的常態信賴區間]] | [[Duncan數據集的常態信賴區間]]、[[CR數據集的常態信賴區間]] |~|~| [[confint|http://stat.ethz.ch/R-manual/R-patched/library/stats/html/confint.html]] |~|~|
|~|~|~| [[斜率參數的假設檢定]] | [[斜率參數的常態假設檢定理論]] | [[Duncan數據集的常態假設檢定]]、[[CR數據集的常態假設檢定]] |~|~| [[linear.hypothesis{car}]] |~|~|
| 5 | 15 |~| [[迴歸參數的信賴區間]] | [[迴歸參數的拔靴信賴區間]] | [[boot.ci|http://stat.ethz.ch/R-manual/R-patched/library/boot/html/boot.ci.html]] ||| [[boot.ci|http://stat.ethz.ch/R-manual/R-patched/library/boot/html/boot.ci.html]] |~|~|
|~|~|~| [[斜率參數的假設檢定]] | [[斜率參數的拔靴假設檢定理論]] | [[我要寫拔靴假設檢定的程式]] |||~|~|~|
|~|~| [[複線性迴歸分析]] | [[估計複線性迴歸模型]]、[[斜率參數的假設檢定]] | [[跟lm()有關的R方法]]、[[linear.hypothesis{car}]] | [[Duncan數據的線性假設檢定]] ||| [[lm|http://stat.ethz.ch/R-manual/R-patched/library/stats/html/lm.html]]、[[summary.lm|http://stat.ethz.ch/R-manual/R-patched/library/stats/html/summary.lm.html]]、[[coef|http://stat.ethz.ch/R-manual/R-patched/library/stats/html/coef.html]]、[[fitted|http://stat.ethz.ch/R-manual/R-patched/library/stats/html/fitted.html]]、[[residuals|http://stat.ethz.ch/R-manual/R-patched/library/stats/html/residuals.html]]、[[predict.lm|http://stat.ethz.ch/R-manual/R-patched/library/stats/html/predict.lm.html]]、[[vcov|http://stat.ethz.ch/R-manual/R-patched/library/stats/html/vcov.html]] | [[Cigarette Consumption|http://www.ilr.cornell.edu/~hadi/RABE4/Data4/P081.txt]] |~|
| 6 | 22 |~| [[共線性]]、[[(RR) Ridge Regression]] | [[http://www.aiaccess.net/English/Glossaries/GlosMod/e_gm_ridge.htm|http://www.aiaccess.net/English/Glossaries/GlosMod/e_gm_ridge.htm]] | [[kappa(model.matrix.lm(cr.lm))]]、[[lm.ridge{MASS}]]、[[An example of ridge regression]] ||| [[lm.ridge|http://stat.ethz.ch/R-manual/R-patched/library/MASS/html/lm.ridge.html]]、[[model.matrix.lm|http://stat.ethz.ch/R-manual/R-patched/library/stats/html/model.matrix.html]]、[[kappa|http://stat.ethz.ch/R-manual/R-patched/library/base/html/kappa.html]] |~|~|
|~|~|~| [[選模]] | [[逐步迴歸]] | [[CC數據的逐步迴歸過程]]、[[Duncan數據的逐步迴歸過程]]、[[範例|逐步迴歸的例子]] ||| [[step|http://stat.ethz.ch/R-manual/R-patched/library/stats/html/step.html]] |~| [[第三次作業|我要比較各式各樣的拔靴信賴區間]] |
|~|~| [[專題]] |>|>|>|>|>|>| [[再一次體會拔靴法的魅力]] |~|
| 7 | 29 | [[迴歸診斷]] | [[迴歸假設]]、[[各種殘差]] | [[論述迴歸假設]] |||| [[plot.lm|http://stat.ethz.ch/R-manual/R-patched/library/stats/html/plot.lm.html]]、[[residuals.lm|http://stat.ethz.ch/R-manual/R-patched/library/stats/html/lm.summaries.html]]、[[rstandard.lm|http://stat.ethz.ch/R-manual/R-patched/library/stats/html/influence.measures.html]]、[[rstudent.lm|http://stat.ethz.ch/R-manual/R-patched/library/stats/html/influence.measures.html]]、[[hatvalues.lm|http://stat.ethz.ch/R-manual/R-patched/library/stats/html/influence.measures.html]]、[[influence.measures|http://stat.ethz.ch/R-manual/R-patched/library/stats/html/influence.measures.html]] | The life-cycle savings data given in Belsley, Kuh and Welsch, Huber's data [Atkinson 1985], Irwin's data [Williams 1987] |~|
|~|~|~| [[診斷簡單線性迴歸模型]] | [[殘差圖]]、[[常態機率圖]]、[[等變異數檢定]] | [[用R繪製殘差圖]] |||~|~|~|
|~|~|~| [[診斷複線性迴歸模型]] || [[範例|R提供的複迴歸診斷範例]] |||~|~|~|
|~|~|~| [[離群值]] | [[R提供的診斷工具]] | [[範例|R提供的離群值範例]] |||~|~|~|
| 8 | 11/05 |>|>|>|>|>|>|>|>| ![[期中考(上機考R)|2009期中考範圍(Scope for the Midterm)]] |~|
| 週次 | 日期 | 大綱 | 內容 | 課堂活動 | R實習 | BB | 考試 | 導讀 | 數據 | 家庭作業 |
| 9 | 12 |>|>|>|>|>|>|>|>| ![[撰寫期末報告企劃書]] |
| 10 | 19 | [[羅吉斯迴歸分析]] | [[羅吉斯迴歸分析]] | [[什麼是羅吉斯迴歸分析模型?]]、[[估計羅吉斯迴歸分析模型]] 、[[羅吉斯迴歸分析的品質保證]] | [[mle()]]、[[glm()]] ||||| [[繳交期末報告企劃書修正版]] |
|~|~|~| [[多重羅吉斯迴歸分析]] | [[什麼是多重羅吉斯迴歸分析模型?]]、[[估計多重羅吉斯迴歸分析模型]] 、[[多重羅吉斯迴歸分析的品質保證]] |~|||||~|
|~|~|~| [[順序多重羅吉斯迴歸分析]] | [[什麼是順序多重羅吉斯迴歸分析模型?]]、[[估計順序多重羅吉斯迴歸分析模型]] 、[[順序多重羅吉斯迴歸分析的品質保證]] |~|||||~|
|~|~| [[多重共線性]] | [[VIF]]、 [[簡單線性迴歸模型的品質測度]]、[[找一個跟反應變數最相關的變數]] ||~|||||~|
| 11 | 26 | [[MLE]]、[[選模的工具]] | [[An R Session]] | [[發放時間表與最後版本的評比計畫]]、[[確定各組模擬與正式比賽日期與時間]]、[[我學過哪些選模的工具?]] |~||||||
|~|~|~|~| [[寫預測NBA比賽結果的R程式]] |~||||| [[寫預測NBA比賽結果的R程式]]、[[比賽了!]] |
| 12 | 12/03 | 第十章的[[迴歸參數的偏誤估計]] | [[主成份迴歸]] | [[什麼是主成份迴歸?]]、[[移去解釋變數的關連性]]、[[迴歸參數的限制式]]、[[主成份迴歸的限制]] |~|| [[分析NBA數據小考]] |||~|
|~|~|~| [[(RR) Ridge Regression]] | [[什麼是脊迴歸?]]、[[脊迴歸估計問題]]、[[脊迴歸的限制]] |~||~|||~|
|~|~|~|| [[學術審查各組的使用說明]] | ![[學長報告相關的R函示]] ||~|||~|
|~|~|~|~| ![[討論第一次作業]] |~||~|||~|
| 12 | 12/03 | [[寫預測NBA比賽結果的R程式]] |>|>|>|>|>|>|>| ![[實作寫預測NBA比賽勝負程式的策略]] |
| 13 | 10 | [[R的使用說明]] |>|>|>|>|>|>|>| ![[報告各組的使用說明15分鐘]]、[[現場上傳報告用投影片到BB]] |
| 14 | 17 | 第五章的[[屬性解釋變數]] | [[共變數]] | [[什麼是共變數?]]、[[迴歸分析的交互作用項]]、[[迴歸方程組]]、[[變異數分析模型]]、[[共變異數分析模型]] | [[lm()]]、[[formula()]]、[[linear.hypothesis{car}]] ||||| [[寫預測NBA比賽結果的R程式]]、[[比賽了!]] |
|~|~|~| [[一個屬性變數]] |~|~|||||~|
|~|~|~| [[多個屬性變數]] |~|~|||||~|
|~|~|~|| ![[討論期中考|2009期中考]] |~|||||~|
|~|~| 第六章的[[變數變換]] | [[我的樣本關係不是線性的!]] | [[發現樣本關係不是線性的]]、[[把關係變成線性的手法]] | [[box.cox{car}]]、[[box.tidwell{car}]] |||||~|
|~|~|~| [[我的迴歸誤差變異數不固定!]] | [[發現迴歸誤差變異數不固定]]、[[移除變異數不固定的手法]] |~|||||~|
| 15 | 24 | 第十一章的[[變數選擇]] | [[我的解釋變數太多了!]] | [[一種建模的策略]] | [[scatterplot{car}]]、[[scatterplot.matrix{car}]]、[[cor()]]、[[vif{car}]]、[[step()]] |||||~|
|~|~|~|~| ![[討論第二次作業]] |~|||||~|
| 16 | 31(放假) | 第八章的[[相關誤差]] | [[我的迴歸誤差是相關的!]] | [[自相關]]、[[DW統計量]]、[[一種移去自相關的轉換]]、[[有自相關的估計手法]]、[[DW手法的限制]] | [[durbin.watson{car}]] |||||~|
|~|~| [[穩健迴歸]] | [[我的數據有離群值!]] | [[再論最小平方法]] | [[rlm{MASS}]] |||||~|
|~|~|~|~| ![[討論第三次作業]] | ![[學姐報告相關的R函示]] |||||~|
| 17 | 01/07 | [[結算比賽成績]] |>|>|>|>|>|>|>| ![[期末比賽甘苦談(錄影)]]、[[大家一起來結算比賽成績]]、[[現場整併所有決戰戰績報告並且上傳與繳交]] |
| 18 | 14 | ![[期末考]] |>|>|>|>|>|>|>| ![[上機期末考一小時]]、[[評頭論足你的海報:第一次審查]]、[[確定第二次審查名單]] |
| 週次 | 日期 | 大綱 | 內容 | 課堂活動 | R實習 | BB | 考試 | 導讀 | 數據 | 家庭作業 |
| 1 | 09/17 | | [[課程簡介|課程簡介(逢甲大學97學年度上學期)]] | [[前測]] || [[電影觀賞感言]] | [[評比計畫|迴歸分析評量計畫]] ||| 再作一遍[[前測]](不列入評分項目) |
|~|~|~|~| 認識大家、購書 |~|~|~|~|~|~|
|~|~|~|~| 深夜加油站遇上蘇格拉底 |~|~|~|~|~|~|
| 2 | 24 | [[相關分析]] | [[Field Goal (I)]] | [[R for the Field Goal (I)]] | [[下載並安裝最新版的R|http://dist.stat.tamu.edu/pub/rvideos/Install2/Install1.html]] | 為棒球運彩數據編碼並且建檔 || [[field goal|http://www.google.com.tw/dictionary?q=field+goal&langpair=en|zh-TW&hl=zh-TW&ei=ib25StigD8yOkQXcm73aBQ&sa=X&oi=translation&ct=result]]、[[踢球員的表現]] | [[FieldGoals2003to2006.csv|http://www.stat.tamu.edu/~sheather/book/docs/datasets/FieldGoals2003to2006.csv]] | [[恰弟與哈弟第47頁練習題2-10]] |
|~|~|~| [[Field Goal (II)]] | [[R for the Field Goal (II)]] | [[觀賞R的第一支指導影片|第一支指導影片]] |~|~| [[setwd|http://stat.ethz.ch/R-manual/R-patched/library/base/html/getwd.html]]、[[read.csv|http://stat.ethz.ch/R-manual/R-patched/library/utils/html/read.table.html]]、[[attach|http://stat.ethz.ch/R-manual/R-patched/library/base/html/attach.html]]、[[plot|http://stat.ethz.ch/R-manual/R-patched/library/graphics/html/plot.html]]、[[lm|http://stat.ethz.ch/R-manual/R-patched/library/stats/html/lm.html]]、[[anova|http://stat.ethz.ch/R-manual/R-patched/library/stats/html/anova.html]]、[[seq|http://stat.ethz.ch/R-manual/R-patched/library/base/html/seq.html]]、[[summary|http://stat.ethz.ch/R-manual/R-patched/library/base/html/summary.html]]、[[lines|http://stat.ethz.ch/R-manual/R-patched/library/graphics/html/lines.html]]、[[detach|http://stat.ethz.ch/R-manual/R-patched/library/base/html/detach.html]]、[[cor|http://stat.ethz.ch/R-manual/R-patched/library/stats/html/cor.html]] |~|~|
| 3 | 10/01 |~| [[Field Goal (III)]] | [[R for the Field Goal (III)]] | [[一處R的影片庫|http://dist.stat.tamu.edu/pub/rvideos/]] |~|| [[mean|http://stat.ethz.ch/R-manual/R-patched/library/base/html/mean.html]]、[[median|http://stat.ethz.ch/R-manual/R-patched/library/stats/html/median.html]]、[[quantile|http://stat.ethz.ch/R-manual/R-patched/library/stats/html/quantile.html]]、[[range|http://stat.ethz.ch/R-manual/R-patched/library/base/html/range.html]]、[[min|http://stat.ethz.ch/R-manual/R-patched/library/base/html/Extremes.html]]、[[max|http://stat.ethz.ch/R-manual/R-patched/library/base/html/Extremes.html]]、[[var|http://stat.ethz.ch/R-manual/R-patched/library/stats/html/cor.html]]、[[sd|http://stat.ethz.ch/R-manual/R-patched/library/stats/html/sd.html]]、[[sample|http://stat.ethz.ch/R-manual/R-patched/library/base/html/sample.html]]、[[t.test|http://stat.ethz.ch/R-manual/R-patched/library/stats/html/t.test.html]]、[[cor.test|http://stat.ethz.ch/R-manual/R-patched/library/stats/html/cor.test.html]]、[[boot|http://stat.ethz.ch/R-manual/R-patched/library/boot/html/boot.html]] |~|~|
|~|~| [[簡單線性迴歸分析]] | [[Computer Repair Data (I)]] | [[三點決定一條直線]] | [[R Session for Simple Linear Regression of the Duncan data set]] |~|~| [[lm|http://stat.ethz.ch/R-manual/R-patched/library/stats/html/lm.html]]、[[summary.lm|http://stat.ethz.ch/R-manual/R-patched/library/stats/html/summary.lm.html]]、[[coef|http://stat.ethz.ch/R-manual/R-patched/library/stats/html/coef.html]]、[[fitted|http://stat.ethz.ch/R-manual/R-patched/library/stats/html/fitted.html]]、[[residuals|http://stat.ethz.ch/R-manual/R-patched/library/stats/html/residuals.html]]、[[predict.lm|http://stat.ethz.ch/R-manual/R-patched/library/stats/html/predict.lm.html]]、[[vcov|http://stat.ethz.ch/R-manual/R-patched/library/stats/html/vcov.html]] | [[Computer Repair Data|http://www.ilr.cornell.edu/~hadi/RABE4/Data4/P027.txt]]、Duncan{car} |~|
|~|~|~|~| [[R for the Computer Repair Data (I)]] |~|~|~|~|~|~|
| 4 | 08 |~| [[迴歸參數的信賴區間]] | [[參數估計式的理論抽樣分配]] | [[Duncan數據集的拔靴信賴區間]] ||| [[confint|http://stat.ethz.ch/R-manual/R-patched/library/stats/html/confint.html]]、[[boot.ci|http://stat.ethz.ch/R-manual/R-patched/library/boot/html/boot.ci.html]] |~|~|
|~|~|~|~| [[二度遇見拔靴法]] |~|~|~|~|~|~|
|~|~|~| [[斜率參數的假設檢定]] | [[斜率參數的常態假設檢定理論]] | [[Duncan數據集的常態假設檢定]] |~|~| [[linear.hypothesis{car}]] |~|~|
|~|~|~|~| [[斜率參數的拔靴假設檢定]] | [[Duncan數據集的拔靴假設檢定]] |~|~| [[自己寫程式!]] |~|~|
| 5 | 15 | ||||||||
| 6 | 22 |~||||||||
| 7 | 29 | ||||||||
| 8 | 11/05 (期中考) |~||||||||
| 9 | 12 |~||||||||
| 10 | 19 | ||||||||
| 11 | 26 |~||||||||
| 12 | 12/03 | ||||||||
| 13 | 10 |~||||||||
| 14 | 17 | ||||||||
| 15 | 24 |~||||||||
| 16 | 31 |~||||||||
| 17 | 01/07 | ||||||||
| 18 | 14(期末考) |||||||||
{{{
信賴區間的建構邏輯?
}}}
----
+++[A]
$$
\begin{array}{rcl}
0.95 & = & Pr(|\bar X - \mu| \le x_{\alpha}) \\
& = & Pr(\frac{|\bar X - \mu|}{\sigma/\sqrt{n}} \le \frac{x_{\alpha}}{\sigma/\sqrt{n}}) \\
& \approx & Pr(|Z| \le \frac{x_{\alpha}}{\sigma/\sqrt{n}}) \\
\end{array}
$$
$$
\begin{array}{rcl}
\frac{x_{\alpha}}{\sigma/\sqrt{n}} & = & z_{(1 - 0.95)/2} \\
x_{\alpha} & = & z_{(1 - 0.95)/2} \times \sigma/\sqrt{n} \\
\end{array}
$$
!!!
$$
\begin{array}{rcl}
|\bar X - \mu| & \le & z_{(1 - 0.95)/2} \times \sigma/\sqrt{n} \\
\end{array}
$$
!請解讀上述的結果...
===
{{{
Plug-in Principle
}}}
----
# (normal)
## (大樣本)$[\hat \theta - z_{1 - \alpha/2} \times \sigma^*(\hat \theta), \hat \theta + z_{1 - \alpha/2} \times \sigma^*(\hat \theta)]$(提示:$\hat \theta \sim N(\theta, \sigma(\hat \theta)^2)$)
## (小樣本)$[\hat \theta - bias^*(\hat \theta) - z_{1 - \alpha/2} \times \sigma^*(\hat \theta), \hat \theta - bias^*(\hat \theta) + z_{1 - \alpha/2} \times \sigma^*(\hat \theta)]$
# (percentile) $[\hat \theta^*_{[(B+1) \times \alpha/2]}, \hat \theta^*_{[(B+1) \times (1 - \alpha/2)]}]$ (提示:建構邏輯。)
# (basic) $[2 \hat \theta - \hat \theta^*_{[(B+1) \times \alpha/2]}, 2 \hat \theta - \hat \theta^*_{[(B+1) \times (1 - \alpha/2)]}]$ (提示:$\hat F_{\hat \theta - \theta} = F^*_{\hat \theta^* - \hat \theta})$)
# (BCa) [[long long story]]
{{{
boot.ci(boot.out, conf = 0.95, type = "all",
index = 1:min(2,length(boot.out$t0)), var.t0 = NULL,
var.t = NULL, t0 = NULL, t = NULL, L = NULL, h = function(t) t,
hdot = function(t) rep(1,length(t)), hinv = function(t) t, ...)
}}}
!範例
{{{
Duncan.b = function(data, indices)
{
Data = data[indices,]
coef(lm(prestige ~ income, data=Data))[2]
}
library(boot)
coef.Duncan = boot(Duncan, Duncan.b, 99)
plot(coef.Duncan)
boot.ci(coef.Duncan, 0.95, type = "all")
}}}
{{{
> boot.ci(coef.Duncan, 0.95, type = "all")
BOOTSTRAP CONFIDENCE INTERVAL CALCULATIONS
Based on 99 bootstrap replicates
CALL :
boot.ci(boot.out = coef.Duncan, conf = 0.95, type = "all")
Intervals :
Level Normal Basic
95% ( 0.870, 1.318 ) ( 0.894, 1.348 )
Level Percentile BCa
95% ( 0.813, 1.267 ) ( 0.810, 1.267 )
Calculations and Intervals on Original Scale
Some basic intervals may be unstable
Some percentile intervals may be unstable
Some BCa intervals may be unstable
Warning message:
In boot.ci(coef.Duncan, 0.95, type = "all") :
bootstrap variances needed for studentized intervals
}}}
!再一個範例
{{{
cr.b = function(data, indices)
{
Data = data[indices,]
coef(lm(Minutes ~ Units, data=Data))[2]
}
library(boot)
coef.cr = boot(cr, cr.b, 9999)
plot(coef.cr)
boot.ci(coef.cr, c(0.95, 0.99), type = "all")
}}}
{{{
utils::example(lm)
step(lm.D9)
summary(lm1 <- lm(Fertility ~ ., data = swiss))
slm1 <- step(lm1)
summary(slm1)
slm1$anova
}}}
記得要有三項重點
# 點出年輕人的元素
# 看到自己的身影
# 最後主角找到什麼?
根據你的模型與北富銀運彩官網的投注內容,提供
# 你的【R報表】與
# 【讓分】結果、
# 【不讓分】結果、
# 【勝分差】結果。