SlideShare a Scribd company logo
1 of 31
Download to read offline
การวิเคราะห์ข้อมูลโดยใช้โปรแกรม R
(DATA ANALYSIS USING R PROGRAMMING LANGUAGE)
ดร. เสาวณิต รัตนรวมการ
สาขาวิชาคณิตศาสตร์และสถิติ
ภาควิชาวิทยาศาสตร์พื้นฐาน
คณะวิทยาศาสตร์และเทคโนโลยี
มหาวิทยาลัยราชภัฏสุรินทร์
sauwanit98@yahoo.com
Sunday, March 6, 2016
1
OUTLINE
 Introduction to R
 Source แหล่ง download R
 ส่วนประกอบของ R
 การอ่านค่าข้อมูลเข้ามาในโปรแกรม R
 รูปแบบการเก็บข้อมูลใน R และ แป้นลัดทาให้ไม่ต้องพิมพ์ซ้าบ่อยๆ
 function เบื้องต้น เช่น การสั่งเรียกใช้ข้อมูล
 Matrix, Inverse Matrix, Determinant
 eigen value, eigen vector
 แบบฝึกหัด
2
INTRODUCTION TO R
 โปรแกรมภาษาที่นิยมใช้กันเป็นอย่างมากในทางสถิติภาษาหนึ่ง
 ดาวน์โหลดได้ฟรี ภายใต้การดูแลขององค์กรที่ไม่แสวงหากาไร ชื่อ R Foundation
 open source
 R พัฒนาขึ้นครั้งแรกเมื่อปี ค.ศ. 1990s โดย Robert Gentleman และ
Ross Ihaka มหาวิทยาลัยโอคแลนด์ ประเทศนิวซีแลนด์
# เป็นไปได้ที่โปรแกรม R จะมาจากชื่อของ Robert และ Ross
 โปรแกรมภาษา R เป็นส่วนที่มีการพัฒนามาจากโปรแกรมภาษา S
3
R YOU READY FOR R?
4
Statistics Adjunct professor Robert Gentleman
who helped developed the R programming language.
(Credit: Stuart Isett for The New York Times)
5
SOURCE
 ดาวน์โลดได้ฟรี จาก http://cran.r-project.org/
 เลือก Download R รุ่นที่ทันสมัยที่สุด
 คือ R-3.1.2.tar.gz
(52 megabytes, 32/64 bit)
 คู่มือการใช้งานเบื้องต้น
http://cran.r-project.org/doc/manuals/r-
release/R-intro.html
6
DOWNLOAD R 3.1.2 FOR WINDOWS
7
Double click ที่นี่
8
ส่วนประกอบของ R
 R Gui ประกอบด้วย 2 วินโดส์ย่อยคือ
R Console ใช้ในการเขียนคาสั่งและแสดงผลลัพธ์
R graph แสดงกราฟต่างๆ
9
10
HOW TO READ DATA?
การอ่านค่าข้อมูลเข้ามาในโปรแกรม R
การอ่านค่าข้อมูลโดยใช้คาสั่ง read-table (Using read-table to
read files)
อ่านจาก external HD ตัวอย่างเช่น
>test<- read.table("g:/drug.dat",header=T)
>test<- read.table("g:/grades.txt",header=T)
11
การอ่านค่าข้อมูลชุดgrades.txt แล้วนาไปเก็บไว้ในfile
ชื่อtest ถ้าfileเดิมมีชื่อตัวแปรให้อ่านเข้ามาด้วย
การนาข้อมูลเข้าจากโปรแกรม SPSS ในรูป TEXT FILE
12
ให้save as file ที่สนใจในรูป Tab delimited
ลักษณะ FILE ที่ SAVE ในโปรแกรม SPSS
13
มีนามสกุลเป็น dat ดังนี้
ชื่อfile.dat
การหาตาแหน่งที่อยู่ของ FILE ที่จัดเก็บไว้
14
คลิกที่ Properties เพื่อหาตาแหน่งที่อยู่ของ file ดังกล่าว
ที่อยู่ของ FILE ที่จัดเก็บไว้
15
ตาแหน่งที่ file จัดเก็บ
HOW TO READ DATA? การอ่านค่าข้อมูลเข้ามาใน
โปรแกรม R
การอ่านค่าข้อมูลโดยใช้คาสั่ง read-table (Using read-table to
read files) อ่านจาก DESKTOP ตัวอย่างเช่น
>test<- read.table
("C:UsersUserDesktopADVStat22558DrPisitreg.
dat” ,header=T)
เราจะต้องเปลี่ยน  slash เป็น / Backslash โปรแกรม R จึงจะเข้าใจและหาfile
ดังกล่าวเจอ ดังนี้
>test<- read.table
("C:/Users/User/Desktop/ADVStat22558/DrPisitreg.
dat” ,header=T)
16
การอ่านค่าข้อมูลชุดDrPisitreg.dat แล้วนาไปเก็บไว้
ในfileชื่อtest ถ้าfileเดิมมีชื่อตัวแปรให้อ่านเข้ามาด้วย
ชนิดของข้อมูลที่ใช้ในโปรแกรม R
 Numeric เป็นตัวเลข สามารถนามาคานวณได้
 Character เป็นข้อมูลที่เป็นตัวอักษรคล้ายกับค่า string ในโปรแกรม SPSS
 Logical เป็นข้อมูลที่มีค่าเป็นจริงหรือเท็จ โดย R จะให้สัญลักษณ์ T หรือ
