Contents

20250227 Multivariate Analysis

Homework 1, make the boxplot for the mileage of American, Japanese and European cars (from left to right).

Introduction

This assignment requires us to draw boxplots of car mileage for the United States, Japan, and Europe as shown in the textbook.

The dataset carc.txt and the R script MVAboxcar.R for generating the image can be downloaded from https://github.com/QuantLet/MVA/tree/master/QID-1485-MVAboxcar.

Dataset

In R, we can use the following command to read the dataset.

1
read.table("carc.txt")

Below is part of the dataset carc.txt.

V1V2V3V4V5V6V7V8V9V10V11V12V13
1409922322.527.5112930186401213.58
2474917313.025.5113350173402582.53
3379922NaNNaN3.018.5122640168351213.08
4969017523.027.0152830189371313.20
5629523332.528.011207017436973.70

From Table 22.3 in the textbook, we know that the dataset contains 13 parameters and 74 vehicle types. The description of each parameter is as follows:

VariableSymbolDescription
$X_1$PPrice
$X_2$MMileage (in miles per gallon)
$X_3$R78Repair record 1978 (rated on a 5-point scale; 5 best, 1 worst)
$X_4$R77Repair record 1977 (scale as before)
$X_5$HHeadroom (in inches)
$X_6$RRear seat clearance (distance from front seat back to rear seat, in inches)
$X_7$TrTrunk space (in cubic feet)
$X_8$WWeight (in pound)
$X_9$LLength (in inches)
$X_{10}$TTurning diameter (clearance required to make a U-turn, in feet)
$X_{11}$DDisplacement (in cubic inches)
$X_{12}$GGear ratio for high gear
$X_{13}$CCompany headquarter (1 for USA, 2 for Japan, 3 for Europe)

Boxplot

Below is an example of a boxplot drawn from the QuantLet/MVA repository on GitHub.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# clear variables and close windows
rm(list = ls(all = TRUE))
graphics.off()

# load data
x = read.table("carc.txt")

# parameter settings
k      = 0
l      = 0
m      = 0
us     = NULL
japan  = NULL
europe = NULL
M      = x[, 2]
C      = x[, 13]

for (i in 1:dim(x)[1]) {
    if (x[i, 13] == 1) {
        k = k + 1
        us[k] = x[i, 2]
    } else if (x[i, 13] == 2) {
        l = l + 1
        japan[l] = x[i, 2]
    } else if (x[i, 13] == 3) {
        m = m + 1
        europe[m] = x[i, 2]
    }
}

m1 = mean(us)
m2 = mean(japan)
m3 = mean(europe)

# plot
boxplot(us, japan, europe, axes = FALSE, frame = TRUE)
axis(side = 1, at = seq(1, 3), label = c("US", "JAPAN", "EU"))
axis(side = 2, at = seq(0, 50, 5), label = seq(0, 50, 5))
title("Car Data")
lines(c(0.6, 1.4), c(m1, m1), lty = "dotted", lwd = 1.2)
lines(c(1.6, 2.4), c(m2, m2), lty = "dotted", lwd = 1.2)
lines(c(2.6, 3.4), c(m3, m3), lty = "dotted", lwd = 1.2)

(five = quantile(x[, 2], c(0.025, 0.25, 0.5, 0.75, 0.975)))

/1140227-multivariate-analysis/image/1740626790418.png
Boxplot for the mileage of different production locations.

Since we only need to draw boxplots of car mileage for different regions, we use only two parameters M = x[, 2] and C = x[, 13]. Here, M represents the miles per gallon, and C represents the company headquarters of the car manufacturer.

The original code was uploaded to GitHub by the author in 2016. We can now achieve the same result with a more concise approach. The updated code is as follows.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
# Clear variables and close graphics window
rm(list = ls(all = TRUE))
graphics.off()

# Load data
x <- read.table("carc.txt")

# Group data and compute mean values
groups <- split(x[, 2], x[, 13])
means <- sapply(groups, mean)

# Draw boxplot
boxplot(groups, names = c("US", "JAPAN", "EU"), frame = TRUE)
title("Car Data")
axis(2, at = seq(0, 50, 5))

# Add dotted lines for mean values
for (i in 1:length(means)) {
  lines(c(i - 0.4, i + 0.4), rep(means[i], 2), lty = "dotted", lwd = 1.2)
}

quantile(x[, 2], c(0.025, 0.25, 0.5, 0.75, 0.975)) # Quartiles

Environment

  • Operating System: Windows 11 24H2
  • Programming Language: R 4.4.2

References