Object class attributes in R

All R objects have a class attribute. This can be viewed (or set) via the class() command. The class​ can be any character string and an object can hold more than one class. You can use the class to help you identify what sort an object is, for example:

bird
               Garden Hedgerow Parkland Pasture Woodland
Blackbird          47       10       40       2        2
Chaffinch          19        3        5       0        2
Great Tit          50        0       10       7        0
House Sparrow      46       16        8       4        0
Robin               9        3        0       0        2
Song Thrush         4        0        6       0        0

At first glance you cannot tell if this object is a data.frame or a matrix. The class() command can help you find out:

class(bird)
[1] "matrix"

Some commands can be pressed into service for special classes – for example plot(), summary() and print() commands can be written to process objects of a specific class. You simply name your custom command to append the class; for example plot.lm(), print.lm() or summary.lm() commands, which are part of the basic distribution of R. When you issue a plot() command for example R looks at the class of the object to see if there is a plot.xxxx() command to match (where xxxx is the class of the object). If there is then this custom command is carried out. If a custom command is not available then the basic plot() command is used.

The class attribute is often used in functions to check that an object is of the right sort before carrying out the commands.

Advertisements

Add comments to objects in R

All R objects have attributes – one of these can be a comment. You can set or view the comment attribute of an object using the comment() command. For example:

x <- c(2, 3, 5, 4, 3, 6)
comment(x)
NULL
comment(x) <- "A simple numeric variable"
comment(x)
[1] "A simple numeric variable"

Use the comment to remind you what the object is – it is easy to forget later.

NA items in R data

The NA item is a special object in R and represents “Not Available;able”. Sometimes this is because data were genuinely not collected (and therefore really are missing). Other times it is because you have columns of unequal length and your data.frame is padded out (with NA) to make a rectangular object with all columns containing the same number of elements.

The na.rm = TRUE parameter can be used to “take care” of NA items in some summary commands, e.g. sum() or mean():

x
[1] 2 4 3 6 2 8 NA NA
mean(x)
[1] NA
mean(x, na.rm = TRUE)
[1] 4.166667

However, this does not always work.

length(x, na.rm = TRUE)
Error in length(x, na.rm = TRUE) :
2 arguments passed to 'length' which requires 1

In this case the na.omit() command can be used to strip out the NA​ items:

length(na.omit(x))
[1] 6

The na.omit() command essentially removes all the NA items.

Manipulating R formula

When you’ve created some kind of analysis model in R you will have specified the variables in some kind of formula. R “recognises” formula objects, which have their own class formula.  If, for example you used the lm() command to create a regression result you will be able to extract the formula from the result.


mod <- lm(Fertility ~ ., data = swiss)
formula(mod)

Fertility ~ Agriculture + Examination + Education + Catholic + Infant.Mortality

It can be useful to be able to extract the components of the model formula. For example you may want to examine how the R2 value alters as you add variables to the model.

Extract the predictor variables

To access the parts of a formula you need the terms() command:

terms(formula(mod))

The result contains various components; you want the term.labels.

attr(terms(formula(mod)), which = "term.labels")
[1] "Agriculture" "Examination" "Education" "Catholic"
[5] "Infant.Mortality"

You now have the variables, that is the predictor variables, from the formula. The next step is to get the response variable.

Extract the response variable

The response variable can be seen using the terms() command and the variables component, like so:

attr(terms(formula(mod)), which = "variables")
list(Fertility, Agriculture, Examination, Education, Catholic,
Infant.Mortality)

The result looks slightly odd but essentially it is a list and the 2nd component is the response.

vv <- attr(terms(formula(mod)), which = "variables")
rr <- as.character(vv[[2]]) # The response variable name
rr
[1] "Fertility"

Now you have the response variable, and the predictors from earlier, which you can use to “build” a formula.

Building a formula

In its most basic sense a formula is simply a character string that “conforms” to the formula syntax: y ~ x + z for example. You can build a formula with the paste() command by joining the response, a ~ character and the predictors you want (these themselves separated by + characters).

The following example uses the swiss dataset, which is built into base R.

mod <- lm(Fertility ~ ., data = swiss)

# Get the (predictor) variables
vars <- attr(terms(formula(mod)), which = "term.labels")

# Get the response
vv <- attr(terms(formula(mod)), which = "variables")
rr <- as.character(vv[[2]]) # The response variable name

# Now the predictors
pp <- paste(vars, collapse = " + ")       # All
pp <- paste(vars[1], collapse = " + ")    # 1st
pp <- paste(vars[1:3], collapse = " + ")  # 1,2,3

# Build a formula
fml <- paste(rr, " ~ ", pp)
fml
[1] "Fertility ~ Agriculture + Examination + Education + Catholic + Infant.Mortality"

Once you have your formula as a character object you can use it in place of a regular formula in commands.

Using a “built” formula

The character string representing a formula can be used exactly as you would a “regular” formula:

lm(fml, data = swiss)
Call:
lm(formula = fml, data = swiss)

Coefficients:
     (Intercept)       Agriculture       Examination         Education  
         66.9152           -0.1721           -0.2580           -0.8709  
        Catholic  Infant.Mortality  
          0.1041            1.0770

One use for building a formula is in model testing. For example you create your regression model containing five predictors but maybe only the first three are really necessary. You can re-build the formula term by term and extract the R2 value for example. This would show you how the explained variance alters as you add more variables.

In another posting I’ll show how this process can be used for cross-validation.


See more tips and tricks at DataAnalytics.org.uk

Add more to a histogram

A basic histogram

A histogram is a standard way to present the distribution of a sample of numbers. It is easy to make a histogram using R with the hist() command. For example:

set.seed(123)
x = norm(n = 50, mean = 10, sd = 1)
hist(x, col = "skyblue")

Produces a histogram resembling this:

Rplot hist
Basic Histogram

Add a rug

A rug plot can be added to more or less any graphic. The rug() command can add the rug to any side of the plot:

  • side = 1 is the bottom axis
  • side = 2 is the left axis

You can alter the colour and width of the rug lines using regular graphical parameters:

rug(x, side = 1, col = "blue")

Adds the rug like so:

Rplot hist rug
A Rug plot added to a histogram

Add a strip chart

A strip chart can also be added to any chart via the stripchart() command. However, you also need to specify add = TRUE to the command. Giving a bit of jitter helps to separate out points that are coincident:

stripchart(x,
             method = "jitter",
             pch = 23,
             bg = "pink",
             add = TRUE)

The final plot looks like so:

Rplot hist rug strip
A histogram with added rug and strip plot

There are many additional options for the stripchart() command.


See more tips and tricks at DataAnalytics.org.uk

Statistics for Ecologists Using R and Excel

This is a book about the scientific process and how you apply it to data in ecology. You will learn how to plan for data collection, how to assemble data, how to analyze data and finally how to present the results. The book uses Microsoft Excel and the powerful Open Source R program to carry out data handling as well as producing graphs.

S4E2e-Cover