I would to take five parametrs: a, b, c, d and mean of standards deviation from every model (it is a normal value, no matrix) and store this information as a vector, then using rbind to crate matrix which I can search for the models and parameters.
I was trying with nested loops like:
for (a in 0:2){
for (b in 0:2){
for (c in 1:2){
for (d in 1:2){
Garch_model <- garchFit(~arma(a,b)+garch(c,d), include.mean = TRUE,cond.dist = c("norm"),data = dax_data)
ValidationBox <- rbind(ValidationBox,c(a,b,c,d,mean(Garch_model#sigma.t))
}
}
}
}
but I'm getting this error:
Error: unexpected '}' in: " ValidationBox <-
rbind(ValidationBox,c(a,b,c,d,mean(Garch_model#sigma.t))
}"
} Error: unexpected '}' in " }"
} Error: unexpected '}' in " }"
} Error: unexpected '}' in "}"
Can anyone tell me what's the issue?
Related
I've written the following code:
extract_distract <- function(dataset, type="increase"){
if(dataset = wfh){
if(type = "decrease"){
decr_distract <- str_subset(decr_product, "distract")
str_extract(decr_distract, ".{0,30}distract+.{0,30}")
} else if(type = "increase"){
incr_distract <- str_subset(incr_product, "distract")
str_extract(incr_distract, ".{0,30}distract+.{0,30}")
} else{stop("NA - use either increase or decrease type")}
} else{stop("NA - use (dataset = wfh)")}}
Everytime i try to run it it gives me the following error:
> } else{stop("NA - use either increase or decrease type")}
Error: unexpected '}' in " }"
> } else{stop("NA - use (dataset = wfh)")}}
Error: unexpected '}' in " }"
I am new to this but would appreciate any help
It's generally a good idea to pay most attention to the first error, not the last ones. If I run your code, I see
> extract_distract <- function(dataset, type="increase"){
+ if(dataset = wfh){
Error: unexpected '=' in:
"extract_distract <- function(dataset, type="increase"){
if(dataset ="
> if(type = "decrease"){
Error: unexpected '=' in " if(type ="
> decr_distract <- str_subset(decr_product, "distract")
Error in str_subset(decr_product, "distract") :
could not find function "str_subset"
> str_extract(decr_distract, ".{0,30}distract+.{0,30}")
Error in str_extract(decr_distract, ".{0,30}distract+.{0,30}") :
could not find function "str_extract"
> } else if(type = "increase"){
Error: unexpected '}' in " }"
> incr_distract <- str_subset(incr_product, "distract")
Error in str_subset(incr_product, "distract") :
could not find function "str_subset"
> str_extract(incr_distract, ".{0,30}distract+.{0,30}")
Error in str_extract(incr_distract, ".{0,30}distract+.{0,30}") :
could not find function "str_extract"
> } else{stop("NA - use either increase or decrease type")}
Error: unexpected '}' in " }"
> } else{stop("NA - use (dataset = wfh)")}}
Error: unexpected '}' in " }"
The first error is Error: unexpected '=' in: ..., which would have led you to the solution mentioned by #GregorThomas: you should have used ==, not =. The reason this caused all the other errors is that R discards the incorrect lines. After they are gone, the rest of your code doesn't make sense.
I am a new student to programming of any kind and am having trouble creating a function to calculate slope in R. What do these errors mean that are preventing the function from working?
slope <- function(x1,y1,x2,y2) {
missingvars=c(missing(x1),missing(y1),missing(x2),missing(y2))
if(sum(missingvars)>0){
stop(paste(“Values were not provided (denoted by TRUE): x1 -”,missingvars[1],“, y1 -”,missingvars[2],“, x2 -”,missingvars[3],“,
Y2 -”,missingvars[4],sep=“”))
}
if(x2-x1==0) {
stop(“Both X values are the same, slope cannot be calculated when X are the same.”)
}
m = (y2-y1)/(x2-x1)
return(m)
}
Error: unexpected '}' in "}"
> slope <- function(x1,y1,x2,y2) {
+
+ missingvars=c(missing(x1),missing(y1),missing(x2),missing(y2))
+
+ if(sum(missingvars)>0){
+ stop(paste(“Values were not provided (denoted by TRUE): x1 -“,missingvars[1],”, y1 -“,missingvars[2],”, x2 -“,missingvars[3],”,
Error: unexpected input in:
"if(sum(missingvars)>0){
stop(paste(�"
> Y2 -“,missingvars[4],sep=“”))
Error: unexpected input in "Y2 -�"
> }
Error: unexpected '}' in "}"
I suspect that your text editor is writing the wrong quotation marks. In the code you have the quotation marks “ and ” (look like 66 and 99) but R can only interpret " (looks like ||). If you replace the “ and ” with ", then code doesn't throw any errors for me.
Using a plain text editor like Notepad (Windows), Gedit (Linux) or TextEdit (MacOS) or an IDE like RStudio to write your code should prevent this from happening to you in the future.
I'm working on an R package where the same input-checking functions are called by multiple "actual" functions that are exported to users. If I use a simple stop() call, the error message is going to say that an error occurred in the input-checking function, which is not that useful...
I thought I'd get around this by wrapping the call to the input-checking function inside a tryCatch(), and then handling the error in the outer function. This does mostly what I want, but doesn't quite give the output that I'd like. The closest I've come is the following:
f <- function(i) {
tryCatch({
check_input(i)
}, error = function(e) stop("in f: ", e$message, call. = FALSE)
)
}
check_input <- function(i) {
if(i < 0)
stop("i is negative, value given was ", i)
}
f(-1)
# Error: in f: i is negative, value given was -1
Ideally, I'd like the error message to be
Error in f: i is negative, value given was -1
, which would be the case if stop were called within f() instead of check_input().
Here's how you can grab the name of the function from the call stack and paste it in to the error message
f <- function(i) {
check_input(i)
}
g <- function(i) {
check_input(i)
}
check_input <- function(i, from=deparse(sys.calls()[[sys.nframe()-1]][[1]])) {
getmsg <- function(m) ifelse(!is.null(from), paste0("in ", from, ": ", m), m)
if(i < 0)
stop(getmsg(paste0("i is negative, value given was ", i)), call. = FALSE)
}
f(-1)
# Error: in f: i is negative, value given was -1
g(-1)
# Error: in g: i is negative, value given was -1
You could also call check_input(i, from="otherfunction") to show whatever function name you want or check_input(i, from=NULL) to suppress the function name.
dbl_var<-lambda
probpois <-function(x, lambda){
#e<-2.718
prob<-exp(((lambda^x)*(2.718^lambda))/factorial(x))
retun(prob)
}
a<-readline((prompt="Enter a value: "))
b<-readline((prompt="Enter b value: "))
lambda<-readline((prompt="Enter lambda value: "))
x<-(a:b)
while (x<b || x>a ) {
dpois(x ,lambda)
}
ı want to write calculate poisson distribution program in R studio. This program will an error. >>
"Error in dpois(x, lambda) : Non-numeric argument to mathematical function"
Console:
> dbl_var<-lambda
> probpois <-function(x, lambda){
+
+ #e<-2.718
+ prob<-exp(((lambda^x)*(2.718^lambda))/factorial(x))
+
+ retun(prob)
+
+
+
+ }
> a<-readline((prompt="Enter a value: "))
Enter a value: 1
> b<-readline((prompt="Enter b value: "))
Enter b value: 4
> lambda<-readline((prompt="Enter lambda value: "))
Enter lambda value: 1.5
> x<-(a:b)
> while (x<b || x>a ) {
+
+ dpois(x ,lambda)
+
+ }
Error in dpois(x, lambda) : Non-numeric argument to mathematical function
>
readline always returns whatever the user types as character data. Wrap your readline statements in as.numeric, like so:
a <- as.numeric(readline(prompt="Enter a value: "))
In addition, I'm not entirely sure of your goal here, but the while loop is being used incorrectly. In fact it seems entirely unnecessary, since dpois can simply be given the four values you've calculated for x.
I continue to get an error on my function, possibly I'm overlooking something simple. I cannot run the code without getting an error when applying the function.
k.nn <- function(k,p1,p) {
k > 0
K <-length(k)
p=matrix()
for (i in p) {
matrix <- cbind(p,p1[1],p1[2])
d <- sqrt((matrix[,1]-matrix[,3])^2+(matrix[,2]-matrix[,4])^2)
}
##use the sort function to find the smallest distance from 1:k and return all nearest k values
sort.d <- function(x) { #implement bubble sort
N=length(x)
N>0
c=class(x)
for (n in length(x):2) { #distinguish the last term in the vector, name it, much be of x length, consists an error of length 1. Error if you compute n in length(x):1, cover length of 1
if(length(x)<2)
return(x)
for (m in 1:(n - 1)) { #distinguish the first term in the vector, name it
if(x[m]>x[m + 1]) { #begin comparing each term to neighboring term
swap<-x[m]
x[m]<-x[m + 1]
x[m + 1]<-swap
}
}
}
return(x)
}
sorted=sort.d(d)
for (n in k){
print(sorted[1:k])}
}
p=matrix(c(6.9,7.6,7.1,.4,6.2,1.8,2.5,2.3,5.7,6.9,.9,4.4,5.2,1.9,.6,7.4,1.2,6.6,3.3,4.9),nrow=10,ncol=2) #given matrix
p1=c(6,6)
k=3 nn.3=k.nn(k,p1,p)
print(nn.3)
There's a missing carriage return or ";" in the penultimate line that is throwing an error. If you remove tha last line so that you can use traceback() it tells you that k.nn throws a " subscript out of bounds" error when a matrix index is 4.
Debugging 101 tells you to put in print functions to see where the function fails and putting in a print after
c=class(x) ; print(c)
... ives you a result, but putting another one in the sort.d function does not get executed. Looking at the code upstream from that point we see:
d <- sqrt((matrix[,1]-matrix[,3])^2+(matrix[,2]-matrix[,4])^2)
So looking at the function and the matrix you have given, ... my guess is that you passed a two-column matrix to a function that expected a four-column argument.