Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 2f1b7fe
Showing
67 changed files
with
4,751 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
Package: lmvar | ||
Type: Package | ||
Title: Linear Regression with Non-Constant Variances | ||
Version: 1.0.0 | ||
Author: Posthuma Partners <info@posthuma-partners.nl> | ||
Maintainer: Marco Nijmeijer <nijmeijer@posthuma-partners.nl> | ||
Description: Runs a linear regression in which both the expected value and the variance can vary per observation. The expected values mu follows the standard linear model mu = X_mu * beta_mu. The standard deviation sigma follows the model log(sigma) = X_sigma * beta_sigma. The package comes with two vignettes: 'Intro' gives an introduction, 'Math' gives mathematical details. | ||
License: GPL-3 | ||
LazyData: TRUE | ||
Imports: Matrix (>= 1.2-4), matrixcalc (>= 1.0-3), nleqslv (>= 3.0.3), | ||
stats (>= 3.2.5) | ||
RoxygenNote: 6.0.1 | ||
Suggests: testthat, knitr, rmarkdown, R.rsp | ||
VignetteBuilder: knitr, R.rsp | ||
NeedsCompilation: no | ||
Packaged: 2017-02-16 16:39:12 UTC; Marc | ||
Repository: CRAN | ||
Date/Publication: 2017-02-17 14:38:14 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
0da3dd6021303151430b92d13b01bc04 *DESCRIPTION | ||
41b7f05cd32d8769ef2208ad6368d7e9 *NAMESPACE | ||
799ae591e93eb076cea0d9f8150235dd *R/AIC.lmvar.R | ||
b0137d370c4c1a01bc5ceb87d8cc6285 *R/alias.lmvar.R | ||
c5a11e9f3431dd97d7e32a2df0f1fbd5 *R/beta_sigma_names.R | ||
08618c12995a54734f49b0735c535257 *R/coef.lmvar.R | ||
010573949388442cd98049ecc6b0e7b2 *R/df.residual.lmvar.R | ||
a9d9d476093ad924986c191b5241769b *R/dfree.R | ||
39b90ad72cc9880282de9bf614d68133 *R/examples/AIC_examples.R | ||
bd9d7d3681defffd188f22a59df22c77 *R/examples/alias_examples.R | ||
13fa82f04c348bab1ab18187053eea3b *R/examples/beta_sigma_names_examples.R | ||
c5ae01de2d2f10f7b4bfa919e3f388a6 *R/examples/coef_examples.R | ||
ee365be6d30d6714e4a6454e6a0bfef7 *R/examples/df.residual_examples.R | ||
9d526fd9bc9b4c72c55cc5cd2614b4fa *R/examples/dfree_examples.R | ||
dd73ddf4aff15db9a6a6407af1a59577 *R/examples/fisher_examples.R | ||
698ac483d76f91ef2a49fc965577ee33 *R/examples/fitted_examples.R | ||
8cb1e5039896f1872911159fc875f758 *R/examples/lmvar_examples.R | ||
e93509e8f18367eae4c7a21672482354 *R/examples/logLik_examples.R | ||
02000462392443088a7b9628b773cd8a *R/examples/nobs_examples.R | ||
f28168301e48d6a216067c9e6f46320a *R/examples/predict_examples.R | ||
014ab1c3a4fb3f2511a5df2dbe775723 *R/examples/residuals_examples.R | ||
3b54dd7601fce2246ecfe4fbebaf345f *R/examples/summary_examples.R | ||
2139927b488f26929811b608c885885f *R/examples/vcov_examples.R | ||
65c5d7d3efb959034969ce25081d0525 *R/fisher.R | ||
bb4e182ff8110e449ab82117c67b5f6f *R/fitted.lmvar.R | ||
d97d4f2dd4613ba9af5c79e8c683d4b7 *R/gaussian_var.R | ||
ae576df528a3f5b30cac7df2df9dce68 *R/lmvar.R | ||
8a5f8803d15359cd28f236e6a59d3d1d *R/logLik.lmvar.R | ||
11a615ab05ce7c6a45d5905a064587b6 *R/make_matrix_full_rank.R | ||
c6eb72596cb95e6d1f12968a110a1fcb *R/matrix_column_names.R | ||
4f63ac17353588bd3d9497d181ace9cb *R/nobs.lmvar.R | ||
f37f6f29c93effe8ebfdc3998dc28a20 *R/predict.lmvar.R | ||
af17a34e533280763874b7faa31208a1 *R/print.summary_lmvar.R | ||
68ba53a968caa6fc52a96117a4b2a537 *R/residuals.lmvar.R | ||
6f3aa4fe0edbf10588592d9bef786368 *R/summary.lmvar.R | ||
b07e75a5335b4375cedc45d4e876594f *R/vcov.lmvar.R | ||
058e5cb3b2ffde63a0e073c229b7ca24 *README.md | ||
9a51ea3e128c9edff4d3fe1f448b53a5 *build/vignette.rds | ||
828d6a905ed48145c432c23f00ef645c *inst/doc/Intro.R | ||
475cfafe2a4d03be48ab649bab4d5f81 *inst/doc/Intro.Rmd | ||
7fff116f96908a44f030b786be788671 *inst/doc/Intro.html | ||
0d2d2347fc49b246c9f6607324c60005 *inst/doc/Math.ltx | ||
604b46aa4979321bd4c90566bfa4cc44 *inst/doc/Math.pdf | ||
cdc8775371ef46701cbbfdd098441d22 *man/AIC.lmvar.Rd | ||
40be9e23383ce6cfb87f6158572eab2e *man/alias.lmvar.Rd | ||
96870ffeb6e33b5476b9014730e99d0f *man/beta_sigma_names.Rd | ||
dc231983bd8fc1c8234230199df56ebc *man/coef.lmvar.Rd | ||
ae69a6216348af4e1787e9550574a03e *man/df.residual.lmvar.Rd | ||
8a46914afe8717717af7868eb56fe898 *man/dfree.Rd | ||
a5e5ccd1c0fafcdaee3bbeb7cab47cd5 *man/fisher.Rd | ||
f84bf4c6fc34202b0f52fc560ac08b84 *man/fitted.lmvar.Rd | ||
9105d49018403123fc7fe8430ec2299a *man/lmvar.Rd | ||
d3041936af6e96cdc0ae4464e0f52796 *man/logLik.lmvar.Rd | ||
c33fe1105392aeab24e540c8f09459a4 *man/nobs.lmvar.Rd | ||
85f6305560b23fac8565e52f25fc606a *man/predict.lmvar.Rd | ||
080d695ec1395b492167dbf7e898c71a *man/print.summary_lmvar.Rd | ||
9b35a0b154bc37c64d22c063fe84850e *man/residuals.lmvar.Rd | ||
3001d4fdbb5a458419ddcac4c7e161f7 *man/summary.lmvar.Rd | ||
103ad2096a2c2810478be7d856ea2b32 *man/vcov.lmvar.Rd | ||
bde5eb3a375b2d0298b8c17d84eda405 *tests/create_test_data.R | ||
b5e9bbfbc1b319bc3baec90d163e1426 *tests/testthat.R | ||
9181c1bb3ff9b24d1d5da2dcf1a0ce1e *tests/testthat/test_extractors.R | ||
3dc4e0f80fc5c12cef9aadd1a98116f7 *tests/testthat/test_lmvar.R | ||
18b7f56763d877e27e3f5083153c384f *tests/testthat/test_predict.R | ||
475cfafe2a4d03be48ab649bab4d5f81 *vignettes/Intro.Rmd | ||
0d2d2347fc49b246c9f6607324c60005 *vignettes/Math.ltx |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
# Generated by roxygen2: do not edit by hand | ||
|
||
S3method(AIC,lmvar) | ||
S3method(alias,lmvar) | ||
S3method(coef,lmvar) | ||
S3method(df.residual,lmvar) | ||
S3method(fitted,lmvar) | ||
S3method(logLik,lmvar) | ||
S3method(nobs,lmvar) | ||
S3method(predict,lmvar) | ||
S3method(print,summary_lmvar) | ||
S3method(residuals,lmvar) | ||
S3method(summary,lmvar) | ||
S3method(vcov,lmvar) | ||
export(beta_sigma_names) | ||
export(dfree) | ||
export(fisher) | ||
export(lmvar) | ||
importFrom(stats,alias) | ||
importFrom(stats,nobs) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
#' @title AIC for an object of class 'lmvar' | ||
#' | ||
#' @description AIC (Aikaike's 'An Information Criterion') for an object of class 'lmvar' | ||
#' | ||
#' @param object Object of class 'lmvar' | ||
#' @param ... For compatibility with \code{\link[stats]{AIC}} generic | ||
#' @param k Numeric, the penalty per parameter to be used. The default k = 2 is the classical AIC. | ||
#' | ||
#' @return the AIC of the object | ||
#' | ||
#' @export | ||
#' | ||
#' @example R/examples/AIC_examples.R | ||
#' | ||
AIC.lmvar <- function(object, ..., k = 2){ | ||
df = dfree(object) | ||
return(k*df - 2 * logLik.lmvar(object)[1]) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
#' | ||
#' @title Aliased coefficients in an 'lmvar' object | ||
#' | ||
#' @description Returns the columns present in the user-specified model-matrices \eqn{X_\mu} and \eqn{X_\sigma} that were removed by | ||
#' \code{lmvar} to make the matrices full-rank. | ||
#' | ||
#' @param object Object of class 'lmvar' | ||
#' @param mu Boolean, specifies whether the aliased columns from the model matrix \eqn{X_\mu} must be returned | ||
#' @param sigma Boolean, specifies whether the aliased columns from the model matrix \eqn{X_\sigma} must be returned | ||
#' @param ... Additional arguments, not used in the current implementation | ||
#' | ||
#' @return A character vector containing the names of the aliased columns | ||
#' | ||
#' @details If \code{mu = TRUE} and \code{sigma = TRUE}, the function returns the aliased columns of both \eqn{X_\mu} | ||
#' and \eqn{X_\sigma}. The string "_s" is appended to the aliased column names from \eqn{X_\sigma} if at least one of those | ||
#' names also appears in \eqn{X_\mu} | ||
#' | ||
#'If \code{mu = TRUE} and \code{sigma = FALSE}, the function returns the aliased columns of \eqn{X_\mu}. | ||
#' | ||
#'If \code{mu = FALSE} and \code{sigma = TRUE}, the function returns the aliased columns of \eqn{X_\sigma}. | ||
#' | ||
#' @export | ||
#' | ||
#' @importFrom stats alias | ||
#' | ||
#' @example R/examples/alias_examples.R | ||
#' | ||
alias.lmvar <- function( object, mu = TRUE, sigma = TRUE, ...){ | ||
|
||
aliased_mu = character() | ||
if (mu){ | ||
aliased_mu = names(object$aliased_mu)[object$aliased_mu] | ||
} | ||
|
||
aliased_sigma = character() | ||
if (sigma){ | ||
aliased_sigma = names(object$aliased_sigma)[object$aliased_sigma] | ||
} | ||
|
||
if (mu & !sigma){ | ||
return(aliased_mu) | ||
} | ||
else if (!mu & sigma){ | ||
return(aliased_sigma) | ||
} | ||
else if (mu & sigma){ | ||
a_names = c( aliased_mu, beta_sigma_names( names(object$aliased_mu), aliased_sigma)) | ||
return(as.character(a_names)) | ||
} | ||
else { | ||
return(character()) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
#' | ||
#' @title Unique names for beta_sigma | ||
#' | ||
#' @description Returns adapted names for the coefficients \eqn{\beta_\sigma} to distinguish them from the names | ||
#' of the coefficients | ||
#' \eqn{\beta_\mu}. This is a helper function which is used in situations where it is necessary or convenient | ||
#' for the coefficient names of \eqn{\beta_\sigma} | ||
#' to be different from \eqn{\beta_\mu}. | ||
#' | ||
#' @param beta_mu_names Character vector with the names of the coefficients \eqn{\beta_\mu} | ||
#' @param beta_sigma_names Character vector with the names of the coefficients \eqn{\beta_\sigma} | ||
#' @param ... Additional arguments, not used in the current implementation | ||
#' | ||
#' @return Named character vector with the names of the coefficients \eqn{\beta_\sigma}. The name of a vector element | ||
#' is the original | ||
#' name of the coefficient. The value is the adapted name. The name and the value are equal if no adaptation was needed. | ||
#' | ||
#' @details When the name of at least one coefficient in \eqn{\beta_\sigma} is equal to one of the names of the | ||
#' coefficients in \eqn{\beta_\mu}, the string '_s' is | ||
#' appended to the names of all coefficients in \eqn{\beta_\sigma}. | ||
#' Otherwise, the names of the coefficients in \eqn{\beta_\sigma} are left unchanged. | ||
#' | ||
#' @export | ||
#' | ||
#' @example R/examples/beta_sigma_names_examples.R | ||
#' | ||
|
||
beta_sigma_names <- function( beta_mu_names, beta_sigma_names, ...){ | ||
|
||
if (is.null(beta_sigma_names)){ | ||
return(character()) | ||
} | ||
else | ||
{ | ||
# Check if at least one name of beta_sigma is identical to beta_mu | ||
bool = any(is.element( beta_sigma_names, beta_mu_names)) | ||
|
||
# Adapt the names of beta_sigma if at least one is identical to beta_mu | ||
if (bool){ | ||
beta_sigma_names_new = sapply( beta_sigma_names, function(x){ | ||
if (x != "(Intercept_s)"){ | ||
return(paste( x, "_s", sep="")) | ||
} | ||
else { | ||
return(x) | ||
} | ||
}) | ||
} | ||
else { | ||
beta_sigma_names_new = beta_sigma_names | ||
} | ||
|
||
names(beta_sigma_names_new) = beta_sigma_names | ||
|
||
return(beta_sigma_names_new) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
#' | ||
#' @title Extracts coefficients from an 'lmvar' object. | ||
#' | ||
#' @description Extracts maximum-likelihood estimators for \eqn{\beta_\mu} and \eqn{\beta_\sigma} from an 'lmvar' object. | ||
#' | ||
#' @param object Object of class 'lmvar' | ||
#' @param mu Boolean, specifies whether or not to return the maximum-likelihood estimator for \eqn{\beta_\mu} | ||
#' @param sigma Boolean, specifies whether or not to return the maximum-likelihood estimator for \eqn{\beta_\sigma} | ||
#' @param ... For compatibility with \code{\link[stats]{coef}} generic | ||
#' | ||
#' @return When \code{mu = TRUE} and \code{sigma = TRUE}, a named numeric vector with the elements of \eqn{\beta_\mu}, | ||
#' followed by the elements of \eqn{\beta_\sigma}. | ||
#' | ||
#' When \code{mu = TRUE} and \code{sigma = FALSE}, a named numeric vector with the elements of \eqn{\beta_\mu}. | ||
#' | ||
#' When \code{mu = FALSE} and \code{sigma = TRUE}, a named numeric vector with the elements of \eqn{\beta_\sigma}. | ||
#' | ||
#' @details When both \code{mu = TRUE} and \code{sigma = TRUE}, the names of the | ||
#' coefficients in \eqn{\beta_\sigma} are adapted to distinguish them from the names in \eqn{\beta_\mu}, if needed. | ||
#' | ||
#' @seealso \code{\link{beta_sigma_names}} for the adaptation of the names of the coefficients in \eqn{\beta_\sigma}. | ||
#' | ||
#' @export | ||
#' | ||
#' @example R/examples/coef_examples.R | ||
#' | ||
|
||
coef.lmvar <- function( object, mu = TRUE, sigma = TRUE, ...){ | ||
|
||
beta_mu = numeric() | ||
beta_sigma = numeric() | ||
beta_mu_names = character() | ||
beta_sigma_names = character() | ||
|
||
if (mu){ | ||
beta_mu = object$coefficients_mu | ||
beta_mu_names = names(object$coefficients_mu) | ||
} | ||
|
||
if (sigma){ | ||
beta_sigma = object$coefficients_sigma | ||
if (mu){ | ||
beta_sigma_names = lmvar::beta_sigma_names( names(object$coefficients_mu), names(object$coefficients_sigma)) | ||
} | ||
else { | ||
beta_sigma_names = names(object$coefficients_sigma) | ||
} | ||
} | ||
|
||
betas = c( beta_mu, beta_sigma) | ||
names(betas) = c( beta_mu_names, beta_sigma_names) | ||
|
||
return(betas) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
#' @title Residual degrees of freedom for an object of class 'lmvar' | ||
#' | ||
#' @description Residual degrees of freedom for an object of class 'lmvar'. The residual degrees of freedom are defined | ||
#' as the number of observations minus the degrees of freedom of the model. | ||
#' | ||
#' @param object Object of class 'lmvar' | ||
#' @param ... For compatibility with \code{\link[stats]{df.residual}} generic | ||
#' | ||
#' @return Residual degees of freedom for \code{object}. | ||
#' | ||
#' @export | ||
#' | ||
#' @seealso \code{\link{dfree}} for the degrees of freedom of an object of class 'lmvar' | ||
#' | ||
#' \code{\link{nobs.lmvar}} for the number of observations in an object of class 'lmvar' | ||
#' | ||
#' @example R/examples/df.residual_examples.R | ||
#' | ||
|
||
df.residual.lmvar <- function(object, ...){ | ||
df = nobs(object) - dfree(object) | ||
return(df) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
#' @title Degrees of freedom for an object of class 'lmvar' | ||
#' | ||
#' @description Degrees of freedom for the model in an object of class 'lmvar'. The degrees of freedom are defined as the rank of the | ||
#' model matrix \eqn{X_\mu} for the expectation values, plus the rank of the model matrix \eqn{X_\sigma} for the standard deviations. | ||
#' | ||
#' @param object Object of class 'lmvar' | ||
#' @param mu Boolean, specifies whether the degrees of freedom for the model for the expectation values must be included. | ||
#' @param sigma Boolean, specifies whether the degrees of freedom for the model for the standard deviations must be included. | ||
#' @param ... Additional arguments, not used in the current implementation | ||
#' | ||
#' @return An integer containing the degrees of freedom for the model in \code{object}. | ||
#' | ||
#' @details If \code{mu = TRUE} and \code{sigma = TRUE}, the function returns the rank of the model-matrix \eqn{X_\mu} plus the | ||
#' rank of the model matrix \eqn{X_\sigma}. | ||
#' | ||
#' If \code{mu = TRUE} and \code{sigma = FALSE}, the function returns the rank of the model-matrix \eqn{X_\mu}. | ||
#' | ||
#' If \code{mu = FALSE} and \code{sigma = TRUE}, the function returns the rank of the model-matrix \eqn{X_\sigma}. | ||
#' | ||
#' Both model matrices contain a column corresponding to an intercept term. This column is added by \code{\link{lmvar}}. | ||
#' See also the vignette 'Intro'. | ||
#' | ||
#' @export | ||
#' | ||
#' @seealso \code{\link{df.residual.lmvar}} for the residual degrees of freedom for an object of class 'lmvar' | ||
#' | ||
#' @example R/examples/dfree_examples.R | ||
#' | ||
|
||
dfree <- function( object, mu = TRUE, sigma = TRUE, ...){ | ||
|
||
if (class(object) != 'lmvar'){ | ||
stop("Object must be an 'lmvar' object") | ||
} | ||
|
||
if (mu & sigma){ | ||
return( ncol(object$X_mu) + ncol(object$X_sigma)) | ||
} | ||
else if (mu){ | ||
return( ncol(object$X_mu)) | ||
} | ||
else { | ||
return( ncol(object$X_sigma)) | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
\dontrun{ | ||
|
||
# Let 'fit' be an object of class 'lmvar'. The classical AIC is | ||
AIC(fit) | ||
|
||
# To calculate the AIC with penalty-parameter k = 3 run | ||
AIC(fit, k = 3) | ||
} |
Oops, something went wrong.