17673_r programming help file

Upload: harmanwalia

Post on 06-Mar-2016

221 views

Category:

Documents


0 download

DESCRIPTION

r tool

TRANSCRIPT

Tutorial how to use R

PAGE

Tutorial: Rapid intro to R

Install programs

R is a software package especially suitable for matrix data analysis and graphical representation. R can be used as a statistical tool but also as a programming language itself, making it very flexible and highly customizable. Graphical tools make R an ideal environment for exploratory data analysis and for preparing publication ready figures (exportable as .jpg files). All work is done in command style text functions and therefore it is different from other windows style programs (like SPSS) that use menus with select and click options for predefined statistical procedures. It takes considerable time to learn to use R, but once you have passed the first burden, it is quite convenient to handle. As a programming language it is not for beginners, but rather for advanced users for whom the statistical functions of Microsoft Excel are no longer enough. For example if you want to do Principal Component Analysis. In comparison to SAS and SPSS which are very expensive commercial programs for doing statistics, R is free software distributed under the GNU and GPL license terms. There are versions of R for Unix, Windows and Macintosh at the official CRAN website: http://cran.r-project.org/Follow the download instructions to install R locally on your computer. When running the program you will be presented with the R Console window.

# I strongly recommend that you install a text editor program like Tinn-R. This can be used to store scripts and send command lines directly into R. Get the editor for free at the website: http://www.sciviews.org/Tinn-R/Tutorial: Rapid intro to R

# When I installed the R program and first saw the R console, I asked myself: now what? How do I enter my data? How can I do a simple T-test or ANOVA with this program? Even though there are many R tutorials on the internet explaining the many functions and features of R, it was quite tedious for me to start using R in a meaningful way. I had to spend over 3 weeks reading instructions and documents to get to a point where I could use R in a productive way. I do use Excel for all my basic calculations and some analysis. I like Excel very much, but I am also aware of its limitations for more advanced statistical analysis, for example for doing PCAs, K-means clustering or general linear models.# Therefore, I decided to write this tutorial as a short cut introduction to R with practical examples of statistical analysis. My intention is not to show all the features of R, but rather to list some commands and functions that I commonly use for my work. I do research within the field of biological sciences, particularly in plant biochemistry, metabolomics, maize breeding and microarrays. # The R console has a prompt in which you can type the commands directly. In the following I will put some lines in blue. This refers to command code for R. The # sign denotes comments that are not interpreted by the R console. I use them here to explain some features of R. Copy the lines containing blue text and paste them into the R console. Instead of doing copy-paste from Word to R, you can also open this document in Tinn and select the blue lines and then click on the button send line to R. By observing the output you will learn to use the R program.

# Introduction to R (Basic usage)# R uses objects and functions. The objects contain data, while the functions use the data inside the objects and perform calculations and give outputs. If you want to see all functions of R then type:help() # will open a window with general help info (list all functions)help(var)

# specific help on the function var (variance)help(sd) # specific help on the function sd (standard deviation)?sd # alternative way to call for help on the function sd#Most functions in R need an open and closed bracket for entering variables or attributes for the respective function. This is needed even though sometimes the brackets are left empty.

#Everything you type in R is case sensitive. As in unix command line, in R you can recall previously typed commands using the up and down arrow keys.# Defining and editing objects manually#To work with R you first need to define objects. An object can be a variable, a vector, a dataframe or a matrix, depending on the amount of data and its structure. Choose a name for an object that is not a name for a function. Some possible names for objects are: a, b, c, x, y, z, data1, data2, matrix1, model1, etc.b = 9 #to create a variable named b with only 1 value. If you need entering more values then use the combine function as follows:x = c(1,3,2,7,9) #the c function creates objects with several values (vector) y = c(a=1,a=3,b=2,c=7,c=9,d=2,d=3) #define y with variable names and valuesz=matrix(c(10,20,30,40,50,60),ncol=2) #define matrix with 2 columns. Values are equally distributed into the two columns. The first column is filled first and downwards.b; x; y; z #show the contents of objects b, x, y and z

