R nls2 "invalid model formula" fitting gamma - r

Working in R 3.1.3 and Rstudio.
I want to fit gamma distributions that include a location parameter to data in order to 'shift' the x values to a new origin.
I am trying to use nls2 with the following code:
library(nls2)
theVals <- data.frame(c(26.76,24.3,34.63,38.05,25.56,21.98,20.62,34,26.75,27.79,28.4,33.31,29.26,18.65,22.77,25.72,25.86,25.32,24.08,27.68,26.2,26.16,25.34,26.91,22.6,23.94,23.3,22.34,41.25,24.83,21.66,30.47,26.53,27.74,29.41,25.65,36.05,18.29,27.2,22.99,25.8,21.9,25.27,30.29,22.72,26.49,18.75,33.57,20.87,21.82,20.73,28.59,19.64,33.21,28.94,27.98,22.2,25.95,30.64,26.56,32.11,26.05,20.66,28.64,22.4,22.4,31.91,21.82,26.82,20.77,24.12,28.83,23.07,26.5,21.14,27.29,19.61,25.28,28.6,27.16,22.46,18.19,22.35,23.79,26.32,26.5,27.39,23.29,25.79,26.35,26.38,24.98,20,37.15,25.61,21.39,21.63,24.12,24.4,27.72,42.74,25.33,17.79,21.33,38.65,25.22,28.39,21.61,23.38,25.25,24.88,23.34,26.26,21.96,22.18,24.78,21.15,24.65,21.23,31.9,28.66,27.66,18.08,22.99,22.46,21.69,28.21,29.8,25.72,27.09,20.02,21.26,21.34,27.18,25.48,20.51,20.96,20.07,20.89,27.56,24.43,21.35,24.3,28.1,26.53,29.03,30.08,19.19,21.27,26.18,23.79,36.52,24.81,26.36,24.44,20.99,19.84,23.32,18.21,26.6,21.48,23.21,29.93,23.4,30.9,23.58,21.58,18.38,25.13,23.03,22.73,24.42,22.89,43.44,23.47,27.09,29.96,23.94,28.51,25.74,28.54,30.41,22.7,29.19,25.66,23.89,21.9,36.26,22.61,19.68,27.85,28.83,28.6,22.68,19.07,20.22,24.35,19.09,37.66,22.55,24.25,22.61,26.09,24.42,26.11,32.15,25.78,21.94,23.93,30.19,23.53,26.49,30.48,25.02,28.14,23.43,20.22,17.57,21.68,36.07,24.92,32.48,32.04,25.86,26.69,22.41,26.4,22.72,28.32,22.82,32.73,28.08,29.16,36.18,21.61,23.9,28.8,23.24,24.89,22.17,27.7,34.75,26.74,29.62,17.46,20.06,22.23,22.09,24.05,22.37,24.98,33.26,30.95,26.24,22.16,30.97,27.22,23.81,42.16,28.2,28.37,26.1,26.28,27.44,20.52,35.02,21.43,23.14,18.37,28.86,25.18,28.15,19.97,24.2,25.91,28.92,23.95,19.48,28.57,21.77,23.46,37.51,22.13,37.18,21.83,23.8,18.93,27.43,26.51,25.64,22.15,22.27,29.21,24.45,18.81,22.62,25.16,24.62,30.53,28.77,27.11,22.07,28.95,26.54,39.23,31.9,33,29.93,24.37,26.4,21.33,25.37,25.9,21.25,19.06,25.69,26.44,26.09,23.24,27.04,20.09,28.73,37.06,32.45,22.93,22.7,24.82,31.23,23.25,22.94,20.47,25.7,23.92,34.71,26.5,20.28,21.78,26.54,30.34,21.97,27.38,27.64,34.08,22.05,27.21,20.11,25.79,33.22,31.24,29.93,21.81,30.68,32.46,30.45,22.62,28.83,33.95,27.12,45.51,25.23,29.61,29.09))
colnames(theVals) <- c("theGamma")
fo <- theGamma ~ dgamma(theX-location, shape=theShape, scale=theScale )
startList <- list(location=5, theShape=3, theScale=3)
theGamma=NULL
theX <- 0:50
mo1 <- nls2(fo, start=startList, data=theVals)
I get an error "invalid model formula in ExtractVars".
Curiosly dgamma works fine:
location<- 5
theShape <- 3
theScale <- 3
dgamma(theX-location, shape=theShape, scale=theScale )
I have search stackoverflow and other sites, but can't find an answer to this one.
Any ideas?