True แทน ข้อมูลที่มีค่าเป็นจริง และ F หรือ False แทนข้อมูลที่มีค่าเป็นเท็จ
17
รูปแบบการเก็บข้อมูลใน R
 Vector ข้อมูล
 Matrices
 Arrays (arrays ซึ่งเป็น matrices เฉพาะ – เป็นที่เก็บรวมเครื่องมือกลางขนาดใหญ่เพื่อการวิเคราะห์ข้อมูล - ภาพ
สาหรับการวิเคราะห์ข้อมูลจะแสดงผลโดยตรงในคอมพิวเตอร์หรือใน hard copy
 - เป็นภาษาพัฒนาได้ง่ายและมีประสิทธิภาพ )
 data frame
หมายเหตุ ในโปรแกรม R ตัวอักษรใหญ่และเล็กมีความหมายแตกต่างกัน
นั่นคือ A จะแตกต่างจาก a
เราสามารสร้างชุดข้อมูลได้อย่างน้อย 26*2 ตัวอักษรภาษาอังกฤษ
18
แป้นลัดทาให้ไม่ต้องพิมพ์ซ้าบ่อยๆ
19
FUNCTION เบื้องต้น
 help(“mean”)
 ?mean
 attributes()
 ls() # list object ว่ามีการเก็บชุดข้อมูลอะไรบ้างในworkspace
 q() #quitจากโปรแกรม R โดย R จะถามว่าต้องการsave งานหรือไม่
20
เครื่องหมาย # เป็นการเขียนโน้ตให้เราทาความเข้าใจได้ง่าย เวลามาอ่านทีหลัง
R จะไม่อ่านข้อความหลัง ”#”
การสั่งเรียกใช้ข้อมูล
 attach(ชื่อชุดข้อมูล) # การระบุชื่อชุดข้อมูลที่จะใช้
 x$mean #เป็นการเรียกผลลัพธ์ของข้อมูลชุดx ตัวแปรmean มาแสดง
 การให้ค่าตัวแปร ใช้เครื่องหมาย “<-” หรือ “=“ ตัวอย่างเช่น
21
> x<-c(4,7,6,3) หรือ
> x=c(4,7,6,3) ก็ได้
ตัวอย่าง
1. จงสร้างเวคเตอร์ x ที่ประกอบด้วยสมาชิก 4, 7, 6, 3
> x<-c(4,7,6,3)
> x
[1] 4 7 6 3
2. จากเวคเตอร์ x สร้างขึ้นในข้อ 1. จงสร้างเมตริกซ์ y ขนาด 2x2
> y<-matrix(x,2,2)
> y
[,1] [,2]
[1,] 4 6
[2,] 7 3
22
3. จงหาอินเวอร์สของเมตริกซ์ y และให้ชื่อว่าเมตริกซ์ z
> z<-solve(y)
> z
[,1] [,2]
[1,] -0.1000000 0.2000000
[2,] 0.2333333 -0.1333333
23
4, จงแสดงให้เห็นว่า y*z = z*y = identity matrix
> y%*%z
[,1] [,2]
[1,] 1.000000e+00 -5.551115e-17
[2,] -5.551115e-17 1.000000e+00
> round(y%*%z) #ทาให้จุดทศนิยมเป็นจานวนเต็ม#
[,1] [,2]
[1,] 1 0
[2,] 0 1
> z%*%y
[,1] [,2]
[1,] 1.000000e+00 -2.775558e-17
[2,] 2.775558e-17 1.000000e+00
> round(z%*%y)
[,1] [,2]
[1,] 1 0
[2,] 0 1 24
 > A<-c(12, 8, 9, 15, 4, 1, 5, 8, 3)
 > A
 [1] 12 8 9 15 4 1 5 8 3
