Using matlab mex files with R [closed] - r

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
I would want to use a certain function written in c for use with matlab, with the mex system.
Any ideas/experience about how to port this to R?
Since comments say this is to vague, some detail. There is a certain well-written matlab function I need for use with R (All the rest of my code are in R). This function have an empty
m-file, only documentation. All the work is in the corresponding .c-file, but this is c-code written specifically for use with matlab, so contains a lot of calls to mx-functions, which I understand are hooks into the inner life of matlab.
The suggestion to communicate via data files will not work, I need this function within an iterative algorithm.

This pdf file might give you some tips. I don't have any experience with using C with R, but it seems possible.
One method not mentioned in the file, which I like to use sometimes, is to simply use your MATLAB/R program to write out a text file of numbers to be input into your C program (e.g. c_data.dat), and then to use a system command to call a standalone C program which processes the data file, before spewing out some output numbers (e.g. c_output.dat). Then get your MATLAB/R program to read in the data from the file.
In MATLAB, this might look like this:
%# custom function to write to file
function_write_data( my_data_matrix, 'c_data.dat');
%# call the c program
system('./c_program c_data.dat c_output.dat');
%# read the results
my_results_matrix = function_read_data('c_output.dat');
(I'm not confident about the R syntax - you'll have to work that out for yourself!)
The advantage of this in your scenario is that you could use exactly the same C program for both R and MATLAB. You have to write the read/write functions though, and for some scenarios the read/write overheads could be prohibitive (i.e not give any noticeable increase in speed)..

Related

In R, what does function() mean if you're NOT defining a function? [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 9 years ago.
I'm using R for the first time to learn how to write classification algorithms. I'm mainly learning from the O'reilly book Machine Learning for Hackers.
There's tons of info in R, R documentation, ebooks, and my book about writing functions. However, there are snippets of code in the book that use the word function in a way that does not seem to conform to my understanding of the use of function. For example:
all.spam <- sapply(spam.docs, function(p) get.msg(paste(spam.path,p,sep="")))
where get.msg is a user defined function, sapply is from base R, and p is not defined anywhere in their code. There's no explanation in the book and I also tried searching and asking other R users in my company to no avail.
Any insight? Thanks in advance. By the way, in RStudio the word function is bright blue when used in this way.
In R, you define anonymous and named functions using the same syntax, so your example:
all.spam <- sapply(spam.docs, function(p) get.msg(paste(spam.path,p,sep="")))
Is equivalent to:
my_spam_func <- function(p) {
get.msg(paste(spam.path, p, sep=""))
}
all.spam2 <- sapply(spam.docs, my_spam_func)

generate D3 html pages with R? [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
I would like to generate an interactive plot with R, for example an xyplot that shows information of the data points on mouseover, and I've read about D3. I would like to be able to write R code to generate the D3 html pages automatically. Is there any R package able to do that?
I've seen there is a r2d3 package on github but I am not sure how far that project is. Any ideas?
Looks to be fairly early in its development: https://github.com/hadley/r2d3. Hadley generally makes announcements when things are ready to test and then puts his packages on CRAN when they are ready for prime-time. (There's also confusion in that some versions of D3 on Github are named r2d3 when they have nothing to do with R): https://www.google.com/url?q=https://github.com/mhemesath/r2d3&sa=U&ei=Ig-YULraMueimQWxhYHgCA&ved=0CAwQFjAC&client=internal-uds-cse&usg=AFQjCNGe9p5iKH6deoHDpd2yKHJds8Qdow

How to perform Particle Swarm Optimization [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
We have been asked to do 5 or 6 iterations of particle swarm optimisation by hand for homework, but i don't really understand how and we were given no examples.
Would it be possible for someone to do the first run through for me so I can see how it works?
Explanations as each step would be fantastic.
Consider an illustrative example of a particle swarm optimisation system composed of three particles and Vmax = 10. To facilitate calculation, we will ignore the fact that r1 and r2 are random numbers and fix them to 0.5 for this exercise. The space of solutions is the two dimensional real valued space R2 and the current state of the swarm is as follows:
Position of particles: x1 = (5,5); x2 = (8,3); x3 = (6,7);
Individual best positions: x∗1 = (5,5); x∗2 = (7,3); x∗3 = (5,6);
Social best position: x∗ = (5,5);
Velocities: v1 = (2,2); v2 = (3,3); v3 = (4,4).
"I don't really understand how and we were given no examples". Let me add a little bit of critique to this sentence. If you're not given any examples it probably means you should be looking for examples for yourself. Have you even put "particle swarm optimization" into Google and look at some of the results? Do you expect everything in your study to be given to you?
There are many resources that explain the working of particle swarm optimization such as wikipedia, Google Scholar, Scholarpedia, or a dedicated website to PSO. The original paper is from Kennedy and Eberhart 1995 and is the top result in the scholar search. Also there are frameworks where PSO is implemented and where you can look at how it works like HeuristicLab. It's an opportunity to explore this topic.

Calling one entry at a time from a dataset in order [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
I have a simple data set (lets just call it data) containing 100 values and i wish to call them one at a time for entry into an equation (i cant post this particular equation here).
The problem im having is that from the equation there needs to be two outputs (probability), and when i call the data set directly, it does not give the desired output. Entering the numbers manually from the dataset gives the correct output (checked manually).
There are two streams of input as evidenced by the two outputs, is there something simple im missing regarding the input of a simple dataset?
EDIT the dataset which just a sample of 100 numbers needs to be entered in the equation i have. another value is to be subtracted from each entry in the dataset so unfortunately the solution below wont work.
the equation has two main sets of information feeding it in the form of two means, and other global parameters. From this, two prob values are calculated, one for each option.
However linking the above 100 value matrix results in 100 values being created which make no sense anyway. Putting in the first value manually from the matrix into the equation results in the correct output - 2 values (checked by hand)
Thanks
If you have a vector of 100 values and a function that returns two values for each invocation then this should work:
res <- sapply( vec, f)
You should get either a list or a two column matrix depending on what type of returned object the f-function gives you.

Data visualization with R igraph package [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
I'm doing a homework on data mining. Now I have a data set which stands for acquaintance network in social network. I want to visualize this network. In this network, vertex stands for user and edge stands for acquaintance relationship. Now I use igraph package for R to achieve my goal. However, my data set is too large with more than 100,000 vertexes and more than 1000,000 edges, which causes the generated graph not clear. In fact, the graph is totally unrecognized. How can I get a clear graph using igraph package? Or, which software can complete this task?
ps: my data set likes that:
100 20
10 2
2 4
each line stands for a relationship between two people
Thank you in advance!
In addition to Cytoscape, you might want to check out Gephi.
Another (quite different) approach to visualizing networks is by using hive plots -- worth checking out, too!
If you want an answer on this forum, you will need to provide at least a small amount of the data that you want to analyse and many people will help you get off to a good start.
You may also want to consider using an application, such as Cytoscape, that is designed for the analysis of networks. It may be easier to start your visualisation and exploration of this large network.

Resources