Parameters of truncated normal distribution using R - r

How can we numerically solve these equations using R when E_(μ,σ) (X)=1 and 〖var〗_(μ,σ) (X)=1 ? I am interested in finding the values of μ and σ.
Here α=(a-μ)/σ and β=(b-μ)/σ. I used the following code, but I'm not getting an answer. Is there any other code or method I may use to get what I want ?
mubar<-1
sigmabar<-1
a<-0.5
b<-5.5
model <- function(x)c(F1 = mubar-x[1]+x[2]*((pnorm((b-x[1])/x[2])-pnorm(a-x[1])/x[2])/(dnorm((b-x[1])/x[2])-dnorm((a-x[1])/x[2]))),
F2 = sigmabar^2-x[2]^2*(1-(((b-x[1])/x[2]*pnorm((b-x[1])/x[2])-(a-x[1])/x[2]*pnorm((a-x[1])/x[2]))/(dnorm((b-x[1])/x[2])-dnorm((a-x[1])/x[2])))-((pnorm((b-x[1])/x[2])-pnorm((a-x[1])/x[2]))/(dnorm((b-x[1])/x[2])-dnorm((a-x[1])/x[2])))^2) )
(ss <- multiroot(f = model, start = c(1, 1)))

Related

Is it possible to adapt standard prediction interval code for dlm in R with other distribution?

Using the dlm package in R I fit a dynamic linear model to a time series data set, consisting of 20 observations. I then use the dlmForecast function to predict future values (which I can validate against the genuine data for said period).
I use the following code to create a prediction interval;
ciTheory <- (outer(sapply(fut1$Q, FUN=function(x) sqrt(diag(x))), qnorm(c(0.05,0.95))) +
as.vector(t(fut1$f)))
However my data does not follow a normal distribution and I wondered whether it would be possible to
adapt the qnorm function for other distributions. I have tried qt, but am unable to apply qgamma.......
Just wondered if anyone knew how you would go about sorting this.....
Below is a reproduced version of my code...
library(dlm)
data <- c(20.68502, 17.28549, 12.18363, 13.53479, 15.38779, 16.14770, 20.17536, 43.39321, 42.91027, 49.41402, 59.22262, 55.42043)
mod.build <- function(par) {
dlmModPoly(1, dV = exp(par[1]), dW = exp(par[2]))
}
# Returns most likely estimate of relevant values for parameters
mle <- dlmMLE(a2, rep(0,2), mod.build); #nileMLE$conv
if(mle$convergence==0) print("converged") else print("did not converge")
mod1 <- dlmModPoly(dV = v, dW = c(0, w))
mod1Filt <- dlmFilter(a1, mod1)
fut1 <- dlmForecast(mod1Filt, n = 7)
Cheers

Using tsboot to obtain confidence interval from a regression with lags