Related

rjags model negative binomial likelihood and gamma prior

I read in my data. I make the model string. I hand it JAGS. I get "Error in node y[1] - Node inconsistent with parents".
Y=read.table("data.txt",header=T)
Y=Y$Y
model_string <- "model{
# Likelihood:
for( i in 1 : N ) {
y[i] ~ dnegbin( l , r )
}
# Prior:
r ~ dgamma(1,1)
l ~ dgamma(.1,.1)
}"
model <- jags.model(textConnection(model_string),
data = list(y=Y,N=200))
First off all, I have no clue if my model is right. I cannot find even basic documentation for JAGS. I'm actually ashamed to admit it, because this should be as simple as an internet search, but I cannot find any document to tell me 1) how a JAGS model is set up or 2) what kinds of functions/distribution/parameters are available in JAGS. I only got this far because I found someone doing a similar model. If anyone knows of a JAGS wiki or documentation, that would be great.
Edit: If someone could even just tell me what the parameters for dnegbin are that would be a huge help. When I plug in random numbers for l and r in dnegbin(l,r) it 'works' as in it draws numbers for l and r, but I have no clue if it means anything.
You can find some info about dnegbin in the JAGS user manual.
The first parameter of dnegbin must be between and 0 and 1. You can assign e.g. a uniform distribution:
library(rjags)
model_string <- "model{
# Likelihood:
for( i in 1 : N ) {
y[i] ~ dnegbin( l , r )
}
# Prior:
r ~ dgamma(1,1)
l ~ dunif(0,1)
}"
y <- rpois(200, 10)
model <- jags.model(textConnection(model_string),
data = list(y=y, N=length(y)))
You also have to be sure that the values of y are non-negative integers.

R: Matrix multiplication error - related to GLM

I've been trying to build some custom code for Logistic regression (i.e. I cannot use the GLM package for this purpose since - happy to explain why.)
Below is the initial R code to provide the data set I'm working with:
## Load the datasets
data("titanic_train")
data("titanic_test")
## Combining Training and Testing dataset
complete_data <- rbind(titanic_train, titanic_test)
library(dplyr)
titanic_test$Survived <- 2
complete_data <- rbind(titanic_train, titanic_test)
complete_data$Embarked[complete_data$Embarked==""] <- "S"
complete_data$Age[is.na(complete_data$Age)] <-
median(complete_data$Age,na.rm=T)
complete_data <- as.data.frame(complete_data)
titanic_data <- select(complete_data,-c(Cabin, PassengerId, Ticket,
Name))
titanic_data <- titanic_data[!titanic_data$Survived == "2", ]
titanic_model <- model <- glm(Survived
~.,family=binomial(link='logit'),data=titanic_data)
y <- titanic_data$Survived
x <- as.data.frame(cbind(rep(1, dim(titanic_data)
[1]),titanic_data[,-2]))
x <- as.matrix(as.numeric(x))
beta <- as.numeric(rep(0, dim(x)[2]))
beta <- as.matrix(beta)
The issue I'm having here is I would like to compute the matrix product of beta (a px1 matrix) and x (a n x p matrix)
I have tried the following -
beta * x
x %*% beta
However, the above the following errors -
Error in FUN(left, right) : non-numeric argument to binary operator
Error in x %*% beta : requires numeric/complex matrix/vector arguments
I'd imagine this is due to the fact I've got non-numeric fields in the data matrix x.
As a bit of a background, calculating the linear predictor will allow me to progress with my custom code for fitting a Logistic regression model.
I would appreciate some help - thank you!

R : Clustered standard errors in fractional probit model

