Ldahist(0 function Object not found - r

I am learning LDA using Wine data.
The dependent variable is called "Type" with 13 predictors.
wine.lda<-lda(Type ~., data = wine)
wine.lda$svd
wine.lda.values <- predict(wine.lda)
ldahist(data = wine.lda.values$x[,1], g=Type)
######OUTPUTS#########
> wine.lda<-lda(Type ~., data = wine)
> wine.lda$svd
[1] 28.18958 19.00634
> wine.lda.values <- predict(wine.lda)
> ldahist(data = wine.lda.values$x[,1], g=Type)
Error in ldahist(data = wine.lda.values$x[, 1], g = Type) :
object 'Type' not found
Everything was just fine. But when I tried to plot histogram. It gave me this error "object not found". I checked my code several times, but i couldnt find the reason.
Any help would be appreciated. Thanks!

Related

Error in Z_aktuell * D : non-conformable arrays

Can anyone figure out why I get the error below after running the following code?
library(haven)
library(survival)
library(dplyr)
library(readr)
library(glmmLasso)
par(mar=c(1,1,1,1))
library(discSurv)
HH<- as.data.frame(read_dta("https://www.stata.com/data/jwooldridge/eacsap/recid.dta") )
HHC <- contToDisc(dataShort = HH, timeColumn = "durat", intervalLimits = 20,equi = TRUE)
dtLong<-dataLong(dataShort = HHC, timeColumn = "timeDisc",
eventColumn = "cens",timeAsFactor = FALSE)
formula.1<-y~factor(black)+factor(alcohol)
family<-binomial(link = "logit")
lambda <- 20
penal.vec<-20
next.try<-TRUE
BIC_vec<-rep(Inf,length(lambda))
Deltama.glm2<-as.matrix(t(rep(0,3)))#coefficients + Intercept
Smooth.glm2<-as.matrix(t(rep(0,20)))
j<-1;test.step<-1;
glm2 <- glmmLasso(formula.1,
rnd = NULL,family = family, data = dtLong, lambda=lambda[j],final.re=T,switch.NR=F,
control = list(smooth=list(formula=~- 1+as.numeric(timeInt),nbasis=20,spline.degree=3,
diff.ord=2,penal=penal.vec[test.step],start=Smooth.glm2[j,]),
method.final="EM", print.iter=T,print.iter.final=T,
eps.final=1e-4,epsilon=1e-4,complexity="non.zero",
start=Deltama.glm2[j,]))
Iteration 41
Final Re-estimation Iteration 9Error in Z_aktuell * D : non-conformable arrays
when I change Deltama.glm2<-as.matrix(t(rep(0,3))) to Deltama.glm2<-as.matrix(t(rep(0,2)))
I get the error
Iteration 1Error in grad.lasso[b.is.0] <- score.beta[b.is.0] - lambda.b * sign(score.beta[b.is.0]) :
NAs are not allowed in subscripted assignments
I have tried to remove the starting values as suggested on glmmLasso error and warning without success
Switching from R version 4.2.2 to 3.6.0 solved the issue. Seems there is a compatibility issue between glmmlasso and the newer versions of R.

Error in eval(expr, p): object 'X' not found; predict (BayesARIMAX)

I am trying to use BayesARIMAX to model and predict us gdp (you can find the data here: https://fred.stlouisfed.org/series/GDP).I followed the example (https://cran.r-project.org/web/packages/BayesARIMAX/BayesARIMAX.pdf) to build my model. I didnt have any major issue to build the model(used error handling to overcome Getting chol.default error when using BayesARIMAX in R issue). However could not get the prediction of the model. I tried to look for solution and there is no example of predicting the model that is build using BayesARIMAX. Every time that I run the "predict" I get the following error:
"Error in eval(expr, p) : object 'X' not found"
Here is my code.
library(xts)
library(zoo)
library(tseries)
library(tidyverse)
library(fpp2)
gdp <- read.csv("GDP.csv", head = T)
date.q <- as.Date(gdp[, 1], "%Y-%m-%d")
gdp <- xts(gdp[,2],date.q)
train.row <- 248
number.row <- dim(merge.data)[1]
gdp.train <- gdp[1:train.row]
gdp.test <- gdp[(train.row+1):number.row]
date.test <- date.q[(train.row+1):number.row]
library(BayesARIMAX)
#wrote this function to handle randomly procuded error due to MCMC simulation
test_function <- function(a,b,P=1,Q=1,D=1,error_count = 0)
{
tryCatch(
{
model = BayesARIMAX(Y=a,X = b,p=P,q=Q,d=D)
return(model)
},
error = function(cond)
{
error_count=error_count+1
if (error_count <40)
{
test_function(a,b,P,Q,D,error_count = error_count)
}
else
{
print(paste("Model doesnt converge for ARIMA(",P,D,Q,")"))
print(cond)
}
}
)
}
set.seed(1)
x = rnorm(length(gdp.train),4,1)
bayes_arima_model <- test_function(a = gdp.train,b=x,P = 3,D = 2,Q = 2)
bayes_arima_pred <- xts(predict(bayes_arima_model[[1]],newxreg = x[1:3])$pred,date.test)
and here is the error code
Error in eval(expr, p) : object 'X' not found
Here is how I resolve the issue after reading through the BayesARIMAX code (https://rdrr.io/cran/BayesARIMAX/src/R/BayesianARIMAX.R) . I basically created the variable "X" and passed it to predict function to get the result. You just have to set the length of X variable equal to number of prediction.
here is the solution code for prediction.
X <- c(1:3)
bayes_arima_pred <- xts(predict(bayes_arima_model[[1]],newxreg = X[1:3])$pred,date.test)
which gave me the following results.
bayes_arima_pred
[,1]
2009-01-01 14462.24
2009-04-01 14459.73
2009-07-01 14457.23

Trying to do a reset test in Rstudio

I'm trying to do a reset test in Rstudio. Im new to this.
This is my code:
model1 <- lm(inntekt ~ arbeidstid + kvinne + alder + leder + utd_hoy, data = d)
summary(model1)
install.packages("lmtest")
library("lmtest")
resettest(lm, power = 2:3, type = "regressor", data = d)
When i am trying to run the test i get this:
resettest(lm, power = 2:3, type = "regressor", data = d)
Error: object of type 'closure' is not subsettable
Can somebody pleas help me? I am happy for all answers :)
May be because you should write name of lm object, not just lm :
resettest(model1, #replace lm by model1
power = 2:3, type = "regressor", data = d)

Error: object not found - cor.ci

I'm trying to use cor.ci to obtain polychoric correlations with significance tests, but it keeps giving me an error message. Here is the code:
install.packages("Hmisc")
library(Hmisc)
mydata <- spss.get("S-IAT for R.sav", use.value.labels=TRUE)
install.packages('psych')
library(psych)
poly.example <- cor.ci(mydata(nvar = 10,n = 100)$items,n.iter = 10,poly = TRUE)
poly.example
print(corr.test(poly.example$rho), short=FALSE)
Here is the error message it gives:
> library(psych)
> poly.example <- cor.ci(mydata(nvar = 10,n = 100)$items,n.iter = 10,poly = TRUE)
Error in cor.ci(mydata(nvar = 10, n = 100)$items, n.iter = 10, poly = TRUE) :
could not find function "mydata"
> poly.example
Error: object 'poly.example' not found
> print(corr.test(poly.example$rho), short=FALSE)
Error in is.data.frame(x) : object 'poly.example' not found
How can I make it recognize mydata and/or select certain variables from this dataset for the analysis? I got the above code from here:
Polychoric correlation matrix with significance in R
Thanks!
You have several problems.
1) As previously commented upon, you are treating mydata as a function, but you need to treat it as a data.frame. Thus the call should be
poly.example <- cor.ci(mydata,n.iter = 10,poly = TRUE)
If you are trying to just get the first 100 cases and the first 10 variables, then
poly.example <- cor.ci(mydata[1:10,1:100],n.iter = 10,poly = TRUE)
2) Then, you do not want to run corr.test on the resulting correlation matrix. corr.test should be run on the data.
print(corr.test(mydata[1:10,1:100],short=FALSE)
Note that corr.test is testing the Pearson correlation.

Error in <my code> : object of type 'closure' is not subsettable (2)

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

Resources