vignettes/tests/test-linreg-estimation-descriptives.Rmd
test-linreg-estimation-descriptives.Rmd
# The Linear Regression Model: Descriptives {#linreg-estimation-descriptives-example}
See jeksterslabRdatarepo::wages.matrix()
for the data set used in this example.
X <- jeksterslabRdatarepo::wages.matrix[["X"]]
# age is removed
X <- X[, -ncol(X)]
y <- jeksterslabRdatarepo::wages.matrix[["y"]]
head(X)
#> constant gender race union education experience
#> [1,] 1 1 0 0 12 20
#> [2,] 1 0 0 0 9 9
#> [3,] 1 0 0 0 16 15
#> [4,] 1 0 1 1 14 38
#> [5,] 1 1 1 0 16 19
#> [6,] 1 1 0 0 12 4
head(y)
#> wages
#> [1,] 11.55
#> [2,] 5.00
#> [3,] 12.00
#> [4,] 7.00
#> [5,] 21.15
#> [6,] 6.92
data <- X[, -1]
data <- cbind(
y,
data
)
n <- nrow(X)
k <- ncol(X)
df1 <- k - 1
df2 <- n - k
muhatX <- as.vector(colMeans(X))
muhatX <- muhatX[-1]
muhaty <- mean(y)
muhat <- c(muhaty, muhatX)
Rhat <- as.vector(cor(data))
RXhat <- as.vector(cor(X[, -1]))
ryXhat <- cor(data)
ryXhat <- as.vector(ryXhat[, 1])
ryXhat <- ryXhat[-1]
Sigmahat <- as.vector(cov(data))
SigmaXhat <- as.vector(cov(X[, -1]))
sigmayXhat <- cov(data)
sigmayXhat <- as.vector(sigmayXhat[, 1])
sigmayXhat <- sigmayXhat[-1]
sigma2Xhat <- as.vector(diag(cov(X[, -1])))
sigma2yhat <- as.vector(var(y))
sigma2hat <- c(sigma2yhat, sigma2Xhat)
sigmahat <- sqrt(sigma2hat)
output <- descriptives(
X = X,
y = y,
mardia = TRUE
)
#>
#> Central Moments:
#> Mean SD Skewness Kurtosis
#> wages 12.3658495 7.8963503 1.85026794 4.8600481
#> gender 0.4972847 0.5001867 0.01087395 -2.0029920
#> race 0.1528317 0.3599648 1.93189913 1.7349237
#> union 0.1590380 0.3658535 1.86682302 1.4873335
#> education 13.1450737 2.8138234 -0.29071984 2.9937154
#> experience 18.7897595 11.6628366 0.37610718 -0.6699994
#>
#> Mardia's Estimate of Multivariate Skewness and Kurtosis:
#> b1 b1.chisq b1.correction b1.chisq.corrected
#> 1.253254e+01 2.692407e+03 1.002328e+00 2.698674e+03
#> b1.df b1.p b1.p.corrected b2
#> 5.600000e+01 0.000000e+00 0.000000e+00 5.848385e+01
#> b2.z b2.p
#> 1.920797e+01 3.174381e-82
#>
#> Correlations:
#> wages gender race union education
#> wages 1.0000000 -0.22330183 -0.12783381 0.102246656 0.456517980
#> gender -0.2233018 1.00000000 0.04327185 -0.088856935 -0.031439159
#> race -0.1278338 0.04327185 1.00000000 0.080587911 -0.087061729
#> union 0.1022467 -0.08885694 0.08058791 1.000000000 0.003966952
#> education 0.4565180 -0.03143916 -0.08706173 0.003966952 1.000000000
#> experience 0.1731733 -0.02265681 -0.03912910 0.154319024 -0.180103012
#> experience
#> wages 0.17317330
#> gender -0.02265681
#> race -0.03912910
#> union 0.15431902
#> education -0.18010301
#> experience 1.00000000
# for coverage
descriptives(
X = X,
y = y,
plot = FALSE,
moments = FALSE,
cor = FALSE,
mardia = FALSE
)
result_X <- as.vector(output[["X"]])
result_y <- as.vector(output[["y"]])
result_data <- as.vector(output[["data"]])
result_n <- as.vector(output[["n"]])
result_k <- as.vector(output[["k"]])
result_df1 <- as.vector(output[["df1"]])
result_df2 <- as.vector(output[["df2"]])
result_muhatX <- as.vector(output[["muhatX"]])
result_muhaty <- as.vector(output[["muhaty"]])
result_muhat <- as.vector(output[["muhat"]])
result_Rhat <- as.vector(output[["Rhat"]])
result_RXhat <- as.vector(output[["RXhat"]])
result_ryXhat <- as.vector(output[["ryXhat"]])
result_Sigmahat <- as.vector(output[["Sigmahat"]])
result_SigmaXhat <- as.vector(output[["SigmaXhat"]])
result_sigmayXhat <- as.vector(output[["sigmayXhat"]])
result_sigma2Xhat <- as.vector(output[["sigma2Xhat"]])
result_sigma2yhat <- as.vector(output[["sigma2yhat"]])
result_sigma2hat <- as.vector(output[["sigma2hat"]])
result_sigmahat <- as.vector(output[["sigmahat"]])
context("Test linreg-estimation-descriptives")
test_that("X", {
X_vector <- as.vector(X)
for (i in 1:length(result_X)) {
expect_equivalent(
result_X[i],
X_vector[i]
)
}
})
#> Test passed 🎊
test_that("y", {
y_vector <- as.vector(y)
for (i in 1:length(result_y)) {
expect_equivalent(
result_y[i],
y_vector[i]
)
}
})
#> Test passed 🥇
test_that("data", {
data_vector <- as.vector(data)
for (i in 1:length(result_data)) {
expect_equivalent(
result_data[i],
data_vector[i]
)
}
})
#> Test passed 🎉
test_that("n", {
expect_equivalent(
n,
result_n
)
})
#> Test passed 🎊
test_that("k", {
expect_equivalent(
k,
result_k
)
})
#> Test passed 😸
test_that("df1", {
expect_equivalent(
df1,
result_df1
)
})
#> Test passed 🎉
test_that("df2", {
expect_equivalent(
df2,
result_df2
)
})
#> Test passed 🎊
test_that("muhatX", {
for (i in 1:length(result_muhatX)) {
expect_equivalent(
muhatX[i],
result_muhatX[i]
)
}
})
#> Test passed 😸
test_that("muhaty", {
expect_equivalent(
muhaty,
result_muhaty
)
})
#> Test passed 🥇
test_that("muhat", {
for (i in 1:length(result_muhat)) {
expect_equivalent(
muhat[i],
result_muhat[i]
)
}
})
#> Test passed 🌈
test_that("Rhat", {
for (i in 1:length(result_Rhat)) {
expect_equivalent(
Rhat[i],
result_Rhat[i]
)
}
})
#> Test passed 😸
test_that("RXhat", {
for (i in 1:length(result_RXhat)) {
expect_equivalent(
RXhat[i],
result_RXhat[i]
)
}
})
#> Test passed 🥳
test_that("ryXhat", {
for (i in 1:length(result_ryXhat)) {
expect_equivalent(
ryXhat[i],
result_ryXhat[i]
)
}
})
#> Test passed 🌈
test_that("Sigmahat", {
for (i in 1:length(result_Sigmahat)) {
expect_equivalent(
Sigmahat[i],
result_Sigmahat[i]
)
}
})
#> Test passed 🥳
test_that("SigmaXhat", {
for (i in 1:length(result_SigmaXhat)) {
expect_equivalent(
SigmaXhat[i],
result_SigmaXhat[i]
)
}
})
#> Test passed 🥳
test_that("sigmayXhat", {
for (i in 1:length(result_sigmayXhat)) {
expect_equivalent(
sigmayXhat[i],
result_sigmayXhat[i]
)
}
})
#> Test passed 🎊
test_that("sigma2Xhat", {
for (i in 1:length(result_sigma2Xhat)) {
expect_equivalent(
sigma2Xhat[i],
result_sigma2Xhat[i]
)
}
})
#> Test passed 🎊
test_that("sigma2yhat", {
expect_equivalent(
sigma2yhat,
result_sigma2yhat
)
})
#> Test passed 😀
test_that("sigma2hat", {
for (i in 1:length(result_sigma2hat)) {
expect_equivalent(
sigma2hat[i],
result_sigma2hat[i]
)
}
})
#> Test passed 😸
test_that("sigmahat", {
for (i in 1:length(result_sigmahat)) {
expect_equivalent(
sigmahat[i],
result_sigmahat[i]
)
}
})
#> Test passed 😸