# You can choose several symbols to define objects. You can use the symbol =, but also with the symbols of depending on the side in which you put the variable. I prefer to use = because it is only one keyboard strike, but for some obscure reason most R tutorials use File -> Change directory and select a folder or directory of your choice. You can also do it manually on the console:setwd("C:/Axel StatisticsR") # sets the working directory manuallygetwd()

#displays the path for the working directorydir()

# lists the files in the current work directory# for importing data from a tab delimited text file you have several options:data1=read.table("file.txt", header=T, sep="\t", row.names = 1)

# The header option T or F lets you specify if your data has a header in the first row. The sep="\t" option lets you define the separation symbol between values, in most cases separated by tab. The sep="\t" option will not work for files with format csv (comma separated values), in those cases use the option sep=",". The row names option lets you specify names for the rows. # The text file must be located in the default directory of R. Use setwd to define the correct directory. A more convenient way to upload files is with the file.choose() command. With this function you do not need to remember or type the filename manually. You can also browse the folder with windows. For importing data from standard tab delimited text file use the command:data1=read.table(file.choose(),header=T) # select file with a popup window# If your datafile has no variable names in first row then use option header=F Remember that everything you type in R is case sensitive. Remember that you can recall previous commands using the up and down arrow keys to correct any typing mistakes.

# Analysis of objects#If you have defined an object x, you can do simple things like:

x

# shows data stored in the object x on screen sort(x)# list data increasing order length(x) # number of items in x#With data objects stored in the workspace one can perform many individual functions like sum(x),max(x), min(x), median(x), mean(x), but it is better to use the summary function that integrates several of them:summary(data1)# shows a summary of x (min, max, mean, median, quartiles)sd(data1, na.rm = T) #standard deviation of elements in data1. Option of NAs removedsd(data1)/sqrt(length(data1))#gives the standard error of data in data1var(data1, na.rm = T)#variance value, or covariance matrix of elements in data1. Option of NAs removed

# Short note on function var(). If only given one dimensional vector object, the result is the total variance of the dataset. If the object is a more dimensional matrix, then var() gives a covariance matrix, equivalent to the function cov(). In a covariance matrix, elements in diagonal are variances within that variable, and the other elements are the covariances between different variables. Covariance is a measure of how much the variables are independent or correlated. A covariance matrix can be numerically scaled to a correlation matrix with the function cov2cor().plot(data1)#matrix correlation plots with all elements in data1

plot(data1[2:5])#matrix plots with selected columns 2-5 of data1

cor(data1, use = "pairwise.complete.obs") #correlation coefficient matrix of all elements in data1round(cor(data1, use = "pairwise.complete.obs"),2) #correlation matrix rounded to 2 decimalsround(cor(data.frame(data1$var1, data1$var2, data1$var3)),2) #correlation matrix rounded to 2 decimals with some selected variables only

round(cor(data1[1:4]),2) #same as previous with columns 1 to 4 selected

# Defining new objects# Functions typed alone show the results only on the screen. If you need to do further calculations with the results of a given function, you need to use object= to define a new object which will store the output in the workspace memory. For example:

matrix1=var(data1)# stores the covariance matrix in new object called Matrix1matrix2=cor(data1) # store the correlation coefficient matrix of elements in data1matrix1; matrix2 #shows the contents of the two newly created objects. The semicolon allows separating two functions given in the same line#All objects are stored in the RAM memory of R. When using R no file is written to the hard-disk unless you save it specifically. When you quit R program you can save the workspace so that all objects created during the current session are available next time you work with R.ls()

#to list all objects stored in the current workspace

rm(x)

#to remove object x from workspacematrix1 # to list the content of the Matrix1 object in the console window

edit(data1)# It lets you print editions on screen but it does not save changes in the object memory. To edit and save changes use the following command:

