<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><title>R: Double-Parameter Base Log-likelihood Function for Gaussian...</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<link rel="stylesheet" type="text/css" href="R.css" />
</head><body>

<table width="100%" summary="page for fbase2.gaussian.identity.log"><tr><td>fbase2.gaussian.identity.log</td><td style="text-align: right;">R Documentation</td></tr></table>

<h2>
Double-Parameter Base Log-likelihood Function for Gaussian GLM
</h2>

<h3>Description</h3>

<p>Vectorized, double-parameter base log-likelihood functions for Gaussian GLM. The link functions map the mean and variance to linear predictors. The base function can be supplied to the expander function <code>regfac.expand.2par</code> in order to obtain the full, high-dimensional log-likleihood and its derivatives.
</p>


<h3>Usage</h3>

<pre>
fbase2.gaussian.identity.log(u, v, y, fgh = 2)
</pre>


<h3>Arguments</h3>

<table summary="R argblock">
<tr valign="top"><td><code>u</code></td>
<td>
<p>First parameter of the base log-likelihood function (usually the mean). This parameter is intended to be projected onto a high-dimensional space using the familiar regression transformation of <code>u &lt;- X%*%beta</code>. In the typical use-case where the caller is <code>regfac.expand.1par</code>, a vector of values are supplied, and return objects will have the same length as <code>u</code>.</p>
</td></tr>
<tr valign="top"><td><code>v</code></td>
<td>
<p>Second parameter of the base log-likelihood function (usually the mean). This parameter is intended to be projected onto a high-dimensional space using the familiar regression transformation of <code>v &lt;- Z%*%gamma</code>. In the typical use-case where the caller is <code>regfac.expand.1par</code>, a vector of values are supplied, and return objects will have the same length as <code>u</code>.</p>
</td></tr>
<tr valign="top"><td><code>y</code></td>
<td>
<p>Fixed slot of the base distribution, corresponding to the response variable in the regression model. For <code>binomial</code> family, it must be an integer vector with values between <code>0</code> and <code>n</code>.</p>
</td></tr>
<tr valign="top"><td><code>fgh</code></td>
<td>
<p>Integer with possible values 0,1,2. If <code>fgh=0</code>, the function only calculates and returns the log-likelihood vector and no derivatives. If <code>fgh=1</code>, it returns the log-likelihood and its first derivative in a list. If <code>fgh=2</code>, it returns the log-likelihood, as well as its first and second derivatives in a list.</p>
</td></tr>
</table>


<h3>Value</h3>

<p>If <code>fgh==0</code>, the function returns <code>-0.5*(v + exp(-v)*(u-y)*(u-y))</code> (ignoring additive terms that are independent of <code>u,v</code>). It will therefore be of the same length as <code>u</code>. If <code>fgh==1</code>, a list is returned with elements <code>f</code> and <code>g</code>, where <code>f</code> is the same object as in <code>fgh==1</code> and <code>g</code> is a matrix of dimensions <code>length(u)</code>-by-2, with first column being the derivative of the above expression with respect to <code>u</code>, and the second column being the derivative of the above expression with respect to <code>v</code>. If <code>fgh==2</code>, the list will include an element named <code>h</code>, which is a matrix of dimensions <code>length(u)</code>-by-3, with the first column being the second derivative of <code>f</code> with respect to <code>u</code>, the second column being the second derivative of <code>f</code> with respect to <code>v</code>, and the third column is the cross-derivative term, i.e. the derivative of <code>f</code> with respect to <code>u</code> and <code>v</code>.
</p>


<h3>Author(s)</h3>

<p>Alireza S. Mahani, Mansour T.A. Sharabiani
</p>


<h3>See Also</h3>

<p>regfac.expand.2par
</p>


<h3>Examples</h3>

<pre>
## Not run: 
library(sns)
library(MfUSampler)
library(dglm)

# defining log-likelihood function
# vd==FALSE leads to constant-dispersion model (ordinary linear regression)
# while vd==TRUE produces varying-dispersion model
loglike.linreg &lt;- function(coeff, X, y, fgh, block.diag = F, vd = F) {
  if (vd) regfac.expand.2par(coeff = coeff, X = X, Z = X, y = y
    , fbase2 = fbase2.gaussian.identity.log, fgh = fgh, block.diag = block.diag)
  else regfac.expand.2par(coeff = coeff, X = X, y = y
    , fbase2 = fbase2.gaussian.identity.log, fgh = fgh, block.diag = block.diag)
}

# simulating data according to generative model
N &lt;- 1000 # number of observations
K &lt;- 5 # number of covariates
X &lt;- matrix(runif(N*K, min=-0.5, max=+0.5), ncol=K)
beta &lt;- runif(K, min=-0.5, max=+0.5)
gamma &lt;- runif(K, min=-0.5, max=+0.5)
mean.vec &lt;- X%*%beta
sd.vec &lt;- exp(X%*%gamma)
y &lt;- rnorm(N, mean.vec, sd.vec)

# constant-dispersion model
# estimation using glm
est.glm &lt;- lm(y~X-1)
beta.glm &lt;- est.glm$coefficients
sigma.glm &lt;- summary(est.glm)$sigma
# estimation using RegressionFactory
# (we set rnd=F in sns to allow for better comparison with glm)
nsmp &lt;- 20
coeff.smp &lt;- array(NA, dim=c(nsmp, K+1)) 
coeff.tmp &lt;- rep(0, K+1)
for (n in 1:nsmp) {
  coeff.tmp &lt;- sns(coeff.tmp, fghEval=loglike.linreg
    , X=X, y=y, fgh=2, block.diag = F, vd = F, rnd = F)
  coeff.smp[n,] &lt;- coeff.tmp
}
beta.regfac.cd &lt;- colMeans(coeff.smp[(nsmp/2+1):nsmp, 1:K])
sigma.regfac.cd &lt;- sqrt(exp(mean(coeff.smp[(nsmp/2+1):nsmp, K+1])))
# comparing glm and RegressionFactory results
# beta's must match exactly between glm and RegressionFactory
cbind(beta, beta.glm, beta.regfac.cd)
# sigma's won't match exactly
cbind(mean(sd.vec), sigma.glm, sigma.regfac.cd)

# varying-dispersion model
# estimation using dglm
est.dglm &lt;- dglm(y~X-1, dformula = ~X-1, family = "gaussian", dlink = "log")
beta.dglm &lt;- est.dglm$coefficients
gamma.dglm &lt;- est.dglm$dispersion.fit$coefficients
# estimation using RegressionFactory
coeff.smp &lt;- array(NA, dim=c(nsmp, 2*K)) 
coeff.tmp &lt;- rep(0, 2*K)
for (n in 1:nsmp) {
  coeff.tmp &lt;- sns(coeff.tmp, fghEval=loglike.linreg
    , X=X, y=y, fgh=2, block.diag = F, vd = T, rnd = F)
  coeff.smp[n,] &lt;- coeff.tmp
}
beta.regfac.vd &lt;- colMeans(coeff.smp[(nsmp/2+1):nsmp, 1:K])
gamma.regfac.vd &lt;- colMeans(coeff.smp[(nsmp/2+1):nsmp, K+1:K])
# comparing dglm and RegressionFactory results
# neither beta's nor gamma's will match exactly
cbind(beta, beta.dglm, beta.regfac.vd)
cbind(gamma, gamma.dglm, gamma.regfac.vd)


## End(Not run)
</pre>


</body></html>