25
6.จากเวคเตอร์ A สร้างขึ้นในข้อ 5. จงสร้างเมตริกซ์ B ขนาด 3x3
> B<-matrix(A,3,3)
> B
[,1] [,2] [,3]
[1,] 12 15 5
[2,] 8 4 8
[3,] 9 1 3
5. จงสร้างเวคเตอร์ A ที่ประกอบด้วยสมาชิก 12, 8, 9, 15, 4, 1, 5, 8, 3
7.จงหาอินเวอร์สของเมตริกซ์ B และให้ชื่อว่าเมตริกซ์ C
 > C<-solve(B)
 > C
 [,1] [,2] [,3]
 [1,] 0.006369427 -0.06369427 0.15923567
 [2,] 0.076433121 -0.01433121 -0.08917197
 [3,] -0.044585987 0.19585987 -0.11464968
26
> round(B%*%C)
[,1] [,2] [,3]
[1,] 1 0 0
[2,] 0 1 0
[3,] 0 0 1
> round(C%*%B)
[,1] [,2] [,3]
[1,] 1 0 0
[2,] 0 1 0
[3,] 0 0 1
27
8. จงแสดงให้เห็นว่า B*C = C*B = identity matrix
การหา EIGENVALUES และ EIGENVECTORS
28
1
e1
ตรวจสอบผลลัพธ์ ของ TRANSPOSE(E1)*E1
29
ผลลัพธ์เป็น 1
REFERENCES
 Krause A and Olson M. (1997). The Basics of S and S-Plus. Springer-Verlag.
New York.
 Gentle, James E. (2002). Elements of Computational Statistics. Springer-
Verlag. New York.
30
THE END
31

More Related Content

What's hot

แนวคิดแผนภาพกระแสข้อมูล
แนวคิดแผนภาพกระแสข้อมูลแนวคิดแผนภาพกระแสข้อมูล
แนวคิดแผนภาพกระแสข้อมูลShengyou Lin
 
พจนานุกรมข้อมูล
พจนานุกรมข้อมูลพจนานุกรมข้อมูล
พจนานุกรมข้อมูลtumetr
 
Data structure intro
Data structure introData structure intro
Data structure introKorn Kpt
 
บทที่ 5 ตัวแปรชุดและตัวแปรกลุ่มอักขระ[w]
บทที่ 5 ตัวแปรชุดและตัวแปรกลุ่มอักขระ[w]บทที่ 5 ตัวแปรชุดและตัวแปรกลุ่มอักขระ[w]
บทที่ 5 ตัวแปรชุดและตัวแปรกลุ่มอักขระ[w]Mook Sasivimon
 
การเขียนแผนภาพ DFD
การเขียนแผนภาพ DFDการเขียนแผนภาพ DFD
การเขียนแผนภาพ DFDskiats
 
บทที่ 5 ตัวแปรชุดและตัวแปรอักขระ
บทที่ 5 ตัวแปรชุดและตัวแปรอักขระบทที่ 5 ตัวแปรชุดและตัวแปรอักขระ
บทที่ 5 ตัวแปรชุดและตัวแปรอักขระMook Sasivimon
 
ตัวแปรชุด
ตัวแปรชุดตัวแปรชุด
ตัวแปรชุดPear Pimnipa
 
ตัวแปรชุดนำเสนอ
ตัวแปรชุดนำเสนอตัวแปรชุดนำเสนอ
ตัวแปรชุดนำเสนอPz'Peem Kanyakamon
 
ตัวแปรชุดและตัวแปรกลุ่มอักขระ
ตัวแปรชุดและตัวแปรกลุ่มอักขระตัวแปรชุดและตัวแปรกลุ่มอักขระ
ตัวแปรชุดและตัวแปรกลุ่มอักขระSirinat Sansom
 
Java Programming [8/12] : Arrays and Collection
Java Programming [8/12] : Arrays and CollectionJava Programming [8/12] : Arrays and Collection
Java Programming [8/12] : Arrays and CollectionIMC Institute
 
ตัวแปรชุดและตัวแปรกลุ่มอิสระ
ตัวแปรชุดและตัวแปรกลุ่มอิสระตัวแปรชุดและตัวแปรกลุ่มอิสระ
ตัวแปรชุดและตัวแปรกลุ่มอิสระLacus Methini
 

What's hot (19)

แนวคิดแผนภาพกระแสข้อมูล
แนวคิดแผนภาพกระแสข้อมูลแนวคิดแผนภาพกระแสข้อมูล
แนวคิดแผนภาพกระแสข้อมูล
 
พจนานุกรมข้อมูล
พจนานุกรมข้อมูลพจนานุกรมข้อมูล
พจนานุกรมข้อมูล
 
Present Unit 5-6 (BC0212)
Present Unit 5-6  (BC0212)Present Unit 5-6  (BC0212)
Present Unit 5-6 (BC0212)
 
11
1111
11
 
Data structure intro
Data structure introData structure intro
Data structure intro
 
บทที่ 5 ตัวแปรชุดและตัวแปรกลุ่มอักขระ[w]
บทที่ 5 ตัวแปรชุดและตัวแปรกลุ่มอักขระ[w]บทที่ 5 ตัวแปรชุดและตัวแปรกลุ่มอักขระ[w]
บทที่ 5 ตัวแปรชุดและตัวแปรกลุ่มอักขระ[w]
 
