Introduction to R

Preview:

DESCRIPTION

Introduction to R. ปราณี นิลกรณ์. R คืออะไร ?. R เป็นภาษาและโปรแกรมสำเร็จรูปสำหรับการคำนวณทางสถิติและสร้างกราฟประเภทให้เปล่า - PowerPoint PPT Presentation

Citation preview

Introduction to R ปราณี� นิ�ลกรณี

2

R เป�นิภาษาและโปรแกรมสำ�าเร�จร�ปสำ�าหร�บการคำ�านิวณีทางสำถิ�ติ�และสำร!างกราฟประเภทให!เปล$า

( free open source package ) ท�%พั�ฒนิาขึ้)*นิมาจากภาษา S ( S language, Bell Labs) โดย Robert Gentleman และ Ross Ihaka แห$งUniversity of Auckland, New Zealandเม.%อป0 2538

เหมาะท�*งสำ�าหร�บการเขึ้�ยนิโปรแกรมเอง และใช้!แบบโปรแกรมสำ�าเร�จร�ป

ม�ฟ2งกช้�นิทางสำถิ�ติ�ให!เร�ยกใช้!มากมาย และม�ผู้�!พั�ฒนิาเพั�%มอย$างติ$อเนิ.%อง

R คื�ออะไร?

3

ขึ้!อม�ลท4กอย$างเก�%ยวก�บ R หาอ$านิได!จาก http://www.R-project.org R system ประกอบด!วย 2 สำ$วนิหล�ก คำ.อ

1.Base system – ประกอบด!วย R language software และสำ$วนิเพั�%มเติ�มอ.%นิๆท�%ม�คำวามจ�าเป�นิติ!องใช้!บ$อยๆ

2. User contributed add-on packages

R คื�ออะไร?

4

จะหาโปรแกรม R ได!จากไหนิ? ไป download ได!ท�% www.r-project.org หร.อท�%http://CRAN.R-project.org โดยเล.อกลงโปรแกรมพั.*นิฐานิ ( Base Package)

ถิ!าติ!องการใช้!แบบเมนิ� จะติ!องติ�ดติ�*งโปรแกรม Rcmdr เพั�%มเติ�ม

การใช้� R

5

การจ�ดการขึ้!อม�ลและหนิ$วยคำวามจ�าการคำ�านิวณีในิร�ป Array และ Matrix

การว�เคำราะหขึ้!อม�ลทางสำถิ�ติ�การสำร!างกราฟการเขึ้�ยนิโปรแกรม

คืวามสามารถของ Base Package

6

RGui ( Gui – Graphical user interface) ประกอบด!วย

ว�นิโดวสำ R Console สำ�าหร�บเขึ้�ยนิคำ�าสำ�%งและแสำดงผู้ลล�พัธ์

ว�นิโดวสำ R Graph สำ�าหร�บแสำดงกราฟ Script Windows สำ�าหร�บเขึ้�ยนิ แก!ไขึ้คำ�าสำ�%ง โปรแกรม

โปรแกรม R

7

The R GUI

8

Open script

9

R Graph Windows

10

R และ สถ�ติ�

R ม� Packages ท�ม�ผู้�!สำร!างสำ�าหร�บการคำ�านิวณีและการว�เคำราะหขึ้!อม�ลทางสำถิ�ติ� ซึ่)%งเราสำามารถิ ดาวนิโหลดมาใช้!ได!อย$างสำะดวกและรวดเร�ว ม�ผู้�!พั�ฒนิา packages สำ�าหร�บเทคำนิ�คำการว�เคำราะหใหม$ๆ นิอกจากว�ธ์�ทางสำถิ�ติ�แบบเด�ม เช้$นิ data/text mining

นิ�กสำถิ�ติ�ท�%ว�จ�ยและพั�ฒนิาว�ธ์�การทางสำถิ�ติ�ใหม$ๆ นิ�ยมเขึ้�ยนิว�ธ์�การเป�นิ R packages

11

การทำ�างานของ R โดยทำ��วไป

การท�างานิก�บ R โดยท�%วไปคำ.อ พั�มพัคำ�าสำ�%ง R ติามท�%ติ!องการ ในิ command line interface

หร.อ โหลดไฟลท�%ม�คำ�าสำ�%ง R อย�$แล!ว(Script file) มา run

ช้!า แติ$ม�ขึ้!อด� คำ.อ เป�นิการบ�นิท)กขึ้�*นิติอนิการว�เคำราะหขึ้!อม�ล เก�บไว!เป�นิไฟลสำ�าหร�บงานิ

