Hello, I'd like to perform a regression using MCMCregress (MCMCpack). One variable therefore should be a function rather than a variable: I want to use X as an input and X should be defined as a random number between to values. Therefore I want to use the function runif like: X <-(1, Xa, Xb) but it seems that runif doesn't allow to use vectors. So I think I've to calculate the new vector X by using a for loop. I tried "for (i in 1:length(lT)) T<-runif(1,lT,uT)" but that doesn't work. What is the correct for-loop function to create this new vector/variable? Can I use that function then as an input for MCMCregress? thank you Johannes --
Johannes, You have the loop set up right, you just need to add indexing to refer to the looping variable, i. lT <- sample(1:10) uT <- sample(21:30) X <- numeric(length(lT)) for (i in 1:length(lT)) X[i] <- runif(1, lT[i], uT[i]) X Note that I changed the name of the result from T to X, because T has special meaning in R. Jean `·.,, ><(((º> `·.,, ><(((º> `·.,, ><(((º> Jean V. Adams Statistician U.S. Geological Survey Great Lakes Science Center 223 East Steinfest Road Antigo, WI 54409 USA From: "Johannes Radinger" <JRadinger@gmx.at> To: r-help@r-project.org Date: 08/10/2011 07:23 AM Subject: [R] function runif in for loop Sent by: r-help-bounces@r-project.org Hello, I'd like to perform a regression using MCMCregress (MCMCpack). One variable therefore should be a function rather than a variable: I want to use X as an input and X should be defined as a random number between to values. Therefore I want to use the function runif like: X <-(1, Xa, Xb) but it seems that runif doesn't allow to use vectors. So I think I've to calculate the new vector X by using a for loop. I tried "for (i in 1:length(lT)) T<-runif(1,lT,uT)" but that doesn't work. What is the correct for-loop function to create this new vector/variable? Can I use that function then as an input for MCMCregress? thank you Johannes -- ______________________________________________ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. [[alternative HTML version deleted]]
On 10/08/2011 7:28 AM, Johannes Radinger wrote:> Hello, > > I'd like to perform a regression using MCMCregress (MCMCpack). > One variable therefore should be a function rather than a variable: > > I want to use X as an input and X should be defined as a random number between to values. Therefore I want to use the function runif like: > X<-(1, Xa, Xb) but it seems that runif doesn't allow to use vectors. > So I think I've to calculate the new vector X by using a for loop.runif() does allow vectors. Assuming Xa and Xb are vectors of length n, then X <- runif(n, Xa, Xb) will work. (Xa and Xb don't both have to be vectors; values will be recycled as necessary.) Duncan Murdoch> I tried "for (i in 1:length(lT)) T<-runif(1,lT,uT)" but that doesn't work. > What is the correct for-loop function to create this new vector/variable? > > Can I use that function then as an input for MCMCregress? > > thank you > > Johannes > > -- > > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code.