1 weka introducing
1 weka introducing1 weka introducing
1 weka introducing
 
การเขียนแผนภาพ DFD
การเขียนแผนภาพ DFDการเขียนแผนภาพ DFD
การเขียนแผนภาพ DFD
 
บทที่ 5 ตัวแปรชุดและตัวแปรอักขระ
บทที่ 5 ตัวแปรชุดและตัวแปรอักขระบทที่ 5 ตัวแปรชุดและตัวแปรอักขระ
บทที่ 5 ตัวแปรชุดและตัวแปรอักขระ
 
Weka introducing
Weka introducingWeka introducing
Weka introducing
 
Evaluation metrics: Precision, Recall, F-Measure, ROC
Evaluation metrics: Precision, Recall, F-Measure, ROCEvaluation metrics: Precision, Recall, F-Measure, ROC
Evaluation metrics: Precision, Recall, F-Measure, ROC
 
ตัวแปรชุด
ตัวแปรชุดตัวแปรชุด
ตัวแปรชุด
 
Record
RecordRecord
Record
 
ตัวแปรชุดนำเสนอ
ตัวแปรชุดนำเสนอตัวแปรชุดนำเสนอ
ตัวแปรชุดนำเสนอ
 
ตัวแปรชุดและตัวแปรกลุ่มอักขระ
ตัวแปรชุดและตัวแปรกลุ่มอักขระตัวแปรชุดและตัวแปรกลุ่มอักขระ
ตัวแปรชุดและตัวแปรกลุ่มอักขระ
 
Java Programming [8/12] : Arrays and Collection
Java Programming [8/12] : Arrays and CollectionJava Programming [8/12] : Arrays and Collection
Java Programming [8/12] : Arrays and Collection
 
Outline
OutlineOutline
Outline
 
ตัวแปรชุดและตัวแปรกลุ่มอิสระ
ตัวแปรชุดและตัวแปรกลุ่มอิสระตัวแปรชุดและตัวแปรกลุ่มอิสระ
ตัวแปรชุดและตัวแปรกลุ่มอิสระ
 
DataSet
DataSetDataSet
DataSet
 

Viewers also liked

2014 pwrs brochure kor
2014 pwrs brochure kor2014 pwrs brochure kor
2014 pwrs brochure korTae Yoon Lee
 
Norwegian export credit agency offer mar 2014
Norwegian export credit agency offer   mar 2014Norwegian export credit agency offer   mar 2014
Norwegian export credit agency offer mar 2014Patrisia Ind
 
R programming language 2/57 teaching on March 8, 2015
R programming language 2/57 teaching on March 8, 2015R programming language 2/57 teaching on March 8, 2015
R programming language 2/57 teaching on March 8, 2015Sauwanit Ratanaruamkarn
 
The Effects of Team Backlog Dependencies on Agile Multiteam Systems: A Graph ...
The Effects of Team Backlog Dependencies on Agile Multiteam Systems: A Graph ...The Effects of Team Backlog Dependencies on Agile Multiteam Systems: A Graph ...
The Effects of Team Backlog Dependencies on Agile Multiteam Systems: A Graph ...Tobias Schimmer
 
Google 캘린더 소개
Google 캘린더 소개Google 캘린더 소개
Google 캘린더 소개Tae Yoon Lee
 
Customer-Centered Product and Service Innovation at SAP
Customer-Centered Product and Service Innovation at SAPCustomer-Centered Product and Service Innovation at SAP
Customer-Centered Product and Service Innovation at SAPTobias Schimmer
 
Building Right Things Right - "Design Thinking" and Agile Development as Key ...
Building Right Things Right - "Design Thinking" and Agile Development as Key ...Building Right Things Right - "Design Thinking" and Agile Development as Key ...
Building Right Things Right - "Design Thinking" and Agile Development as Key ...Tobias Schimmer
 
Customer-Centered Innovation at SAP
Customer-Centered Innovation at SAPCustomer-Centered Innovation at SAP
Customer-Centered Innovation at SAPTobias Schimmer
 
Retinal diagram dr sabin sahu
Retinal diagram dr sabin sahuRetinal diagram dr sabin sahu
Retinal diagram dr sabin sahuvoveran312
 
магадлалын онол
магадлалын онолмагадлалын онол
магадлалын онолTsagaanaa Sambuu
 
SAP's Road to Innovation - Design Thinking and Lean Develompent in Practice
SAP's Road to Innovation - Design Thinking and Lean Develompent in PracticeSAP's Road to Innovation - Design Thinking and Lean Develompent in Practice
SAP's Road to Innovation - Design Thinking and Lean Develompent in PracticeTobias Schimmer
 