data1=edit(data1)# to re-edit the contents of object data1 and saving it

fix(data1) # a simpler command that does the same as the above

# Working with matrices# Perform some matrix manipulations like:data3=2*data1# define a new matrix with scalar matrix multiplication by factor 2data4=data1+data3 # matrix additiondata5=solve(data1) # define a new matrix using the inverse matrix of data1data2=t(data1) # define a new matrix using the transposed matrix from data1data6=data.frame(data1,data3) # use data.frame for adding elements to objects# Extracting the component of a matrix involves one or two indices [row, column]. data1[2:3] #elements of data1 at the second and third columnsz=data1[2:3] #define new object with selected columns from data1data1[2,3] #element of data1 at the second row, third columndata3=read.table(file="clipboard", header=F, sep="\t") # import data without header

data3=data.matrix(data3) # convert data.frame to a data matrix.# Eigenvalues and eigenvectors of a matrix is handled by eigen() function:

m2=matrix(c(10,20,30,40),ncol=2)

eigen(m2)#Attach and detach

# The attach function is used to access the variables inside an object. Use detach to get rid of those variables.attach(data1) # allows direct access to the variables defined inside an objecthist(var1) # plots an histogram of variable var1 inside attached object data1detach(data1) # detaches variableshist(data1$var1) # plots an histogram of var1 from data1 without attaching#Conditional functions#conditional recoding of values within an object

data1= x = rnorm(1000) #create a set of 1000 random valuesx[x > 1.2] = NA # to recode all values of x > 1.2 to NAdata1= data.frame(a = rnorm(1000), b= rnorm(1000) , d=rnorm(1000), entry=c(1:10))data1$a[data1$b > 1] = 5 # to recode all values of b > 1 to 5 data5$avRep=ave(data5$Yield,data5$Rep) # create new column with averages among Repsdata5$avEntry=ave(data5$Yield,data5$Entry) # create new column with average among Entries

#Creating graphs# There are many simple functions to plot graphs using the data in the objects.plot(data1) #graphical output with plot arrays of elements in data1. This graphical feature is particularly impressive since it plots all variables of the matrix against all variables. With this feature of R you can do exploratory analysis and discover some hidden correlations. If you want to do this in Excel, you will need much more time. (later in the tutorial I will present a more informative version of multiple scatterplots)

#Later in the tutorial I will explain how to make special scatterplots.

boxplot(data1) #graphical output with boxplots of all elements in data1attach(data1) #to make the variables defined in data1 accesible to the consolevar1 # to show the contents of var1 (defined inside attached data1)boxplot(var1, var2) #produces boxplots of var1 and var2

boxplot(var1 ~ var2) #produces a boxplot of var1 grouped by factor var2plot(data.frame(var1, var2, var3)) #plot arrays of some selected elements only.

plot(data1[1:4]) #plot only selected columns 1 to 4 of data1detach(data1) #to detach the variables defined in data1data1$var1 # to show the contents of var1 from data1 without the attach function

# If you want to get the statistics involved in the boxplots, the following commands show them: b=boxplot(data1); b$stats # gives the value of the lower end of the whisker, the first quartile (25th percentile), second quartile (median=50th percentile), third quartile (75th percentile), and the upper end of the whisker.# plot() is a general graphic command with numerous options. plot(x)

# plots the data x

plot(var1, var2) # plots var1 in dependence of var2

abline(line(var1,var2))

# add a regression lineplot(y,z, main="Enter Title Here") # scatterplot with variables y and zfit=lm(y~z) #A fitted straight line is shown in the plot by executing two more commandsabline(fit)

# Exporting graphs#Right clicking anywhere inside the active graphics window shows a context sensitive menu, allowing saving the plot as metafile (EMF) or postscript format (PS). The options Copy as metafile or Copy as bitmap (BMP) puts the graphics in the clipboard. You can then paste it in some applications, e.g., MS Word. More graphical formats are available from the main menu. While the graphic window is active, click File| Save As from the menu and it lists six file formats (metafile, postscript, PDF, PNG, BMP, and JPG at three quality levels) in total so you have plenty of choices.