I need to estimate a fractional (response taking values between 0 and 1) model with R. I also want to cluster the standard errors. I have found several examples in SO and elsewhere and I built this function based on my findings:
require(sandwich)
require(lmtest)
clrobustse <- function(fit, cl){
M <- length(unique(cl))
N <- length(cl)
K <- fit$rank
dfc <- (M/(M - 1))*((N - 1)/(N - K))
uj <- apply(estfun(fit), 2, function(x) tapply(x, cl, sum))
vcovCL <- dfc*sandwich(fit, meat = crossprod(uj)/N)
coeftest(fit, vcovCL)
}
I estimate my model like this:
fit <- glm(dep ~ exp1 + exp2 + exp3, data = df, fam = quasibinomial("probit"))
clrobustse(fit, df$cluster)
Everything works fine and I get the results. However, I suspect that something is not right as the non-clustered version:
coeftest(fit)
gives the exact same standard errors. I checked that Stata reports and that displays different clustered errors. I suspect that I have misspecified the function clrobustse but I just don't know how. Any ideas about what could be going wrong here?

exponential fit singularity in r

I'm trying to fit a bi-exponential function on this dataset but I can't seem to get it to converge. I've tried using nls2 grid search for the best starting point, I've also tried using nlsLM but neither method works. Does anyone have any suggestions?
function: y = a1*exp(-n1*t) + a2*exp(-n2*t) + c
here is the code:
y <- c(1324,1115,1140,934,1013,982,1048,1143,754,906,895,900,765,808,680,731,728,794,706,531,629,629,519,514,516,454,465,630,415,347,257,363,275,379,329,263,301,315,283,354,230,257,196,268,262,236,220,239,255,213,275,273,294,169,257,178,207,169,169,297,
227,189,214,168,263,227,185,220,169,229,174,231,178,141,195,223,258,206,181,200,150,200,169,194,230,162,174,194,225,216,196,213,150,235,231,224,244,161,219,222,210,
186,188,197,177,251,248,223,273,145,257,236,214,194,211,213,175,168,223,192,318,
263,234,163,202,239,189,216,206,185,185,191,340,145,188,305,112,252,213,245,240,196,196,179,235,241,177,196,191,181,240,164,202,201,306,214,212,185,192,178,203,203,239,141,203,190,216,174,219,153,177,223,207,186,213,173,210,191,258,277)
t <- seq(1,length(y),1)
mydata <- data.frame(t=t,y=y)
library(nls2)
fo <- y~a1*exp(-n1*t)+a2*exp(-n2*t)+c
grd <- expand.grid(a1=seq(-12030,1100,by=3000),
a2=seq(-22110,1900,by=2000),
n1=seq(0.01,.95,by=0.4),
n2=seq(0.02,.9,by=0.25),
c=seq(100,400,by=50))
fit <- nls2(fo, data=allout, start=grd, algorithm='brute-force', control=list(maxiter=100))
fit2 <- nls(fo, data=allout, start=as.list(coef(fit)), control=list(minFactor=1e-12, maxiter=200),trace=F)
error: maximum iteration exceeded
However, if I use nlsLM then I get singularity gradient matrix at initial parameter estimate.

R gmm package use for prediction

I wish to try the R gmm algorithm to predict.
Question #1: is it possible to use gmm to predict? (the word "predict" does not appear in the manual)
Question #2: if it is possible to do prediction with gmm, how does one do it?
I am looking for the simplest example; for example using svm would be done with:
model <- svm(train, trainLabels)
testpred <- predict(model, test)
Question #3:
I cannot even reproduce the examples mentioned in the manual.
Page 24 shows the code:
## CAPM test with GMM
data(Finance)
r <- Finance[1:300, 1:10]
rm <- Finance[1:300, "rm"]
rf <- Finance[1:300, "rf"]
z <- as.matrix(r-rf)
t <- nrow(z)
zm <- rm-rf
h <- matrix(zm, t, 1)
res <- gmm(z ~ zm, x = h)
summary(res)
but even after installing the package gmm, R does not know the data Finance.
> data(Finance)
Warning message:
In data(Finance) : data set ‘Finance’ not found
What am I missing?
you need the following commands at the top
library(gmm)
data(Finance)

Resources