Explanation of R: Options(Expressions=) to Non-Computer Scientists

plot thousands lines with ggplot

I think you need something like this...

data<-data.frame(y=rnorm(95040))
data$x <- 1:15
data$group <- (1:nrow(data)) %/% 15
p<-ggplot(data=data,aes(x=x,y=y,group=group))+geom_line(alpha=I(1/7),size=1)

How to make the previous loop value effective in current iteration in apply ( apply -family version of a for loop)?

AR(1) process:

$$ y_t= \alpha + \rho y_{t-1} + \nu_t, \hspace{1cm} t=2,...,n+1 $$
$$ (\nu_t \sim N(0,1)) $$

(t can be finalized at t=n if one includes initial $$ y_1 $$ value in N obs)

By math'l equivalent:
$$ y_t=ρ^t (\frac{y_1}{\rho} + \sum_{j=2}^{t} \frac{\alpha+\nu_{j}}{\rho^j} ) (t=2,…,n; y_1 is prespecified) $$

Solution 1 (Henry's elegance):

rm(list = ls()) # Clear workspace by deleting all objects
N <- 500; ro <- 0.6; a <- 1
set.seed(1)
v <- ts(rnorm(N,0,1))
y <- ts(rep(0,N)) # y[1]:=0 defined
y[-1] <- ro^(2:N) * (y[1]/ro + cumsum((a+v[-1]) / ro^(2:N)))
y # yields exactly the same values in the "for" loop
(mean(y)) # 2.549763

$$\rho^t\rightarrow 0$$ as $$ t \rightarrow \infty $$, even very early: 0.6^1458=4.940656e-324
; 0.6^1459=0
This blows the model. When N=1400, it gives y[1388]=1.114; 1389...1396:Inf; 1397...1400:NaN

Solution 2 (Nate's formalism):

rm(list = ls()) #  Clear workspace by deleting all objects
my.fct <- function(t, initial.value){
if(t == 1){ return(initial.value) }
if(t > 1){
vec <- my.fct(t-1, initial.value)
val <- vec[length(vec)]
newval <- a + ro*val + v[t]
newvec <- c(vec, newval)
return(newvec)
}
}

N <- 500; ro <- 0.6; a <- 1
set.seed(1)
v <- ts(rnorm(N,0,1))
my.fct(500,0) # yields exactly the same values in the "for" loop
mean(my.fct(500,0)) # 2.549763

N=833 works. N=834 gives Error: evaluation nested too deeply: infinite recursion / options(expressions=)? with the default options(expressions=5000).

(I could not figure out how to make math and text in-line in latex above)

In programming, what is an expression?

In Javascript:

An expression is any valid unit of code that resolves to a value.

Conceptually, there are two types of expressions: those that assign a value to a variable and those that simply have a value.

The expression x = 7 is an example of the first type.

This expression uses the = operator to assign the value seven to the variable x. The expression itself evaluates to seven.

The code 3 + 4 is an example of the second expression type.

This expression uses the + operator to add three and four together without assigning the result, seven, to a variable.

JavaScript has the following expression categories:

  • Arithmetic: evaluates to a number, for example 3.14159. (Generally uses arithmetic operators.)
  • String: evaluates to a character string, for example, "Fred" or "234". (Generally uses string operators.)
  • Logical: evaluates to true or false. (Often involves logical operators.)
  • Object: evaluates to an object. (See special operators for various ones that evaluate to objects.)"

Source: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Expressions_and_Operators

here is Microsoft's explanation of expressions in .NET

Difference between a Computer Scientist and Computer programmer

Computer science is an academic field. It's a little like studying mathematics. It's studying and researching algorithms, data structures, and similar.

Computer Programmers write programs; the term tends to be used to describe people in industry, although of course computer scientists write programs too.

An archetypal computer scientist would be someone like Don Knuth. His work on algorithms is legendary.

An archetypal computer programmer might be Jeff Atwood. We're using his site right now.

What's the difference between an argument and a parameter?

A parameter is a variable in a method definition. When a method is called, the arguments are the data you pass into the method's parameters.

public void MyMethod(string myParam) { }

...

string myArg1 = "this is my argument";
myClass.MyMethod(myArg1);


Related Topics



Leave a reply



Submit