I have defined MV1 below with a value, and have used the MV1 in the output name. However, when I run the summary function on my model output I get the following meesage
'Error: unexpected symbol in: "assign(paste("Model", MV1, sep = '') <- model1 summary" '
MVx is a value that is defined as a numeric in my code already, and MV1 equates to "_3" in my code.
MV = MVx+1
MV1= paste("_", MV, sep="")
assign(paste("Model", MV1, sep = '') = model1 <- glm(tv1~., family=binomial(link='logit'), data=train70)
summary(Model_3) #Error occurs here
Would anyone know how to get around that?
Try this code
MV = MVx+1
MV1= paste("_", MV, sep="")
model1 <- paste('Model', MV1, sep="")
Related
I'm fitting two mixed-effects regression models to data samples in a for-loop and writing model summaries to file. I diverted the console stream to a text file so I can keep track any warnings or errors. I'm handling the errors with tryCatch()so that my loop does not stop after an error.
As far as I can tell, there are two issues with my code. First, when I query the returned exception in tryCatch(), e.g. poss.err.mod1 I get the following error:
<simpleError in tryCatchList(expr, classes, parentenv, handlers): argument "expr" is missing, with no default>
Still, the warnings are correctly written to file, which is puzzling. Second, when no error is detected, the code seems to be ignored (i.e. no model summaries are written to file). So the loop is running vacuously, skipping errors but apparently handling them and doing nothing else. What am I doing wrong?
N.B. My code is modeled on this SO post.
# divert console stream to file
options(warn=1)
wngs=file("warnings_log.txt",open="w+",blocking=TRUE)
sink(wngs,type="message")
for(iter in 1:1000){
data = read.csv(paste("sample", iter,".csv", sep=""), header=TRUE)
#error handling case #1
poss.err.mod1 = tryCatch(
lmer(y ~ x1 + (1|ranef), data = data, REML = FALSE), #try part
error=function(e) e #catch part
)
#error handling case #2
poss.err.mod2 = tryCatch(
lmer(y ~ x1 + x2 + (1|ranef), data = data, REML = FALSE), #try part
error=function(e) e #catch part
)
#real work #1
if(!inherits(poss.err.mod1, "error")){
mod1 = lmer(y ~ x1 + (1|ranef1), data = data, REML = FALSE)
write.csv(summmary$mod2, paste("mod2.sum_", iter, ".csv", sep="")
}else{
write(unlist(mod1#optinfo$conv$lme4$messages), paste("mod1.errors_", iter, ".txt", sep=""))
}
#real work #2
if(!inherits(poss.err.mod2, "error")){
mod2 = lmer(y ~ x1 + x2 + (1|ranef1), data = data, REML = FALSE)
write.csv(summmary$mod2, paste("mod2.sum_", iter, ".csv",)
}else{
write(unlist(mod2#optinfo$conv$lme4$messages), paste("mod1.errors_", iter, ".txt", sep=""))
}
cat("Iteration", iter, "completed!\n")
}
#close log file & restore warnings stream to console
closeAllConnections()
I'm trying to optimize my code by saving parameters in vector form and pass it to lda() for modeling. The following method works fine for lm, but not for qda or lda. The error message I received is highlighted in yellow.
intvars <- c("x*y","y*t","z*w")
intfm <- paste("clickthrough", "~", paste(intvars, collapse = " + "))
lda_model_int <- lda(intfm, data = s_train)
Error in lda.default(intfm, data = s_train) : 'x' is not a matrix
You will have to change your string to formula or you can reformulate
intvars <- c("x*y","y*t","z*w")
intfm <- reformulate(intvars,"clickthrough")
lda_model_int <- lda(intfm, data = s_train)
If you wanted to do it your way you will have to do
intvars <- c("x*y","y*t","z*w")
intfm <- as. formula(paste("clickthrough", "~", paste(intvars, collapse = " + ")))
lda_model_int <- lda(intfm, data = s_train)
I cannot seem to get past or understand what is wrong with this error while trying to run the ADF (Augmented Dickey Fuller) test on R-Studio. Will appreciate comments.
library(AER)
library("dynlm", lib.loc="~/R/win-library/3.2")
x<-read.table("C://R Files/protein.csv", header=T, sep=",")
"adf" <- function(x,k = 0, int = TRUE, trend = FALSE){
require(dynlm)
dx <- diff(x)
formula <- paste("dx ~ L(x)")
if(k > 0)
formula <- paste(formula," L(dx,1:k)")
if(trend){
s <- time(x)
t <- ts(s - s[1],start = s[1],freq = frequency(x))
formula <- paste(formula," t")
}
if(!int) formula <- paste(formula," - 1")
summary(dynlm(as.formula(formula)))
}
a<-ts(x$a)
adf(a, k=1, int=T, trend=T)
The error message that I get after this is :
Error in parse(text = x, keep.source = FALSE) :
:1:13: unexpected symbol
1: dx ~ L(x) L
^
It might be this:
formula <- paste(formula," L(dx,1:k)")
You can't just add something to a model. Try doing:
formula <- paste(formula,"+ L(dx,1:k)")
and see if that helps. If not you might want to share the contents of protein.csv so I can try to reproduce your problem.
I am applying rpart function to a data frame named train having all the integer values.
There are too many features so for that I have created a formula.
columns_features <- (paste(colnames(train)[31:50], collapse = "+"))
formulas <- as.formula(train$left_eye_center_x ~ columns_features)
tree_pred <- rpart(formulas , data = train)
Here , I get the error message
Error in model.frame.default(formula = formulas, data = train, na.action = function (x) : variable lengths differ (found for 'columns_features')
When I check formulas it has
train$left_eye_center_x ~ columns_features
and for column_features it has
[1] "l_1+ l_2+ l_3+ l_4+ l_5+ l_6+ l_7+ l_8+ l_9+ l_10+ l_11+ l_12+ l_13+ l_14+ l_15+ l_16+ l_17+ l_18+ l_19+ l_20"
For checking purpose when I manually enter the column names here, it works
formulas <- as.formula(train$left_eye_center_x ~ l_1+ l_2+ l_3+ l_4+ l_5+ l_6+ l_7+ l_8+ l_9+ l_10+ l_11+ l_12+ l_13+ l_14+ l_15+ l_16+ l_17+ l_18+ l_19+ l_20 )
tree_pred <- rpart(formulas , data = train)
Is double quote creating the error? What could be solution to this? I have many features so I cannot afford to enter each and every feature manually.
From the ?as.formula examples:
## Create a formula for a model with a large number of variables:
xnam <- paste0("x", 1:25)
(fmla <- as.formula(paste("y ~ ", paste(xnam, collapse= "+"))))
Which implies that in your case the following should work:
formulas <- as.formula(paste("train$left_eye_center_x ~", paste(colnames(train)[31:50], collapse = "+")))
A work-around, instead of using your approach would be (NB: I never used rpart, but I am confident that this works):
formulas <- as.formula(train$left_eye_center_x ~ .)
tree_pred <- rpart(formulas , data = train[,31:50])
If rpart does not like getting indexed data you could define a new dataframe:
train4rpart <- train[,31:50]
tree_pred <- rpart(formulas , data = train4rpart)
Actually, reading through ?rpart, you can skip the whole formula thing:
tree_pred <- rpart(train$left_eye_center_x ~ . , data = train[,31:50])
OR
tree_pred <- rpart(train$left_eye_center_x ~ . , data = train4rpart)
Printing a fitted model object from auto.arima() includes a line such as
"ARIMA(2,1,0) with drift,"
which would be a nice item to include in sweave (or other) output illustrating the fitted model. Is it possible to extract that line as a block? At this point the best I am doing is to extract the appropriate order from the arma component (possibly coupled with verbiage from the names of the coefficients of the fitted model, e.g., "with drift" or "with non-zero mean.")
# R 3.0.2 x64 on Windows, forecast 5.3
library(forecast)
y <- ts(data = c(-4.389, -3.891, -4.435, -5.403, -2.501, -1.858, -4.735, -1.085, -2.701, -3.908, -2.520, -2.009, -6.961, -2.891, -0.6791, -1.459, -3.210, -2.178, -1.972, -1.207, -1.376, -1.355, -1.950, -2.862, -3.475, -1.027, -2.673, -3.116, -1.290, -1.510, -1.736, -2.565, -1.932, -0.8247, -2.067, -2.148, -1.236, -2.207, -1.120, -0.6152), start = 1971, end = 2010)
fm <- auto.arima(y)
fm
# what I want is the line: "ARIMA(2,1,0) with drift`"
str(fm)
paste("ARIMA(", fm$arma[1], ",", fm$arma[length(fm$arma)-1], ",", fm$arma[2], ") with ",intersect("drift", names(fm$coef)), sep = "")
Checking the auto.arima function, I noticed it internally calls another function which is named arima.string.
Then I did:
getAnywhere(arima.string)
and the output was:
A single object matching ‘arima.string’ was found
It was found in the following places
namespace:forecast
with value
function (object)
{
order <- object$arma[c(1, 6, 2, 3, 7, 4, 5)]
result <- paste("ARIMA(", order[1], ",", order[2], ",", order[3],
")", sep = "")
if (order[7] > 1 & sum(order[4:6]) > 0)
result <- paste(result, "(", order[4], ",", order[5],
",", order[6], ")[", order[7], "]", sep = "")
if (is.element("constant", names(object$coef)) | is.element("intercept",
names(object$coef)))
result <- paste(result, "with non-zero mean")
else if (is.element("drift", names(object$coef)))
result <- paste(result, "with drift ")
else if (order[2] == 0 & order[5] == 0)
result <- paste(result, "with zero mean ")
else result <- paste(result, " ")
return(result)
}
Then I copied the function code and pasted it in a new function which I named arima.string1
arima.string1(fm)
# [1] "ARIMA(2,1,0) with drift "
Today I discovered the block of text I sought is also stored in the forecast from the fitted model object:
forecast(fm)$method
Thanks to Fernando for properly markup-ing my question and to Davide for providing a pair of useful insights - getAnywhere() and arima.string() ready for modification.