### Run Package Examples Source: https://cran.r-project.org/web/packages/DirichletReg/refman/DirichletReg.html Executes example code for plotting DirichletRegData and fitting DirichReg models. ```R example(plot.DirichletRegData) example(DirichReg) ``` -------------------------------- ### Summary of Dirichlet Regression Model Example Source: https://cran.r-project.org/web/packages/DirichletReg/refman/DirichletReg.html Generates and prints a summary of the Dirichlet regression model 'mod1'. ```R summary(mod1) ``` -------------------------------- ### Example: Create and Summarize DirichletRegData Source: https://cran.r-project.org/web/packages/DirichletReg/refman/DirichletReg.html Demonstrates creating a DirichletRegData object from a subset of the ArcticLake data and then summarizing and viewing the first few entries. ```R # create a DirichletRegData object from the Arctic Lake data head(ArcticLake[, 1:3]) AL <- DR_data(ArcticLake[, 1:3]) summary(AL) head(AL) ``` -------------------------------- ### Print Dirichlet Regression Model Object Example Source: https://cran.r-project.org/web/packages/DirichletReg/refman/DirichletReg.html Prints the Dirichlet regression model object 'mod1'. ```R mod1 ``` -------------------------------- ### Log-Likelihood Example for Dirichlet Model Source: https://cran.r-project.org/web/packages/DirichletReg/refman/DirichletReg.html Extracts and displays the log-likelihood value for the Dirichlet regression model 'mod1'. ```R logLik(mod1) ``` -------------------------------- ### Dirichlet Regression Model Example Source: https://cran.r-project.org/web/packages/DirichletReg/refman/DirichletReg.html Demonstrates fitting a Dirichlet regression model using the 'alternative' model type with transformed data. ```R ALake <- ArcticLake ALake$AL <- DR_data(ArcticLake[, 1:3]) mod1 <- DirichReg(AL ~ depth + I(depth^2) | depth, data = ALake, model="alternative") ``` -------------------------------- ### Fitted Values Example for Dirichlet Model Source: https://cran.r-project.org/web/packages/DirichletReg/refman/DirichletReg.html Extracts and displays the first few fitted values from the Dirichlet regression model 'mod1'. ```R head(fitted(mod1)) ``` -------------------------------- ### Residuals Example for Dirichlet Model Source: https://cran.r-project.org/web/packages/DirichletReg/refman/DirichletReg.html Extracts and displays the first few residuals from the Dirichlet regression model 'mod1'. ```R head(residuals(mod1)) ``` -------------------------------- ### Confidence Intervals Example for Dirichlet Model Source: https://cran.r-project.org/web/packages/DirichletReg/refman/DirichletReg.html Computes and displays the default confidence intervals for the parameters of the Dirichlet regression model 'mod1'. ```R confint(mod1) ``` -------------------------------- ### Exponentiated Confidence Intervals Example for Dirichlet Model Source: https://cran.r-project.org/web/packages/DirichletReg/refman/DirichletReg.html Computes and displays the confidence intervals for the parameters of the Dirichlet regression model 'mod1', with parameters returned in exponentiated form. ```R confint(mod1, exp = TRUE) ``` -------------------------------- ### Covariance Matrix Example for Dirichlet Model Source: https://cran.r-project.org/web/packages/DirichletReg/refman/DirichletReg.html Extracts and displays the covariance matrix of the parameter estimates for the Dirichlet regression model 'mod1', rounded to 5 decimal places. ```R round(vcov(mod1), 5) ``` -------------------------------- ### Prediction Example for Dirichlet Model Source: https://cran.r-project.org/web/packages/DirichletReg/refman/DirichletReg.html Predicts values for a new dataset with specified depth values using the Dirichlet regression model 'mod1'. ```R predict(mod1, newdata = data.frame("depth" = seq(10, 100, 10))) ``` -------------------------------- ### Drop-One Analysis Example for Dirichlet Model Source: https://cran.r-project.org/web/packages/DirichletReg/refman/DirichletReg.html Performs a drop-one analysis on the Dirichlet regression model 'mod1'. Note: may issue a caveat on first use in an R session. ```R drop1(mod1) ``` -------------------------------- ### Update Dirichlet Regression Model Formula Example Source: https://cran.r-project.org/web/packages/DirichletReg/refman/DirichletReg.html Shows how to update the formula of an existing Dirichlet regression model object. The 'evaluate = FALSE' argument prevents immediate evaluation. ```R update(mod1, . ~ . | . + I(depth^2), evaluate = FALSE) ``` -------------------------------- ### Get Fitted Values from DirichletRegModel Source: https://cran.r-project.org/web/packages/DirichletReg/refman/DirichletReg.html Extracts fitted values (expected values, alpha, or phi) from a DirichletRegModel object. ```R ## S3 method for class 'DirichletRegModel' fitted(object, mu = TRUE, alpha = FALSE, phi = FALSE, ...) ``` -------------------------------- ### Calculate Dirichlet Distribution Densities Source: https://cran.r-project.org/web/packages/DirichletReg/refman/DirichletReg.html Computes densities for the Dirichlet distribution. Use 'sum.up = TRUE' to get the (log-)likelihood. Returns NaN for non-positive alpha values. ```R ddirichlet(X1, c(5, 5, 10)) ddirichlet(X2, a.mat) ddirichlet(X2[1:3,], c(1, 2, -1)) ddirichlet(X2[1:3,], c(1, 2, -1), sum.up = TRUE) ``` -------------------------------- ### Get BibTeX Citation for DirichletReg Source: https://cran.r-project.org/web/packages/DirichletReg/citation.html Use this R command to retrieve the BibTeX citation for the DirichletReg package. This is useful for including package references in academic papers using LaTeX. ```r toBibtex(citation("DirichletReg")) ``` -------------------------------- ### Load and Inspect Arctic Lake Data Source: https://cran.r-project.org/web/packages/DirichletReg/refman/DirichletReg.html Demonstrates how to load and view the first few rows of the ArcticLake dataset, and prepare it for Dirichlet regression analysis. ```R head(ArcticLake) AL <- DR_data(ArcticLake[,1:3]) plot(AL) summary(AL) ``` -------------------------------- ### Fit Dirichlet Regression Models Source: https://cran.r-project.org/web/packages/DirichletReg/refman/DirichletReg.html Demonstrates fitting common and alternative Dirichlet regression models. Use 'model="alternative"' for precision parameter modeling. ```R ALake <- ArcticLake ALake$Y <- DR_data(ALake[,1:3]) # fit a quadratic Dirichlet regression models ("common") res1 <- DirichReg(Y ~ depth + I(depth^2), ALake) # fit a Dirichlet regression with quadratic predictor for the mean and # a linear predictor for precision ("alternative") res2 <- DirichReg(Y ~ depth + I(depth^2) | depth, ALake, model="alternative") # test both models anova(res1, res2) res1 summary(res2) ``` -------------------------------- ### Load and Inspect Blood Samples Data Source: https://cran.r-project.org/web/packages/DirichletReg/refman/DirichletReg.html Shows how to load and view the first few rows of the BloodSamples dataset, and prepare it for Dirichlet regression analysis. ```R head(BloodSamples) Bl <- DR_data(BloodSamples[,1:4]) summary(Bl) ``` -------------------------------- ### Load and Prepare Arctic Lake Data Source: https://cran.r-project.org/web/packages/DirichletReg/vignettes/DirichletReg-vig.html Loads the ArcticLake dataset and prepares it for Dirichlet regression by converting the first three columns into a Dirichlet distribution data format. ```R library("DirichletReg") head(ArcticLake) AL <- DR_data(ArcticLake[,1:3]) AL[1:6,] ``` -------------------------------- ### Prepare and Predict with Dirichlet Regression for Arctic Lake Source: https://cran.r-project.org/web/packages/DirichletReg/vignettes/DirichletReg-vig.html Prepares the ArcticLake data for a quadratic Dirichlet regression model and generates predictions for a sequence of depths. ```R AL <- ArcticLake AL$AL <- DR_data(ArcticLake[,1:3]) dd <- range(ArcticLake$depth) X <- data.frame(depth=seq(dd[1], dd[2], length.out=200)) pp <- predict(DirichReg(AL~depth+I(depth^2),AL), X) ``` -------------------------------- ### Quick Analyses with DR_data in DirichletReg Source: https://cran.r-project.org/web/packages/DirichletReg/news.html This demonstrates a quick analysis using DR_data for preliminary checks. Note that this functionality is intended for temporary use and may be removed in future versions. ```R DirichReg(DR_data(A) ~ 1) ``` -------------------------------- ### Visualize Arctic Lake Quadratic Model Fit Source: https://cran.r-project.org/web/packages/DirichletReg/vignettes/DirichletReg-vig.html Plots the observed sediment composition against depth and overlays the fitted values from the quadratic Dirichlet regression model, including the precision parameter. ```R par(mar=c(4, 4, 4, 4)+0.1) plot(rep(ArcticLake$depth,3),as.numeric(AL), pch=21, bg=rep(c("#E495A5", "#86B875", "#7DB0DD"),each=39), xlab="Depth (m)", ylab="Proportion", ylim=0:1, main="Sediment Composition in an Arctic Lake") Xnew <- data.frame(depth=seq(min(ArcticLake$depth), max(ArcticLake$depth), length.out=100)) for(i in 1:3)lines(cbind(Xnew, predict(lake2, Xnew)[,i]),col=c("#E495A5", "#86B875", "#7DB0DD")[i],lwd=2) legend("topleft", legend=c("Sand","Silt","Clay"), lwd=2, col=c("#E495A5", "#86B875", "#7DB0DD"), pt.bg=c("#E495A5", "#86B875", "#7DB0DD"), pch=21,bty="n") par(new=TRUE) plot(cbind(Xnew, predict(lake2, Xnew, F,F,T)), lty="24", type="l",ylim=c(0,max(predict(lake2, Xnew, F,F,T)))),axes=F,ann=F,lwd=2) axis(4) mtext(expression(paste("Precision (",phi,")",sep="")), 4, line=3) legend("top",legend=c(expression(hat(mu[c]==hat(alpha)[c]/hat(alpha)[0])),expression(hat(phi)==hat(alpha)[0]))),lty=c(1,2),lwd=c(3,2),bty="n") ``` -------------------------------- ### Visualize Blood Sample Box Plots and Fitted Values Source: https://cran.r-project.org/web/packages/DirichletReg/vignettes/DirichletReg-vig.html Generates box plots for each component of the blood sample composition, stratified by disease type, and overlays fitted values from the Dirichlet regression model. ```R par(mfrow=c(1,4), mar=c(4,4,4,2)+.25) for(i in 1:4){ boxplot(Bld$Smp[,i]~Bld$Disease, ylim=range(Bld$Smp[,1:4]), main=paste(names(Bld)[i]), xlab="Disease Type", ylab="Proportion") segments(c(-5,1.5),unique(fitted(blood2)[,i]), c(1.5,5),unique(fitted(blood2)[,i]),lwd=2,lty=2) } ``` -------------------------------- ### Fit and Compare Dirichlet Regression Models for Arctic Lake Source: https://cran.r-project.org/web/packages/DirichletReg/vignettes/DirichletReg-vig.html Fits a simple Dirichlet regression model and a quadratic model to the Arctic Lake data, then compares them using ANOVA. ```R lake1 <- DirichReg(AL~depth, ArcticLake) lake1 coef(lake1) lake2 <- update(lake1, .~.+I(depth^2)|.+I(depth^2)|.+I(depth^2)) anova(lake1,lake2) summary(lake2) ``` -------------------------------- ### Arctic Lake Data (Aitchison) Source: https://cran.r-project.org/web/packages/DirichletReg/refman/DirichletReg.html Dataset containing sediment composition and depth information from Aitchison (2003). Used for analyzing the relation of sediment composition with depth in an Arctic lake. ```APIDOC ## ArcticLake ### Description These data are taken from Aitchison (2003) and contain information on the relation of sediment composition with depth in an Arctic lake. ### Format A data frame with 39 observations on the following 4 variables: `sand`, `silt`, `clay` relative frequencies of sand, silt, and clay `depth` water depth in meters ### Source Aitchison, J. (2003). _The Statistical Analysis of Compositional Data._ The Blackburn Press, Caldwell, NJ. ### Examples ```R head(ArcticLake) AL <- DR_data(ArcticLake[,1:3]) plot(AL) summary(AL) ``` ``` -------------------------------- ### Fit and Compare Dirichlet Regression Models for Blood Samples Source: https://cran.r-project.org/web/packages/DirichletReg/vignettes/DirichletReg-vig.html Fits two Dirichlet regression models to the BloodSamples data using the 'alternative' parametrization, one with a simple disease effect and another with a disease-specific effect, and compares them using ANOVA. ```R Bld <- BloodSamples Bld$Smp <- DR_data(Bld[,1:4]) blood1 <- DirichReg(Smp~Disease|1, Bld, model="alternative", base=3) blood2 <- DirichReg(Smp~Disease|Disease, Bld, model="alternative", base=3) anova(blood1, blood2) summary(blood1) ``` -------------------------------- ### Compare OLS and Dirichlet Regression Predictions for Arctic Lake Source: https://cran.r-project.org/web/packages/DirichletReg/vignettes/DirichletReg-vig.html Visualizes the predictions from both OLS and Dirichlet regression models for the Arctic Lake data, comparing their performance on a ternary plot. ```R plot(AL$AL, cex=.1, reset_par=FALSE) points(toSimplex(AL$AL), pch=16, cex=.5, col=gray(.5)) lines(toSimplex(pp), lwd=3, col=c("#6E1D34", "#004E42")[2]) Dols <- log(cbind(ArcticLake[,2]/ArcticLake[,1], ArcticLake[,3]/ArcticLake[,1])) ols <- lm(Dols~depth+I(depth^2), ArcticLake) p2 <- predict(ols, X) p2m <- exp(cbind(0,p2[,1],p2[,2]))/rowSums(exp(cbind(0,p2[,1],p2[,2]))) lines(toSimplex(p2m), lwd=3, col=c("#6E1D34", "#004E42")[1], lty="21") ``` -------------------------------- ### Model Summaries and Confidence Intervals Source: https://cran.r-project.org/web/packages/DirichletReg/vignettes/DirichletReg-vig.html This snippet shows how to obtain summary statistics for a linear regression model (rlr) and a Dirichlet regression model (rs2), as well as calculate confidence intervals for the Dirichlet regression parameters, both on the original scale and exponentiated. ```R a <- RS$accuracy logRa_a <- log(a/(1-a)) rlr <- lm(logRa_a~dyslexia*iq, RS) summary(rlr) ``` ```R summary(rs2) ``` ```R confint(rs2) ``` ```R confint(rs2, exp=TRUE) ``` -------------------------------- ### Fitting Dirichlet Regression Models Source: https://cran.r-project.org/web/packages/DirichletReg/vignettes/DirichletReg-vig.html This snippet demonstrates fitting two Dirichlet regression models with different parameterizations ('alternative' model) and comparing them using an ANOVA test. Ensure the 'DR_data' function is available for data transformation. ```R RS <- ReadingSkills RS$acc <- DR_data(RS$accuracy) RS$dyslexia <- C(RS$dyslexia, treatment) rs1 <- DirichReg(acc ~ dyslexia*iq | dyslexia*iq, RS, model="alternative") rs2 <- DirichReg(acc ~ dyslexia*iq | dyslexia+iq, RS, model="alternative") anova(rs1,rs2) ``` -------------------------------- ### Serum Protein Composition in Blood Samples Source: https://cran.r-project.org/web/packages/DirichletReg/refman/DirichletReg.html Dataset containing blood samples' compositions of Albumin, Pre-Albumin, Globulin A, and Globulin B in relation to diseases, from Aitchison (2003). ```APIDOC ## BloodSamples ### Description These data (Aitchison, 2003) list blood samples' compositions of _Albumin_ , _Pre-Albumin_ , _Globulin A_ , and _Globulin B_ in relation to two types of diseases. 14 patients suffer from disease A, 16 from disease B and 6 are unclassified. ### Format A data frame with 36 observations on the following 6 variables. `Albumin`, `Pre.Albumin`, `Globulin.A`, `Globulin.B` the amounts of Albumin, Pre-Albumin, Globulin A, and Globulin B. `Disease` diagnosis of disease `A`, `B`, or `NA` for unclassified observations. `New` a factor indicating whether the observations are old and classified (`No`) or new and unclassified (`Yes`). ### Source Aitchison, J. (2003). _The Statistical Analysis of Compositional Data._ The Blackburn Press, Caldwell, NJ. ### Examples ```R head(BloodSamples) Bl <- DR_data(BloodSamples[,1:4]) summary(Bl) ``` ``` -------------------------------- ### Pure R Versions of Dirichlet Density Functions Source: https://cran.r-project.org/web/packages/DirichletReg/news.html Pure R versions of the Dirichlet density functions are available for reference or when C-based routines are not preferred. Refer to '?ddirichlet' for more details. ```R ?ddirichlet ``` -------------------------------- ### print.DirichletRegConfint Source: https://cran.r-project.org/web/packages/DirichletReg/refman/DirichletReg.html Prints confidence intervals for Dirichlet regression model parameters. ```APIDOC ## print.DirichletRegConfint ### Description Prints confidence intervals for Dirichlet regression model parameters. ### Usage ```R print(x, digits = 3, ...) ``` ### Arguments * `x`: An object of class `DirichletRegConfint`. * `digits`: The number of digits to display. * `...`: Further arguments. ``` -------------------------------- ### summary.DirichletRegModel Source: https://cran.r-project.org/web/packages/DirichletReg/refman/DirichletReg.html Provides a summary of the Dirichlet regression model. ```APIDOC ## summary.DirichletRegModel ### Description Provides a summary of the Dirichlet regression model. ### Usage ```R summary(object, ...) ``` ### Arguments * `object`: An object of class `DirichletRegModel`. * `...`: Further arguments. ``` -------------------------------- ### Compare Dirichlet Regression Models Source: https://cran.r-project.org/web/packages/DirichletReg/refman/DirichletReg.html Perform pairwise likelihood ratio tests (LRT) to compare Dirichlet regression models. The test statistic is computed as LR = -2 * [log(La) - log(Lb)], where La and Lb are the likelihoods of the models being compared. ```R ALake <- ArcticLake ALake$AL <- DR_data(ArcticLake[,1:3]) mod0 <- DirichReg(AL ~ 1, ALake) mod1 <- DirichReg(AL ~ depth, ALake) mod2 <- DirichReg(AL ~ depth + I(depth^2), ALake) anova(mod1, mod0, mod2, sorted = TRUE) ``` -------------------------------- ### summary Method for DirichletRegData Source: https://cran.r-project.org/web/packages/DirichletReg/refman/DirichletReg.html Provides a summary of a DirichletRegData object. ```APIDOC ## summary.DirichletRegData ### Description Provides a summary of a `DirichletRegData` object. ### Usage ```R summary(object, ...) ``` ### Arguments * `object` (`DirichletRegData` object): The object to summarize. * `...`: Further arguments. ### Details Outputs summary statistics and information about the processed `DirichletRegData` object. ``` -------------------------------- ### Summarize DirichletRegModel Object Source: https://cran.r-project.org/web/packages/DirichletReg/refman/DirichletReg.html Provides a summary of a DirichletRegModel object. ```R ## S3 method for class 'DirichletRegModel' summary(object, ...) ``` -------------------------------- ### Prepare Compositional Data with DR_data Source: https://cran.r-project.org/web/packages/DirichletReg/refman/DirichletReg.html Use DR_data to prepare matrices or data frames with compositional variables for Dirichlet regression. It handles normalization and optional transformations. ```R DR_data(Y, trafo = sqrt(.Machine$double.eps), base = 1, norm_tol = sqrt(.Machine$double.eps)) ``` -------------------------------- ### Transform Data to Simplex Source: https://cran.r-project.org/web/packages/DirichletReg/refman/DirichletReg.html Use these functions to transform matrices with three or four components into a two- or three-dimensional simplex for plotting or analysis. Note that functions other than toSimplex() lack input checks. ```R toSimplex(x) toTernary(abc) toTernaryVectors(c1, c2, c3) toQuaternary(abcd) toQuaternaryVectors(c1, c2, c3, c4) ``` -------------------------------- ### print.DirichletRegModel Source: https://cran.r-project.org/web/packages/DirichletReg/refman/DirichletReg.html Prints the results of a Dirichlet regression model. ```APIDOC ## print.DirichletRegModel ### Description Prints the results of a Dirichlet regression model. ### Usage ```R print(x, digits = max(3, getOption("digits") - 3), ...) ``` ### Arguments * `x`: An object of class `DirichletRegModel`. * `digits`: The number of digits to display. * `...`: Further arguments. ``` -------------------------------- ### Plot Arctic Lake Data Source: https://cran.r-project.org/web/packages/DirichletReg/vignettes/DirichletReg-vig.html Generates a ternary plot and a depth vs. composition plot for the Arctic Lake dataset. ```R plot(AL, cex=.5, a2d=list(colored=FALSE, c.grid=FALSE)) ``` ```R plot(rep(ArcticLake$depth,3),as.numeric(AL), pch=21, bg=rep(c("#E495A5", "#86B875", "#7DB0DD"),each=39), xlab="Depth (m)", ylab="Proportion", ylim=0:1) ``` -------------------------------- ### BibTeX Entry for DirichletReg Package (Current Version) Source: https://cran.r-project.org/web/packages/DirichletReg/citation.html This is the BibTeX entry for the current version of the DirichletReg R package. It includes title, author, year, version, and URL. ```bibtex @Manual{ title = {DirichletReg: Dirichlet Regression}, author = {Marco J. Maier}, year = {2025}, note = {R package version 0.7-2}, url = {https://github.com/maiermarco/DirichletReg}, } ``` -------------------------------- ### Compute Confidence Intervals for DirichletRegModel Source: https://cran.r-project.org/web/packages/DirichletReg/refman/DirichletReg.html Calculates confidence intervals for parameters (beta, gamma, or all) of a DirichletRegModel object. Can return parameters in exponentiated form. ```R ## S3 method for class 'DirichletRegModel' confint(object, parm, level, ..., type=c("all", "beta", "gamma"), exp = FALSE) ``` -------------------------------- ### Transform Compositional Data for a Simplex Source: https://cran.r-project.org/web/packages/DirichletReg/refman/DirichletReg.html These functions transform a matrix with three or four components to fit into a two- or three-dimensional simplex (triangle or tetrahedron). They are useful for plotting compositional data in a reduced dimensional space. ```APIDOC ## Transform Compositional Data for a Simplex ### Description These functions transform a matrix with three or four components to fit into a two- or three-dimensional simplex (triangle or tetrahedron). ### Usage ```R toSimplex(x) toTernary(abc) toTernaryVectors(c1, c2, c3) toQuaternary(abcd) toQuaternaryVectors(c1, c2, c3, c4) ``` ### Arguments `x` | a matrix-like object with 3 or 4 columns. `abc` | a matrix-like object with 3 columns. `abcd` | a matrix-like object with 4 columns. `c1` | a numeric vector with values of the first component. `c2` | a numeric vector with values of the second component. `c3` | a numeric vector with values of the third component. `c4` | a numeric vector with values of the fourth component. ### Details Most of these functions are only used internally, but sometimes it might be useful to plot “custom” ternary or quaternary graphics. Note that, apart from `toSimplex()`, functions do not have _any_ checks, so it is advisable to use this function if elements are added to plots or own graphics are created. ### Value The function returns a `matrix` object with coordinates in two or three dimensions ``` -------------------------------- ### Print DirichletRegConfint Object Source: https://cran.r-project.org/web/packages/DirichletReg/refman/DirichletReg.html Prints confidence intervals for a DirichletRegConfint object, typically obtained from confint.DirichletRegModel. ```R ## S3 method for class 'DirichletRegConfint' print(x, digits = 3, ...) ``` -------------------------------- ### confint.DirichletRegModel Source: https://cran.r-project.org/web/packages/DirichletReg/refman/DirichletReg.html Computes confidence intervals for the parameters of a Dirichlet regression model. ```APIDOC ## confint.DirichletRegModel ### Description Computes confidence intervals for the parameters of a Dirichlet regression model. ### Usage ```R confint(object, parm, level, ..., type=c("all", "beta", "gamma"), exp = FALSE) ``` ### Arguments * `object`: An object of class `DirichletRegModel`. * `parm`: A vector containing names of the parameters to compute confidence intervals for. * `level`: Confidence level(s), defaults to 0.95. * `type`: Defines the type of parameter for which confidence values are returned: "all", "beta", or "gamma". * `exp`: Logical; if TRUE, returns parameters in exponentiated form. * `...`: Further arguments. ``` -------------------------------- ### Predict and Calculate Dirichlet Probabilities for Blood Samples Source: https://cran.r-project.org/web/packages/DirichletReg/vignettes/DirichletReg-vig.html Predicts Dirichlet parameters for two disease groups (A and B) and calculates the probability density of observed blood sample compositions under these predicted parameters. ```R alpha <- predict(blood2, data.frame(Disease=factor(c("A","B"))), F, T, F) L <- sapply(1:2, function(i) ddirichlet(DR_data(Bld[31:36,1:4]), unlist(alpha[i,]))) LP <- L / rowSums(L) dimnames(LP) <- list(paste("C",1:6), c("A", "B")) print(data.frame(round(LP * 100, 1),"pred."=as.factor(ifelse(LP[,1]>LP[,2], "==> A", "==> B"))),print.gap=2) ``` -------------------------------- ### Print DirichletRegModel Object Source: https://cran.r-project.org/web/packages/DirichletReg/refman/DirichletReg.html Prints a DirichletRegModel object. Adjusts digits based on global options. ```R ## S3 method for class 'DirichletRegModel' print(x, digits = max(3, getOption("digits") - 3), ...) ``` -------------------------------- ### Compare Dirichlet Regression Models using an LRT Source: https://cran.r-project.org/web/packages/DirichletReg/refman/DirichletReg.html This function allows for pairwise tests of Dirichlet regression models using a likelihood ratio test (LRT). It helps in selecting the best model among a set of fitted Dirichlet regression models. ```APIDOC ## Compare Dirichlet Regression Models using an LRT ### Description This function allows for pairwise tests of Dirichlet regression models using a likelihood ratio test (LRT). ### Usage ```R ## S3 method for class 'DirichletRegModel' anova(object, ..., sorted = FALSE) ``` ### Arguments `object` | the model to be compared against those listed in ... `...` | models to be tested against the one specified as `object` `sorted` | should the models be sorted according to their numbers or parameters? ### Details The test statistic is computed `LR=−2[log⁡(La)−log⁡(Lb)]` where `Li` is the likelihood of model `i` with `df` equal to the difference of the number of parameters in the models. ### Examples ```R ALake <- ArcticLake ALake$AL <- DR_data(ArcticLake[,1:3]) mod0 <- DirichReg(AL ~ 1, ALake) mod1 <- DirichReg(AL ~ depth, ALake) mod2 <- DirichReg(AL ~ depth + I(depth^2), ALake) anova(mod1, mod0, mod2, sorted = TRUE) ``` ``` -------------------------------- ### rdirichlet Function Source: https://cran.r-project.org/web/packages/DirichletReg/refman/DirichletReg.html Generates random numbers from a Dirichlet distribution. ```APIDOC ## rdirichlet(n, alpha) ### Description Generates random numbers from a Dirichlet distribution. ### Arguments - `n` (numeric): The number of random observations to draw. - `alpha` (numeric vector or matrix): The Dirichlet distribution's parameters. Can be a vector (one set of parameters for all observations) or a matrix (a different set of parameters for each observation). ### Value Returns a matrix with random numbers according to the supplied alpha vector or matrix. ### Examples ```R X1 <- rdirichlet(100, c(5, 5, 10)) a.mat <- cbind(1:10, 5, 10:1) a.mat X2 <- rdirichlet(10, a.mat) # note how the probabilities in the first an last column relate to a.mat round(X2, 2) ``` ``` -------------------------------- ### logLik.DirichletRegModel Source: https://cran.r-project.org/web/packages/DirichletReg/refman/DirichletReg.html Extracts the log-likelihood of a Dirichlet regression model. ```APIDOC ## logLik.DirichletRegModel ### Description Extracts the log-likelihood of a Dirichlet regression model. ### Usage ```R logLik(object, ...) ``` ### Arguments * `object`: An object of class `DirichletRegModel`. * `...`: Further arguments. ``` -------------------------------- ### Print DirichletRegData Object Source: https://cran.r-project.org/web/packages/DirichletReg/refman/DirichletReg.html Prints a DirichletRegData object, showing either the processed or original data. Useful for inspecting the prepared data. ```R print(x, type = c("processed", "original"), ...) ``` -------------------------------- ### Summarize DirichletRegData Object Source: https://cran.r-project.org/web/packages/DirichletReg/refman/DirichletReg.html Provides a summary of a DirichletRegData object, offering insights into its structure and properties. ```R summary(object, ...) ``` -------------------------------- ### Visualize Blood Sample Predictions on Ternary Plot Source: https://cran.r-project.org/web/packages/DirichletReg/vignettes/DirichletReg-vig.html Plots observed blood sample compositions and overlays predicted values for disease groups A and B on a ternary plot, using a color gradient to represent the predicted proportions. ```R B2 <- DR_data(BloodSamples[,c(1,2,4)]) plot(B2, cex=.001, reset_par=FALSE) div.col <- colorRampPalette(c("#023FA5", "#c0c0c0", "#8E063B"))(100) # expected values temp <- (alpha/rowSums(alpha))[,c(1,2,4)] points(toSimplex(temp/rowSums(temp)), pch=22, bg=div.col[c(1,100)], cex=2, lwd=.25) ``` -------------------------------- ### ddirichlet_R Function Source: https://cran.r-project.org/web/packages/DirichletReg/refman/DirichletReg.html Computes the density function of the Dirichlet distribution using R implementation. ```APIDOC ## ddirichlet_R(x, alpha, log = FALSE, sum.up = FALSE) ### Description Computes the density function of the Dirichlet distribution, implemented purely in R. ### Arguments - `x` (matrix): A matrix containing observations. - `alpha` (numeric vector or matrix): The Dirichlet distribution's parameters. - `log` (logical): If `TRUE`, logarithmic densities are returned. - `sum.up` (logical): If `TRUE`, the (log-)likelihood is returned. ### Value As `ddirichlet`, only implemented purely in R. ### Examples ```R X1 <- rdirichlet(100, c(5, 5, 10)) ddirichlet_R(X1, c(5, 5, 10)) ``` ``` -------------------------------- ### DR_data Function Source: https://cran.r-project.org/web/packages/DirichletReg/refman/DirichletReg.html Prepares a matrix with compositional variables for further processing in the DirichletReg package. It handles normalization and optional transformations. ```APIDOC ## DR_data ### Description This function prepares a matrix with compositional variables for further processing in the DirichletReg package. ### Usage ```R DR_data(Y, trafo = sqrt(.Machine$double.eps), base = 1, norm_tol = sqrt(.Machine$double.eps)) ``` ### Arguments * `Y` (matrix or data.frame): Compositional variables with nonnegative values. Can also be a vector in some cases. * `trafo` (logical or numeric): Controls transformation of variables to shrink extreme values. If numeric, it acts as a threshold. * `base` (numeric): The "base" component to use in the reparametrized model. Defaults to 1. * `norm_tol` (numeric): Tolerance for testing near equality to 1 for row sums of Y. Defaults to `sqrt(.Machine$double.eps)`. ### Details #### `Y` If row sums of `Y` do not equal 1, normalization is applied. Rows with `NA` values will result in `NA` for the entire row. Beta-distributed variables can be supplied as a single vector, with the second variable generated as `1 - Y`. #### `trafo` Applies a transformation to components `y` using the formula `y* = y(n-1) + 1/d / n`, where `n` is the number of observations and `d` is the number of dimensions. This is a generalization of methods used in `betareg`. #### `base` Sets the omitted component for the "alternative" (mean/precision) model. By default, it's the first variable in `Y`. ### Value Returns a `matrix` object of class `DirichletRegData` with attributes like original data, dimensions, observation counts, normalization status, transformation status, and base component. ### Examples ```R # create a DirichletRegData object from the Arctic Lake data head(ArcticLake[, 1:3]) AL <- DR_data(ArcticLake[, 1:3]) summary(AL) head(AL) ``` ``` -------------------------------- ### Drop Variables from DirichletRegModel Source: https://cran.r-project.org/web/packages/DirichletReg/refman/DirichletReg.html Performs a drop-one analysis for a DirichletRegModel object, testing the significance of variables. Use with caution as it may issue a caveat on first use. ```R ## S3 method for class 'DirichletRegModel' drop1(object, scope, test = c("LRT", "none"), k = 2, sort = TRUE, ...) ``` -------------------------------- ### drop1.DirichletRegModel Source: https://cran.r-project.org/web/packages/DirichletReg/refman/DirichletReg.html Performs a sequential analysis of competing models by dropping terms from a Dirichlet regression model. ```APIDOC ## drop1.DirichletRegModel ### Description Performs a sequential analysis of competing models by dropping terms from a Dirichlet regression model. ### Usage ```R drop1(object, scope, test = c("LRT", "none"), k = 2, sort = TRUE, ...) ``` ### Arguments * `object`: An object of class `DirichletRegModel`. * `scope`: Defines the scope of variables to be dropped. * `test`: Defines the type of test for `drop1`. * `k`: Number for the weighting of parameters. * `sort`: If TRUE, p-values will be sorted in decreasing order. * `...`: Further arguments. ``` -------------------------------- ### ddirichlet Function Source: https://cran.r-project.org/web/packages/DirichletReg/refman/DirichletReg.html Computes the density function of the Dirichlet distribution. ```APIDOC ## ddirichlet(x, alpha, log = FALSE, sum.up = FALSE) ### Description Computes the density function of the Dirichlet distribution. ### Arguments - `x` (matrix): A matrix containing observations. - `alpha` (numeric vector or matrix): The Dirichlet distribution's parameters. - `log` (logical): If `TRUE`, logarithmic densities are returned. - `sum.up` (logical): If `TRUE`, the (log-)likelihood is returned. ### Value Returns a vector of densities (if `sum.up = FALSE`) or the (log-)likelihood (if `sum.up = TRUE`) for the given data and alphas. Returns `NaN` if any element of `alpha` is `≤0`. ### Examples ```R X1 <- rdirichlet(100, c(5, 5, 10)) ddirichlet(X1, c(5, 5, 10)) X2 <- rdirichlet(10, cbind(1:10, 5, 10:1)) ddirichlet(X2, cbind(1:10, 5, 10:1)) ddirichlet(X2[1:3,], c(1, 2, -1)) ddirichlet(X2[1:3,], c(1, 2, -1), sum.up = TRUE) ``` ``` -------------------------------- ### Residual Plots for OLS and Dirichlet Regression Source: https://cran.r-project.org/web/packages/DirichletReg/vignettes/DirichletReg-vig.html This code generates a series of residual plots for both OLS (rlr) and Dirichlet regression (rs2) models. It includes Normal Q-Q plots and plots of residuals against fitted values and IQ scores, with points colored by group (dyslexia status). ```R gcol <- c("#E495A5", "#39BEB1")[3-as.numeric(RS$dyslexia)] tmt <- c(-3,3) par(mfrow=c(3,2), cex=.8) qqnorm(residuals(rlr,"pearson"), ylim=tmt, xlim=tmt, pch=21, bg=gcol, main="Normal Q-Q-Plot: OLS Residuals",cex=.75,lwd=.5) abline(0,1, lwd=2) qqline(residuals(rlr,"pearson"), lty=2) qqnorm(residuals(rs2,"standardized")[,2], ylim=tmt, xlim=tmt, pch=21, bg=gcol, main="Normal Q-Q-Plot: DirichReg Residuals",cex=.75,lwd=.5) abline(0,1, lwd=2) qqline(residuals(rs2,"standardized")[,2], lty=2) plot(ReadingSkills$iq, residuals(rlr,"pearson"), pch=21, bg=gcol, ylim=c(-3,3),main="OLS Residuals",xlab="IQ",ylab="Pearson Residuals",cex=.75,lwd=.5) abline(h=0,lty=2) plot(ReadingSkills$iq, residuals(rs2,"standardized")[,2], pch=21, bg=gcol ,ylim=c(-3,3),main="DirichReg Residuals",xlab="IQ",ylab="Standardized Residuals",cex=.75,lwd=.5) abline(h=0,lty=2) plot(fitted(rlr), residuals(rlr,"pearson"), pch=21, bg=gcol ,ylim=c(-3,3),main="OLS Residuals",xlab="Fitted",ylab="Pearson Residuals",cex=.75,lwd=.5) abline(h=0,lty=2) plot(fitted(rs2)[,2], residuals(rs2,"standardized")[,2], pch=21, bg=gcol ,ylim=c(-3,3),main="DirichReg Residuals",xlab="Fitted",ylab="Standardized Residuals",cex=.75,lwd=.5) abline(h=0,lty=2) ``` -------------------------------- ### Predict New Values using DirichletRegModel Source: https://cran.r-project.org/web/packages/DirichletReg/refman/DirichletReg.html Predicts new values (expected values, alpha, or phi) for a DirichletRegModel object using new data. ```R ## S3 method for class 'DirichletRegModel' predict(object, newdata, mu = TRUE, alpha = FALSE, phi = FALSE, ...) ``` -------------------------------- ### print Method for DirichletRegData Source: https://cran.r-project.org/web/packages/DirichletReg/refman/DirichletReg.html Prints a DirichletRegData object, showing either the processed or original data. ```APIDOC ## print.DirichletRegData ### Description Prints a `DirichletRegData` object. ### Usage ```R print(x, type = c("processed", "original"), ...) ``` ### Arguments * `x` (`DirichletRegData` object): The object to print. * `type` (character): Specifies whether to display the `"processed"` or `"original"` data. Defaults to `"processed"`. * `...`: Further arguments. ### Details Displays either the (possibly normalized or transformed) `"processed"` data or the `"original"` data based on the `type` argument. ``` -------------------------------- ### Visualizing Dirichlet Regression and OLS Predictions Source: https://cran.r-project.org/web/packages/DirichletReg/vignettes/DirichletReg-vig.html This code generates a plot comparing predicted reading accuracy from a Dirichlet regression model (rs2) and an OLS regression model, with different colors and line types distinguishing groups and model types. It also plots precision (phi) for the Dirichlet model. ```R g.ind <- as.numeric(RS$dyslexia) g1 <- g.ind == 1 # normal g2 <- g.ind != 1 # dyslexia par(mar=c(4,4,4,4)+0.25) plot(accuracy~iq, RS, pch=21, bg=c("#E495A5", "#39BEB1")[3-g.ind], cex=1.5, main="Dyslexic (Red) vs. Control (Green) Group", xlab="IQ Score",ylab="Reading Accuracy", xlim=range(ReadingSkills$iq)) x1 <- seq(min(RS$iq[g1]), max(RS$iq[g1]), length.out=200) x2 <- seq(min(RS$iq[g2]), max(RS$iq[g2]), length.out=200) n <- length(x1) X <- data.frame(dyslexia=factor(rep(0:1, each=n), levels=0:1, labels=c("no", "yes")),iq=c(x1,x2)) pv <- predict(rs2, X, TRUE, TRUE, TRUE) lines(x1, pv$mu[1:n,2], col=c("#E495A5", "#39BEB1")[2],lwd=3) lines(x2, pv$mu[(n+1):(2*n),2], col=c("#E495A5", "#39BEB1")[1],lwd=3) a <- RS$accuracy logRa_a <- log(a/(1-a)) rlr <- lm(logRa_a~dyslexia*iq, RS) olds <- 1/(1+exp(-predict(rlr, X))) lines(x1, ols[1:n], col=c("#AD6071", "#00897D")[2],lwd=3,lty=2) lines(x2, ols[(n+1):(2*n)], col=c("#AD6071", "#00897D")[1],lwd=3,lty=2) ### precision plot par(new=TRUE) plot(x1, pv$phi[1:n], col=c("#6E1D34", "#004E42")[2], lty="11", type="l",ylim=c(0,max(pv$phi)),axes=F,ann=F,lwd=2, xlim=range(RS$iq)) lines(x2, pv$phi[(n+1):(2*n)], col=c("#6E1D34", "#004E42")[1], lty="11", type="l",lwd=2) axis(4) mtext(expression(paste("Precision (",phi,")",sep="")), 4, line=3) legend("topleft",legend=c(expression(hat(mu)),expression(hat(phi)),"OLS"),lty=c(1,3,2),lwd=c(3,2,3),bty="n") ``` -------------------------------- ### Plot Dirichlet-Distributed Data Source: https://cran.r-project.org/web/packages/DirichletReg/refman/DirichletReg.html With this function you can plot Dirichlet-distributed data in 2, 3 and 4 dimensions. It supports various customization options for visualizing the data distribution. ```APIDOC ## Plot Dirichlet-Distributed Data ### Description With this function you can plot Dirichlet-distributed data in 2, 3 and 4 dimensions. ### Usage ```R ## S3 method for class 'DirichletRegData' plot(x, dims, ticks = TRUE, ref.lines = NULL, dim.labels, a2d = list(colored = TRUE, c.grid = TRUE, col.scheme = c("dims", "entropy"), entropy.contours = FALSE, entropy.colors = FALSE), a3d = list(rgl = TRUE, ...), rug = TRUE, reset_par = TRUE, ...) ``` ### Arguments `x` | data prepared with `DR_data` `dims` | select two, three, or four Dimensions of your data `x` to plot `ticks` | display ticks? `ref.lines` | . `dim.labels` | a character vector giving labels for the dimensions/variables `a2d` | a named list of settings for ternary plots (3 variables), see Details `a3d` | a named list of settings for quaternary plots (4 variables), see Details `rug` | display a rug for a one-dimensional plot (2 variables) `reset_par` | reset graphical parameters of `DR_data` after creating a two-dimensional plot (2 variables), see Details `...` | further graphical arguments as `col`, `pch`, `cex`, ... ### Examples ```R # plot of "Sand" in the Arctic Lake data set plot(DR_data(ReadingSkills[, 1]), main="Reading Accuracy") # ternary plot of Arctic Lake data plot(DR_data(ArcticLake[, 1:3]), a2d = list(colored = FALSE)) ``` ``` -------------------------------- ### nobs.DirichletRegModel Source: https://cran.r-project.org/web/packages/DirichletReg/refman/DirichletReg.html Returns the number of observations in a Dirichlet regression model. ```APIDOC ## nobs.DirichletRegModel ### Description Returns the number of observations in a Dirichlet regression model. ### Usage ```R nobs(object, ...) ``` ### Arguments * `object`: An object of class `DirichletRegModel`. * `...`: Further arguments. ``` -------------------------------- ### update.DirichletRegModel Source: https://cran.r-project.org/web/packages/DirichletReg/refman/DirichletReg.html Updates a Dirichlet regression model. ```APIDOC ## update.DirichletRegModel ### Description Updates a Dirichlet regression model. ### Usage ```R update(object, formula., ..., evaluate = TRUE) ``` ### Arguments * `object`: An object of class `DirichletRegModel`. * `formula.`: The new formula to be updated. * `evaluate`: If FALSE, the updated call will be returned, but not evaluated. * `...`: Further arguments. ``` -------------------------------- ### Generate Dirichlet Distribution Random Numbers Source: https://cran.r-project.org/web/packages/DirichletReg/refman/DirichletReg.html Generates random numbers from a Dirichlet distribution. Supports a single alpha vector or a matrix of alpha parameters for each observation. ```R X1 <- rdirichlet(100, c(5, 5, 10)) a.mat <- cbind(1:10, 5, 10:1) a.mat X2 <- rdirichlet(10, a.mat) # note how the probabilities in the first an last column relate to a.mat round(X2, 2) ``` -------------------------------- ### Extract Log-Likelihood from DirichletRegModel Source: https://cran.r-project.org/web/packages/DirichletReg/refman/DirichletReg.html Extracts the log-likelihood of a DirichletRegModel object. ```R ## S3 method for class 'DirichletRegModel' logLik(object, ...) ``` -------------------------------- ### Plot Dirichlet-Distributed Data Source: https://cran.r-project.org/web/packages/DirichletReg/refman/DirichletReg.html Visualize Dirichlet-distributed data in 2, 3, or 4 dimensions. Customize plots with options for dimensions, ticks, reference lines, dimension labels, and specific settings for 2D (ternary) and 3D (quaternary) plots. ```R # plot of "Sand" in the Arctic Lake data set plot(DR_data(ReadingSkills[, 1]), main="Reading Accuracy") # ternary plot of Arctic Lake data plot(DR_data(ArcticLake[, 1:3]), a2d = list(colored = FALSE)) ```