Use the ToothGrowth data. On the same plotting window create two separage graphs side by side.
par(mfrow = c(1, 2))
plot(dose, len, type = "p", pch = 0, col = "red", xlab = "Dose", ylab = "Length", main = "Length vs Dose")
plot(supp, len, xlab = "Supplement", ylab = "Length", main = "Length vs Supplement")
Recreate the plot of length against dose, incorporating the following changes:
dose[supp == "OJ"] to select the elements of dose which correspond to supp = OJ. plot(dose, len, type = "n", xlab = "Dose", ylab = "Length") # Draw the axes only
points(dose[supp == "OJ"], len[supp == "OJ"], col = 2, pch = 0)
points(dose[supp == "VC"], len[supp == "VC"], col = 3, pch = 5)
bhat = cor(dose,len)*sd(len)/sd(dose)
ahat = mean(len) - bhat*mean(dose)
abline(ahat, bhat)
legend("bottomright", legend = c("OJ", "VC", "LS fit"), pch = c(0, 5, NA),
lty = c(NA, NA, 1), col = c(2, 3, 1))
A number of functions are used for drawing on the 2D plane. These have the general call fcn(x, y, z, opt1 = val1, opt2 = val2, ...) where x and y are numeric vectors of coordinates on the 2D plane with elements in increasing order and z is a matrix with as many rows as the elements of x and as many columns as the elements of y. The [i, j] element of z corresponds to z[i, j] = z(x[i], y[j]).
These functions are
image(x, y, z, ...) and contour(x, y, z, ...) Creates an image and contour plot respectively. The optional argument add, if set to TRUE, will not overwrite an existing plot. persp(x, y, z, ...) Draws a surface over the 2D plane.## Plot of the bivariate standard normal distribution density
dbsnorm = function(x, y, rho) {
nx = length(x)
ny = length(y)
xmat = matrix(x, nx, ny)
ymat = t(matrix(y, ny, nx)) # t() gives the transpose of a matrix
(1/(2*pi*sqrt(1-rho^2))) * exp(- .5 * (xmat^2 + ymat^2 - 2*rho*xmat*ymat))
}
sval = seq(-4, 4, length.out = 101) # Range of standard normal values
xval = sval
yval = sval
zval = dbsnorm(sval, sval, rho = 0.8)
persp(xval, yval, zval, theta = 30, col = "yellow")
Consider the data generated using the following R commands
{r, eval = FALSE}
n <- 500
x <- rnorm(n, mean = 1, sd = 2)
y <- 2*x + rnorm(n, mean = 0, sd = 3)
y against x.x and y in red colour.Hint. If $(Z_1, Z_2)$ are bivariate standard normally distributed with correlation $\rho$, then $(X_1, X_2)$ where $X_1 = \mu_1 + \sigma_1 Z_1$ and $X_2 = \mu_2 + \sigma_2 Z_2$ are bivariate normally distributed with correlation $\rho$, mean $(\mu_1, \mu_2)$ and standard deviations $\sigma_1$ and $\sigma_2$. Then the density of $(X_1, X_2)$ is $$\frac{1}{\sigma_1 \sigma_2} f_{(Z_1,Z_2)}\biggl( \frac{x_1 - \mu_1}{\sigma_1}, \frac{x_2 - \mu_2}{\sigma_2} ; \rho \biggr)$$ where $f_{(Z_1,Z_2)}(z_1,z_2;\rho)$ is the density of $(Z_1,Z_2)$.
n = 500
x = rnorm(n, mean = 1, sd = 2)
y = 2*x + rnorm(n, mean = 0, sd = 3)
plot(x, y, xlim = c(-7, 9), ylim = c(-18, 22))
sval = seq(-4, 4, length.out = 101) # Range of standard normal values
xval = 1 + 2*sval
yval = 2 + 5*sval
zval = dbsnorm(sval, sval, rho = 8/(2*5))/(2*5)
contour(xval, yval, zval, add = TRUE, col = "red")