Design Thinking and Business Model Innovation at SAP - From Efficiency to Inn...
Design Thinking and Business Model Innovation at SAP - From Efficiency to Inn...Design Thinking and Business Model Innovation at SAP - From Efficiency to Inn...
Design Thinking and Business Model Innovation at SAP - From Efficiency to Inn...Tobias Schimmer
 

Viewers also liked (18)

2014 pwrs brochure kor
2014 pwrs brochure kor2014 pwrs brochure kor
2014 pwrs brochure kor
 
Two Page Paper
Two Page PaperTwo Page Paper
Two Page Paper
 
Norwegian export credit agency offer mar 2014
Norwegian export credit agency offer   mar 2014Norwegian export credit agency offer   mar 2014
Norwegian export credit agency offer mar 2014
 
Engage thailand
Engage thailandEngage thailand
Engage thailand
 
สหรัฐอเมริกา
สหรัฐอเมริกาสหรัฐอเมริกา
สหรัฐอเมริกา
 
Camtasiastudio surgalt
Camtasiastudio surgaltCamtasiastudio surgalt
Camtasiastudio surgalt
 
Courseoutlineadvstat
CourseoutlineadvstatCourseoutlineadvstat
Courseoutlineadvstat
 
R programming language 2/57 teaching on March 8, 2015
R programming language 2/57 teaching on March 8, 2015R programming language 2/57 teaching on March 8, 2015
R programming language 2/57 teaching on March 8, 2015
 
Turtle art dadlaga ajil
Turtle art dadlaga ajilTurtle art dadlaga ajil
Turtle art dadlaga ajil
 
The Effects of Team Backlog Dependencies on Agile Multiteam Systems: A Graph ...
The Effects of Team Backlog Dependencies on Agile Multiteam Systems: A Graph ...The Effects of Team Backlog Dependencies on Agile Multiteam Systems: A Graph ...
The Effects of Team Backlog Dependencies on Agile Multiteam Systems: A Graph ...
 
Google 캘린더 소개
Google 캘린더 소개Google 캘린더 소개
Google 캘린더 소개
 
Customer-Centered Product and Service Innovation at SAP
Customer-Centered Product and Service Innovation at SAPCustomer-Centered Product and Service Innovation at SAP
Customer-Centered Product and Service Innovation at SAP
 
Building Right Things Right - "Design Thinking" and Agile Development as Key ...
Building Right Things Right - "Design Thinking" and Agile Development as Key ...Building Right Things Right - "Design Thinking" and Agile Development as Key ...
Building Right Things Right - "Design Thinking" and Agile Development as Key ...
 
Customer-Centered Innovation at SAP
Customer-Centered Innovation at SAPCustomer-Centered Innovation at SAP
Customer-Centered Innovation at SAP
 
Retinal diagram dr sabin sahu
Retinal diagram dr sabin sahuRetinal diagram dr sabin sahu
Retinal diagram dr sabin sahu
 
магадлалын онол
магадлалын онолмагадлалын онол
магадлалын онол
 
SAP's Road to Innovation - Design Thinking and Lean Develompent in Practice
SAP's Road to Innovation - Design Thinking and Lean Develompent in PracticeSAP's Road to Innovation - Design Thinking and Lean Develompent in Practice
SAP's Road to Innovation - Design Thinking and Lean Develompent in Practice
 
Design Thinking and Business Model Innovation at SAP - From Efficiency to Inn...
Design Thinking and Business Model Innovation at SAP - From Efficiency to Inn...Design Thinking and Business Model Innovation at SAP - From Efficiency to Inn...
Design Thinking and Business Model Innovation at SAP - From Efficiency to Inn...
 

Similar to Rppt258 march62016

เอกสารประกอบการอบรม Analysis
เอกสารประกอบการอบรม Analysisเอกสารประกอบการอบรม Analysis
เอกสารประกอบการอบรม Analysisdnavaroj
 
บทนำ
บทนำบทนำ
บทนำSsab Sky
 
การเขียนคำสังควบคุมขั้นพื้นฐาน 2
การเขียนคำสังควบคุมขั้นพื้นฐาน 2การเขียนคำสังควบคุมขั้นพื้นฐาน 2
การเขียนคำสังควบคุมขั้นพื้นฐาน 2Tay Atcharawan
 
พื้นฐานภาษาจาวา
พื้นฐานภาษาจาวาพื้นฐานภาษาจาวา
พื้นฐานภาษาจาวาAeew Autaporn
 
บทนำ1
บทนำ1บทนำ1
บทนำ1Ssab Sky
 
การเขียนคำสั่งขั้นพื้นฐาน(ภาษาC)
การเขียนคำสั่งขั้นพื้นฐาน(ภาษาC)การเขียนคำสั่งขั้นพื้นฐาน(ภาษาC)
การเขียนคำสั่งขั้นพื้นฐาน(ภาษาC)Visaitus Palasak
 
