# Tagged Questions

R is a free, open source programming language and software environment for statistical computing and graphics.

19 views

### Forecasting function using auto.arima for 15 samples of data

I have a workbook with data sampled. i have multiple samples based on date range. one sample starts from january, where other can start from february and so on. I need to build forecasting function ...
51 views

### Finding travel distance between airports

I have 2 nested for loops which i want to get rid of. Any thoughts? I am calculating distance between cities based on their longitude and latitude. There is a custom function ...
25 views

### Matrix to calculate effects using delta method

I have written some code to calculate effects using the Delta method. I have a dataframe dpcp with variables x1, ...
34 views

### Rolling z-score function in R

I have written a rolling z-score function myself. Is my implementation of rolling z-score function correct? ...
666 views

### Benchmarks of various scientific programming languages for theoretical modelling

For a theoretical modelling course for biology students, I am trying to decide which would be the best technical programming language for doing evolutionary simulations in terms of elegance and ...
40 views

### Row-wise mean imputation in R

Someone on SO asked how to fill in the NA's with row means. His example code was: ...
22 views

### Profit sensitivity analysis in base R

Using base R, I've conducted a simple profit sensitivity analysis, i.e. measuring the impact on profit if there is a change in price, variable cost per sale, unit sales or fixed costs assuming that ...
40 views

### Solving for a seed value in R

I'm trying to reproduce a gbm model which was estimated without a set.seed value. To do so I need to determine what seed was ...
46 views

### Fastest way for multiplying a matrix to a matrix [migrated]

Let A and B be n$\times$n matrices. In my code this multiplication takes one minute, is there any way or packages that can make this calculation faster? I only found a command in R for a matrix by ...
65 views

### Predict new ratings for each user based on their pearson correlation with other users

I am new to R and programming. I have a set of ratings for 45000 users and 40 odd movies. I need to predict new ratings for each user based on their pearson correlation with other users. I also need ...
72 views

### Counting matrix elements that have “4” or “5” as a neighbor

I have a matrix (500x500) of integers. For each entry, I need to look at its surrounding neighbours (so 8 elements) and determine what the integers are, and run a function on these integers. Here ...
30 views

### Roots of Unity in R

I have a paper on the definition of (i) available here. I was curious about the simultaneous real numbers associated with the roots of unity, and noticed there was not a routine in R. I have this ...
36 views

### Dataframe helper in R

In order to have a "unified" (I realize how ambitious this actually is) syntax when working on dataframes, I wrote the following functions that is a general purpose dataframe set of tools in R. ...
34 views

### Function for most frequent level of string or factor categorical variable in R

I have a function that finds the most frequent level of string and factor variables. This is used in a data.table aggregation on Big Data, for non-numeric columns. ...
27 views

### Ordering data.frames elegantly by factor

I have a mock_Data: ...
45 views

### Efficient date-based lookup function in R

Right now this function takes an excessively long time to run, please let me know how to make it more efficient. lunarCal is 144x7 and the ...
17 views

### Applying kmodes on every “column wise subset” of a dataframe

I want to apply kmodes for 2 clusters on every possible combination of columns from a dataframe. Finally, I want to compare the clusters with another column that ...
86 views

### Random Chutes and Ladders Board Generator (June 2016 Community Challenge)

This is my submission for the June 2016 Community Challenge. It is R code that takes as input the number of squares on the board, the desired number of chutes and ladders, and the desired total sum of ...
43 views

### Calculate profit and loss for trades

I have this script which calculates profit and loss of trades. It works fine but I think it can be improved. It will be great to get rid of the for loops at least to make the code look compact. The ...
33 views

### Generate sample of permuted sequences with non-repeating elements

I have a very long sequence of letters (>20000 elements long). I want to permute them so that I have another sequence using the same letters but in a different order but still with the property of no ...
28 views

### Estimate Hidden Markov Model with both continuous and discrete observations, possible missing data

My code estimates a Hidden Markov Model (HMM) using the expectations-maximization (EM) algorithm. I followed https://www.princeton.edu/~rvan/orf557/hmm080728.pdf with modifications to allow for panel ...
36 views

### Matching dataframe rows for finding trade match

I need some help to refactor the script. I have a script which matches the buy and sell trades based on FIFO order. The initial dataframe looks like this: ...
33 views

### Self organizing maps

I have already asked similar question here, but I figured this place might be better on getting some actual implementation feedback. I tried to implement a simple SOM. You can see the training ...
57 views

