R segmented regression predict gives error: "subscript out of bounds" - r

I'm building a segmented regression model using R's Segmented package.
I was able to create the model but have trouble using the predict.segmented function. It always throws an error saying "subscript out of bounds"
This is the exact error message:
Error in newdata[[nameZ[i]]] : subscript out of bounds
Traceback just gives this:
1: predict.segmented(seg_model, xtest)
I created a simple case that gives the same error:
require(segmented)
x = c(1:90, 991:1000)
y = c((x[1:10]/2), (x[11:100]*2))
lm_model = lm(y~x)
seg_model = segmented(lm_model, seg.Z=~x, psi=list(x=NA),
control=seg.control(display=FALSE, K=1, random=TRUE))
xtest = c(1:1000)
predict.segmented(seg_model, xtest)
I am starting to think this could be a bug. I'm new to R and not sure how to debug this either. Any help is appreciated!

You are using predict.segemented incorrectly. Like nearly all the predict() functions, your newdata parameter should be a data.frame, not a vector. Also, it needs to have names that match the variables used in your regression. Try
predict.segmented(seg_model, data.frame(x=xtest))
instead. When using a function for the first time, be sure the read the help page (?predict.segmented) to know what the function expects for each of the parameters.

Related

How do I solve an error in x : non-conformable arguments (R)?

I am trying to implement ordinal logistic regression on my dataset in r. I use the function 'polr' for this, but cannot seem to find a lot of information regarding its implementation.
The following errors are the ones I'm stuck on:
> dat.polr <- polr(as.factor(relevance)~allterms+idf.title, data=dat.one)
Warning message:
In polr(as.factor(relevance) ~ allterms + idf.title + idf.desc + :
design appears to be rank-deficient, so dropping some coefs
> dat.pred <- predict(dat.polr,dat.test,type="class")
Error in X %*% object$coefficients : non-conformable arguments
I want to train my model to guess the relevance of a new dataset. dat.one is the dataset I'm using to train the data, dat.test is the dataset I'm using to test the data. I believe that the predict variable's error is caused by the warning in polr. However, I have no clue how to resolve this. Any help would be appreciated :)

R implementation of kohonen SOMs: prediction error due to data type.

I have been trying to run an example code for supervised kohonen SOMs from https://clarkdatalabs.github.io/soms/SOM_NBA . When I tried to predict test set data I got the following error:
pos.prediction <- predict(NBA.SOM3, newdata = NBA.testing)
Error in FUN(X[[i]], ...) :
Data type not allowed: should be a matrix or a factor
I tried newdata = as.matrix(NBA.testing) but it did not help. Neither did as.factor().
Why does it happen? And how can I fix that?
You should put one more argument to the predict function, i.e. "whatmap", then set its value to 1.
The code would be like:
pos.prediction <- predict(NBA.SOM3, newdata = NBA.testing, whatmap = 1)
To verify the prediction result, you can check using:
table(NBA$Pos[-training_indices], pos.prediction$predictions[[2]], useNA = 'always')
The result may be different from that of the tutorial, since it did not declare the use of set.seed() function.
I suggest that the set.seed() with an arbitrary number in it was declared somewhere before the training phase.
For simplicity, put it once on the top most of your script, e.g.
set.seed(12345)
This will guarantee a reproducible result of your model next time you re-run your script.
Hope that will help.

error r: invalid subscript type "closure" in a simple regression

unfortunately i am a beginner in r. I d like to run a simple linear regression model in r with the comand lm, but every time i try the following error occurs:
Error in xj[i] : invalid subscript type 'closure'
The regression model ist just as follows:
REG1 <- lm(flowpercent~ret+tna+fundage+number_shr_cl,data = reg, na.omit)
#-flowpercent is a calculated variable:
reg$flowpercent <- reg$flow_dollar/lag(reg$tna, n=1)
#-fundage is also calculated:
reg$fundage <- as.numeric(difftime(ref_date,reg$InceptionDate, units = "days")/365.25)
ret, tna, number_shr_cl are variables from a database
hopefully some can help me to solve my problem.
Many thanks in advance.
Your third argument is na.omit. You probably saw someone writing something like na.action = na.omit. However, if you look up the help for lm by typing ?lm, you will see:
Usage:
lm(formula, data, subset, weights, na.action, ... # etc
which tells you that the third argument to lm is subset. So, you are passing the object called na.omit to the subset argument, which lm tries to use to subset your data. Unfortunately, na.omit is an R function (aka a "closure"). Not surprisingly, R does not know how to use this function to subset your data. Hence the error.

Error from logistic regression Zelig R

I'm running a logit model using the zelig package in R.
m3 <- zelig (newdata$bes.Turnout ~ civicduty + newdata$bes.CivicDuty2+newdata$bes.Age, model="logit", data=newdata)
newdata$bes.Turnout is binary, the rest are numerical.
I try to set the values of x.
mcos<- median(newdata$bes.CivicDuty2)
civicmean<-mean(civicduty)
mage<-mean(newdata$bes.Age)
test<-setx(m3, civicduty=civicmean, newdata$bes.CivicDuty2=mcos, newdata$bes.Age=mage)
I get the following error message:
Error: unexpected '=' in "test<-setx(m3, civicduty=civicmean, newdata$bes.CivicDuty2="
I have tried the same code for the survival data in Zelig, and here the setx worked.
Any ideas what I am doing wrong and how to fix it?
That call to setx doesn't make sense to me. My best guess is that you want something like
test<-setx(m3,
civicduty = mean(newdata$civicduty),
bes.CivicDuty2 = mean(newdata$bes.CivicDuty2,
bes.Age = mean(newdata$bes.Age))

Error for predict() with lmer

I'm using a leave-one-out method to evaluate how well a model with one datapoint excluded predicts that datapoint (rotating through all datapoints). The code below has successfully run on essentially the same data with a slightly different DV, so I'm stumped as to why I'm getting the error that I'm getting. Here's the relevant chunk of code:
dataPennTrim.lmer <- lmer(logDur.PENN~cNewNounDen*ContextCode+
Vowel.Contrasts+BlockCode+
(1|subject)+(0+ cNewNounDen +ContextCode|subject)+
(1|word)+(0+ContextCode|word),
data=pennTrim,
control = lmerControl(optimizer = "bobyqa"),REML=FALSE)
pennPred <- predict(dataPennTrim.lmer, newdata = dataFull2)
dataFull2 has the same columns as pennTrim, it just has more rows. Pretty standard use of the predict() function. I get this error:
Error in t(.Call(Csparse_dense_crossprod, y, x)) :
error in evaluating the argument 'x' in selecting a
method for function 't': Error: Cholmod error 'X and/or Y
have wrong dimensions' at file ../MatrixOps/cholmod_sdmult.c, line 90
Any thoughts about what might be causing this error? I can use essentially the same code with the same dataframes swapping out logDur.PENN for logDur.Manual (measurements from a different source) and the code gives no errors.

Resources