#Which is the best choice for the graphic format? In general metafile format retains graphic quality even when it is resized in the application. On the other hand, JPG is a very popular choice because the file size is usually much smaller and internet compatible. Except for rare circumstances, I would not recommend bitmap file format because it is usually very large and shows very poor picture quality when resized. Postscript file format is useful when including the graphic file in another postscript file or when postscript printer is available. Picture quality does not deteriorate when resized.

# Use metafile format for MS office applications (e.g. Word, Powerpoint). Within the graphic window, right click the mouse button and select copy as metafile. This will transfer the figure into the windows clipboard. Go to Powerpoint and paste as metafile. For publications save graphics in jpeg format.# Exporting datawrite.table(x, file = "dataOut.txt", append = FALSE, col.names = NA, sep = " ")

# with the above commands a textfile will be created in in the working directory. To change the working directory use the followinggetwd()

#displays the path for the working directorydir()

# lists the files in the current work directorysetwd("C:/Axel StatisticsR") # sets the working directory manually# open the text files in excel using the text import wizard.

#Handling of R Packages#There are dedicated webpages for specific statistical topics. For example, for the analysis of microarray data with R, consult: http://bioconductor.org/

#Download R packages from diverse websites and save them in a folder as zip files. Within the R program use the option install from local zip files. Install packages from R directly with these commands:install.packages("corrgram")install.packages("agricolae")install.packages("gplots")install.packages("UsingR")install.packages("lattice")install.packages(maanova")# Once you have downloaded the packages, activate them with the library function. Then you can use the extended functions in those packages.library(agricolae) #load and activate the librarylibrary(corrgram); library(gplots);library(UsingR);library(lattice);library(maanova)

# Example of group comparisons (agricolae)data1=read.table(file="clipboard", header=T, sep="\t") # import dataattach(data1) #to make the variables defined in data1 accessible to the consolelibrary(agricolae) #load and activate librarymodel1=aov(Yield~Gen*Env) #define a model for variance analysisdf=df.residual(model1)

MSerror=deviance(model1)/df

comparison = HSD.test(Yield,Gen,df,MSerror, group=TRUE)

bar.err(comparison,std=TRUE) #for plotting a graph#Make an additional LSD test with:

comparison = LSD.test(Yield,Gen,df,MSerror, p.adj="bonferroni", group=FALSE)

bar.err(comparison,std=TRUE) #for plotting a graph#For adjusting the graph you can modify some options like:bar.err(comparison,std=TRUE,ylim=c(0,45),density=4,border="blue")

#AMMIS biplots:

model= AMMI(Env, Entry, Rep, Yield, graph="biplot",number=FALSE)#For adjusting the graph you can modify some options like:

model= AMMI(Env, Entry, Rep, Yield, xlim=c(-4,4),ylim=c(-4,4), graph="biplot",number=FALSE)# Example (Pairwise.T.test)data1=read.table(file="clipboard", header=T, sep="\t") # import dataattach(data1) #to make the variables defined in data1 accessible to the consolepairwise.t.test(Var1, Var2) # make t.tests of Var1 grouped by category Var2# if you want to export the data you need to use following commands:x=pairwise.t.test(Var1, Var2) # create an object x for exportingwrite.table(x$p.value, file = "dataOutPairwise.txt", append = FALSE, col.names = NA, sep = " ")

# a textfile will be created in in the working directory. getwd()

#displays the path for the working directorydir()

# lists the files in the current work directory# open the text file in excel using the text import wizard.

# Another option to make multiple comparisons.model1=aov(var1~var2)

TukeyHSD(model1, "var1")#Example PCA analysis (bioconductor)#first you need to define the source to download the package source("http://bioconductor.org/biocLite.R")

