Skip to Content
R 錦囊妙計
book

R 錦囊妙計

by Paul Teetor
January 2014
Beginner to intermediate
488 pages
8h 1m
Chinese
GoTop Information, Inc.
Content preview from R 錦囊妙計
264
|
第十章
其次,對於標籤與點類型(或是線類型、顏色)之間的對應,並不是自動產生的;您有
責任透過程式碼設定讓它們正確地對應。此任務很容易出錯,而且不容易自動化執行。
10.6 繪製散佈圖的迴歸線
問題點
在繪製成對資料點時,您想增加一條呈現線性關係的迴歸線。
解決方案
建立一個線性迴歸模型物件(model object),然後為成對資料 (
x
,
y
) 值繪製散佈圖,最
後使用
abline
函數繪製迴歸模型物件:
> m <- lm(y ˜ x)
> plot(y ˜ x)
> abline(m)
討論說明
當您使用
abline(m)
繪製一個迴歸模型物件時,它繪製配適的迴歸線。這與產生迴歸分
析診斷圖的
plot(m)
函數完全不同(錦囊 11.15)。
假設我們使用
faraway
套件中的
strongx
資料集來建立迴歸模型:
> library(faraway)
> data(strongx)
> m <- lm(crossx ˜ energy, data=strongx)
接著,我們使用
plot
函數繪製散佈圖,然後使用
abline
函數增加迴歸線,如圖 10-6
示:
> plot(crossx ˜ energy, data=strongx)
> abline(m)
其中值得注意的是,我們使用一個迴歸式(
crossx
˜
energy
)來定義繪圖的內容。由此
可見迴歸表達式語法與迴歸公式的一致性。另一種表達式寫法為
plot(strongx$energy,
strongx$crossx)
,但是它非常容易出錯;因為
plot ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

Python数据科学

Python数据科学

肯尼迪·贝曼, 诺亚·吉夫特
Java数据科学实战

Java数据科学实战

Michael R. Brzustowicz, PhD

Publisher Resources

ISBN: 9789862769829