Java Programming [12/12] : Thread
Java Programming [12/12] : ThreadJava Programming [12/12] : Thread
Java Programming [12/12] : ThreadIMC Institute
 
การเขียนคำสั่งควบคุมขั้นพื้นฐาน
การเขียนคำสั่งควบคุมขั้นพื้นฐานการเขียนคำสั่งควบคุมขั้นพื้นฐาน
การเขียนคำสั่งควบคุมขั้นพื้นฐานKEk YourJust'one
 
Java Programming: อะเรย์และคอลเล็กชั่น
Java Programming: อะเรย์และคอลเล็กชั่นJava Programming: อะเรย์และคอลเล็กชั่น
Java Programming: อะเรย์และคอลเล็กชั่นThanachart Numnonda
 
ใบงานที่7 การประยุกต์ใช้งาน
ใบงานที่7 การประยุกต์ใช้งานใบงานที่7 การประยุกต์ใช้งาน
ใบงานที่7 การประยุกต์ใช้งานKrittamook Sansumdang
 

Similar to Rppt258 march62016 (20)

Static excel
Static excelStatic excel
Static excel
 
เอกสารประกอบการอบรม Analysis
เอกสารประกอบการอบรม Analysisเอกสารประกอบการอบรม Analysis
เอกสารประกอบการอบรม Analysis
 
บทนำ
บทนำบทนำ
บทนำ
 
การเขียนคำสังควบคุมขั้นพื้นฐาน 2
การเขียนคำสังควบคุมขั้นพื้นฐาน 2การเขียนคำสังควบคุมขั้นพื้นฐาน 2
การเขียนคำสังควบคุมขั้นพื้นฐาน 2
 
พื้นฐานภาษาจาวา
พื้นฐานภาษาจาวาพื้นฐานภาษาจาวา
พื้นฐานภาษาจาวา
 
บทนำ1
บทนำ1บทนำ1
บทนำ1
 
การเขียนคำสั่งขั้นพื้นฐาน(ภาษาC)
การเขียนคำสั่งขั้นพื้นฐาน(ภาษาC)การเขียนคำสั่งขั้นพื้นฐาน(ภาษาC)
การเขียนคำสั่งขั้นพื้นฐาน(ภาษาC)
 
..Arrays..
..Arrays....Arrays..
..Arrays..
 
Java Programming [12/12] : Thread
Java Programming [12/12] : ThreadJava Programming [12/12] : Thread
Java Programming [12/12] : Thread
 
Learn 3
Learn 3Learn 3
Learn 3
 
การเขียนคำสั่งควบคุมขั้นพื้นฐาน
การเขียนคำสั่งควบคุมขั้นพื้นฐานการเขียนคำสั่งควบคุมขั้นพื้นฐาน
การเขียนคำสั่งควบคุมขั้นพื้นฐาน
 
Know1 3
Know1 3Know1 3
Know1 3
 
08 arrays
08 arrays08 arrays
08 arrays
 
Java Programming: อะเรย์และคอลเล็กชั่น
Java Programming: อะเรย์และคอลเล็กชั่นJava Programming: อะเรย์และคอลเล็กชั่น
Java Programming: อะเรย์และคอลเล็กชั่น
 
Power point
Power pointPower point
Power point
 
Pbl2
Pbl2Pbl2
Pbl2
 
Database1
Database1Database1
Database1
 
ใบงานที่7 การประยุกต์ใช้งาน
ใบงานที่7 การประยุกต์ใช้งานใบงานที่7 การประยุกต์ใช้งาน
ใบงานที่7 การประยุกต์ใช้งาน
 
Db1
Db1Db1
Db1
 
Epi info unit06
Epi info unit06Epi info unit06
Epi info unit06
 

