R segmented package "variable lengths differ" - r

I'm having trouble getting started with the segmented package in R.
When running the basic example below I get the error:
Error in model.frame.default(formula = y ~ x + U1.x + psi1.x, data = mfExt, :
variable lengths differ (found for 'x')
I was expecting segmented to return a piecewise linear model with 2 segments. I'm clearly making a mistake in my call, but am unable to work out from the error message and the documentation what my mistake is. Help would be appreciated.
require(segmented)
test.df = data.frame(x = c(1:100),
y = c(c(1:50),seq(from = 52, by = 2, length = 50)))
test.mod = lm(y ~ x,
test.df)
segmented(test.mod,
seg.Z = ~ x,
psi = list(x = 40))

It turns out that I had an object in my workspace called 'x'. After removing this object the call to segmented gave expected results.
I can replicate the error any time I have an object of length 1 called x regardless of whether that object is a list or a vector.
If the object has length greater than 1, the error disappears and segmented behaves as expected.
Weird. Thanks #Pascal for your input.

Related

I am facing this problem in R, AOV function

This is my code:
> av = aov(r ~ tf);av
r = matrix with numerical data
tf= factor data
This is the error:
Error in model.frame.default(formula = r ~ tf, drop.unused.levels = TRUE) :
variable lengths differ (found for 'tf')
What is possibly wrong? I am very new to this, I have checked my previous steps and everything seems right. Please let me know if you need any additional information
Number of rows of the matrix should be the same as the length of the vector 'tf'. If that is not the case, it would show the length difference error. Below code works as the number of rows of 'r' is 10 and length of 'tf' is 10
r <- matrix(rnorm(5 * 10), 10, 5)
tf <- factor(sample(letters[1:3], 10, replace = TRUE))
aov(r ~ tf)

Unable to plot p values when using facet.by from ggsurvplot. Error message: "variable lengths differ"

I have a problem that I don't know how to solve. And it seems to be related to my data set (or is it?). Indeed, I am actually able to plot different p values when using facet.by when I use your example from issue#205 via the "colon" data set. However,it does not work with my data set that is available on my Github profile here (https://github.com/CroixJeremy2/Data-frame-for-stack-overflow.git).
Expected behavior
I would like to be able to plot different p values as in issue#205 with my data set.
Actual behavior
I am only able to plot the curves via facet.by. But I can't plot the p values that should be automatically calculated for a log-rank test. Instead, an error message is returned in my R console saying:
"Error in model.frame.default(formula = Survival ~ Sex, data = list(ID = c(147L, :"
"les longueurs des variables diffèrent (trouvé pour 'Sex')"
# the last line translated from French to English means:
"variable lengths differ (found for Sex)"
Steps to reproduce the problem
library(survival)
Survival = Surv(time = D$Age, event = D$outcome)
library(survminer)
fit = survfit(data = D, formula = Survival ~ Sex + Genotype)
ggsurvplot(fit = fit, data = D, pval = TRUE, facet.by = 'Genotype') #error message
ggsurvplot(fit = fit, data = D, facet.by = 'Genotype') #curves can be plotted
Remarks
Note that the survdiff() function works perfectly on my data sets in order to calculate p values from log-rank tests. Therefore, I do not know if I am doing something wrong in ggsurvplot() (most likely hypothesis) or if there is something wrong in the ggsurvplot() function itself (unlikely).
survdiff(data = D, subset = D$Ctrl, formula = Survival ~ Sex)
survdiff(data = D, subset = D$nKO, formula = Survival ~ Sex)
survdiff(data = D, subset = D$CRE_Ctrl, formula = Survival ~ Sex)
#works fine, p value returned, no message/warning/error returned.
Moreover, the variable lengths seems equal... And I don't have any "NA" values in my dataframe... So I really don't understand why I have this error message...
sapply(D,function(x) length(x))
# length = 298 for all my variables...
Thanks in advance for your response and help,

LME error in model.frame.default ... variable lengths differ

I am trying to run a random effects model with LME. It is part of a larger function and I want it to be flexible so that I can pass the fixed (and ideally random) effects variable names to the lme function as variables. get() worked great for this where I started with lm, but it only seems to throw the ambiguous "Error in model.frame.default(formula = ~var1 + var2 + ID, data = list( : variable lengths differ (found for 'ID')." I'm stumped, the data are the same lengths, there are no NAs in this data or the real data, ...
set.seed(12345) #because I got scolded for not doing this previously
var1="x"
var2="y"
exdat<-data.frame(ID=c(rep("a",10),rep("b",10),rep("c",10)),
x = rnorm(30,100,1),
y = rnorm(30,100,2))
#exdat<-as.data.table(exdat) #because the data are actually in a dt, but that doesn't seem to be the issue
Works great
lm(log(get(var1))~log(get(var2)),data=exdat)
lme(log(y)~log(x),random=(~1|ID), data=exdat)
Does not work
lme(log(get(var1,pos=exdat))~log(get(var2)),random=(~1|ID), data=exdat)
Does not work, but throws a new error code: "Error in model.frame.default(formula = ~var1 + var2 + rfac + exdat, data = list( : invalid type (list) for variable 'exdat'"
rfac="ID"
lme(log(get(var1))~log(get(var2)),random=~1|get(rfac,pos=exdat), data=exdat)
Part of the problem seems to be with the nlme package. If you can consider using lme4, the desired results can be obtained by with:
lme4::lmer(log(get(var1)) ~ log(get(var2)) + (1 | ID),
data = exdat)

MIDAS regression (midasr package)

I am trying to estimate a MIDAS regression on a subsample of my data using the window function. However, when I use this, the midas_r() function throws me back the error:
Error in prepmidas_r(y, X, mt, Zenv, cl, args, start, Ofunction, weight_gradients, :
Starting values for weight parameters must be supplied
Here is my code:
install.packages("midasr")
library(midasr)
yrs <- 10
x <- ts(rnorm(12*yrs),start=c(1900,1),frequency = 12)
y <- ts(rnorm(yrs),start=c(1900,1))
midas_r(y~fmls(x,3,12,nealmon),start=list(x=rep(0,3)))
x_est <- window(x,end=c(1910,0))
y_est <- window(y,end=(1910))
midas_r(y_est~fmls(x_est,3,12,nealmon)+1,start=list(x=rep(0,3)))
Does anyone know what's the issue? Thanks in advance!
The issue is in list(x=rep(0, 3)). This list has indeed to be named, but this name needs to coincide with the variable name. Hence,
midas_r(y_est ~ fmls(x_est, 3, 12, nealmon), start = list(x_est = rep(0, 3)))
works.

Odd errors when using party package "contrasts cannot be applied ...." and "object of type closure...."

I am using the party package.
When I run:
tree1 <- mob(incarcerated~priors+opens+concrearr+postrearr+anyrearr+postconvfel+postconvmis+
ag_vfo+ag_cla2+in_custody |PRIOR_FELONY_ARREST ,
data = jamaal,
control = ctrl,
model = glinearModel,
family = binomial)
I get the error
Error in `contrasts<-`(`*tmp*`, value = contr.funs[1 + isOF[nn]]) :
contrasts can be applied only to factors with 2 or more levels
But I checked and every factor variable has at least 2 levels.
I then tried a much simpler tree
treetest <- mob(incarcerated~priors|in_custody,
data = jamaal,
control = ctrl,
model = glinearModel,
family = binomial)
and got one of the infamous R error messages
Error: object of type 'closure' is not subsettable
Any help appreciated
UPDATE
I found the source of the first error (it was a problem with how I was using factor()) but not the second. Also, rpart works on the same data with no problem.
The data are confidential, but I will check with the client if posting a small subset is OK
FURTHER UPDATE
Here is an small example with made up data:
priors <- c(rep('Y', 5), rep('N', 5))
incarcerated <- rep(c('Y', 'N'), 5)
in_custody <- rep(c(rep('Y', 3), rep('N', 2)),2)
testdata <- data.frame(cbind(priors, incarcerated, in_custody))
treetest <- mob(incarcerated~priors|in_custody, data = testdata,
model = glinearModel, family = binomial)
gives the same error.
party is looking for the results of a binomial() call, rather than the function binomial or the string "binomial". (In my opinion the glm() function in base R has made things very confusing by accepting any of these three as acceptable variants.)
priors <- c(rep('Y', 5), rep('N', 5))
incarcerated <- rep(c('Y', 'N'), 5)
in_custody <- rep(c(rep('Y', 3), rep('N', 2)),2)
testdata <- data.frame(cbind(priors, incarcerated, in_custody))
library(party)
treetest <- mob(incarcerated~priors|in_custody, data = testdata,
model = glinearModel, family = binomial())
In hindsight, this error message is at least somewhat informative -- it tells us to look for a function that it is being passed somewhere that R expects an object that has elements that can be extracted ...

Resources