แติ$ละงานิได! เวลาพับคำวามผู้�ดพัลาด ทราบได!ว$าผู้�ดพัลาดขึ้�*นิติอนิไหนิ ถิ!าการว�เคำราะหติ!องท�าหลายขึ้�*นิติอนิ สำามารถินิ�าคำ�าสำ�%งมา run ซึ่�*า

ใหม$ได!โดยไม$ติ!อง click ใหม$ซึ่�*า ๆ

12

Getting help

>? t.test

or

>help(t.test)

13

R Advantages Disadvantages

Not user friendly @ start - steep learning curve, minimal GUI.No commercial support; figuring out correct methods or how to use a function on your own can be frustrating.Easy to make mistakes and not know. Working with large datasets is limited by RAMData prep & cleaning can be messier & more mistake prone in R vs. SPSS or SASSome users complain about hostility on the R listserve

Fast and free.State of the art: Statistical researchers provide their methods as R packages. SPSS and SAS are years behind R!2nd only to MATLAB for graphics.Mx, WinBugs, and other programs use or will use R.Active user communityExcellent for simulation, programming, computer intensive analyses, etc.Forces you to think about your analysis.Interfaces with database storage software (SQL)

14

R Commercial packagesMany different datasets (and other “objects”) available at same timeDatasets can be of any dimensionFunctions can be modifiedExperience is interactive-you program until you get exactly what you wantOne stop shopping - almost every analytical tool you can think of is availableR is free and will continue to exist. Nothing can make it go away, its price will never increase.

One datasets available at a given timeDatasets are rectangularFunctions are proprietaryExperience is passive-you choose an analysis and they give you everything they think you needTend to be have limited scope, forcing you to learn additional programs; extra options cost more and/or require you to learn a different language (e.g., SPSS Macros)They cost money. There is no guarantee they will continue to exist, but if they do, you can bet that their prices will always increase

15

ติ�วอย างประเภทำข�อม#ลและการเข$ยนคื�าส��งใน R

>Variables

> a = 49> sqrt(a)[1] 7

> a = "The dog ate my homework"> sub("dog","cat",a)[1] "The cat ate my homework“

> a = (1+1==3)> a[1] FALSE

numeric

character string

logical

16

> a = c(7,5,1)> a[2][1] 5

ล�สติ%: an ordered collection of data of arbitrary types.

> doe = list(name="john",age=28,married=F)> doe$name[1] "john“> doe$age[1] 28

เวกเติอร%

17

Data framesdata frame: is supposed to represent the typical data table that researchers come up with – like a spreadsheet.It is a rectangular table with rows and columns; data within each column has the same type (e.g. number, text, logical), but different columns may have different types.Example:localisation tumorsize progressXX348 proximal 6.3 FALSEXX234 distal 8.0 TRUEXX987 proximal 10.0 FALSE

18

> x<-c(1,3,2,10,5); y<-1:5 # creation of 2 vectors x [1]  1  3  2 10 5 > x+y

[1]  2  5  5 14 10 > x*y [1]  1  6  6 40 25 > x/y [1] 1.0000000 1.5000000 0.6666667 2.5000000 1.0000000 > x^y [1]     1     9     8 10000  3125

> sum(x)            #sum of elements in x [1] 21 > cumsum(x)         #cumulative sum vector [1]  1  4  6 16 21

R as a simple calculator

Basic math/stat tools# 20 numbers from 0 to 20,> x = round(runif(20,0,20),

digits=1) > x [1] 10.0 1.6 2.5 15.2 3.1 12.6 19.4

6.1[9] 9.2 10.9 9.5 14.1 14.3 14.3 12.8 [16] 15.9 0.1 13.1 8.5 8.7 > min(x) [1] 0.1 > max(x) [1] 19.4 > median(x) # médiane [1] 10.45 > mean(x) # moyenne [1] 10.095 > var(x) # variance [1] 27.43734

> sd(x) # standard deviation [1] 5.238067 > sqrt(var(x)) [1] 5.238067 > length(x) [1] 20 > round(x) [1] 10 2 2 15 3 13 19 6 9 11 10 14 14 14 13 16 0 13 8 9 > cor(x,sin(x/20)) # corrélation [1] 0.997286 > quantile(x) # les quantiles, 0% 25% 50% 75% 100% 0.10 7.90 10.45 14.15 19.40

Statistical tools Samples tests

◦ Checking normality Kolmogorov-Smirnov test

> #generate 500 observations from uniform (0,1) distribution > F500<-runif(500);a<-c(mean(F500),sd(F500)) > qqnorm(F500)    #normal probability plot > qqline(F500)    #ideal sample will fall near the straight line >ks.test(F500, "pnorm", mean=a[1], sd=a[2])         One-sample Kolmogorov-Smirnov test data:  F500 D = 0.0655, p-value = 0.02742 alternative hypothesis: two.sided

Recommended