What R package is the "Error" function in? - r

I'm trying to call the Error() function but it says could not find function "Error". I checked the docs and Error does not seem to be a part of R base package. This is a very hard function to search for because "Error" is a very overloaded word. What package is Error() in? For context, I'm running an anova. I'm pretty sure that this isn't a user defined since I see multiple tutorials referencing it without defining.
EDIT:
Here are the tutorials:
https://datascienceplus.com/two-way-anova-with-repeated-measures/ , http://personality-project.org/r/r.guide/r.anova.html#withinone (look at usages of Error() in within sujects/repeated measures anova)
EDIT2:
Here is the model answer from the tutorial. There does not seem to be any information about how the 'Error' function is defined or where it comes from:
model <- aov(wm$iq ~ wm$condition + Error(wm$subject / wm$condition))

The Error() in this case is specifying the error term for the aov function. It's a parameter passed to the function aov() and thus is not a function on its own. I've also tried searching for Error using the package sos, which yields 0 results:
# install.packages("sos")
library(sos)
results <- findFn("Error")
filtered_results <- results[results$Function == 'Error']
nrow(filtered_results)
Output:
[1] 0
You might want to read this Cross Validated post on how to set the Error term within the aov() function.

Related

What is the sense of the is.single() function in R?

I stumbled on the function is.single() and do not understand what is does. The description says:
is.single reports an error. There are no single precision values in R.
And indeed, if I try to test different things inside the function I get an error. Example:
is.single(1)
Error in is.single(1) : type "single" unimplemented in R
I see no sense in a function that seemingly always gives an error. What is the function for?

Parsing error in MonteCarlo::MonteCarlo function in R

I am trying to run a power analysis using a MonteCarlo approach in R.
I have created a function of two parameters that does output a boolean (tested manually for all relevant values of the parameters). I also have run baby-examples of the MonteCarlo function to make sure that I understand it and that it works well.
Yet when I try to run the real thing, I get the following error message:
Error in parse(text = all_funcs_found[i]) : <text>:1:1: unexpected '::'
1: ::
I read through the source code of the MonteCarlo function (which I found here) and found
#loop through non-primitive functions used in func and check from which package they are
for(i in 1:length(all_funcs_found)){
if(environmentName(environment(eval(parse(text=all_funcs_found[i]))))%in%env_names){
packages<-c(packages,env_names[which(env_names==environmentName(environment(eval(parse(text=all_funcs_found[i])))))])
}
}
which doesn't really make sense to me - why should there be a problem there?
Thank you for any ideas.
I found the answer: the function I wrote was calling a function from a specific library in the form libraryname::functionname.
This works OK if you use the function once manually, but makes MonteCarlo break.
I solved the problem by first loading the relevant library, then removing the 'libraryname::' part from the definition of the main function. MonteCarlo then runs just fine.

R gamlss::predict.gamlss not an exported object from 'namespace:gamlss'

I am experiencing some confusing behavior from gamlss in R. The documentation lists predict.gamlss and ?predict.gamlss returns the function documentation in RStudio. However, the function does not autocomplete when typing out predict.gamlss, and trying to run it returns Error: 'predict.gamlss' is not an exported object from 'namespace:gamlss'. How does that happen? Is the function deactivated somehow? There is a separate function predictAll that does work.
The documentation does state
This function is under development
I am trying to access the function because I am experiencing some confusing results with predict and predictAll.
R version is 4.0.0. gamlss version is 5.1.6.
As far as I can tell, what you describe is expected and is normal S3 method dispatching. The method predict.gamlss is called when you call predict on a object of class gamlss.
Consider the following (from the documentation of ?predict.gamlss)
data(abdom)
aa <- gamlss(y ~ cs(x^.5), data = abdom)
#[1] 371.3931
predict(aa)[610]
Looking at the class of aa:
class(aa)
#[1] "gamlss" "gam" "glm" "lm"
The function is not exported, but the S3 method is registered.
As to the difference between predictAll and predict.gamlss, you'll have to read the documentation (the two are documented together). My guess is that predictAll predict all listed in the what-argument of predict.gamlss.

Where did the forecast.Holtwinters go in R 3.4.3?

I'm using R Studio based on R 3.4.3. However, when I tried to call the forecast.HoltWinters function, R told me that "could not find function "forecast.HoltWinters"". Inspect the installed package (v8.2) told me that it's true, there is no forecast.HoltWinters. But the manual in https://cran.r-project.org/web/packages/forecast/ clearly stated that forecast.HoltWinters is still available.
I have also tried stats::HoldWinters, but it's working wrong. The code run fine on another computer, but it couldn't run at all on mine. Is there any solution?
Here is the code. Book2.csv has enough data to last more than 3 periods.
dltt <- read.csv("book2.csv", header = TRUE)
dltt.ts <- ts(dltt$Total, frequency=12, start=c(2014,4))
dltt.ts.hw <- HoltWinters(dltt.ts)
library(forecast)
dltt.ts.hw.fc <- forecast.HoltWinters(dltt.ts.hw) //Error as soon as I run this line
Fit a HoltWinters model using the HoltWinters function and then use forecast. Its all in the help for HoltWinters and forecast, namely "The function invokes particular _methods_ which depend on the class of the first argument". I'll copy the guts of it here:
m <- HoltWinters(co2)
forecast(m)
Note this will call the non-exported forecast.HoltWinters function, which you should never call directly using triple-colon notation as some may suggest.

Looking into the predict function in R

I am trying to understand how the SVM predict function works when using command ksvm from R package kernlab.
I tried the look into the predict function using the following commands:
methods(class="ksvm")
getAnywhere(ksvm:::predict)
However, I get the following output and not the complete predict function:
A single object matching ‘:::’ ‘ksvm’ ‘predict’ was found
It was found in the following places
package:base
namespace:base
with value
function (pkg, name)
{
pkg <- as.character(substitute(pkg))
name <- as.character(substitute(name))
get(name, envir = asNamespace(pkg), inherits = FALSE)
}
<bytecode: 0x00000000088be4f8>
<environment: namespace:base>
Warning message:
In find(x, numeric = TRUE) :
elements of 'what' after the first will be ignored
Can someone help with how to obtain the complete predict function?
Update 1:
Suggestion from misspelled worked fine on predict function for ksvm in kernlab package but doesn't seem to work on svm in e1071 package.
It throws the following error:
> getMethod("predict", "svm")
Error in getMethod("predict", "svm") :
no generic function found for 'predict'
In general, how to know which get method to use?
You were close. I was able to get the function code with getMethod("predict", "ksvm"). This answer describing S4 method dispatch was helpful. View source code for function
Per your updated question, I can get the source code for predict.svm using the ::: function. Specifically with e1071:::predict.svm. The link above also describes this in the section on S3 method dispatch.
There are at least a couple of things going on here. First is that in the former case you are dealing with S4 objects and S3 objects in the latter. The two systems have different method dispatches and different ways to view the source code. Another wrinkle is that the predict.svm function is an invisible function and can only be viewed either with ::: or getAnywhere().

Resources