自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

在使用 R 時會發(fā)生什么?

大數(shù)據(jù)
備注:本文不是一部 R 教程。下面的示例僅試圖讓您了解 R 會話看起來是什么樣的。

備注:本文不是一部 R 教程。下面的示例僅試圖讓您了解 R 會話看起來是什么樣的。

R 二進(jìn)制文件可用于 Windows、Mac OS X 和多個 Linux® 發(fā)行版。源代碼也可供人們自行編譯。

在 Windows® 中,安裝程序?qū)?R 添加到開始菜單中。要在 Linux 中啟動 R,可打開一個終端窗口并在提示符下鍵入 R。您應(yīng)看到類似圖 1 的畫面。

R

圖 1. R 工作區(qū)

在提示符下鍵入一個命令,R 就會響應(yīng)。

此時,在真實(shí)的環(huán)境中,您可能會從一個外部數(shù)據(jù)文件將數(shù)據(jù)讀入 R 對象中。R 可從各種不同格式的文件讀取數(shù)據(jù),但對于本示例,我使用的是來自 MASS 包的 michelson 數(shù)據(jù)。這個包附帶了 Venables and Ripley 的標(biāo)志性文本 Modern Applied Statistics with S-Plus(參見 參考資料)。michelson 包含來自測量光速的流行的 Michelson and Morley 實(shí)驗(yàn)的結(jié)果。

清單 1 中提供的命令可以加載 MASS 包,獲取并查看 michelson 數(shù)據(jù)。圖 2 顯示了這些命令和來自 R 的響應(yīng)。每一行包含一個 R 函數(shù),它的參數(shù)放在方括號 ([]) 內(nèi)。

清單 1. 啟動一個 R 會話

    2+2             # R can be a calculator. R responds, correctly, with 4.
    library("MASS") # Loads into memory the functions and data sets from 
                    # package MASS, that accompanies Modern Applied Statistics in S

    data(michelson) # Copies the michelson data set into the workspace.

    ls()            # Lists the contents of the workspace. The michelson data is there.

    head(michelson) # Displays the first few lines of this data set.
                    # Column Speed contains Michelson and Morleys estimates of the 
                    # speed of light, less 299,000, in km/s.
                    # Michelson and Morley ran five experiments with 20 runs each.
                    # The data set contains indicator variables for experiment and run.
    help(michelson) # Calls a help screen, which describes the data set.

圖 2. 會話啟動和 R 的響應(yīng)

R

現(xiàn)在讓我們看看該數(shù)據(jù)(參見 清單 2)。輸出如 圖 3 中所示。

清單 2. R 中的一個箱線圖

    # Basic boxplot

    with(michelson, boxplot(Speed ~ Expt)) 

    # I can add colour and labels. I can also save the results to an object.

    michelson.bp = with(michelson, boxplot(Speed ~ Expt, xlab="Experiment", las=1, 
                    ylab="Speed of Light - 299,000 m/s", 
                    main="Michelson-Morley Experiments",
                    col="slateblue1")) 
                 
    # The current estimate of the speed of light, on this scale, is 734.***dd a horizontal line to highlight this value.

    abline(h=734.5, lwd=2,col="purple")  #Add modern speed of light

Michelson and Morley 似乎有計(jì)劃地高估了光速。各個實(shí)驗(yàn)之間似乎也存在一定的不均勻性。

圖 3. 繪制一個箱線圖

R

在對分析感到滿意后,我可以將所有命令保存到一個 R 函數(shù)中。參見清單 3。

清單 3. R 中的一個簡單函數(shù)

    MyExample = function(){
        library(MASS)
        data(michelson)
        michelson.bw = with(michelson, boxplot(Speed ~ Expt, xlab="Experiment", las=1, 
        ylab="Speed of Light - 299,000 m/s", main="Michelsen-Morley Experiments", 
            col="slateblue1"))
        abline(h=734.5, lwd=2,col="purple")

    }

這個簡單示例演示了 R 的多個重要功能:

保存結(jié)果—boxplot() 函數(shù)返回一些有用的統(tǒng)計(jì)數(shù)據(jù)和一個圖表,您可以通過類似 michelson.bp = … 的負(fù)值語句將這些結(jié)果保存到一個 R 對象中,并在需要時提取它們。任何賦值語句的結(jié)果都可在 R 會話的整個過程中獲得,并且可以作為進(jìn)一步分析的主題。boxplot 函數(shù)返回一個用于繪制箱線圖的統(tǒng)計(jì)數(shù)據(jù)(中位數(shù)、四分位等)矩陣、每個箱線圖中的項(xiàng)數(shù),以及異常值(在 圖 3 中的圖表上顯示為開口圓)。請參見圖 4。

圖 4. 來自 boxplot 函數(shù)的統(tǒng)計(jì)數(shù)據(jù)

R

 

公式語言— R(和 S)有一種緊湊的語言來表達(dá)統(tǒng)計(jì)模型。參數(shù)中的代碼 Speed ~ Expt 告訴函數(shù)在每個 Expt (實(shí)驗(yàn)數(shù)字)級別上繪制 Speed 的箱線圖。如果希望執(zhí)行方差分析來測試各次實(shí)驗(yàn)中的速度是否存在顯著差異,那么可以使用相同的公式:lm(Speed ~ Expt)。公式語言可表達(dá)豐富多樣的統(tǒng)計(jì)模型,包括交叉和嵌套效應(yīng),以及固定和隨機(jī)因素。

用戶定義的 R 函數(shù)— 這是一種編程語言。

責(zé)任編輯:李英杰 來源: 36大數(shù)據(jù)
相關(guān)推薦

2019-02-27 10:18:26

重置Windows 10Windows

2018-01-19 12:56:19

Linux進(jìn)程

2023-12-13 17:04:51

終端命令shell

2024-04-02 11:31:33

USBAndroid

2022-07-25 12:01:10

終端Linux

2021-08-19 17:27:41

IT數(shù)據(jù)中心災(zāi)難

2021-12-27 08:24:08

漏洞網(wǎng)絡(luò)安全

2019-09-03 14:15:05

2023-08-26 07:44:13

系統(tǒng)內(nèi)存虛擬

2023-02-27 13:35:16

ChatGPT機(jī)器學(xué)習(xí)

2023-06-27 16:53:50

2021-03-10 10:40:04

Redis命令Linux

2021-09-16 14:26:32

網(wǎng)絡(luò)9.11網(wǎng)絡(luò)攻擊網(wǎng)絡(luò)安全

2015-11-19 00:11:12

2024-01-18 11:50:28

2016-01-04 11:03:00

2023-04-27 07:40:08

Spring框架OpenAI

2020-12-10 07:37:42

HashMap數(shù)據(jù)覆蓋

2015-04-16 10:40:29

2011-10-11 15:42:54

大數(shù)據(jù)數(shù)據(jù)庫
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號