an introduction to r: get familiar with r guangxu liu bio7932
TRANSCRIPT
![Page 1: An introduction to R: get familiar with R Guangxu Liu Bio7932](https://reader035.vdocuments.us/reader035/viewer/2022081515/56649e5d5503460f94b55413/html5/thumbnails/1.jpg)
An introduction to R: get An introduction to R: get familiar with Rfamiliar with R
Guangxu LiuGuangxu Liu
Bio7932Bio7932
![Page 2: An introduction to R: get familiar with R Guangxu Liu Bio7932](https://reader035.vdocuments.us/reader035/viewer/2022081515/56649e5d5503460f94b55413/html5/thumbnails/2.jpg)
1. Download and installation of R1. Download and installation of R2. Get familiar with R2. Get familiar with R3. Input data and read data from file3. Input data and read data from file4. Commonly used R code 4. Commonly used R code 5. Something need to be aware5. Something need to be aware6. Reference for R6. Reference for R
Step by step proc.Step by step proc.
Outline
![Page 3: An introduction to R: get familiar with R Guangxu Liu Bio7932](https://reader035.vdocuments.us/reader035/viewer/2022081515/56649e5d5503460f94b55413/html5/thumbnails/3.jpg)
1.Downloading R 1.Downloading R official website: official website: www.R-project.orgwww.R-project.org
Where to download
R.
![Page 4: An introduction to R: get familiar with R Guangxu Liu Bio7932](https://reader035.vdocuments.us/reader035/viewer/2022081515/56649e5d5503460f94b55413/html5/thumbnails/4.jpg)
After we choose the correct server and computer system, After we choose the correct server and computer system, we will get something like below. we will get something like below.
What we want is here!
![Page 5: An introduction to R: get familiar with R Guangxu Liu Bio7932](https://reader035.vdocuments.us/reader035/viewer/2022081515/56649e5d5503460f94b55413/html5/thumbnails/5.jpg)
Choose again?!!Choose again?!!
Here it is!
![Page 6: An introduction to R: get familiar with R Guangxu Liu Bio7932](https://reader035.vdocuments.us/reader035/viewer/2022081515/56649e5d5503460f94b55413/html5/thumbnails/6.jpg)
Installation:Installation:
Proc: Proc: Simply use the download installation file.Simply use the download installation file.
Available options: Available options: Choose install directionChoose install direction language (English, simple Chinese only) ?language (English, simple Chinese only) ? Set up the methods for viewing help informatioSet up the methods for viewing help informatio
n(html, txt, chm)n(html, txt, chm) Select components, customize ….Select components, customize ….
![Page 7: An introduction to R: get familiar with R Guangxu Liu Bio7932](https://reader035.vdocuments.us/reader035/viewer/2022081515/56649e5d5503460f94b55413/html5/thumbnails/7.jpg)
2. Get familiar with R (Sorry about using chinese version)
File; Edit; Others; Package; Help function
Basic introduction of R, such as Version…
Working place, where we write code, see the verbal or numerial r
esults
![Page 8: An introduction to R: get familiar with R Guangxu Liu Bio7932](https://reader035.vdocuments.us/reader035/viewer/2022081515/56649e5d5503460f94b55413/html5/thumbnails/8.jpg)
3. Input data and read data from file3. Input data and read data from file
Basic proc:Basic proc: x<x<-function() -function() x=data name; <- assignment; function()=the mx=data name; <- assignment; function()=the m
ethods for reading dataethods for reading data
Function():Function(): c() for example, x<-c(1,2,3,4,5) c() for example, x<-c(1,2,3,4,5) read.table() read.table() scan()scan() Read data using Rcmdr packageRead data using Rcmdr package
![Page 9: An introduction to R: get familiar with R Guangxu Liu Bio7932](https://reader035.vdocuments.us/reader035/viewer/2022081515/56649e5d5503460f94b55413/html5/thumbnails/9.jpg)
Examples: Read data with Rcmdr package
Load package from package
option
![Page 10: An introduction to R: get familiar with R Guangxu Liu Bio7932](https://reader035.vdocuments.us/reader035/viewer/2022081515/56649e5d5503460f94b55413/html5/thumbnails/10.jpg)
Rcmdr
![Page 11: An introduction to R: get familiar with R Guangxu Liu Bio7932](https://reader035.vdocuments.us/reader035/viewer/2022081515/56649e5d5503460f94b55413/html5/thumbnails/11.jpg)
Rcmdr windows (right windows below)
![Page 12: An introduction to R: get familiar with R Guangxu Liu Bio7932](https://reader035.vdocuments.us/reader035/viewer/2022081515/56649e5d5503460f94b55413/html5/thumbnails/12.jpg)
Rcmdr-Data-Inport data (Thanks god, Rcmdr is in English!)
File can be read by Rcmdr includes txt,spss,mintab,stata,excel….
![Page 13: An introduction to R: get familiar with R Guangxu Liu Bio7932](https://reader035.vdocuments.us/reader035/viewer/2022081515/56649e5d5503460f94b55413/html5/thumbnails/13.jpg)
For example: open the excel file contains the data, select the data you want to input and copy it by right click and copy or ctl+c.
Then switch back to Rcmdr windows and choose data i
nput from txt & clipboard
![Page 14: An introduction to R: get familiar with R Guangxu Liu Bio7932](https://reader035.vdocuments.us/reader035/viewer/2022081515/56649e5d5503460f94b55413/html5/thumbnails/14.jpg)
Data name
1.Variable name in first line or not
2. NA
How the data are separated(Tabs here for
excel)
![Page 15: An introduction to R: get familiar with R Guangxu Liu Bio7932](https://reader035.vdocuments.us/reader035/viewer/2022081515/56649e5d5503460f94b55413/html5/thumbnails/15.jpg)
All setting looks fine
now
Read the data
![Page 16: An introduction to R: get familiar with R Guangxu Liu Bio7932](https://reader035.vdocuments.us/reader035/viewer/2022081515/56649e5d5503460f94b55413/html5/thumbnails/16.jpg)
The R code for what we did
Output window? Do not show graphic
results
General information of
the data
![Page 17: An introduction to R: get familiar with R Guangxu Liu Bio7932](https://reader035.vdocuments.us/reader035/viewer/2022081515/56649e5d5503460f94b55413/html5/thumbnails/17.jpg)
An example show how t-test results displayed in output windows
T test R code
Results form the t-test
![Page 18: An introduction to R: get familiar with R Guangxu Liu Bio7932](https://reader035.vdocuments.us/reader035/viewer/2022081515/56649e5d5503460f94b55413/html5/thumbnails/18.jpg)
Lot’s functions
here, such as make graph, load statistics
package
![Page 19: An introduction to R: get familiar with R Guangxu Liu Bio7932](https://reader035.vdocuments.us/reader035/viewer/2022081515/56649e5d5503460f94b55413/html5/thumbnails/19.jpg)
R code for box plot
If we make graph (box plot for example)
![Page 20: An introduction to R: get familiar with R Guangxu Liu Bio7932](https://reader035.vdocuments.us/reader035/viewer/2022081515/56649e5d5503460f94b55413/html5/thumbnails/20.jpg)
Graph result shows in R graphics window (right windows)
![Page 21: An introduction to R: get familiar with R Guangxu Liu Bio7932](https://reader035.vdocuments.us/reader035/viewer/2022081515/56649e5d5503460f94b55413/html5/thumbnails/21.jpg)
View data and edit
your data
View data window shows the data
View data and edit data after data input
![Page 22: An introduction to R: get familiar with R Guangxu Liu Bio7932](https://reader035.vdocuments.us/reader035/viewer/2022081515/56649e5d5503460f94b55413/html5/thumbnails/22.jpg)
Edit data in the data edit windows
![Page 23: An introduction to R: get familiar with R Guangxu Liu Bio7932](https://reader035.vdocuments.us/reader035/viewer/2022081515/56649e5d5503460f94b55413/html5/thumbnails/23.jpg)
4. Commonly used R code4. Commonly used R code ls()ls() show all the available data (objects) show all the available data (objects) in the work space or memoryin the work space or memory rm()rm() delete data, remove objects delete data, remove objects ? ? get online help about *** get online help about *** lm()lm() fit a simple regression and look at it fit a simple regression and look at it resid()resid() residuals from one model residuals from one model fitted()fitted() fits from one model fits from one model plot()plot() scatter plot scatter plot
5. Something needs to be kept in mind
Capital letter matters: v≠V
Data (Objects) are vector or matrix
Some examples
![Page 24: An introduction to R: get familiar with R Guangxu Liu Bio7932](https://reader035.vdocuments.us/reader035/viewer/2022081515/56649e5d5503460f94b55413/html5/thumbnails/24.jpg)
VCL and vcl are different dat
a objects
Commends: don’t use capital letter at all is a good idea
Use ls() show the data in workspac
e
![Page 25: An introduction to R: get familiar with R Guangxu Liu Bio7932](https://reader035.vdocuments.us/reader035/viewer/2022081515/56649e5d5503460f94b55413/html5/thumbnails/25.jpg)
Use “?ls” to learn more about ls() fuct
ion
Online help(don’t need
internet connection
in fact)
![Page 26: An introduction to R: get familiar with R Guangxu Liu Bio7932](https://reader035.vdocuments.us/reader035/viewer/2022081515/56649e5d5503460f94b55413/html5/thumbnails/26.jpg)
Use ?lm learn more about lm()
There are lot’s components in lm()
Details about all those components
![Page 27: An introduction to R: get familiar with R Guangxu Liu Bio7932](https://reader035.vdocuments.us/reader035/viewer/2022081515/56649e5d5503460f94b55413/html5/thumbnails/27.jpg)
A simple example (without data input part)A simple example (without data input part)
Step 1:Read and built
a new data with name f
Use f to view the data f in workspace
Data from fertilization data of Liu (2006), and we know there should have a simple linear relationship between ln and density.
Density=log(sperm/egg density) ln=log(fertilized egg/unfertilized egg).
So the model will be ln=a+b*density+error
![Page 28: An introduction to R: get familiar with R Guangxu Liu Bio7932](https://reader035.vdocuments.us/reader035/viewer/2022081515/56649e5d5503460f94b55413/html5/thumbnails/28.jpg)
Use fm<-lm(ln~density,data=f) to build up the linear model
fm<-lm(ln~density,data=f)
Assign fm as the linear model
Model is basedon ln and density,
use ln as dependentand density as variable
There ln and density come from
![Page 29: An introduction to R: get familiar with R Guangxu Liu Bio7932](https://reader035.vdocuments.us/reader035/viewer/2022081515/56649e5d5503460f94b55413/html5/thumbnails/29.jpg)
Use fm to view fm
Shows what’s
fm
![Page 30: An introduction to R: get familiar with R Guangxu Liu Bio7932](https://reader035.vdocuments.us/reader035/viewer/2022081515/56649e5d5503460f94b55413/html5/thumbnails/30.jpg)
Use summary(fm) to see more details
Details of fm, including df, SE, t value and P
![Page 31: An introduction to R: get familiar with R Guangxu Liu Bio7932](https://reader035.vdocuments.us/reader035/viewer/2022081515/56649e5d5503460f94b55413/html5/thumbnails/31.jpg)
Use plot(fitted(fm),resid(fm),xlab="fitted",ylab="residual",main="residual vs fUse plot(fitted(fm),resid(fm),xlab="fitted",ylab="residual",main="residual vs fits") check residual vs fits. Graph shown on the right side windowits") check residual vs fits. Graph shown on the right side window
Here xlab(),ylab(),main() are R codes to give x,y and the whole graph labels.
![Page 32: An introduction to R: get familiar with R Guangxu Liu Bio7932](https://reader035.vdocuments.us/reader035/viewer/2022081515/56649e5d5503460f94b55413/html5/thumbnails/32.jpg)
6.Reference for R (http://www.r-project.org)6.Reference for R (http://www.r-project.org)
This makes me change my presentation title to “An introduction to R: get familiar with R”
1.Free reference on R official website2. Practical Regression and Anova using R, Julian J. Faraway, 2002
![Page 33: An introduction to R: get familiar with R Guangxu Liu Bio7932](https://reader035.vdocuments.us/reader035/viewer/2022081515/56649e5d5503460f94b55413/html5/thumbnails/33.jpg)
ThanksThanks
Picture above from www.R-project.org