Error in using by in R - r

I'm new to R and started using it to implement a gam model. I've been following this tutorial examples and R keeps throwing an error when I try to use by. I really have no idea what's wrong and would appreciate it if anybody can suggest something.
Thanks a lot in advance.
>require(mgcv)
>set.seed(10)
## simulate date from y = f(x2)*x1 + error
>dat <- gamSim(3,n=400)
Continuous `by' variable example
>b<-gam(y ~ s(x2,by=x1),data=dat)
Error in s(x2, by = x1) : unused argument (by = x1)
Additional information: R version = 3.3.1 (2013-06-21)
OS = Windows 10

Code
require(mgcv)
set.seed(10)
## simulate date from y = f(x2)*x1 + error
dat <- gamSim(3,n=400)
b <- gam(y ~ s(x2,by=x1),data=dat)
b
Result
Continuous `by' variable example
Family: gaussian Link function: identity
Formula: y ~ s(x2, by = x1)
Estimated degrees of freedom:
9.2 total = 10.2
GCV score: 4.518245
Please start a fresh R session and see if you get the same error. Also, please check your version of mgcv and make sure it's up to date.

Related

Syntax error when trying to parse Bayesian model using RJAGS

I'm running the following code to attempt Bayesian modelling using rjags but coming up with the syntax error below.
Error in jags.model(file = "RhoModeldef.txt", data = ModelData, inits
= ModelInits, : Error parsing model file: syntax error on line 4 near "~"
RhoModel.def <- function() {
for (s in 1:S) {
log(rhohat[s]) ~ dnorm(log(rho[s]),log(rhovar[s]))
rho[s] ~ dgamma(Kappa,Beta)
}
Kappa ~ dt(0,2.5,1) # dt(0, pow(2.5,-2), 1) https://stackoverflow.com/questions/34935606/cauchy-prior-in-jags https://arxiv.org/pdf/0901.4011.pdf
sig.k <- abs(Kappa)
Beta ~ dt(0,2.5,1)
sig.b <- abs(Beta)
}
S <- length(africasad21)-1 # integer
Rhohat <- afzip30$Rho # vector
Rhovar <- afzip30$RhoVar # vector
ModelData <-list(S=S,rhohat=Rhohat,rhovar=Rhovar)
ModelInits <- list(list(rho = rep(1,S),Kappa=0.1,Beta=0.1))
Model.1 <- jags.model(file = 'RhoModeldef.txt',data = ModelData,inits=ModelInits,
n.chains = 4, n.adapt = 100)
Does anyone have any ideas how I might be able to fix this? I'm thinking it might have something to do with my attempts to fit a logged model? Please let me know if more details are needed.
Thanks!
Line 4 of the file 'RhoModeldef.txt' is most likely this one:
log(rhohat[s]) ~ dnorm(log(rho[s]),log(rhovar[s]))
JAGS does not allow log transformations on the left hand side of stochastic relations, only deterministic ones. Given that you are providing rhohat as data, the easiest solution is to do the log transformation in R and drop that part in JAGS i.e.:
log_rhohat[s] ~ dnorm(log(rho[s]), log(rhovar[s]))
ModelData <-list(S=S, log_rhohat=log(Rhohat), rhovar=Rhovar)
Alternatively, you could use dlnorm rather than dnorm in JAGS.
Does that solve your problem? Your example is not self-contained so I can't check myself, but I guess it should work now.

How to solve glmmTMB object not found error?

It seems a bit silly to post this as a question, but I haven't been able to find an answer for this.
I want to use the glmmTMB package for modelling data with zero-inflation, but when I try to fit the model I want, the function glmmTMB() returns the following error:
Error in glmmTMB(y ~ x + (1 | z), data = df, ziformula = x) : object 'x' not found
This only happens after I introduce the term for ziformula. The model without that term works just fine. For some reason, it seems that the ziformula overrides the data command in the function.
My glmmTMB package version is 1.1.3 and my R version is 4.1.1.
Here's a little sample data to reproduce the problem:
df <- data.frame(y=c(2,2,5,7,3,8,3,2,4,6),
x = c(23,21.7,22.5,18,20,19.9,21,20.3,20,20.5),
z = c(rep("a",5),rep("b",5)))
glmmTMB(y ~ x + (1|z), data = df, ziformula = x)
If anyone has had any similar issue, I would be grateful for any tips on how to solve it!

Confusion matrix in R

Im having a little trouble with a confusion matrix. Has anyone had this issue before? I checked the observations and they are the same
library(e1071) # Please install the package first before running library syntax
Svm_Model = svm(t.total_hbat ~ t.inbound_calls_tied +
t.hbat_on_tickets_closed_by_agent +
t.inb_customer_hold_time,
Test_Data,
kernel="linear",
cost=.1,
Scale=F)
Svm_Prediction = predict(Svm_Model,Test_Data,type="class")
View(as.data.frame(Svm_Prediction))
data_final<-cbind(Test_Data,Svm_Prediction)
confusionMatrix(table(Test_Data$t.total_hbat,Svm_Model))
Console:
confusionMatrix(table(Training_Data$t.total_hbat,Svm_Model))
Error in table(Training_Data$t.total_hbat, Svm_Model) :
all arguments must have the same length

Using broom::tidy on felm result with clustered standard errors

I'm trying to extract point estimates and confidence intervals from a panel data model. The following reproduces the error using the canned example from the lfe documentation. The only small change I've made is to cluster standard errors at the firm-level to replicate my issue in est2.
## create covariates
x <- rnorm(1000)
x2 <- rnorm(length(x))
## individual and firm
id <- factor(sample(20,length(x),replace=TRUE))
firm <- factor(sample(13,length(x),replace=TRUE))
## effects for them
id.eff <- rnorm(nlevels(id))
firm.eff <- rnorm(nlevels(firm))
## left hand side
u <- rnorm(length(x))
y <- x + 0.5*x2 + id.eff[id] + firm.eff[firm] + u
## estimate and print result
est1 <- felm(y ~ x+x2| id + firm)
summary(est1)
## estimate and print result with clustered std errors
est2 <- felm(y ~ x+x2| id + firm | 0 | firm)
summary(est2)
I can tidy in the non-clustered SE version or without including the fixed effects:
tidy(est1)
tidy(est2)
tidy(est1, fe = TRUE)
But I can't if I ask for the fixed effects:
tidy(est2, fe = TRUE)
The error is this: Error in overscope_eval_next(overscope, expr) : object 'se' not found
I'm not sure if this is a broom side problem or an lfe side problem. It is possible I'm doing something wrong, but there should be point estimates and standard errors for the fixed effects whether or not I cluster the SEs. (And the fact that there are fewer clusters than FEs is probably an econometric issue, but it doesn't seem to be driving this particular problem.) Any suggestions?
The problem here is that lfe::getfe() is supposed to return the columns c('effect','se','obs','comp','fe','idx') according to its help page. However, if you run
lfe::getfe(est1, se = TRUE) and
lfe::getfe(est2, se = TRUE)
in the second instance, the standard errors are in a column named clusterse instead of se.
The error message is a result of the function broom:::tidy.felm using lfe::getfe() and then dplyr::select(se).
I guess technically it's an lfe problem but I'm not sure which package will be easier to amend
Update: I emailed Simen Gaure (the package author) and he'll be releasing to CRAN some time this spring

Resolving symmetry in gnls model

I'm trying to fit a logistic growth model in R, using gnls in the nlme package.
I have previously successfully fit a model:
mod1 <- gnls(Weight ~ I(A/(1+exp(b + v0*Age + v1*Sum.T))),
data = df,
start = c(A= 13.157132, b= 3, v0= 0.16, v1= -0.0059),
na.action=na.omit)
However, I now wish to constrain b so that it is not fitted by the model, so have tried fitting a second model:
mod2 <- gnls(Weight ~ I(A/(1+exp(log((A/1.022)-1) + v0*Age + v1*Sum.T))),
data = df,
start = c(A= 13.157132, v0= 0.16, v1= -0.0059),
na.action=na.omit)
This model returned the error:
Error in gnls(Weight ~ A/(1 + exp(log((A/1.022) - 1) + v0 * Age + :
approximate covariance matrix for parameter estimates not of full rank
Warning messages:
1: In log((A/1.022) - 1) : NaNs produced
Searching the error suggests that the problem is caused by symmetry in the model, and solutions to specific questions involve adapting the formula with different parameters. Unfortunately, my statistical knowledge is not good enough to a) fully understand the problem or b) adapt the formula myself.
As for the warning messages (there were 15 in all, all the same) I can't see why they arise because this section of the model works alone (with example numbers).
Any help with any of these queries would be greatly appreciated.
It may be informative for users to know that I finally solved this with what turned out to be a simple solution (with help from a friend).
Since exp(a+b) = exp(a)*exp(b), the equation can be rewritten:
Weight ~ I(A/(1+((A/1.022)-1) * exp(v0*Age + v1*Sum.T))
Which fits without any problems. In general, writing the equation in a different form would seem to be the answer.

Resources