Tutorial for R vectorised programming [closed] - r

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking us to recommend or find a book, tool, software library, tutorial or other off-site resource are off-topic for Stack Overflow as they tend to attract opinionated answers and spam. Instead, describe the problem and what has been done so far to solve it.
Closed 8 years ago.
Improve this question
Can someone point me to a good tutorial for using vectorized programming methods in R.
At the moment it feels very magical to me and I don't really understand what R's doing.
Especially with regards to if statements and addressing values neighboring rows.

I am not aware of a specific tutorial on vectorized programming for R.
I have a few versions of my Intro to High-Performance Computing with R tutorial here. The benefit of vectorized code is mentioned in the context of profiling, but it doesn't explain 'how to vectorize code'. I think that is hard to teach -- my best bet would be to read other people's code. Pick a few packages from CRAN and poke around.
Other than that, decent general purpose documents about R and programming in R are e.g. Pat Burns S Poetry and the more recent R Inferno.

The best way to learn this is to experiment with it since it's an interactive environment, and it's easy to create dummy data.
With regards to making comparisons in neighboring rows, the easiest thing to do is to use the - operator (which means "exclude this index") to eliminate the first and last row, as in this example:
a <- 1:10
a[5] <- 0
a[-1] > a[-length(a)] # compare each row with the preceding value
If you want to do an if statement, you have two options:
1) The if command only evaluates one value, so you need to ensure that it evaluates to TRUE/FALSE (e.g. use the all or any functions):
if(all(a[-1] > a[-length(a)])) {
print("each row is incrementing")
} else {
print(paste("the",which(c(FALSE, a[-1] <= a[-length(a)])),"th row isn't incrementing"))
}
2) You can do a vectorized if statement with the ifelse function. See help("ifelse") for more details. Here's an example:
ifelse(a[-1] > a[-length(a)], 1, 0)

Related

