I'm trying to run a functional data analysis model with scalar-on-function and on scalar regression in R. But by removing the intercept I'm getting the following error (the example is based on this discussion),
library(refund)
data(DTI)
DTI1 <- DTI[DTI$visit==1 & complete.cases(DTI),]
par(mfrow=c(1,2))
fit_af <- pfr(pasat ~ -1 +sex + case + af(cca, k=c(5, 8), bs="ps"), data = DTI1)
#Error in str2lang(x) : <text>:1:9: unexpected symbol
#1: pasat~0 sex
# ^
#Calls: pfr -> formula -> formula.character -> str2lang
How can I remove the intercept from a pfr model?
This is a bug. Running prf() in the debugging mode identifies the problem. When there is no intercept, it does:
if (!attr(tf, "intercept")) {
newfrml <- paste(newfrml, "0", sep = "")
}
which I think should be fixed to
if (!attr(tf, "intercept")) {
newfrml <- paste(newfrml, "0 +", sep = "")
}
Consider reporting this Stack Overflow thread, i.e., https://stackoverflow.com/q/72856108 to package maintainer Julia Wrobel: julia.wrobel#cuanschutz.edu
Note: This was tested using the latest refund_0.1-26 to date (released to CRAN on 2022-04-16).
Related
I am trying to implement a new nonlinear function to use in nlmer function in lme4 package. But I'm not sure what the problem is. This is the first time I'm trying to use nlmer but I'm following all the instructions I've found on the internet. The first error is about my dataframe.
data <- read.csv(paste("C:/Users/oguz/Desktop/Runs4SiteModels/db/", "DB4NLSiteModel", Periods[i],".txt", sep=""), sep = "", header = TRUE)
psa_rock <- data$PSAr
nparams <- c("c")
nonl_fn <- deriv(~ log(( psa_rock + c)/c),
namevec = c("c"),
function.arg=c("c", psa_rock))
fm <- nlmer(log(data$PSAm) ~ nonl_fn(c, psa_rock) ~ 1 + data$M1 + data$M3 + data$M85 + data$Nflag + data$Rflag + data$FDepth +
data$Dist1 + data$Dist3 + data$VN + (exp(-1*exp(2*log(data$Vs)- 11)) * log((data$PSAr + c) / c) ) +
(1|data$EQID) + (1|data$STID), data=data, start=c(c=0.1))
When I run this code, I'm getting the following error:
Error in model.frame.default(data = data, drop.unused.levels = TRUE, formula = log(data$PSAm) ~ :
invalid type (list) for variable 'data'
which I wasn't getting it while using lmer function (of course without the nonlinear function). That's why I'm thinking my problem is not about my dataframe.
Other issue that I couldn't stop thinking about, the part in the fixed-effects:
(exp(-1*exp(2*log(data$Vs)- 11)) * log((data$PSAr + c) / c) )
as you can see my nonlinear function also takes a part in my fixed-effects formula and I'm not quite sure how to implement that. I hope my way is correct but because of my first problem, I couldn't find an opportunity to test that.
#2개년(use: df_3 , MSE: 0.02313121)
#선형회귀모델
lm2 <- lm(data = df_3, formula = OPS_y1 ~ (OPS_y2+OPS_y3 + AVG_y2+AVG_y3 + G_y2+G_y3 + GW.RBI_y2+GW.RBI_y3 + H_y2+H_y3 + SAC_y2+SAC_y3)^2) %>% step(direction = "both")
Error in length(obj) : class name too long in 'length'
Once the code has been executed, it will be executed normally. However, if you change the data set and run it again, the same error occurs when you run the first code again. What's the problem? I went to the registry editing window and changed the value of 'LongPathsEnabled' to 1, but it was not resolved. Please solve the problem.
I had the sample problem when using stepwise regression with the step function. In my case there was some package conflict and all was fine after I specified stats::step(...) as follows:
lm2 <- lm(data = df_3, formula = OPS_y1 ~ (OPS_y2+OPS_y3 + AVG_y2+AVG_y3 + G_y2+G_y3 + GW.RBI_y2+GW.RBI_y3 + H_y2+H_y3 + SAC_y2+SAC_y3)^2) %>% stats::step(direction = "both")
As pointed out by user12282991, the problem may be due to package conflicts. The error most probably results from attaching the package recipes, which masks "step" from package:stats. Thus, stepAIC from package MASS or stats::step works.
library(recipes)
step(subclass = paste(rep("A",1000),collapse=""))
gives
Error in (function (x, ...) : class name too long in 'print'.
I'm attempting to create a genetic algorithm (not picky about library, ga and genalg produce same errors) to identify potential columns for use in a linear regression model, by minimizing -adj. r^2. Using mtcars as a play-set, trying to regress on mpg.
I have the following fitness function:
mtcarsnompg <- mtcars[,2:ncol(mtcars)]
evalFunc <- function(string) {
costfunc <- summary(lm(mtcars$mpg ~ ., data = mtcarsnompg[, which(string == 1)]))$adj.r.squared
return(-costfunc)
}
ga("binary",fitness = evalFunc, nBits = ncol(mtcarsnompg), popSize = 100, maxiter = 100, seed = 1, monitor = FALSE)
this causes:
Error in terms.formula(formula, data = data) :
'.' in formula and no 'data' argument
Researching this error, I decided I could work around it this way:
evalFunc = function(string) {
child <- mtcarsnompg[, which(string == 1)]
costfunc <- summary(lm(as.formula(paste("mtcars$mpg ~", paste(child, collapse = "+"))), data = mtcars))$adj.r.squared
return(-costfunc)
}
ga("binary",fitness = evalFunc, nBits = ncol(mtcarsnompg), popSize = 100, maxiter = 100, seed = 1, monitor = FALSE)
but this results in:
Error in terms.formula(formula, data = data) :
invalid model formula in ExtractVars
I know it should work, because I can evaluate the function by hand written either way, while not using ga:
solution <- c("1","1","1","0","1","0","1","1","1","0")
evalFunc(solution)
[1] -0.8172511
I also found in "A quick tour of GA" (https://cran.r-project.org/web/packages/GA/vignettes/GA.html) that using "string" in which(string == 1) is something the GA ought to be able to handle, so I have no idea what GA's issue with my function is.
Any thoughts on a way to write this to get ga or genalg to accept the function?
Turns out I didn't consider that a solution string of 0s (or indeed, a string of 0s with one 1) would cause the internal paste to read "mpg ~ " which is not a possible linear regression.
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
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.