I would like to do a bootstrap of regression coefficient in a return model that includes two lags.
I have snp_ret vector with returns obtained from quantmod. The data looks like this:
head(snp_ret)
ret
1998-10-13 -0.2920975
1998-10-14 1.0728374
1998-10-15 4.0882022
1998-10-16 0.8489058
1998-10-19 0.5635226
1998-10-20 0.1448549
Obtaining bootstrap for coefficients should be simple:
getC=function(myData){
return(coef(lm(formula = dyn(ret ~ lag(ret, c(-1,-9))), data=myData) ))
}
tsboot(snp_ret, getC, R = 100, l = 18, sim = "fixed")
The following error appears:
Error in merge.zoo(ret, lag(ret, c(-1, -9)), retclass = "list", all
= TRUE) : series cannot be merged with non-unique index entries in a series
I suspect that it has to do with the fact that regression has two lags, but do not know how to proceed.
If possible, please help.
All right, I found a workaround, so maybe this will be interesting to somebody else... Using arima function instead of lag operators helped.
getC <- function(myData) {
reg <- suppressWarnings(arima(myData, order = c(9, 0, 0), fixed = c(NA, 0,0,0,0,0,0,0,NA,NA)))
return((coef(reg)[c(1,9,10)]))
Note that arima has a weird way of selecting lags - you have to force to zero coefficients on lags that you don't want to include

Using R INLA hyperparameter to.theta and from.theta functions

R-INLA model hyperparameters have to.theta and from.theta functions that appear to be for converting between different parameterisations. It would be convenient to use those conversion functions but how does one do so?
Example with ar1
From the ar1 documentation (http://www.math.ntnu.no/inla/r-inla.org/doc/latent/ar1.pdf):
The parameter rho is represented as theta_2 = log((1 + rho)/(1 - rho))
and further down under hyper, theta2 we have to.theta 'function(x) log((1+x)/(1-x))'. It would be nice if we could use that to convert between rho and theta_2.
Let's try using an example
library(INLA)
# Example from ar1 documentation (http://www.math.ntnu.no/inla/r-inla.org/doc/latent/ar1.pdf)
#simulate data
n = 100
rho = 0.8
prec = 10
## note that the marginal precision would be
marg.prec = prec * (1-rho^2)
E=sample(c(5,4,10,12),size=n,replace=T)
eta = as.vector(arima.sim(list(order = c(1,0,0), ar = rho), n = n,sd=sqrt(1/prec)))
y=rpois(n,E*exp(eta))
data = list(y=y, z=1:n, E=E)
## fit the model
formula = y~f(z,model="ar1")
result = inla(formula,family="poisson", data = data, E=E)
That runs fine.
Can we use to.theta like this?
formula.to.theta = y~f(z,model="ar1",
hyper = list(rho = list(initial = to.theta(0.25))))
result = inla(formula.to.theta,family="poisson", data = data, E=E)
# Error in to.theta(0.25) : could not find function "to.theta"
So we can't use it like that. Is there another way to specify formula.to.theta that would work?
Pretty sure the answer to your question is "no". The Documentation is saying, not that there are functions by those names in the package, but rather that the hyper hyperparameter element will have functions by those names with values as given in the documentation. There is no reason to think that pasting those names after formula. would result in a meaningful function. Here is how to examine the value of from.theta in the environment of a specific call to the f-function:
library(INLA)
eval( f(z, model = "ar1") )$hyper$theta3$from.theta
===== result ========
function (x)
x
<environment: 0x7fdda6214040>
attr(,"inla.read.only")
[1] TRUE
The result from f( , "ar1") actually has three theta's each with a to and from function. You may be trying to change the hyper$thetax$param value which does not have an attr(,"inla.read.only") value of TRUE.
It would probably be more informative for you to execute this:
eval( f(z, model = "ar1") )$hyper

How to weight observations in mxnet?

I am new to neural networks and the mxnet package in R. I want to do a logistic regression on my predictors since my observations are probabilities varying between 0 and 1. I'd like to weight my observations by a vector obsWeights I have, but I'm not sure where to implement the weights. There seems to be a weight= option in mx.symbol.FullyConnected but if I try weight=obsWeights I get the following error message
Error in mx.varg.symbol.FullyConnected(list(...)) :
Cannot find argument 'weight', Possible Arguments:
----------------
num_hidden : int, required
Number of hidden nodes of the output.
no_bias : boolean, optional, default=False
Whether to disable bias parameter.
How should I proceed to weight my observations? Here is my code at the moment.
# Prepare data
train.mm = model.matrix(obs ~ . , data = train_data)
train_label = train_data$obs
# Normalize
train.mm = apply(train.mm, 2, function(x) (x-min(x))/(max(x)-min(x)))
# Create MXDataIter compatible iterator
batch_size = 128
train.iter = mx.io.arrayiter(data=t(train.mm), label=train_label,
batch.size=batch_size, shuffle=T)
# Symbolic model definition
data = mx.symbol.Variable('data')
fc1 = mx.symbol.FullyConnected(data=data, num.hidden=128, name='fc1')
act1 = mx.symbol.Activation(data=fc1, act.type='relu', name='act1')
final = mx.symbol.FullyConnected(data=act1, num.hidden=1, name='final')
logistic = mx.symbol.LogisticRegressionOutput(data=final, name='logistic')
# Run model
mxnet_train = mx.model.FeedForward.create(
symbol = logistic,
X = train.iter,
initializer = mx.init.Xavier(rnd_type = 'gaussian', factor_type = 'avg', magnitude = 2),
num.round = 25)
Assigning the fully connected weight argument is not what you want to do at any rate. That weight is a reference to parameters of the layer; i.e., what you multiply in the inputs by to get output values These are the parameter values you're trying to learn.
If you want to make some samples matter more than others, then you'll need to adjust the loss function. For example, multiply the usual loss function by your weights so that they do not contribute as much to the overall average loss.
I do not believe the standard Mxnet loss functions have a spot for assigning weights (that is LogisticRegressionOutput won't cover this). However, you can make your own cost function that does. This would involve passing your final layer through a sigmoid activation function to first generate the usual logistic regression output value. Then pass that into the loss function you define. You could do squared error, but for logistic regression you'll probably want to use the cross entropy function:
l * log(y) + (1 - l) * log(1 - y),
where l is the label and y is the predicted value.
Ideally, you'd write a symbol with an efficient definition of the gradient (Mxnet has a cross entropy function, but its for softmax input, not a binary output. You could translate your output to two outputs with softmax as an alternative, but that seems less easy to work with in this case), but the easiest path would be to let Mxnet do its autodiff on it. Then you multiply that cross entropy loss by the weights.
I haven't tested this code, but you'd ultimately have something like this (this is what you'd do in python, should be similar in R):
label = mx.sym.Variable('label')
out = mx.sym.Activation(data=final, act_type='sigmoid')
ce = label * mx.sym.log(out) + (1 - label) * mx.sym.log(1 - out)
weights = mx.sym.Variable('weights')
loss = mx.sym.MakeLoss(weigths * ce, normalization='batch')
Then you want to input your weight vector into the weights Variable along with your normal input data and labels.
As an added tip, the output of an mxnet network with a custom loss via MakeLoss outputs the loss, not the prediction. You'll probably want both in practice, in which case its useful to group the loss with a gradient-blocked version of the prediction so that you can get both. You'd do that like this:
pred_loss = mx.sym.Group([mx.sym.BlockGrad(out), loss])

Get ARIMA white noise with known parameter in R

I have a MA(1) model with known parameter and known .
I'd like to know, is there a function in R that can return for me?
I also tried to get by iteration:
However, in reality, is unknown and cannot be specified at the first place.
I'm having this question because I used gnls to estimate a nonlinear model with residuals being MA(1) process. The code is something like:
model = gnls(y ~ c + log( x1^g + x2^g), start = list(c = 0.04, g = 0.3),
correlation = corARMA(c(0.5), form = ~ 1, p = 0, q = 1, fixed = FALSE))
It returns every parameter estimation including . But residuals(model) returns instead of .
So any suggestions?
Thank you for the help in advance.
Yes. You can use Arima function available in R.
fit <- arima(ts(data), order=c(0,0,1))
as you do not want AR and I part. You can set it to zero.
summary(fit)
You can observe parameters learned and errors by summary function.
For more information, refer to : https://www.otexts.org/fpp/8/7

Resources