biocLite("pcaMethods") #downloading needs only to be done the first timelibrary(pcaMethods) #load the library. Necessary to do it every time you run R# For importing data you have 3 options

## 1) load the matrix with data. Considering that row names are presentdata=read.table(file.choose(), header=T, sep="\t", row.names=1)

## or 2) not considereing the ID of metabolites or genes

data=read.table(file.choose(), header=T, sep="\t")

## or 3) importing data from clipboard

data=read.table("clipboard", header=T, sep="\t")

# depends on the type of data, you can use:

## 1) variables in the rows and samples in the columns

md = prep(t(data), scale = "none", center = TRUE)

## or 2) variables in the columns and samples in the rows

md = prep((data), scale = "none", center = TRUE)

## then do the analysis. In this case for the first 5 principal components.resPPCA = pca(md, method = "ppca", center = FALSE, nPcs = 5)

slplot(resPPCA) # to plot the resultsresPPCA@scores # to see all the principal components (treatment)

resPPCA@loadings # to see all the loadings#Example PCA analysis (native R)data=read.table("clipboard", header=T, sep="\t") #importing data from clipboard# depends on the dataframe you have:## 1) variables in the rows and samples in the columns

md = prep(t(data), scale = "none", center = TRUE)

## or 2) variables in the columns and samples in the rows

md = prep((data), scale = "none", center = TRUE)

md=data.frame(md[,2],md[,3],md[,4],md[,5],md[,6],md[,7])## Analysis for 2 principal components.pc=princomp(md, scale=TRUE)

loadings(pc)

biplot(pc)

pc

summary(pc)

#Example AMMIS analysis (agricolae)

data5=read.table(file="clipboard", header=T, sep="\t") # import data

# data must be in the following format:

RepPlotEntryEnvYield

11AE136.3

24AE137.9

12BE149.9

25BE150.3

attach(data5) #to make the variables defined in data1 accessible to the consolelibrary(agricolae) #load and activate librarymodel= AMMI(Env, Entry, Rep, Yield, graph="biplot",number=FALSE)#For changing the range of the axes use xlim and ylim:

model= AMMI(Env, Entry, Rep, Yield, xlim=c(-3,3),ylim=c(-4,4), graph="biplot",number=FALSE)#Example: Conditional Averagingdata1$var3=ave(data1$var1,data1$var2) # create new column named var10 with averages of var1 grouped by var2attach(data1); data1$var3=ave(var1,var2) # same as above but using attach functiondata5$avEntry=ave(data5$Yield,data5$Entry) # create new column with average of Yield grouped by entries

#Tests for significancet.test(var1~var2) #T.test within data of variable1 grouped by categories specified in variable 2. Grouping factor of var2 must have exactly 2 levels.

# Note the important difference between the t.test(var1,var2) and t.test(var1~var2) since in the first case you are comparing all values of var1 against all values of var2. In the second case, you make subgroups of the values contained in var1 depending in the grouping levels defined in var2. If you have more than 2 grouping levels in var2, then it is better to use the pairwise.t.test function.

pairwise.t.test(var1, var2) # make t.tests of var1 grouped by categories var2. The result is a pairwise comparison matrix. Note the comma in (var1, var2) ave(var1, var2) # make averages of var1 grouped by factor categories var2. The result is a vector of the same length as var1.#To make an analysis of variance:a=aov(var1~var2+var3+var4) #defines a new object a with the results of the analysis of variancea #lists the content of the previously created object asummary(a) #lists the summary of the statistical results of object aattributes(a) #lists all attributes of new object#To make a linear model:model1=lm(var1 ~var2+var3+var4)

summary(model1) # show summary of the linear model

attributes(model1) # shows all attributes of the new object

#Making special graphics# Multiple plots per page

#You can combine multiple plots into one overall graph, using either the

par( ) or layout( ) function. With the par( ) function, the option mfrow=c(nrows, ncols) creates a matrix of nrows x ncols plots that are filled in by row. mfcol=c(nrows, ncols) fills in the matrix by columns.

