logo
down
shadow

Generating simulation data based on a specified distribution


Generating simulation data based on a specified distribution

By : tmacrina
Date : November 22 2020, 01:01 AM
wish helps you I hate to be that guy who answers their own post but I just found the same example used on page 15 here in the "rpart" documentation. I'll continue writing up the answer but I'll delete the question at the end of the day unless I hear differently from the community. Sorry about my oversight.
code :
# the data for this example is generated by a malfunctioning calculator 
set.seed(1953) # An auspicious year
n <- 200
y <- rep(0:9, length=200)
temp <- c(1,1,1,0,1,1,1,
          0,0,1,0,0,1,0,
          1,0,1,1,1,0,1,
          1,0,1,1,0,1,1,
          0,1,1,1,0,1,0,
          1,1,0,1,0,1,1,
          0,1,0,1,1,1,1,
          1,0,1,0,0,1,0,
          1,1,1,1,1,1,1,
          1,1,1,1,0,1,0)

# The true light pattern 0-9
lights <- matrix(temp, 10, 7, byrow = TRUE)
# Noisy lights
temp1 <- matrix(rbinom(n*7, 1, 0.9), n, 7)
temp1 <- ifelse(lights[y+1, ] == 1, temp1, 1-temp1)
# Random lights
temp2 <- matrix(rbinom(n*17, 1, 0.5), n, 17)
x <- cbind(temp1, temp2)


Share : facebook icon twitter icon
Generating a set random list of integers based on a distribution

Generating a set random list of integers based on a distribution


By : Agus Fernandez
Date : March 29 2020, 07:55 AM
like below fixes the issue This generates all the possible combinations that sum to 10 and selects a random one
code :
from itertools import product
from random import choice
n=10
m=5
finalList = choice([x for x in product(*[range(2*n/m+1)]*m) if sum(x) == 10])
finalList = choice([x for x in product(*[range(5)]*5) if sum(x) == 10])
finalList = choice([x for x in product(range(5),range(5),range(5),range(5),range(5)) if sum(x) == 10])
Generating a random number based off normal distribution in matlab

Generating a random number based off normal distribution in matlab


By : Kamran Chohan
Date : March 29 2020, 07:55 AM
I hope this helps you . You can use the built-in randn function which yields random numbers pulled from a standard normal distribution with a zero mean and a standard deviation of 1. To alter this distribution, you can multiply the output of randn by your desired standard deviation and then add your desired mean.
code :
% Define the distribution that you'd like to get
mu = 2.5;
sigma = 2.0;

% You can any size matrix of values
sz = [10000 1];

value = (randn(sz) * sigma) + mu;

%   mean(value)
%       2.4696
%
%   std(value)
%       1.9939
value = (randn * sigma) + mu;
Monte Carlo Simulation (Triangular Distribution) across rows of CSV cost data

Monte Carlo Simulation (Triangular Distribution) across rows of CSV cost data


By : Jack Adams
Date : March 29 2020, 07:55 AM
it should still fix some issue Rather then doing it directly from the CSV you're better off reading the CSV into a matrix, creating your total cost matrix, and then running the for loop to simulate the values.
For example in this way:
code :
runs<-1000 #Set number of runs
Info_costs<- read.csv( "Your_file_name.csv") #Read in the information
Total_cost_items<-matrix(,nrow=runs,ncol=length(Info_costs$Item)) #Create an empty matrix to contain your simulations
for (i in 1:length(Info_costs$Item))
   {Total_cost_items[,i]<-rtriangle(n=runs,Info_costs$l[i],Info_costs$u[i],Info_costs$c[i]) } 
#Fill the matrix
Total_cost_items<-data.frame(Total_cost_items, rowSums(Total_cost_items)) #append the matrix with the row sums
simulation of normal distribution data contaiminated with outliers

simulation of normal distribution data contaiminated with outliers


By : Nasga
Date : March 29 2020, 07:55 AM
hop of those help? I need to simulate 1000 sets of normal distribution(each 60 subgroups, n=5) by using r programming. Each set of normal distribution is contaiminated with 4 outliers(more than 1.5 IQR). can anyone help? , A very simple approach to create a data.frame with a few outliers :
code :
# Create a vector with normally distributed values and a few outliers
# N - Number of random values
# n.out - number of outliers
my.rnorm <- function(N, num.out, mean=0, sd=1){
  x <- rnorm(N, mean = mean, sd = sd)
  ind <- sample(1:N, num.out, replace=FALSE )
  x[ind] <- (abs(x[ind]) + 3*sd) * sign(x[ind])
  x
}