### Look up value from one dataframe and find percent of values in another dataframe that are less than that

Is there a one-liner that can accomplish this same thing? Essentially I have two dataframes: one that has my raw data (data), and one that has values that I want to ...
46 views

### Numerical differentiation using the y-intercept

I have a routine for determining the derivative of a function using the y-intercept (B) to infer the finite difference step (h). ...
68 views

### Code for finding repeated entries with different data

project is the data frame. For the purpose of the code, HOUSE.NO is a column of the type character, and ...
96 views

### Rolling regressions in R

I want to run rolling regressions over each group and store the coefficient. The following works, but it's slow, since I have too many series and I want to run too many regressions for each group. ...
47 views

### Clinical decision tree using R

I'm trying to code this decision tree using R. I was wondering if there was a better way to go about this rather than using a ton of conditionals? ...
73 views

### Permutations with repetitions algorithm in R

Given a string of length n, print all permutation of the given string. The code bellow generates all permutations (with repetitions) for the given string and stores them in object ...
43 views

### Caching calculation results in R

Here is what I came to for calculation result caching/invalidating. What do you think? How to use: ...
74 views

### Filtering numeric vectors by proximity to any elements of another vector in R

I frequently want to filter and select elements from a long (10,000s to millions of elements) vector in R. Often, this vector is a column in a data.frame. I am ...
54 views

### Nested loops - Random Forest, multiple parameters

I'm writing a code which task is to grow Random Forest trees based on multiple parameters. In short: Firstly, I declare a data frame in which model parameters and some stats will be saved. Secondly, ...
60 views

The data: ...
53 views

### Cumulative transition matrix Markov Chain simulation

I have a cumulative transition matrix with probabilities for all the possible states from 1 to 5. Now the algorithm for simulating future states is following: the initial state is selected randomly, ...
55 views

### Achieve a dataframe from a dynamic list of lists

This code works for me, but I am trying to improve my coding and am looking for advice on how to improve it. Specifically I want to simplify this so that perhaps I can have a dynamically updating ...
61 views

### Generating time series for generator ON or OFF

Want to create time series for the nuclear power plant performance using the equation for Time to fail and time to repair for 8736 hours in the year so that I have the time series in hours when the ...
40 views

### Design a main function which determines which secondary function to be used

I am writing an R package. The package consists of a main function and lots of auxiliary functions that perform different calculations. The main function The idea behind the package is to make the ...
123 views

### Nonlinear Nonparametric Regression

I explain the nonlinear correlation coefficients in this paper and provided the code in this post. The nonlinear nonparametric regression is an extension of this work and its description can be found ...
39 views

### Performing several hundred raster multiplications

I have several hundred "A" rasters, numbered from 001_A.tif to 100_A.tif. I also have several hundred "B" rasters, numbered ...
37 views

### Naive-Bayes classifier, to be packaged in a function

I need to create a Naive-Bayes classifier. I have eight labels (S) stored in tumors object, and 20531 attributes (A), I have stored the P(S,A) in objects of name <...
37 views

### Complex Space Factorization

I have posted a discussion on Math.SE regarding visualizing the complex space and effects of factorization methods on that space. A quick link to the paper is available here. I have 3 routines in ...
102 views

### Correlation and Dependence

It is well known that 0 dependence implies 0 correlation, while 0 correlation does not imply 0 dependence. I have a paper here illustrating nonlinear correlation coefficients from relationship subsets ...
87 views

### Computing the distance and weight matrix of a graph

I would like to create an igraph object. First, I set up the size of igraph object — n, m, then create a random matrix. As a result matrix ...
45 views

I created a simple extension to handle JSON RPC 2.0 requests on flask I would like some feedback regarding usage here is an usage example: ...
135 views

### Convert A for loop in R to apply function

My goal is to iterate over a datamatrix, calculate mean, standard error of the mean, and then have a bar plot representing the 2 groups of my data matrix in R. My code below works as a for loop. ...
57 views

### UK postcode validation and format correction tool

I've been working on a regex-based UK postcode validation and format correction tool - with the aim of creating a list of postcodes that can be readily geocoded. The variety of postcode formats is ...
81 views

### Picking the top 5 results for each individual

For loops are the devil. I can't see a way to speed this up with apply or other more speedy functions though... ...
112 views

### Import data from XML files into data.frame

I have a number of XML files containing data I would like to analyse. Each XML contains data in a format similar to this: ...