Rppt258 march62016

  • 1. การวิเคราะห์ข้อมูลโดยใช้โปรแกรม R (DATA ANALYSIS USING R PROGRAMMING LANGUAGE) ดร. เสาวณิต รัตนรวมการ สาขาวิชาคณิตศาสตร์และสถิติ ภาควิชาวิทยาศาสตร์พื้นฐาน คณะวิทยาศาสตร์และเทคโนโลยี มหาวิทยาลัยราชภัฏสุรินทร์ sauwanit98@yahoo.com Sunday, March 6, 2016 1
  • 2. OUTLINE  Introduction to R  Source แหล่ง download R  ส่วนประกอบของ R  การอ่านค่าข้อมูลเข้ามาในโปรแกรม R  รูปแบบการเก็บข้อมูลใน R และ แป้นลัดทาให้ไม่ต้องพิมพ์ซ้าบ่อยๆ  function เบื้องต้น เช่น การสั่งเรียกใช้ข้อมูล  Matrix, Inverse Matrix, Determinant  eigen value, eigen vector  แบบฝึกหัด 2
  • 3. INTRODUCTION TO R  โปรแกรมภาษาที่นิยมใช้กันเป็นอย่างมากในทางสถิติภาษาหนึ่ง  ดาวน์โหลดได้ฟรี ภายใต้การดูแลขององค์กรที่ไม่แสวงหากาไร ชื่อ R Foundation  open source  R พัฒนาขึ้นครั้งแรกเมื่อปี ค.ศ. 1990s โดย Robert Gentleman และ Ross Ihaka มหาวิทยาลัยโอคแลนด์ ประเทศนิวซีแลนด์ # เป็นไปได้ที่โปรแกรม R จะมาจากชื่อของ Robert และ Ross  โปรแกรมภาษา R เป็นส่วนที่มีการพัฒนามาจากโปรแกรมภาษา S 3
  • 4. R YOU READY FOR R? 4 Statistics Adjunct professor Robert Gentleman who helped developed the R programming language. (Credit: Stuart Isett for The New York Times)
  • 5. 5
  • 6. SOURCE  ดาวน์โลดได้ฟรี จาก http://cran.r-project.org/  เลือก Download R รุ่นที่ทันสมัยที่สุด  คือ R-3.1.2.tar.gz (52 megabytes, 32/64 bit)  คู่มือการใช้งานเบื้องต้น http://cran.r-project.org/doc/manuals/r- release/R-intro.html 6
  • 7. DOWNLOAD R 3.1.2 FOR WINDOWS 7 Double click ที่นี่
  • 8. 8
  • 9. ส่วนประกอบของ R  R Gui ประกอบด้วย 2 วินโดส์ย่อยคือ R Console ใช้ในการเขียนคาสั่งและแสดงผลลัพธ์ R graph แสดงกราฟต่างๆ 9
  • 10. 10
  • 11. HOW TO READ DATA? การอ่านค่าข้อมูลเข้ามาในโปรแกรม R การอ่านค่าข้อมูลโดยใช้คาสั่ง read-table (Using read-table to read files) อ่านจาก external HD ตัวอย่างเช่น >test<- read.table("g:/drug.dat",header=T) >test<- read.table("g:/grades.txt",header=T) 11 การอ่านค่าข้อมูลชุดgrades.txt แล้วนาไปเก็บไว้ในfile ชื่อtest ถ้าfileเดิมมีชื่อตัวแปรให้อ่านเข้ามาด้วย
  • 12. การนาข้อมูลเข้าจากโปรแกรม SPSS ในรูป TEXT FILE 12 ให้save as file ที่สนใจในรูป Tab delimited
  • 13. ลักษณะ FILE ที่ SAVE ในโปรแกรม SPSS 13 มีนามสกุลเป็น dat ดังนี้ ชื่อfile.dat
  • 14. การหาตาแหน่งที่อยู่ของ FILE ที่จัดเก็บไว้ 14 คลิกที่ Properties เพื่อหาตาแหน่งที่อยู่ของ file ดังกล่าว
  • 16. HOW TO READ DATA? การอ่านค่าข้อมูลเข้ามาใน โปรแกรม R การอ่านค่าข้อมูลโดยใช้คาสั่ง read-table (Using read-table to read files) อ่านจาก DESKTOP ตัวอย่างเช่น >test<- read.table ("C:UsersUserDesktopADVStat22558DrPisitreg. dat” ,header=T) เราจะต้องเปลี่ยน slash เป็น / Backslash โปรแกรม R จึงจะเข้าใจและหาfile ดังกล่าวเจอ ดังนี้ >test<- read.table ("C:/Users/User/Desktop/ADVStat22558/DrPisitreg. dat” ,header=T) 16 การอ่านค่าข้อมูลชุดDrPisitreg.dat แล้วนาไปเก็บไว้ ในfileชื่อtest ถ้าfileเดิมมีชื่อตัวแปรให้อ่านเข้ามาด้วย
  • 17. ชนิดของข้อมูลที่ใช้ในโปรแกรม R  Numeric เป็นตัวเลข สามารถนามาคานวณได้  Character เป็นข้อมูลที่เป็นตัวอักษรคล้ายกับค่า string ในโปรแกรม SPSS  Logical เป็นข้อมูลที่มีค่าเป็นจริงหรือเท็จ โดย R จะให้สัญลักษณ์ T หรือ True แทน ข้อมูลที่มีค่าเป็นจริง และ F หรือ False แทนข้อมูลที่มีค่าเป็นเท็จ 17
  • 18. รูปแบบการเก็บข้อมูลใน R  Vector ข้อมูล  Matrices  Arrays (arrays ซึ่งเป็น matrices เฉพาะ – เป็นที่เก็บรวมเครื่องมือกลางขนาดใหญ่เพื่อการวิเคราะห์ข้อมูล - ภาพ สาหรับการวิเคราะห์ข้อมูลจะแสดงผลโดยตรงในคอมพิวเตอร์หรือใน hard copy  - เป็นภาษาพัฒนาได้ง่ายและมีประสิทธิภาพ )  data frame หมายเหตุ ในโปรแกรม R ตัวอักษรใหญ่และเล็กมีความหมายแตกต่างกัน นั่นคือ A จะแตกต่างจาก a เราสามารสร้างชุดข้อมูลได้อย่างน้อย 26*2 ตัวอักษรภาษาอังกฤษ 18
  • 20. FUNCTION เบื้องต้น  help(“mean”)  ?mean  attributes()  ls() # list object ว่ามีการเก็บชุดข้อมูลอะไรบ้างในworkspace  q() #quitจากโปรแกรม R โดย R จะถามว่าต้องการsave งานหรือไม่ 20 เครื่องหมาย # เป็นการเขียนโน้ตให้เราทาความเข้าใจได้ง่าย เวลามาอ่านทีหลัง R จะไม่อ่านข้อความหลัง ”#”
  • 21. การสั่งเรียกใช้ข้อมูล  attach(ชื่อชุดข้อมูล) # การระบุชื่อชุดข้อมูลที่จะใช้  x$mean #เป็นการเรียกผลลัพธ์ของข้อมูลชุดx ตัวแปรmean มาแสดง  การให้ค่าตัวแปร ใช้เครื่องหมาย “<-” หรือ “=“ ตัวอย่างเช่น 21 > x<-c(4,7,6,3) หรือ > x=c(4,7,6,3) ก็ได้
  • 22. ตัวอย่าง 1. จงสร้างเวคเตอร์ x ที่ประกอบด้วยสมาชิก 4, 7, 6, 3 > x<-c(4,7,6,3) > x [1] 4 7 6 3 2. จากเวคเตอร์ x สร้างขึ้นในข้อ 1. จงสร้างเมตริกซ์ y ขนาด 2x2 > y<-matrix(x,2,2) > y [,1] [,2] [1,] 4 6 [2,] 7 3 22
  • 23. 3. จงหาอินเวอร์สของเมตริกซ์ y และให้ชื่อว่าเมตริกซ์ z > z<-solve(y) > z [,1] [,2] [1,] -0.1000000 0.2000000 [2,] 0.2333333 -0.1333333 23
  • 24. 4, จงแสดงให้เห็นว่า y*z = z*y = identity matrix > y%*%z [,1] [,2] [1,] 1.000000e+00 -5.551115e-17 [2,] -5.551115e-17 1.000000e+00 > round(y%*%z) #ทาให้จุดทศนิยมเป็นจานวนเต็ม# [,1] [,2] [1,] 1 0 [2,] 0 1 > z%*%y [,1] [,2] [1,] 1.000000e+00 -2.775558e-17 [2,] 2.775558e-17 1.000000e+00 > round(z%*%y) [,1] [,2] [1,] 1 0 [2,] 0 1 24
  • 25.  > A<-c(12, 8, 9, 15, 4, 1, 5, 8, 3)  > A  [1] 12 8 9 15 4 1 5 8 3 25 6.จากเวคเตอร์ A สร้างขึ้นในข้อ 5. จงสร้างเมตริกซ์ B ขนาด 3x3 > B<-matrix(A,3,3) > B [,1] [,2] [,3] [1,] 12 15 5 [2,] 8 4 8 [3,] 9 1 3 5. จงสร้างเวคเตอร์ A ที่ประกอบด้วยสมาชิก 12, 8, 9, 15, 4, 1, 5, 8, 3
  • 26. 7.จงหาอินเวอร์สของเมตริกซ์ B และให้ชื่อว่าเมตริกซ์ C  > C<-solve(B)  > C  [,1] [,2] [,3]  [1,] 0.006369427 -0.06369427 0.15923567  [2,] 0.076433121 -0.01433121 -0.08917197  [3,] -0.044585987 0.19585987 -0.11464968 26
  • 27. > round(B%*%C) [,1] [,2] [,3] [1,] 1 0 0 [2,] 0 1 0 [3,] 0 0 1 > round(C%*%B) [,1] [,2] [,3] [1,] 1 0 0 [2,] 0 1 0 [3,] 0 0 1 27 8. จงแสดงให้เห็นว่า B*C = C*B = identity matrix
  • 28. การหา EIGENVALUES และ EIGENVECTORS 28 1 e1
  • 30. REFERENCES  Krause A and Olson M. (1997). The Basics of S and S-Plus. Springer-Verlag. New York.  Gentle, James E. (2002). Elements of Computational Statistics. Springer- Verlag. New York. 30