Object not found error in anova - r

I am new to coding and R and am trying to run an anova on my dataset for a project. I am looking for the effect of condition on response times (resp.rt). I keep getting the following error though:
Error in eval(expr, envir, enclos) : object 'resp.rt' not found
Here is my code:
setwd('C:/Users/Dasha/Documents/R/stroop')
files <- list.files(path = ".", pattern = "_stroop.csv")
data_frame <- do.call(rbind,lapply(files,read.csv, header = T))
print(i)
#Change independent variables to factors
data_frame$congruent <- as.factor(data_frame$congruent)
data_frame$session <- as.factor(data_frame$session)
data_frame$participant <- as.factor(data_frame$participant)
model_rt <- lm (resp.rt ~ participant + session + congruent + condition + condition*session, data_frame = data_frame)
anova(model_rt)
Any help would be appreciated!

Your data_frame variable (descriptive) most likely does not have a "resp.rt" field...

Related

PiecewiseSEM - Error in cbind(ret, isSig(ret[, 5])) : object 'ret' not found

I'm trying to run a path analysis using the piecewiseSEM package. When I try to run summary or coefs(), I get this error:
Error in cbind(ret, isSig(ret[, 5])) : object 'ret' not found
or:
Error in cbind(ret, isSig(ret[, 5])) : object 'ret' not found
I have no idea what "object ret is. There is nothing in my code named "ret". My spelling is correct. All I have is a model list I called "mod" with two mixed models. The codes I tried to use are below:
mod <- psem(modn, modm)
summary(mod, standardize = "scale", conserve = TRUE)
#####
coefs(mod)
I have to run this as soon as possible and there is no one else to help me. I would really appreciate any comments. Please remember I'm not a skilled programmer so, I'll quote Michael Scott: "Explain this to me like I'm five." lol
Thank you.
modn<-lmer(Nestedness ~ (Category+Prop_ex+Connectance+Assimetry+N_Pol+N_Pla+Dist_eq) + (1|Eco_code), data = sem)
modm<-lmer(Modularity ~ (Category+Connectance+Assimetry+Prop_ex+Dist_eq+N_Pla+N_Pol) + (1|Eco_code),data = sem)
#try 1
mod <- psem(
lmer(Nestedness ~ (Category+Prop_ex+Connectance+Assimetry+N_Pol+N_Pla+Dist_eq) + (1|Eco_code), data = sem),
lmer(Modularity ~ (Category+Connectance+Assimetry+Prop_ex+Dist_eq+N_Pla+N_Pol) + (1|Eco_code),data = sem),
data = sem)
coefs(mod)
#try2
mod <- psem(modn, modm)
summary(mod, standardize = "scale", conserve = TRUE)
Resulting error:
Error in cbind(ret, isSig(ret[, 5])) : object 'ret' not found
in cbind(ret, isSig(ret[, 5])) : object 'ret' not found

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

Error running neural net

library(nnet)
set.seed(9850)
train1<- sample(1:155,110)
test1 <- setdiff(1:110,train1)
ideal <- class.ind(hepatitis$class)
hepatitisANN = nnet(hepatitis[train1,-20], ideal[train1,], size=10, softmax=TRUE)
j <- predict(hepatitisANN, hepatitis[test1,-20], type="class")
hepatitis[test1,]$class
table(predict(hepatitisANN, hepatitis[test1,-20], type="class"),hepatitis[test1,]$class)
confusionMatrix(hepatitis[test1,]$class, j)
Error:
Error in nnet.default(hepatitis[train1, -20], ideal[train1, ], size = 10, :
NA/NaN/Inf in foreign function call (arg 2)
In addition: Warning message:
In nnet.default(hepatitis[train1, -20], ideal[train1, ], size = 10, :
NAs introduced by coercion
hepatitis variable consists of the hepatitis dataset available on UCI.
This error message is because you have character values in your data.
Try reading the hepatitis dataset with na.strings = "?". This is defined in the description of the dataset on the uci page.
headers <- c("Class","AGE","SEX","STEROID","ANTIVIRALS","FATIGUE","MALAISE","ANOREXIA","LIVER BIG","LIVER FIRM","SPLEEN PALPABLE","SPIDERS","ASCITES","VARICES","BILIRUBIN","ALK PHOSPHATE","SGOT","ALBUMIN","PROTIME","HISTOLOGY")
hepatitis <- read.csv("https://archive.ics.uci.edu/ml/machine-learning-databases/hepatitis/hepatitis.data", header = FALSE, na.strings = "?")
names(hepatitis) <- headers
library(nnet)
set.seed(9850)
train1<- sample(1:155,110)
test1 <- setdiff(1:110,train1)
ideal <- class.ind(hepatitis$Class)
# will give error due to missing values
# 1st column of hepatitis dataset is the class variable
hepatitisANN <- nnet(hepatitis[train1,-1], ideal[train1,], size=10, softmax=TRUE)
This code will not give your error, but it will give an error on missing values. You will need to do address those before you can continue.
Also be aware that the class variable is the first variable in the dataset straight from the UCI data repository
Edit based on comments:
The na.action only works if you use the formula notation of nnet.
So in your case:
hepatitisANN <- nnet(class.ind(Class)~., hepatitis[train1,], size=10, softmax=TRUE, na.action = na.omit)

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