Sunday, June 28, 2020

table


The table command lets you build contingency tables (two-way table, cross tablulation, or crosstab). It is similar to xtabs. Both are depictions of spread, which display multivariate frequency distribution of two variables, though from the R documentation, it looks like you can use xtabs for more than two variables.

Wikipedia provides this example for a contingency table. Dominant handedness is broken down by gender:

Handedness
Sex
Right handed
Left handed
Total
Male
43
9
52
Female
44
4
48
Total
87
13
100

The red values are the column and row margin totals.

You see contingency tables everywhere. One of my stats classes focused very heavily on them, pumping nearly all the homework and class examples through Excel.

The table command has 9 parameters, of which you will often use row.names and responseName. The example below uses quantile(Temp) as row.names, and Month as the responseName.

R Example

This example from the R docs uses the build in dataset airquality, taking the Temp column from airquality, and creating a contingency table with a row grouping by quartile of temperature values, and a column grouping by month number.

with(airquality, table(cut(Temp, quantile(Temp)),Month))

        Month
          5 6 7 8 9
(56,72] 24 3 0 1 10
(72,79] 5 15 2 9 10
(79,85] 1 7 19 7 5
(85,97] 0 5 10 14 5



The table reveals, for example, that when the value of the temperature is within the 1st quartile of temp values (between 56 and 72), the counts of when those temperatures occurred between May and September are:

Month
# of times the temperature was between 56 and 72
May
24
June
3
July
0
August
1
September
10

Sunday, June 14, 2020

cor

Correlation coefficient. A measure of the linear relationship between two variables. This is a unitless value.

In the R docs, var is listed on the same page as var (variance) and cov (covariance).

When you want to try and predict the effect an x value has on a y value, your calculate correlation (usually represented with 'r').

When r is greater than zero, it indicates a positive correlation, and when less than zero, it indicates a negative correlation. An r of zero means no correlation.

Correlation is a linear measure. Therefore you can only use it when a plot of the x/y relationship is more linear than otherwise.

R Example

> duration = faithful$eruptions   # eruption durations 
> waiting = faithful$waiting      # the waiting period 
> cor(duration, waiting)          # apply the cor function 

[1] 0.90081 

The value of r is nearly 1 (one), a positive correlation.

Friday, June 12, 2020

kurtosis

A measure of shape. Provides a number to represent the relative flatness of a distribution. You can select one of three algorithms for computing kurtosis (Type 1 is the default).


The kurtosis for a normal distribution is 3.0. Several terms are used to define different shape tendencies:

  • leptokurtic - steep spike and heavy tails (g2 positive)
  • platykurtic - flat shape and thin tails (g2 negative)
  • mesokurtic - rounded peak and moderate tails (normal dist) (g2 ~0)

 A kurtosis number is a value minus 3 (that is, a certain amount away from Normal).

R Example

> library(e1071)
> x <- rnorm(100)
> kurtosis(x)
[1] -0.7979941

Tuesday, June 9, 2020

skewness

A measure of shape. Provides a number to represent the relative positive or negative skew of a distribution. Perfectly normal distributions have a skewness of 0. Negative numbers indicate left skew, with positive numbers indicating right skew. If you get a skew number less than -1 or greater than 1, it is considered to be approaching extreme.

I was hoping for a table of values which showed .5, 1.0, 1.5, etc and the degree of extremeness of skew, but didn't find one.

R Example

> library(e1071)
> x <- rnorm(100)
> skewness(x)
[1] 0.1277998

rnorm is a random distribution generator.

Sunday, May 17, 2020

var

Variance. A measure of spread. It's the expectation of the squared deviation of a random variable from its mean.

In the R docs, var is listed on the same page as cor (correlation) and cov (covariance).

sd is the square root of var. You need to use sum of squares to calculate variance, because if you use simple deviation, the values will cancel out to zero, because the deviations of the values vary plus or minus the mean, which is a calculation on the same values.

All three have the same six possible parameters. Along with the X and Y, you can opt to remove NA values. The 'use' and 'method' parameters allow you to choose computing methods. There's a 'v' method I don’t understand yet.

R Example

> var(rivers)
[1] 243908.4

I may need to look into the utility for variance. Up to this point it's been the standard deviation I make most use of.

Friday, May 15, 2020

scale (z-score)

The R docs present this as a generic function (Scaling and Centering of Matrix-like Objects). In Stat 3743, Kern uses it. It appears to calculate the z-score for an element based on the data.frame. I'm not sure yet how to get just one z-score for a particular value in a data.frame.

R Example

For example, the data.frame precip provides rainfall data from 7 to 67 inches:

> range(precip)
[1]  7 67

And scale(range(precip)) provides the min and max z-scores for all the elements in precip:

> range(scale(precip))
[1] -2.034466  2.342971

Wednesday, May 13, 2020

fivenum

A measure of spread and shape. Returns Tukey's five number summary for a dataset:


minimum

smallest value

lower hinge

~ 25% value

median

middle value

upper hinge

~75% value

maximum

greatest value


These are depicted pretty well with box plots. I'll link to that when I write it.

R Example

> fivenum(faithful$waiting)
[1] 43 58 76 82 96