Properly reading recursive functions [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 5 years ago.
Improve this question
I currently teach myself programming and I came to a point where I was introduced to the recursive functions. I understand the basic principle behind them but whenever I try to read a code which contains recursive functions I have tough times tracing them. Honestly, If I don't write down the whole function on a paper and manually follow it I can't really understand what it does, I can't follow it mentally is what I am saying. Can you give some tips on how to do it mentally? I am pretty much average intelligent ( IQ 117 ), maybe that's the problem? Thanks in advance.
I think this is an excellent question. Many people make the mistake of trying to follow the call-stack of a recursive function, which is the wrong approach.
I find the only way is to look at what is written and ask yourself whether it is logically correct and whether it will end. If those things are true, it is going to work and you don't really need to understand how.
In more detail:
Are all possible input values considered?
Are the statements true in each case?
Is there a (are there) value(s) at which point the function will not recurse?
Do all remaining input values ultimately lead to that value?
For example (pseudo code):
factorial(n):
if (n < 0) throw InvalidArgument
if (n == 0) return 1
else return factorial(n - 1) * n
All input values are covered. The statements are true. There is a value at which it does not recurse (<=0) and the all remaining values reduce by 1 until the exit value is reached. Therefore it works.
You may of course make mistakes in your thinking. A good IQ does help here, and you may need at some point to resort to tracing or debugging to check your assumptions and find the error in your logic, but this should not be the first approach.
That is the beauty of recursive solutions: they tend to be statements of fact rather than procedural instructions!
I think IQ does not matter. Anyway, i think it's all about practice. Try to write some recursive functions. And after alot of practice and experience you will find it more easy to understand.
There is some good ideas.
Good luck.

Phase diagram in R [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
This is a borderline question for Stackoverflow, I know, but I am looking for a package. If I can't get an answer here I will transfer to https://stats.stackexchange.com/. I am looking for a R package or a method to create a phase diagram. This means I have e.g. two variables, like air pressure and temperature, and a binary variable (to make it easier) indicating if the substance is liquid or frozen. Below you find a typical example of a phase diagram. I need to estimate the transition borders or something however just in a case with two groups. Every hint is appreciated.
I think about the closet you will get is function diagram in package CHNOSZ. There's a lot to read about in this package and it has some nice vignettes. But, it seems to calculate phase diagrams from first principles or theory. Perhaps if you look at the code for diagram you can figure out a fairly easy way to use your empirical data.

Distributed computing in R [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking us to recommend or find a tool, library or favorite off-site resource are off-topic for Stack Overflow as they tend to attract opinionated answers and spam. Instead, describe the problem and what has been done so far to solve it.
Closed 9 years ago.
Improve this question
I worked out an estimator, and I would like check its performance by doing simulation studies with R. I want to repeat the experiment for 500 times. Unfortunately, the computation involved in the estimator is sophisticated. Each replication will take 15 minutes on my desktop. I am looking for some distributed computation approaches with R. How should I start? I googled this topic. There are some many posts about this.
I'd suggest starting with the foreach package. If you're using mac or linux the following is the simplest way to do parallel computing:
# First we register a parallel backend. This will work on mac and linux.
# Windows is more complicated, try the `snow` package.
library(doMC)
registerDoMC(cores=4) # substitute for number of cores you want to run on.
# now we can run things in parallel using foreach
foreach (i = 1:4) %dopar% {
# What's in here will run on a separate core for each iteration.
}
You should read the vignette for foreach as it's quite different to for (especially nested loops) and it is also quite powerful for combining results at the end and returning them.
First step with any R problem as broad as this should be checking the CRAN Task Views. Oh look:
http://cran.r-project.org/web/views/HighPerformanceComputing.html
Note that StackOverflow isn't really the place for asking broad questions that are best answered with 'read that documentation over there' or 'why don't you try using tool X?'

Free software for mathematical modeling. Is R a good one? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking us to recommend or find a tool, library or favorite off-site resource are off-topic for Stack Overflow as they tend to attract opinionated answers and spam. Instead, describe the problem and what has been done so far to solve it.
Closed 9 years ago.
Improve this question
I am looking for a free software for mathematical modeling.
Here is a list of things I might be willing to achieve with this software: Integrating functions, solving differential equations, graph theory, analyzing infinite series, local stability analysis, Taylor series, get eigenvectors, compute the long term behaviour of a system of equations, etc...
Here is a related SE post. I am surprised that nobody is suggesting R. I am currently a R user and already use R for graph theory. Therefore I would appreciate to use R also to make other mathematical modeling. Is R less efficient that Sage, SimPy, Mathematica and others for mathematical modeling? Why? Do you know a manual providing exaplanation for how to make mathematical modeling with R?
Thank you
Sounds like R is your first way to go. It does not make to good sense to compare R with any other tool in such a braod way you are asking for. R packages differ largely in efficiency, some are in fact C tools while others are written in the R language. As a start R can hardly be any wrong and is free.
Matlab might be a stable alternative, Julia is rising but still pre alpha.

Simple MCMC Bayesian Inference in R [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking us to recommend or find a tool, library or favorite off-site resource are off-topic for Stack Overflow as they tend to attract opinionated answers and spam. Instead, describe the problem and what has been done so far to solve it.
Closed 9 years ago.
Improve this question
I'm looking for a simple MCMC Bayesian network Inference function/package in R. Essentially, I just want a function that accepts the matrix containing my samples x my variables ( + optional parameters like burn-in and iteration counts) and returns the adjacency matrix of the inferred network.
I had been using the Matlab toolkit "BayesNet", which offers a simple 'learn_struct_mcmc' function which offers most of what I'm looking for. I'm looking for an equivalent in R.
I've been looking through the packages in http://cran.r-project.org/web/views/Bayesian.html, but haven't seen anything that quite does what I'm looking for. I wasn't trained as a statistician, and many of the packages I've looked at on that list either lack documentation or have more complicated statistics than I'm comfortable wiring together myself. I just need a simple function with "reasonable" defaults to get started.
Bonus points for something that leverages Rmpi or snow.
This gave me 132 possible relevant functions.
library(sos)
findFn("bayesian network")
How about this package.
http://cran.r-project.org/web/packages/MCMCpack/index.html
The closest thing to what I had in mind that I've found is the hc() function in the blearn package. They have a variety of other Bayesian network inference functions, as well, some of which can use snow.

Resources