I've been experiencing an issue when trying to estimate a model using the 'nls' function. I am trying to estimate the parameters (i.e. the aj's for j=0,1,...,9) in the following equation:
log(y) = log(a0 + a1*x1 + a2*x2 + a3*x3 + a4*(x1)^2 + a5*(x2)^2 +
a6*(x3)^2 + a7*(x1*x2) + a8*(x1*x3) + a9*(x2*x3)) + error
In order to avoid negative values inside the log function, I set the starting values for the parameters in the non-linear least squares model according to the following code:
ols.model <- lm(y ~ x1 + x2 + x3 + (x1)^2 + (x2)^2 + (x3)^2 + x1*x2 +
x1*x3 + x2*x3)
ols.coefficients <- ols.model$coefficients
fitted <- fitted(ols.model)
fitted <- fitted-ols.coefficients[1]
min.fitted <- min(fitted)
b0.start <- -min.fitted + 0.1
The above ensured that none of the starting fitted values will be negative inside the log function. My call of the nls regression looked like this:
nls.model <- nls(log(y) ~ log(b0 + b1*x1 + b2*x2 + b3*x3 + b4*(x1)^2 +
b5*(x2)^2 + b6*(x3)^2 + b7*(x1*x2) + b8*(x1*x3) +
b9*(x2*x3)),
start=list(b0=b0.start, b1=ols.coefficients[2],
b2=ols.coefficients[3], b3=ols.coefficients[4],
b4=ols.coefficients[5],b5=ols.coefficients[6],
b6=ols.coefficients[7], b7=ols.coefficients[8],
b8=ols.coefficients[9], b9=ols.coefficients[10]),
trace=TRUE)
Despite these carefully selected starting parameter values, I keep on getting an error message that reads:
Error in numericDeriv(form[[3L]], names(ind), env) :
Missing value or an infinity produced when evaluating the model
In addition: Warning message:
In log(b0 + b1 * x1 + b2 * x2 + b3 * x3 + b4 * x4 + :
NaNs produced
Does anyone have any idea how I can resolve this issue and estimate the non-linear model without getting an error message? My dataset does not contain any missing or zero values so that is definitely not the problem.
Related
I am using the following model in R:
glmer(y ~ x + z + (1|id), weights = specification, family = binomial, data = data)
which:
y ~ binomial(y, specification)
Logit(y) = intercept + a*x + b*z
a and b are coefficients for x and z variables.
a|a0+a1 = a0 + a1*I
One of the variables (here x) depends on other variable (here I), so I need a hierarchical model to see the heterogeneity in mean of the x.
I would appreciate it if anyone could help me with this problem?
Sorry if the question does not look professional! This is one of my first experiences.
I'm not perfectly sure I understand the question, but: if Logit(y) = intercept + a*x + b*z and a = a0 + a1*I, then it would seem that
Logit(y) = intercept + (a0+a1*I)*x + b*z
This looks like a straightforward interaction model:
glmer(y ~ 1 + x + x:I + z + (1|id), ...)
to make it more explicit, this could also be written as
glmer(y ~ 1 + x + I(x*I) + z + (1|id), ...)
(although the use of I as a predictor variable and in the I() function is a little bit confusing at first glance ...)
While I was working on a project, I stuck into an error saying
glm.fit: fitted probabilities numerically 0 or 1 occurred
when I tried glm() with full model. Before including quadratic and interaction terms into the model, it worked fine. How can I resolve this?
My data temp has 11 variables and 1 response variable (12 columns) with 201 rows (observations).
The below is my code:
test2 <- glm(Y ~ (Age_Q16 + COPD + HighSch + Widodiv + Married + DISABLED + CEREBROVASCULAR_DISEASE + VALVE_DISEASE + NITRATES + Q9 + Q14Down)^2
+ Age_Q16sq + Q9sq + Q14Downsq,temp,family=binomial("logit"),maxit=100)
summary(test2)
I'm trying to run a fixed effects regression in my panel data (using plm package). The regression on levels worked well, so as the first regressions using log variables (I'm putting log on only the dependent and some independent variables, which are in monetary terms). However, my regressions with logs stopped working.
require (AER)
library (AER)
require(plm)
library("plm")
#Indicates the panel and the time and individual columns
dd <- pdata.frame(painel, index = c ('Estado', 'Ano'))
#Model 1 - Model within with individual fixed effects
mod_1_within <- plm(PIB ~ txinad + op + desoc + Divliq + Esc_15 + RT + DC + DK + Gini + I(DK*Gini) + I(DC*Gini), data = dd, effect = 'individual')
summary (mod_1_within)
#this worked well
#Model 2 - Model 1 with the monetary variables in log (the others are % or indexes):
mod_1_within_log<- plm(log(PIB) ~ txinad + log(RT) + op + desoc + Divliq + Esc_15 + log(DC) + log(DK) + Gini + I(Gini*log(DC)) + I(Gini*log(DK)), data = dd, effect = 'individual')
summary (mod_1_within_log)
#This returns:
> mod_1_within_log<- plm(log(PIB) ~ txinad + log(RT) + op + desoc + Divliq + Esc_15 + log(DC) + log(DK) + Gini + I(Gini*log(DC)) + I(Gini*log(DK)), data = dd, effect = 'individual')
Error in model.matrix.pdata.frame(data, rhs = 1, model = model, effect = effect, :
model matrix or response contains non-finite values (NA/NaN/Inf/-Inf)
> summary (mod_1_within_log)
Error in summary(mod_1_within_log) : object 'mod_1_within_log' not found
This is ocurring even though there are no log variables with negative or zero values. I will take this opportunity to ask another question: if there is a variable with a zero value, is there a way I can make that value null and them take the log of that variable?
Thanks in advance!
I assume the reason why you're getting that error might be that you have Inf or -Inf values logged predictors or logged outcomes.
In order to check whether that is the case see the untransformed variables (before log) and check whether any observation has a value of zero. If it does, that is the problem. Why? R returns Inf from log(0). So when you run the FE model, plm is giving you that error because it can't deal with NAN or Inf values.
I am trying to build a predictive model from survey data. My DVs are questions on NPS and other like data points. My IVs are mainly demographical question. I keep getting a Variable lengths error using the following lines of code:
Model <- lm(Q6 ~ amount_spent + first_time + gender +
workshop_participation + adults + children +
household_adults + Below..25K. + X.25K.to..49K. +
X.50K.to..74K. + X.75K.to..99K. + X.100K.to..124K. +
X18.24. + X25.34. + X35.44. + X45.64.,
data = diy_festival2)
Here is the error:
Error in model.frame.default(formula = Q6 ~ amount_spent + first_time + :
variable lengths differ (found for 'Below..25K.')
What are some possible causes and what are some potential fixes I can try?
Your formula object is referencing (a) variable(s) that is not in diy_festival2. It is in the global environment, the debug suggests it is Below..25K.
x <- data.frame(x1=rnorm(100))
x2 <- rnorm(10)
model.matrix( ~ x1 + x2, data=x)
gives the error you have.
It's a bit of a long question so thanks for bearing with me.
Here's my data
https://www.dropbox.com/s/jo22d68a8vxwg63/data.csv?dl=0
I constructed a mixed effect model
library(lme4)
mod <- lmer(sqrt(y) ~ x1 + I(x1^2) + x2 + I(x2^2) + x3 + I(x3^2) + x4 + I(x4^2) + x5 + I(x5^2) +
x6 + I(x6^2) + x7 + I(x7^2) + x8 + I(x8^2) + (1|loc) + (1|year), data = data)
All the predictors are standardised and I am interested in knowing how does y changes with changes in x5while keeping other variables at their mean values (equal to 0 since all the variables are standardised).
This is how I do it.
# make all predictors except x5 equal to zero
data$x1<-0
data$x2<-0
data$x3<-0
data$x4<-0
data$x6<-0
data$x7<-0
data$x8<-0
# Use the predict function
library(merTools)
fitted <- predictInterval(merMod = mod, newdata = data, level = 0.95, n.sims = 1000,stat = "median",include.resid.var = TRUE)
Now I want to plot the fitted as a quadratic function of x5. I do this:
i<-order(data$x5)
plot(data$x5[i],fitted$fit[i],type="l")
I expected this to produce a plot of y as a quadratic function of x5. But As you can see, I get the following plot which does not have any quadratic curve. Can anyone tell me what I am doing wrong here?
I'm not sure where predictInterval comes from, but you can do this with predict. The trick is just to make sure you set your random effects to 0. Here's how you can do that
newdata <- data
newdata[,paste0("x", setdiff(1:8,5))] <- 0
y <- predict(mod, newdata=newdata, re.form=NA)
plot(data$x5, y)
The re.form=NA part drops out the random effect