I'm trying to perform prediction of a testing set using a refund::pfr model. My model is scalar-on-function and on scalar and looks like the following example. When I'm using lf(cca) for the functional predictor I get the predictions without problem.
data(DTI)
DTI_train <- DTI[DTI$visit==1 & complete.cases(DTI),]
DTI_train <- DTI_train[1:60, ]
DTI_test <- DTI[DTI$visit==1 & complete.cases(DTI),]
DTI_test <- DTI_test[61:66, ]
fit_lf <- pfr(pasat ~ sex + case + lf(cca), data = DTI_train)
predict(fit_lf, newdata = as.list(DTI_test))
However, when I'm using fpc(cca) I'm getting the following error.
fit_fpc <- pfr(pasat ~ sex + case + fpc(cca), data = DTI_train)
predict(fit_fpc, newdata = as.list(DTI_test))
Error in eval(predvars, data, env) : object 'X.tmat' not found
Calls: predict ... eval -> model.frame -> model.frame.default -> eval -> eval
In addition: Warning message:
In (function (object, newdata, type = "link", se.fit = FALSE, terms = NULL, :
not all required variables have been supplied in newdata!
Calls: predict -> predict.pfr -> eval -> eval -> <Anonymous>
I have tried to include the X.tmat of the newdata by including in the list of newdata the fpc(newdata) but I'm getting another type of problem.
new_fit <- as.list(DTI_test)
new_fit$cca <- fpc(DTI_test[,'cca' ])
predict(fit_fpc, newdata = new_fit)
Error in predict.pfr(fit_fpc, newdata = new_fit) :
length(unique(sapply(newdata, function(x) ifelse(is.matrix(x), .... is not TRUE
Calls: predict -> predict.pfr -> stopifnot
Any idea of how to perform predictions with fpc on my model?
Thanks
Related
Hello everyone I am having an issue with my R software. When I try to run a simple regression i get this error:
model1 <- linReg(data = dat, dep = 'Intent', blocks = list(c('Enjoy')), modelTest = 'f', stdEst = TRUE)
model1
Error: Argument 'modelTest' must be either TRUE or FALSE
I have tried changing the to a capital and typing in FALSE instead but when I type in FALSE I get this error:
model1 <- linReg(data = dat, dep = 'Intent', blocks = list(c('Enjoy')), modelTest = FALSE, stdEst = TRUE)
Error in eval(predvars, data, env) : object 'XRW5qb3k' not found
Can anyone please help me with this? All o the descriptives, graphs, and plots run just fine. I have the packages jmv, psych, and car loaded
Thank you for your help!
After loading and partitioning my data successfully I am trying to get the following code to run
*logit.reg <- glm(Financial.Condition ~ ., data = train.df, family = "binomial")
options(scipen = 999)
summary(logit.reg)*
but I am getting the following error message:
*> logit.reg <- glm(Financial.Condition ~ ., data = train.df, family = "binomial")
Error in model.matrix.default(mt, mf, contrasts) :
variable 1 has no levels
> options(scipen = 999)
> summary(logit.reg)
Error in summary(logit.reg) : object 'logit.reg' not found*
What is the proper way to write the logit.reg function?
I can get variable importance out from "nnet" and "knn" models, but not from "lda", "lda2", and "qda".
I am using varImp(). I've tried everything I can think of and just can't get a proper idea of what the variable importance is.
Here is my code for training the model:
lda_model <- train(quality2 ~ .,
data = train_data,
method = "lda",
preProcess = c("center", "scale"),
trControl = trainControl(method = "repeatedcv",
number = 10,
repeats = 2),
importance = TRUE)
and here is the error I get when I try to check importance:
> varImp(lda_model)
Error in model.frame.default(formula = y ~ x, na.action = na.omit, drop.unused.levels = TRUE) :
invalid type (list) for variable 'y'
In addition: Warning messages:
1: In mean.default(y, rm.na = TRUE) :
argument is not numeric or logical: returning NA
2: In Ops.factor(left, right) : ‘-’ not meaningful for factors
I know this means it's treating it as an object class list instead of a trained model, and I've tried it on lda_model$finalmodel and others, but it's still not working.
How can I get proper feedback when using lda/qda on how my model is performing and which variables are performing best?
I had the same problem and it seems to come from the way of the dataset is imported in R. I first imported with the {readxl} package and varImp() didn't work. Then I tried to import throught the clipboard and now varImp is working on my lda model build with {caret}.
My code with {readxl} :
library(readxl)
glauc <- read_excel("Glaucome.xlsx", sheet="GlaucomaM")
rownames(glauc) <- glauc$IDENT
glauc$IDENT <- NULL
glauc$Class <- as.factor(glauc$Class)
library(caret)
numappr <- createDataPartition(glauc$Class, p=0.7)
appr <- glauc[numappr$Resample1,]
test <- glauc[-numappr$Resample1,]
Ctrl <- trainControl(summaryFunction=twoClassSummary,
classProbs=TRUE)
appr.lda <- train(Class~., data=appr, method="lda",
trControl=Ctrl, preProc = c("center","scale"),
metric="ROC")
varImp(appr.lda)
This leads to the same error message as yours.
Error: $ operator is invalid for atomic vectors
In addition: Warning messages:
1: In mean.default(y, rm.na = TRUE) :
argument is not numeric or logical: returning NA
2: In Ops.factor(left, right) : ‘-’ not meaningful for factors
And my code with read.table() and the clipboard :
glauc <- read.table("clipboard", header=T, sep="\t", dec=".")
rownames(glauc) <- glauc$IDENT
glauc$IDENT <- NULL
library(caret)
numappr <- createDataPartition(glauc$Class, p=0.7)
appr <- glauc[numappr$Resample1,]
test <- glauc[-numappr$Resample1,]
Ctrl <- trainControl(summaryFunction=twoClassSummary,
classProbs=TRUE)
appr.lda <- train(Class~., data=appr, method="lda",
trControl=Ctrl, preProc = c("center","scale"),
metric="ROC")
varImp(appr.lda)
This one leads to the result (only the first ones here):
varImp(appr.lda)
ROC curve variable importance
only 20 most important variables shown (out of 62)
Importance
vari 100.00
varg 97.14
vars 94.52
phci 93.69
hic 92.02
phcg 90.55
tms 89.96
Hope it helps.
Sophie
I know this question has been raised before (Error in <my code> : object of type 'closure' is not subsettable). But I could not get my head around it.
Here is the packages I use and how I prepare my data
library(mlogit)
data(CollegeDistance, package="AER")
Data <- CollegeDistance
Data$Dist[Data$distance<0.4] <- 1
Data$Dist[Data$distance<1 & Data$distance>=0.4] <- 2
Data$Dist[Data$distance<2.5 & Data$distance>=1] <- 3
Data$Dist[Data$distance>=2.5] <- 4
Now when I define a mlogit object and use it for prediction I get that error.
Formula <- paste('Dist ~', paste('1|',paste(c("urban", "unemp", "tuition"), collapse = " + "),'|1'))
Model <- mlogit(as.formula(Formula), Data, shape='wide', choice='Dist')
Predict <- predict(Model, newdata=mlogit.data(Data, shape='wide', choice='Dist'), returnData=FALSE)
The interesting part is that if I replace Formula with formulathen it works!
UPDATE
I encounter that problem while using mlogit in a function. I really appreciate it if you can show me a way out of it.
modelmaker <- function(variables){
Formula <- paste('Dist ~', paste('1|',paste(variables, collapse = " + "),'|1'))
MODEL <- mlogit(as.formula(Formula), Data, shape='wide', choice='Dist')
return(MODEL)
}
Model <- modelmaker(c("urban", "unemp", "tuition"))
Predict <- predict(Model, newdata=mlogit.data(Data, shape='wide', choice='Dist'), returnData=FALSE)
This time is does not solve even by avoiding using formula or Formula. If you change it to XXX the error will be
object 'XXX' not found
I try to run
ltv_pos_model$prob3 <- predict(tree_before_deposit_3m_equal_100, newdata = ltv_pos_model,
type = "response")
in batch mode and this is what I get:
Error in get(ctr, mode = "function", envir = parent.frame()) :
object 'contr.Treatment' of mode 'function' was not found
Calls: predict ... model.matrix -> model.matrix.default -> contrasts -> get
Execution halted
In interactive mode everything is OK
Thanks