logo
down
shadow

In R after generating a mvrnorm distribution, Y, what does Y[,1] do?


In R after generating a mvrnorm distribution, Y, what does Y[,1] do?

By : AJ Greene
Date : November 22 2020, 03:01 PM
Does that help You're sampling from a multivariate normal distribution. For example in the bivariate case
code :
x <- mvrnorm(
    n = 500, 
    mu = c(1, 2), 
    Sigma=matrix(c(4, 2, 2, 3), ncol = 2))
ggplot(data.frame(x1 = x[, 1], x2 = x[, 2]), aes(x1, x2)) + geom_density_2d()


Share : facebook icon twitter icon
Generating a probability distribution

Generating a probability distribution


By : SmallTownOutlaw
Date : March 29 2020, 07:55 AM
This might help you The task you are trying to accomplish is tantamount to drawing a random point from the N-dimensional unit simplex.
http://en.wikipedia.org/wiki/Simplex#Random_sampling might help you.
code :
public static double[] getArray(int n)
    {
        double a[] = new double[n];
        double s = 0.0d;
        Random random = new Random();
        for (int i = 0; i < n; i++)
        {
           a [i] = 1.0d - random.nextDouble();
           a [i] = -1 * Math.log(a[i]);
           s += a[i];
        }
        for (int i = 0; i < n; i++)
        {
           a [i] /= s;
        }
        return a;
    }
generating a log normal distribution using R

generating a log normal distribution using R


By : Katelynn
Date : March 29 2020, 07:55 AM
it helps some times In R you can use rlnorm but why don't you use numpy and do it directly in Python.
Look at this document: http://docs.scipy.org/doc/numpy/reference/generated/numpy.random.lognormal.html
R: can 'mu' or 'Sigma' be vectorized in MASS::mvrnorm() when generating bivariate normal samples?

R: can 'mu' or 'Sigma' be vectorized in MASS::mvrnorm() when generating bivariate normal samples?


By : Oscar Eduardo Plaza
Date : March 29 2020, 07:55 AM
I hope this helps you . Consider the data below, , basic solution
No, it can't be vectorized. Write a for loop.
code :
v <- matrix(0, n, 2)
for (i in 1:n) {
  sig11 <- sigma1[i]
  sig21 <- rho * sqrt(sig11)
  Sigma <- matrix(c(sig11, sig21, sig21, 1), 2)
  v[i, ] <- mvrnorm(1, c(0,0), Sigma)
  }
sig11 ^ 2                rho * sig11 * sig22
rho * sig11 * sig22      sig22 ^ 2
sig11          0
rho * sig22    sqrt(1 - rho ^ 2) * sig22
# mu1, mu2, sig11, sig22, rho can be
## length-n vectors
## scalars
## vectors than can be recycled to be length-n
birnorm <- function (n, mu1, mu2, sig11, sig22, rho) {
  x1 <- rnorm(n)
  x2 <- rnorm(n)
  z1 <- sig11 * x1 + mu1
  z2 <- rho * sig22 * x1 + sqrt(1 - rho ^ 2) * sig22 * x2 + mu2
  cbind(z1, z2)
  }
v <- birnorm(3, 0, 0, sqrt(sigma1), 1, 0.4)
mvrnorm2 <- function (n, mu, Sigma) {
  sig11 <- sqrt(Sigma[1])
  sig22 <- sqrt(Sigma[4])
  rho <- Sigma[2] / (sig11 * sig22)
  birnorm(n, mu[1], mu[2], sig11, sig22, rho)
  }
mu <- c(0,0)
Sigma <- matrix(c(1,0.5,0.5,1),2)
library(microbenchmark)
microbenchmark(mvrnorm(1000, mu, Sigma), mvrnorm2(1000, mu, Sigma))
how to use mvrnorm for multiple mean vector in R

how to use mvrnorm for multiple mean vector in R


By : user3459195
Date : March 29 2020, 07:55 AM
wish help you to fix your issue You can create a sequence to subset mu at every column and sig1 every 3 columns and pass it to mvrnorm function.
code :
t(sapply(seq_len(ncol(mu)), function(x) {
   ind <- (x - 1) * 3 
   MASS::mvrnorm(1,mu[,x],sig1[, (ind + 1):(ind + 3)])
}))

