I'm very newbie in R and i am creating a function to calculate the Furnival Index, trying to make it cleaner, where the user will only need to insert the adjusted model. I'm having some trouble to identify if the log transformation in the model, is a natural log or any other kind, because the index will change according to this information. So i thought to calculate this information using a=b^1/x, where "a" is the logarithm base, "x" and "b" are the formula information with/without log transformation respectively. But for doing that i need the original value from the model, because by using "model$model" i only get the logarithm value.
Here is what i have done so far:
furnival=function(object=NULL)
{
w <- object$weights
if(!is.null(object) && is.numeric(object))
stop("'object' must be a formula")
if(is.null(w <- object$weights)){
w <- 1
}else{
w
}
if(length(grep("log", formula(object)))!=0){
y <- as.numeric(as.matrix(object$model[1L]))
modelValues <- object[Something to identify the original value]
routine <- object$model == 1
if(any(routine))
modelValues[!routine]
modelValues <- sample(modelValues,1)
a <- modelValues^(1/y)
if(grep("log", formula(object))[1L]==2)
y <- a^y
if(a == exp(1)){
df <- df.residual(object)
MSE <- sum((residuals(object)^2)*w)
index <- (exp(mean(log(y))))*(sqrt(MSE/df))
return(index)
}else{
df <- df.residual(object)
MSE <- sum((residuals(object)^2)*w)
index <- (a^(mean(log(y,a))))*(sqrt(MSE/df))*(log(exp(1),a)^-1)
return(index)
}
}
else{
df <- df.residual(object)
MSE <- sum((residuals(object)^2)*w)
index <- sqrt((MSE/df))
return(index)
}
}
If there is some way to do this, or even if there is a smarter way to make this function.