par(mfrow=c(2,2))

plot(var1,var2, main="Title plot1")

plot(var1,var3, main="Title plot2")

hist(var1, main="Title Histogram")

boxplot(var1, main="Title Boxplot", xlab="label1", ylab="label2")# The layout( ) function has the form layout(mat) where mat is a matrix object specifying the location of the N figures to plot.

# Example of one figure in row 1 and two figures in row 2layout(matrix(c(1,1,2,3), 2, 2, byrow = TRUE))

hist(var1)

hist(var2)

hist(var3)

# Histograms# How many bins should an histrogram contain? This depends on the number of observations n. Use the approximation of Sturges k= log2(n)+1

#This means that for 100 observations you should use around 7-8 bins

# Sturges, H. A. (1926). "The choice of a class interval". J. American Statistical Association: 6566.

# Example (gplots)data1=read.table(file="clipboard", header=T, sep="\t") # import data attach(data1) #to make the variables defined in data1 accessible to the consolelibrary(gplots) #load extended plot library

plotmeans(var1~var2) #plot means with error bars

# Example (lattice)x=read.table(file="clipboard", header=T, sep="\t") # import data attach(x)

#make variables accessiblelibrary(lattice)

#load library

xyplot(var1~ var2)

dotplot(var1~ var2)barchart(var1~ var2)stripplot(var1~ var2)bwplot(var1~ var2)

# Example (Simple)install.packages("UsingR")require(UsingR)data1=read.table("clipboard", header=T, sep="\t")

attach(data1)

simple.violinplot(var1 ~ var2, col=8)

par(new=TRUE) #for adding to an existing graph. Next function will add.plot(var1 ~ var2, add = TRUE) # so that a new plot is not started# Example (ellipse)data1=read.table("clipboard", header=T, sep="\t")

library(ellipse)plotcorr(cor(data1, use = "pairwise.complete.obs"))

library(corrgram)

corrgram(data1)

#Trellis graphs#In order to produce Trellis plots you must load the Lattice" library and start a trellis aware" device. library(lattice)

trellis.device()#Make conditional plots with the coplot functioncoplot(var1 ~ var2 | var3) # plot var1 against var2, subdivided in categories var3

# It is possible to use two conditional variables in the form of var3*var4

coplot(var1 ~ var2 | var3*var4) # plot var1 against var2, subdivided in categories var3 and var4

#Options for the coplot function:

coplot(formula, data, given.values, panel = points, rows, columns, show.given = TRUE, col = par("fg"), pch = par("pch"), xlab = paste("Given :", a.name), ylab = paste("Given :", b.name), number = 6, overlap = 0.5, ...) co.intervals(x, number = 6, overlap = 0.5)

# formula # Add a trendline#Use the functions lm() and abline() to add a trendline to an existing plot. data1=read.table("clipboard", header=T, sep="\t")

attach(data1)

plot(var1, var2)

model1=lm(var1~var2)

abline(model1,lwd=2) #The lwd specifies the line widthmodel1

#To show the slope and intercept of the trendline

# To add text to a graphictext (x, ...) text.default (x, y = NULL, labels = seq(along = x), adj = NULL, pos = NULL, offset = 0.5, vfont = NULL, cex = 1, col = NULL, font = NULL, xpd = NULL)mtext(text, side = 3, line = 0, outer = FALSE, at = NULL, adj = NA, cex = NA, col = NA, font = NA, vfont = NULL)title(main="Name")# 3D Perspective Plot0.8410.7460.6780.6280.5870.5540.5260.502

0.9090.7890.7100.6520.6060.5700.5400.514

0.1410.1410.1400.1400.1390.1390.1380.138

-0.757-0.687-0.634-0.592-0.558-0.530-0.505-0.484

-0.959-0.819-0.730-0.667-0.619-0.580-0.548-0.521

-0.279-0.276-0.272-0.269-0.266-0.263-0.260-0.257

