36
กก R

การใช้งานโปรแกรม R เบื้องต้น

Embed Size (px)

DESCRIPTION

 

Citation preview

Page 1: การใช้งานโปรแกรม R เบื้องต้น

ก����������ก�� R ���������

Page 2: การใช้งานโปรแกรม R เบื้องต้น

��������ก�� R

� �����ก�������������������ก������� �!��"�����#$%���&!��"�"����'��ก���������%���&!���&���%��ก��( #����)�*�+������,-�-�+�.ก'�ก'���ก�������/0�&�����"�"����. ��1� SPSS,MATLAB

� �����ก����������������������,-�(�� �������������&� �!���ก��+':���;1���1������� ���������0�������������-�����ก������� -�����"�"����������;�1�!"%�"�<"=��ก�������������

� �����ก�����������;���'�0�ก�'ก��"�"0�ก�'��!ก ��ก��������������;1���+�1#!�; ��ก��+':�� package ���"���ก������� �"�����#$�����"�"�;&1���0�������ก �#��������!��ก������,-������������ก��

� �����ก�����������������,-���#!�;����D"�'�"ก�� ,-��ก1 Linux, MacOS X, Windows

Page 3: การใช้งานโปรแกรม R เบื้องต้น

ก�� Download �!��"-�'����ก�� R1. �%��,��� http://www.r-project.org/ 0���!"�ก$ download R -'����!&ก[�������&

���#�'��#� download ��ก�� R ,������

Page 4: การใช้งานโปรแกรม R เบื้องต้น

ก�� Download �!��"-�'����ก�� R2. ������%������� download R �!��0�+�ก'�#��� Mirror Links �#��!����!���-���!1�� 0�+� Mirror Links %�� Thailand ]^��0����#��!��ก�������;&1 2 Mirror Links -'����&

�#�ก-�!��ก�%��,�� link �-ก/,-�

Page 5: การใช้งานโปรแกรม R เบื้องต้น

ก�� Download �!��"-�'����ก�� R3.�!��ก����`"�'�"ก�����0������ก�� R ,������ ]^��0����#��!��ก�;&1 3 ������

Linux, MaxOS X, Windows ���������!��ก�������`"�'�"ก�� windows

Page 6: การใช้งานโปรแกรม R เบื้องต้น

ก�� Download �!��"-�'����ก�� R4. #�ก����D"�'�"ก����� windows 64 bits �#��!��ก,��� !"�ก$���!&ก[� #��;�!%1 �!��0�ก�'���!��ก,���!"�ก$ base �1�����#�ก��� windows 32 bits �#��!��ก,���!"�ก$ base ���!&ก[�#��;�!% 2

2

1

Page 7: การใช้งานโปรแกรม R เบื้องต้น

ก�� Download �!��"-�'����ก�� R5. 0�ก�'�����ก��,�!"ก���!"�ก$ Download R 2.11.1 for windows ���!&ก[�������

Page 8: การใช้งานโปรแกรม R เบื้องต้น

ก�� Download �!��"-�'����ก�� R6. 0�ก�'��0���#����1�� File Download %^�����#��!��ก -;�#��!��ก Save �!��ก��#�-�1�0� Save ,�����,#� 0�ก�'����ก��ก/0���"���&ก download ������������%�����

Page 9: การใช้งานโปรแกรม R เบื้องต้น

ก�� Download �!��"-�'����ก�� R7. ����� Download ,(!$�"-�'����ก�� R 0����/0���&� $��� 100% �!���#��!��กก-

kl� Run -'����& #�����ก�"<�����#����ก��-'���"!�!"ก���,(!$ R-2.11.1-win32.exe ���,-�

������ก�� download ���+�����"�����ก���"-�'����ก��

Page 10: การใช้งานโปรแกรม R เบื้องต้น

ก�� Download �!��"-�'����ก�� R

8. 0���#����1���#��!��ก*�n����������1������"-�'����ก�� ]̂���#��!��ก���

�1� default ���*�n� English ����& ]̂���#�ก-kl� OK ,-��!;

Page 11: การใช้งานโปรแกรม R เบื้องต้น

ก�� Download �!��"-�'����ก�� R9. 0�ก�'�����ก���!"กkl� Next �!���!"ก Next ��ก����#����1��ก���"-�'����ก��-'����

��-����&�'�����

Page 12: การใช้งานโปรแกรม R เบื้องต้น

ก�� Download �!��"-�'����ก�� R10. 0�ก�'��0����̂�#����1�������ก�� 0�����1�0��#��"-�'����ก��!�������#�1���������ก��ก��#�-#���,�1 #���)&�������0�ก��#�-�#�1ก/�#�ก-kl� Browses���������#��!��ก��������ก��ก��#�- �!���#��!"กkl� Next �+���,;'�ก���"-�'��!��-'��1�,

Page 13: การใช้งานโปรแกรม R เบื้องต้น

ก�� Download �!��"-�'����ก�� R11. 0�ก�'��0����̂�#����1��ก���"-�'����ก�����0��#��!��ก�&�����ก���"-�'����ก�� ]̂�����#��!��ก 4 ��� ,-��ก1 ��� User installation �������������'��., ��� Minimal user installation �������������!/ก.���;. �'-�1�����,�10�������ก ��� Full installation ����"-�'���kก�;1��������#��!��ก �!���� Custom installation����#�)&��������������!��ก�1�����0��"-�'��,-� -;���������!��ก������ Full installation 0�ก�'���!"กkl� Next

Page 14: การใช้งานโปรแกรม R เบื้องต้น

ก�� Download �!��"-�'����ก�� R12. ��ก���"-�'��!��-'��1�,�#��!"กkl� Next ,�����;. 0�ก���'���̂�#����1��

�"-�'����ก��!��������0�"� ������"-�'�����/0�!��ก/ก-kl� Finish

Page 15: การใช้งานโปรแกรม R เบื้องต้น

ก����������ก�� R ������������ก���w-��ก�� R -;-'���"!�!"ก��� Shortcut ������� R 2.11.1 ���#��� Desktop %^����0�,-�#����1�� R Console -'����&

Page 16: การใช้งานโปรแกรม R เบื้องต้น

ก�� Execute ����'���� R Console�&������ 1 ก��+"�+$����'��!��� R Console -;���;ก�'��;1����1� ก��+"�+$����'�� sqrt(625) �!��ก- Enter ��ก��ก/0�����!)!�!���1�)!!'+<$��ก��-'����& ��� 25 ]^��ก����������!'กn ����0��#������#�'�ก��������������k-����'�����;. ,�1;��#���]'�]�����ก

Page 17: การใช้งานโปรแกรม R เบื้องต้น

ก�� Execute ����'���� R Console�&������ 2 ก��+"�+$�k-����'��!���,(!$ R]^�����������,-�-'����1. ���ก���w-��ก�� text editor �'��,%^���� ��1� notepad �!�����ก��+"�+$�k-����'���������ก��!�,-'����&�'��;1��

Page 18: การใช้งานโปรแกรม R เบื้องต้น

ก�� Execute ����'���� R Console2.��������ก��+"�+$�k-����'���������ก�����/0�!��ก/���ก�� save �#��;&1���&���,(!$ R ]^�����������,-�-'���� ��� �'������,(!$���������ก�� save �#��;&1���&��� *.r ��1� test.r ������ �!�����ก���!��ก Save as type ��� All Files 0�ก�'��0^����ก�� Save -'����&�'��;1��

Page 19: การใช้งานโปรแกรม R เบื้องต้น

ก�� Execute ����'���� R Console3. ���ก��#!-,(!$ R ������0k�k-����'��%^�������ก�� Execute ]^�����������,-�-'�������

��� R Console ,��� File->Source R code �!�����ก���!��ก,(!$ R �������ก�� -'����&

�'��;1�� �!���k-����'��0��&ก����!)!-;�'���

Page 20: การใช้งานโปรแกรม R เบื้องต้น

ก�� Execute ����'���� R Console��ก��#!-,(!$����'���%�������������������,-���ก�"<� ��� +"�+$����'�� �+������ก��

#!-,(!$�k-����'���%��������!)! -;�������'�� source(~����,(!$ R�) �!�� Enter

;ก�'��;1�� ��1� source(~c:/test.r�) ������

Page 21: การใช้งานโปรแกรม R เบื้องต้น

ก���"-�'���!����;ก package ���"��%�������������#�'�ก���"-�'�� package ���"��%�����������'�� �"�����%�-,�1,-���� �����������������'������������

�������1�ก'� Internet ,-� ���#�'�ก���"-�'���'�����,-�-;�%��,��� ���& packages %�� R Console �!��

�!��ก,��� install package(s) ������!��ก�!��0��� CRAN mirror %^�����#��!��ก �#�����!��ก Mirror %��

Thailand ]̂�����#��!��ก�;&1 2 ������ ���ก�k���+ �!���� #�-�#�1 �#��!��ก�'��-�'�#�̂�� �!��0�����;����

packages �1��.%^������-��#��!��ก�"-�'��-'������-����&�'��;1��

Page 22: การใช้งานโปรแกรม R เบื้องต้น

ก���"-�'���!����;ก package ���"��%������������������0���-�ก���"-�'�� package ������� xlsReadWrite

]^����� package �������#���ก�� R �'���������1��

�!��%�;�%���&!��,(!$ excel(2003) ,-� ������!��ก

package �������ก���"-�'�� ]^����� xlsReadWrite -'����&

�!��ก- OK ก/0���"��ก�� Download package �!�0��&ก

�"-�'���#�-;�'���'�

Page 23: การใช้งานโปรแกรม R เบื้องต้น

ก���"-�'���!����;ก package ���"��%�������������#�'� package ����&ก�"-�'���!���'�� 0�;'�,�1������������,-��'��� ������0�ก��ก�� R 0�#!-

��+�� package +��������1��'��%^�����#�-;�'���'� ����������ก��#!- package ����"-�'�����"�

�'����� -;ก��,������& Packages �� R Console �!��0�ก�'���!��ก���& load packages #!'�0�ก

�!��ก0��� ��;���� package ����"-�'���!���������������#��!��ก�+���#!-�%���������� ���������#��!��ก

xlsReadWrite 0�ก�'�� package ก/0��&ก load �%�����!���-���;!����;-�ก��;�ก'� package -'����&

Page 24: การใช้งานโปรแกรม R เบื้องต้น

ก���"-�'���!����;ก package ���"��%����������0�กก��#!- package xlsReadWrite �%�����������'��,-���%������%^������-� �#����

���ก���"-�'��,(!$���0������+"�� �+����#� package ������,-����ก�" -;�������'��������

%��������������� xls.getshlib() �����+"�$����'���!�� Enter ��ก�� R ก/0����ก��

download �!��"-�'��,(!$�+"����"��#��'���'�" �!����#�ก,�1��%��)"-+!�-�-. ก/0�

��-�%�������1��"-�'�����/0���&� $ ]^�������1��'���-�;���1��'�� ��ก��#!- package

��'���1�,,�1���������ก

Page 25: การใช้งานโปรแกรม R เบื้องต้น

�-��������� package ����"-�'�����#�'�%���&!��ก�������� package ���#!-�%�����������'�� ������-&,-�-; �������'�� help(~���� package�) ;ก�'��;1����1� Help(~xlsReadWrite�) �!��ก- Enter ก/0��� ��;!����;-ก��������%^�����#�[̂กn� �����������0��-��������� ก���1��%���&!0�ก,(!$ excel �%��������ก�� R)1�� package xlsReadWrite -;�������'�� ������[̂กn��� help %�� package ]̂�������������'��-'������� rdata <- read.xls( "c:/test.xls") �!�� Enter �!��+"+�$ rdata �!�� Enter �+�����-�%���&!�� rdata 0�,-�)!-'��&�'��;1��

Page 26: การใช้งานโปรแกรม R เบื้องต้น

�'��;1��ก�������ก�� R ,���0�"��������� ก��� ���� �������ก��� ����������������� (Completely Randomized Design)0�;$���#�'�ก���-!��

)&�0'-ก�������)!"�!�ก������#1�#�^������ก��[̂กn��1� �'�����/���;+�����#�'�%'���!����!�ก��������)!"�,-�0�

��)!�1�0�����ก�������������#�"�1�!/��( ���0k!/��!� 500 ก����� ) #���,�1�;1��,� �%�0̂��-"��������0'ก�-��;�'�����/�

4 ��-'���� ��-'� 1, 2, 3 �!� ��-'� 4 ����1!���-'��!��ก�'��;1���� 4, 5, 3 �!� 5 !/�� ���!��-'� ��1!�!/���'�0�����

ก�������������#�",-�-'����

��� R ��ก���"�����#$����������+������0����1��'���������/�%����;+�����#�'�%'���!����!�ก��������

)!"�,-�0���)!�1�0�����ก�������������#�" ,-�-'����

Page 27: การใช้งานโปรแกรม R เบื้องต้น

�012�3���2�4�����25����ก��67ก8� ��� ������/�%����;+�� ]̂���� 4 ��-'������09��ก��67ก8� ��� �'���������/�%����;+�����#�'�%'���!����!�ก��������)!"�,-���)!�1�0�����ก�������������#�"�1�!/��:����04�����25����ก��� ��� ��� 0�����ก�������������#�"�1�!/�� µj

-;��� µj ���0�����ก�������������#�"�1�!/�� 0�ก��;+�����#�'�%'���!����!�ก���������'���������/���-'���� j, j = 1, 2, 3, 4ก��4��2��;����� �������������;�%���&!-;����� R ,-�-'�����#� y ���0�����ก�������������#�"�1�!/�� �#� treatment ��� �'�����/���;+���� R console �#�+"�+$����'��-'����

y <- c(37,35,38,36,37,35,38,36,37,32,36,40,35,27,33,31,29)treatment <- rep(c(1,2,3,4),c(4,5,3,5))

0�ก����'��%������ ���ก���������ก����$ y ���������"ก���0�����ก�������������#�"�1�!/��0�ก��1!��������$ �!��������ก����$ treatment ���������"ก�����-'�%����������$������ก'��1�%�� y �+������0����,�����ก������� �1��1��.�����"�"�1�, ]̂��0�,-�)!-'����&�'��;1���1�,���

Page 28: การใช้งานโปรแกรม R เบื้องต้น
Page 29: การใช้งานโปรแกรม R เบื้องต้น

0�ก�'��0^������ #��1�����k- �1��&��k- �1���!��; �!��1�����;�����������%��0�����

ก�����!�ก�������������#�" 0����ก����'���������/�%����;+��%'���!���� ]^��

����������� ,-�-;�������'�� tapply() ��;kก�$ก'�����'������ -'����

tapply(y,treatment,min) ����� �1�����k-

tapply(y,treatment,max) ����� �1��&��k-

tapply(y,treatment,mean) ����� �1���!��;

tapply(y,treatment,sd) ����� �1�����;�����������

]^��0�,-�)!-'����&�'��;1�������-���#����'-,

Page 30: การใช้งานโปรแกรม R เบื้องต้น
Page 31: การใช้งานโปรแกรม R เบื้องต้น

0�ก%���&!���,-�0�กก������� �������������k�������� ��"�"��"�

+�� �,-�-'����

Page 32: การใช้งานโปรแกรม R เบื้องต้น

0�ก�'�����ก������� box plot %��0�����ก�������������#�" 0����ก

����'�����/�%����;+�����#�'�%'���!����

-;�������'��-'����

treatment <- factor(treatment)

plot (treatment, y, xlab=~treatment level�)

Page 33: การใช้งานโปรแกรม R เบื้องต้น

0�ก�'�����ก���-���ก����1�ก'�%������������!��"�����#$��������� -'�����'��-'����

Barrtlett.test(y~treatment)]̂��0�,-�)!-'��'��;1���1�,���

Page 34: การใช้งานโปรแกรม R เบื้องต้น

�-���������1�ก'�%�����������

H0 : σ12= σ2

2= σ32= σ4

2

H1 : σn2≠ σm

2 , ���#�'���� n ≠ m

Bartlett�s K-squared ���1���1�ก'� 5.8242 �!��1� P ���1� 0.1205 ]̂����กก�1� 0.05 0̂���������k,-��1��1����������

%��0�����ก�������������#�"0�ก��;+��%'���!���������-'�������/��'�� 4 ��-'���ก�1��ก'��;1��,�1���';����'�

���ก��#� ANOVA �!������k��ก��-��;����'���1�,���

candefect.anova <- aov(y~treatment)

summary(candefect.anova)]̂��0�,-�)!-'����&�'��;1���1�,���

Page 35: การใช้งานโปรแกรม R เบื้องต้น

���<����0�10:�;����2�4����

�����09��ก��� ���

H0 : µ1 = µ2 = µ3 = µ4

H1 : µm ≠ µn ,���#�'���� m ≠ n = 1, 2, 3, 4

ก��#�- α = 0.05 �1���"�" F= 5.497 ���1���กก�1��1��"ก��" F0.05,(3,13) = 3.41 #����1� p ���1���1�ก'�

0.012 ]̂�����;ก�1� 0.05 0̂�D"��< H0

���= �'���������/�%����;+�����#�'�%'���!����!�ก��������)!"�,-����"�<"+!�1�0�����ก�������������#�"�1�!/��

�;1�����';����'������"�" (α = 0.05)>���4>�� 0�;$�'��;1��%�������0�ก#�'���� �)����ก���-!���!�ก���"�����#$%���&!-;�����ก��

��1�-; ����"+;$ 0'���กk! ��%��"����"�" *���"��� "�[����$ � ��"�;�[����$ �#��"�;�!';��%!����"���$

Page 36: การใช้งานโปรแกรม R เบื้องต้น

�#!1�%���&!ก����������ก�� R ����1����0

� http://www.rforthai.net as the web community for Thai R-Project users� http://www.clinicalepi.com/2009/02/r-odds-ratio.html as the example of the use of R to

compute Odds Ratio� http://www.hsri.or.th/upload/R2R2010/powerpoint/R2R-Using%2520R%2520Program.pdf ก��

�����ก�� R �1�;��ก���"�����#$�"0'; R2R� http://bls.buu.ac.th/~f52325/Homework_3_9_52/50034418/50034418.doc R on Ubuntu� http://www.kmitl.ac.th/~kskallay/pdf/R%20program1.pdf ก�������ก�� R ��ก���"�����#$

%���&!�����"�"� http://cran.r-project.org/manuals.html R manual� #�'��������)�ก���-!���!��"�����#$%���&!-;�����ก�� R -; ����"+;$ 0'���กk!