N=60
num.out = 4
df <- data.frame( col1 = my.rnorm(N, num.out),
                  col2 = my.rnorm(N, num.out),
                  col3 = my.rnorm(N, num.out),
                  col4 = my.rnorm(N, num.out),
                  col5 = my.rnorm(N, num.out))
Generating synthetic data with Gaussian distribution

Generating synthetic data with Gaussian distribution


By : xDeveloper
Date : March 29 2020, 07:55 AM
To fix this issue You have cut off the data at +/- 0.1. A normalised Gausian distribution only 'looks Gaussian' if you look over the range approximately +/- 3. Try this:
code :
import numpy as np

def generate_gaussian(size=1000, lb=-3, up=3):
    data = np.random.randn(5000)
    data = data[(data <= up) & (data >= lb)][:size]
    return data

np.random.seed(1234)
base = generate_gaussian()
background_pos = base + 5
background_neg = base + 15
Related Posts Related Posts :
  • Error Handling with Lapply
  • data.table - split multiple columns
  • How to compute the overall mean for several files in R?
  • R: Graph Plotting: Subscripts in the legend like LaTeX
  • Restructuring data in R
  • Distance of pointsfrom cluster centers after K means clustering
  • R incorrect value of date function
  • Package "Imports" not loading in R development package
  • r - run a user defined function several times by taking column elements as parameters
  • Create input$selection to subset data AND radiobuttons to choose plot type in Shiny
  • Generate crosstabulations from dataframe of categorical variables in survey
  • Restructure output of R summary function
  • New behavior in data.table? .N / something with `by` (calculate proportion)
  • search certain number vector in R
  • R version doesn't support quartz graphic device - RStudio won't plot
  • Referencing a function parameter in R
  • How to synchronize signals using a cross-correlation and FFT in R?
  • Plotting coefficients and corresponding confidence intervals
  • passing expressions to curve() within a function
  • More effective merging of matched column with duplicates in data.table
  • Easy way to export multiple data.frame to multiple Excel worksheets
  • R Foreach Iterator - Walkforward
  • Table format and output in R
  • Restructuring data and duplicating rows in R
  • use ggplot2 to plot two lines with ribbons
  • how to plot a graph on lattice with two different colors
  • How can I keep a date formatted in R using sqldf?
  • Joining list of data frames in R
  • Subset data in R
  • R: How to avoid 2 'for' loops in R in this function
  • + signs appearing in console in R
  • how to create a dataframe form a lists within a list in R
  • Best way to combine and keep columns
  • Using identify and attach in a function
  • Apply function to each submatrix
  • How to assign regular strings for quarterly and monthly observation labels to the row names of a data frame?
  • Adjust hexbin legend breaks
  • Different lowess curves in plot and qplot in R
  • Extract words only with R
  • switch case: several equivalent cases expressions in r
  • R data.table to calculate a formula using a column as a variable across levels of a factor
  • how to create a line plot frame in ggplot2
  • Subset by row number within magrittr chain
  • GGPLOT - two curves in one plot in B_W mode
  • How can i build a for function for matrix?
  • How to Word-like-merge columns or rows of a data frame for displaying purposes in R?
  • How to keep all rows of a table on the same page in RMarkdown when rendering a PDF file?
  • Add transparency to GoogleMap plot (loa package)
  • replace a column in a dataframe given a corresponding vector in r
  • subset data and plot this subsetted data with Shiny
  • How can i count the numbers in every subset?
  • Request URL failed/timeout in R
  • IF then do end equivalent in r... EDIT: in dplyr
  • how to check if each cells (list) of a column of a dataframe are unique in R?
  • Column widths not aligned with table data in pander tables sent from R with sendmailr
  • Getting the value of a Variable which has its name based upon another variable (in R)
  • Web Page Click Through Heat Map using R
  • Add a label to map at each leg start
  • R Caret Random Forest view miss-classified
  • For each line of a Data.Frame, get the column name where a value is TRUE
  • shadow
    Privacy Policy - Terms - Contact Us © ourworld-yourmove.org