0.6570.6110.5730.5430.5160.4940.4740.456

0.9890.8360.7420.6760.6250.5850.5530.525

0.4120.4010.3900.3800.3710.3630.3550.347

-0.544-0.518-0.495-0.475-0.457-0.441-0.427-0.414

data3=read.table(file="clipboard", header=F, sep="\t") # import data without header. It is important that it is without header in order to convert it to a matrix.data3=data.matrix(data3) # convert table to a data matrix. Important step because otherwise data will be treated as a dataframe and not as a datamatrix.x=c(1:10) # define x variable. In this case 10 values

y=c(1:8) # define y variable. In this case 8 valuespar(mfrow=c(2,2)) # for plotting 4 graphs in one pagecontour(x, y, data3, col="blue") # plot contourimage(x, y, data3) # plot imagepersp(x, y, data3, col="blue", theta = -20, phi = 15, scale = TRUE, axes = TRUE)

# Special Scatterplots

## In order to do special scatterplots, you need first to define extra functions, and then call them from within the function pairs.

## Extra function to put histograms on the diagonal

panel.hist = function(x, ...)

{

usr = par("usr"); on.exit(par(usr))

par(usr = c(usr[1:2], 0, 1.5) )

h = hist(x, plot = FALSE)

breaks = h$breaks

nB = length(breaks)

y = h$counts; y = y/max(y)

rect(breaks[-nB], 0, breaks[-1], y, col="cyan", ...)

}

## Extra function to put correlations R on the upper panelspanel.cor = function(x, y, digits=2, prefix="R=", cex.cor)

{

usr = par("usr"); on.exit(par(usr))

par(usr = c(0, 1, 0, 1))

r = abs(cor(x, y, use="pairwise.complete.obs"))

txt = format(c(r, 0.123456789), digits=digits)[1]

txt = paste(prefix, txt, sep="")

if(missing(cex.cor)) cex.cor = 0.8/strwidth(txt)

text(0.5, 0.5, txt, cex = cex.cor)

}

## Extra function to put R2 values instead of simple R values. It also puts p values.panel.cor2 = function(x, y, digits=2, prefix="", cex.cor)

{

usr = par("usr"); on.exit(par(usr))

par(usr = c(0, 1, 0, 1)) r = abs(cor(x, y, use="pairwise.complete.obs"))

r2 = abs(cor(x, y, use="pairwise.complete.obs")*cor(x,y, use="pairwise.complete.obs")) txt = format(c(r2, 0.123456789), digits=digits)[1]

txt = paste(prefix, txt, sep="")

if(missing(cex.cor)) cex.cor = 0.8/strwidth(txt)

text(0.5, 0.5, txt, cex = cex.cor)

modelo=summary(lm(x~y))

valorP=signif(modelo$coefficients[8], digits=digits) text(0.7, 0.2, valorP)

}

## To put histograms in diagonal and smooth lines in lower panel:pairs(USJudgeRatings[1:5], panel=panel.smooth, cex = 1.5, pch = 1, bg="light blue", diag.panel=panel.hist, cex.labels = 2, font.labels=2)

## To put correlations on the upper panels and smooth lines in lower panel:pairs(USJudgeRatings[1:6], lower.panel=panel.smooth, upper.panel=panel.cor)

## To put correlations R2 on the upper panels and smooth lines in lower panel:pairs(USJudgeRatings[1:6], lower.panel=panel.smooth, upper.panel=panel.cor2)

## To put correlations R on the upper panels, histograms in diagonal, and smooth lines in lower panel:pairs(USJudgeRatings[1:5], lower.panel=panel.smooth, upper.panel=panel.cor, diag.panel=panel.hist)## To put correlations R2 on the upper panels, histograms in diagonal, and smooth lines in lower panel:

pairs(USJudgeRatings[1:5], lower.panel=panel.smooth, upper.panel=panel.cor2, diag.panel=panel.hist)

# Exercises# Example of Graph:

p