Monte-Carlo: R script not returning anything -


my r script is

mcnorm.r <- function(m,n) { library("mvtnorm", lib.loc="~/r/win-library/3.2") r <- as.matrix(read.csv("data.csv", header=false)) mu <- colmeans(r) sigma <- cov(r) r <- array(0,dim=c(m,ncol(r),n)) for(n in 1:n)   {     r[,,n] <- rmvnorm(m,mu,sigma)   } } 

it monte-carlo simulation of n matrices of multivariate-normal data of sample size m, dimension determined data set.

but when call

> data <- mcnorm.r(12,10) 

i data empty. why isn't code returning anything?

edit: defining global variable including

 r <<- r 

before last curly bracket seems work.

it doesn't return because don't give function value return.

you assign bunch of stuff local variable, function doesn't know return it.

you either need have unassigned expression @ end of function (which value of function), or explicitly use return.

contrast these 2 functions:

f1=function(x) x+1    # returns value f2=function(x) x=x+1  # assigns value, doesn't return 'f1';f1(1) 'f2';f2(1) 

now try this:

f2=function(x) {    x=x+1  # assigns value    x      # gives expression return }  'f2';f2(1) 

Comments

Popular posts from this blog

asp.net mvc - SSO between MVCForum and Umbraco7 -

Python Tkinter keyboard using bind -

ubuntu - Selenium Node Not Connecting to Hub, Not Opening Port -