# Continuous Variable

including by group

# Continuous Variable

## Basic Statistics

• mean(dataSet, na.rm = T)
• var(dataSet, na.rm=T)
• sd(dataSet, na.rm=T)
• stderr <- function(x) sqrt(var(x,na.rm=TRUE)/length(na.omit(x)))
• cor(dataFrame, method = "pearson", use = "pairwise.complete.obs")

## One Continuous Variable by Group

aggregate()自动把NA删掉
example

Example: mean

Example: standard deviation

## Multiple Continuous Variables by Group

## Pearson Correlation Matrix

### basic syntax

* dataFrame: one data frame

cor(dataFrame, method = "pearson", use = "pairwise.complete.obs")

Example: mtcars dataset

When there are missing values, cor and Hmisc::rcorr give different results

## Test Two Independent Samples of Equal or Different Sample Sizes

### Step 1: non-parametric: one-way ANOVA on ranks

i.e. one-way analysis of variance on ranks

alternative name:

• the Kruskal–Wallis test by ranks
• Kruskal–Wallis H test

Summary

• A non-parametric method for testing whether samples originate from the same distribution.

• extend the Mann–Whitney U test when there are more than two groups.

• post-hoc analysis: Dunn test: appropriate for groups with unequal numbers of observations: package FSA function dunnTest()

#### Basic syntax

• contVar: one continous variable

• catVar: one categorical variable
• dataSet: one dataset

kruskal.test(contVar ~ catVar, data = dataSet) or kruskal.test(contVar, catVar, data = dataSet)

Example: mtcars dataset

Parameter Argument Meaning
contVar mpg Miles/(US) gallon
contVar cyl Number of cylinders
contVar disp Displacement (cu.in.)
contVar hp Gross horsepower
contVar wt Weight (1000 lbs)
contVar drat Rear axle ratio
catVar gear Number of forward gears
data mtcars Motor Trend Car Road Tests

or

## Step 2: Pairwise Wilcoxon Rank Sum Tests

• the Mann–Whitney U test
• the Mann–Whitney–Wilcoxon (MWW)
• Wilcoxon rank-sum test
• Wilcoxon–Mann–Whitney test)

a nonparametric test of the null hypothesis that it is equally likely that a randomly selected value from one sample will be less than or greater than a randomly selected value from a second sample.

Unlike the t-test it does not require the assumption of normal distributions. It is nearly as efficient as the t-test on normal distributions.

This test can be used to determine whether two independent samples were selected from populations having the same distribution; a similar nonparametric test used on dependent samples is the Wilcoxon signed-rank test.

#### basic syntax

• contVar: one continous variable
• catVar: one categorical variable

pairwise.wilcox.test(contVar, catVar, p.adjust.method = "bonferroni")

#### one continous variable

Example: mtcars dataset

Parameter Argument Meaning
contVar wt Weight (1000 lbs)
catVar gear Number of forward gears
data mtcars Motor Trend Car Road Tests

#### multiple continous variables

Example: mtcars dataset

Parameter Argument Meaning
contVar mpg Miles/(US) gallon
contVar cyl Number of cylinders
contVar disp Displacement (cu.in.)
contVar hp Gross horsepower
contVar wt Weight (1000 lbs)
contVar drat Rear axle ratio
catVar gear Number of forward gears
data mtcars Motor Trend Car Road Tests

### parametric: one-way ANOVA

i.e. one-way analysis of variance (ANOVA)

## > 2 Indepent Samples

### non-parametric: one-way ANOVA on ranks

i.e. one-way analysis of variance on ranks

alternative name:

• the Kruskal–Wallis test by ranks
• Kruskal–Wallis H test

Summary

• A non-parametric method for testing whether samples originate from the same distribution.

• extend the Mann–Whitney U test when there are more than two groups.

• post-hoc analysis: Dunn test: appropriate for groups with unequal numbers of observations: package FSA function dunnTest()

#### Basic syntax

• contVar: one continous variable

• catVar: one categorical variable
• dataSet: one dataset

kruskal.test(contVar ~ catVar, data = dataSet) or kruskal.test(contVar, catVar, data = dataSet)

Example: mtcars dataset

Parameter Argument Meaning
contVar mpg Miles/(US) gallon
contVar cyl Number of cylinders
contVar disp Displacement (cu.in.)
contVar hp Gross horsepower
contVar wt Weight (1000 lbs)
contVar drat Rear axle ratio
catVar gear Number of forward gears
data mtcars Motor Trend Car Road Tests

or

## Plots

### Scatter plots by Groups

#### basic syntax

• contVar: one continous variable
• catVar: one categorical varaible
• data: dataset

Example: mtcars dataset

Parameter Argument Meaning
contVar mpg Miles/(US) gallon
contVar cyl Number of cylinders
contVar disp Displacement (cu.in.)
contVar hp Gross horsepower
contVar wt Weight (1000 lbs)
contVar drat Rear axle ratio
catVar gear Number of forward gears
data mtcars Motor Trend Car Road Tests ### Box plots by Groups

#### basic syntax

• contVar: one continous variable
• catVar: one categorical varaible
• data: dataset

#### one box plot by groups

Example: mtcars dataset

Parameter Argument Meaning
contVar wt Weight (1000 lbs)
catVar gear Number of forward gears
data mtcars Motor Trend Car Road Tests #### multiple box plots by groups in one dataset

Example: mtcars dataset

Parameter Argument Meaning
contVar mpg Miles/(US) gallon
contVar cyl Number of cylinders
contVar disp Displacement (cu.in.)
contVar hp Gross horsepower
contVar wt Weight (1000 lbs)
contVar drat Rear axle ratio
catVar gear Number of forward gears
data mtcars Motor Trend Car Road Tests ### Histogram by Groups

#### basic syntax

• contVar: one continous variable
• catVar: one categorical varaible
• data: dataset
• breaks: the number of breakpoints

#### one histogram by groups

Example: mtcars dataset

Parameter Argument Meaning
contVar wt Weight (1000 lbs)
catVar gear Number of forward gears
data mtcars Motor Trend Car Road Tests
breaks 4 Number of breakpoints #### multiple histograms by groups in one dataset

Example: mtcars dataset

Parameter Argument Meaning
contVar mpg Miles/(US) gallon
contVar cyl Number of cylinders
contVar disp Displacement (cu.in.)
contVar hp Gross horsepower
contVar wt Weight (1000 lbs)
contVar drat Rear axle ratio
catVar gear Number of forward gears
data mtcars Motor Trend Car Road Tests