#       [,1]   [,2]   [,3]
#[1,] 1.0665 3.5964 4.4052
#[2,] 4.9421 9.7464 9.6109
Generating a binomial distribution around zero

Generating a binomial distribution around zero


By : Joachim
Date : March 29 2020, 07:55 AM
I wish this helpful for you What you're doing is fine if you want a "discretized" normal distribution centered around 0. If you want integer values, you should round n*p before subtracting.
But the limit of the binomial distribution is just the normal distribution when n becomes large and with p bounded away from 0 or 1. since n*p is not going to be an integer except for certain values, why not just use the normal distribution?
Related Posts Related Posts :
  • how to loop for division funciton in r
  • Why does ggplot not allow suppressing of messages generated by its geoms?
  • Download multiple excel files linked through urls in R
  • sparklyr : spark_apply function is not working in cluster mode
  • dplyr mutate - How do I pass one row as a function argument?
  • R selecting rows by conditions given in an external table
  • Native regex way to replace multiple leading chars with equal number spaces
  • stan - difficulty vectorizing
  • How to define a function that calls shiny functions?
  • How to count number of observations in a "n" dimensional range in R
  • Superimposing asymmetric t-distribution using ggplot2
  • Makefile to render all targets of all .Rmd files in directory
  • Authentication failure with rdrop2
  • DT data table display error
  • Issue when adding new rows (with nested dataframes within) to a dataframe
  • R-How to compare two dataframe and update list column value
  • Series vector for approximating pi
  • what is difference between "variance explained " in Random Forest and "merror" in XGBoost
  • R - Cast dataframe on unique rows - reshape2
  • ggplot2: plot correct proportions using geom_bar
  • Speedup query for R data.table - can this two-argument function be applied by group more quickly?
  • apply a function to several columns at once with mutate
  • R 'cowplot' neatly produce gridded plot with shared (common) legends and unique legends
  • Repeat R script for many times and save results to text file
  • How to negative lookbehind for special characters
  • data.table inner join produces error when no match is found
  • Create a new column base on existing column, but row above
  • Is there a way to visualize the process of source() in RStudio?
  • google places api consumes 10 request but I am doing only 1
  • Statistical mode of a categorical variable in R (using mlv)
  • Using for-loop to mutate a data.frame in r
  • Make plot with regression line for mixed model
  • Shortcut to select matces cases in R studio
  • vectoriced norm/matrix multiplication
  • Negative log10 transformation in R
  • Plot data with duplicate points
  • Visualizing crosstab tables with a plot in R - changing colours
  • How to manually modify automated numbers and labels in plot
  • How can I follow any redirections of a url in R?
  • Add jitter to box plot using markers in plotly
  • Adding an extra item to the legend
  • ggplot fills in data in the wrong order
  • Convert list to data frame
  • R: filtering by list(s) of strings and returning all results that start with the content of the lists
  • R:How to attach parts of a data frame with different headers and/or an overflowing piece of the dat frame
  • How to use 'par' for manipulating plot margins?
  • Can dplyr::case_when return mix of NAs and non-NAs?
  • Text preprocessing and topic modelling using text2vec package
  • Uploading multiple files in Shiny, process the files, rbind the results and return a download
  • R levelplot: color green-white-red (white on 0) according to one variable, but show the values of another variable
  • Why [i] doesn't point to the starting point in a vector
  • expand a data frame to have as many rows as range of two columns in original row
  • Getting started with R and CFA
  • Re order x-axis in ggplot so time goes from 12AM to 11PM in R
  • R - Automatically stack every nth column of a data frame and save them as new objects
  • How to format dplyr output in R into doubles (or other workable format)?
  • Dataframe to matrix conversion using tapply turns zeros to NAs
  • Smallest multiple of 1:20 - How can I make it quicker?
  • How to specify the size of a graph in ggplot2 independent of axis labels
  • How can I find the number of a vector's elements in another vector?
  • shadow
    Privacy Policy - Terms - Contact Us © voile276.org