Displaying 20 results from an estimated 10000 matches similar to: "R: repeat loops"
2012 Mar 16
2
Elegant Code
Hi,
Can anyone help to write a more elegant version of my code? I am sure
this can be put into a loop but I am having trouble creating the
objects b1,b2,b3,...,etc.
b1 <- rigamma(50,1,1)
theta1 <- rgamma(50,0.5,(1/b1))
sim1 <- rpois(50,theta1)
b2 <- rigamma(50,1,1)
theta2 <- rgamma(50,0.5,(1/b2))
sim2 <- rpois(50,theta2)
b3 <- rigamma(50,1,1)
theta3 <-
2007 Jul 14
3
How to read many files at one time?
I want to load many files in the R. The names of the files are "Sim1.txt", "
Sim2.txt", "Sim3.txt", "Sim4.txt", "Sim5.txt" and so on.
Can I read them at one time? What should I do? I can give the same names in
R.
Thanks.
For example:
> tst=paste("Sim",1:20,".txt",sep="") # the file names
> tst
[1]
2008 Jul 23
1
Questions on weighted least squares
Hi all,
I met with a problem about the weighted least square regression.
1. I simulated a Normal vector (sim1) with mean 425906 and standard deviation 40000.
2. I simulated a second Normal vector with conditional mean b1*sim1, where b1 is just a number I specified, and variance proportional to sim1. Precisely, the standard deviation is sqrt(sim1)*50.
3. Then I run a WLS regression without the
2010 Dec 26
4
how to replace my double for loop which is little efficient!
Dear all,
My double for loop as follows, but it is little efficient, I hope all
friends can give me a "vectorized" program to replace my code. thanks
x: is a matrix 202*263, that is 202 samples, and 263 independent variables
num.compd<-nrow(x); # number of compounds
diss.all<-0
for( i in 1:num.compd)
for (j in 1:num.compd)
if (i!=j) {
S1<-sum(x[i,]*x[j,])
2005 Jul 04
2
Lack of independence in anova()
If the observations are normally distributed and the 2xk design is
balanced, theory requires that the tests for interaction and row effects be
independent. In my program, appended below, this would translate to cntT
(approx)= cntR*cntI/N if all R routines were functioning correctly. They
aren't.
sim2=function(size,N,p){
cntR=0
cntC=0
cntI=0
cntT=0
cntP=0
for(i in 1:N){
2010 Jan 29
1
FracSim set.seed
Hi,
I am using the FracSim library to simulate a time series. However, the
simulate function ignores my attempt to set the RNG seed I need for
reproducible research. The published docs and google have not yielded an
answer, so any help greatly received.
Thanks,
Selwyn
## Example code snippet
library(FracSim)
## simulate some 1d fractal data
set.seed(1234)
sim1 = fracsim.1d(h=0.5,k=1000,n=5000)
2001 May 27
3
plotting circles
I'd like to make some polar plots with R, but I can't seem to find anything
designed to help with that. Is there anyway to get a real polar mode, where
instead of x and y you have r and theta? How about a way to draw a circle?
-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send
2007 Aug 07
1
R2WinBUGS results not different with different runs
Hi All
I dont know if anyone else has noticed the same thing, but with 2 subsequent
runs of the same syntax, I am getting exactly the same results. I was expecting
that results differ slighlty, say in the 4th or 5th decimal place.
Is this a specialty with R2WinBUGS? Does it have something to do with the seed
value? Isnt the seed value reset everytime I restart winbugs?
Thanks Toby
2010 Nov 20
1
Grid newbie: aligning & scaling viewports
Oh Knowledgeable Ones:
I'm working on a project using grid graphics (for the first time). A toy
example is given below, run it simply with >foobar() I am wondering why the
pink dot and concentric circles are not centered on the 3-color axis system.
Further, I feel like the concentric circles don't have the intended radius -
if my math is right, the outmost circle should touch the
2002 Apr 23
1
Bug: persp and colors (PR#1476)
Full_Name: Oliver Niggemann
Version: Windows 1.4 patched
OS: Windows NT 4.0
Submission from: (NULL) (141.169.250.14)
Hello,
I am working with the persp command. I want to change to color of the facets
according
to their z-value. The colors seems to be transposed, i.e. the color of a facet A
seems
to be used for a facet B which is a (defined?) way off....
An example:
nx <- 21
ny <- 21
2010 Jun 17
2
constrOptim( ): conflict between help page and code
There is a contradiction between what the help page says and what constrOptim actually
does with the constraints. The issue is what happens on the boundary.
The help page says
The feasible region is defined by ?ui %*% theta - ci >= 0?,
but the R code for constrOptim reads
if (any(ui %*% theta - ci <= 0))
stop("initial value not feasible")
The following example
2005 Mar 16
1
Code to replace nested for loops
Dear list members,
How can I replace the nested for loops at then end of the script
below with more efficient code?
# Begin script__________________________________________________
# Dichotomous scores for 100 respondents on 3 items with
# probabilities of a correct response = .6, .4, and .7,
# respectively
x1 <- rbinom(100,1,.6)
x2 <- rbinom(100,1,.4)
x3 <- rbinom(100,1,.7)
#
2011 Oct 06
2
Titles changing when a plot is redrawn
I ran into a problem with titles on graphs. I wanted a graph with
multiple subplots, with each having a title that involved both
a Greek letter and an identifier for each graph. Below is a
simplified version of code to do this. The graph appears fine,
with the first graph having "i=1" in the title, and the second
graph having "i=2" in the title. However, when I resize the
2003 Nov 27
2
Getting rid of loops?
I wrote a function to calculate cosine distances between rows of a matrix.
It uses two loops and is slow. Any suggestions to speed this up? Thanks in
advance.
theta.dist <- function(x){
res <- matrix(NA, nrow(x), nrow(x))
for (i in 1:nrow(x)){
for(j in 1:nrow(x)){
if (i > j)
res[i, j] <- res[j, i]
else {
v1 <- x[i,]
v2 <- x[j,]
1999 Sep 20
3
image legend
Dear R users,
Does anyone have a function for putting a legend on an image
plot? I couldn't locate an R equivalent of image.legend....has
anyone written such a thing?
kind regards
andy
---------------------------------------------------------------------
J. Andy Royle, U.S. Fish and Wildlife Service - Office of Migratory
Bird Management; 11510 American Holly Drive , Laurel, MD
2009 Jul 06
3
Speed up code, profiling, optimization, lapply vs. loops
High everybody,
currently I'm writinig a package that, for a given family of variance
functions depending on a parameter theta, say, computes the extended
quasi likelihood (eql) function for different values of theta.
The computation involves a couple of calls of the 'glm' routine. What
I'm doing now is to call 'lapply' for a list of theta values and a
function, that
2007 Aug 11
1
Artifacts in pdf() of image() (w/o comments)
Dear r-helpers,
In my previous message there were comments in the code that may have
made cutting and pasting awkward. Here it is w/o them.
I have two questions:
(1) The following produces a pdf with artifacts. How do I prevent them?
require(grDevices)
imSize <- 200
lambda <- 10
theta <- 15
sigma <- 40
x <- 1:imSize
x0 <- x / imSize -.5
freq = imSize/lambda
xf = x0 * freq *
2004 Dec 02
1
Arrow and text on a 3d plot
I am currently working on surface plots (with R 2.0.1) and I want to
improve the aspect of them. Having a typical 'cow-boy hat' surface
[source: Spector (1994), p. 206]:
pts <- seq(from = -8, to = 8, length = 50)
cow <- function(x, y)
{
z <- sqrt(x^2+y^2)
sin(z)/z
}
out <- outer(pts, pts, cow)
sur <- persp(x=pts, y=pts, z=out, theta=45, phi=30)
What I exactly wanted is:
2012 Feb 09
1
Constraint on one of parameters.
Dear all,
I have a function to optimize for a set of parameters and want to set a
constraint on only one parameter. Here is my function. What I want to do is
estimate the parameters of a bivariate normal distribution where the
correlation has to be between -1 and 1. Would you please advise how to
revise it?
ex=function(s,prob,theta1,theta,xa,xb,xc,xd,t,delta) {
expo1=
2009 Aug 17
2
Newbie that don't understand R code
I got some R code that I don't understand.
Question as comment in code
//where is t comming from, what is phi inverse
rAC <- function(name, n, d, theta){
#generic function for Archimedean copula simulation
illegalpar <- switch(name,
clayton = (theta < 0),
gumbel = (theta < 1),
frank = (theta < 0),
BB9 = ((theta[1] < 1) | (theta[2] < 0)),
GIG = ((theta[2] < 0) |