# [R] add lines to a plot with a loop without erase the last one

Stephanie d'Agata sdagata at ucsd.edu
Sat Apr 8 13:26:43 CEST 2006

```Hello,

I want to plot several lines to a main plot but at the end of each loop, it
erases the last one.
Here my program:

alpha<-5
beta<-10
var<-0.5

s<-runif(1000,0,50)
m<-length(s)

variancealpha<-0.002
variancebeta<-0.051
variancevar<-0.001

alphachap<-vector()
betachap<-vector()
varchap<-vector()
epsilon<-vector()
b<-vector()
u<-vector()
varbeta<-vector()
varalpha<-vector()
biaisbeta<-vector()
biaisalpha<-vector()
biaisvar<-vector()

data<-matrix(99,length(s),n)

# III) Je génère les erreurs suivant une loi normale centrée sur 0 et de
variance sigma² déjà fixée.

d<-c(5,10,20,100,200,400,450,500,800)

for (k in 1:length(d))

{

n<-d[k]
print(n)
for (i in 1:n)
{

epsilon<-rnorm(length(s),0,sqrt(var))

for (j in 1:(length(s)))
{

data[j,i]<-((alpha*s[j]) / (s[j] + beta)) + (epsilon[j]*(s[j]/(s[j] +
beta)))

b<-data[,i]

}

#print(b)
X<-b[-1]/s[-1]
#print(X)
#print(X)
#print(length(b[-1]))
#print(length(X))
meanX<-mean(X)
#print(meanX)
meanV<-mean(b)
#print(meanV)

betachap[i]<-((meanX*squareproduct(b[-1],b[-1]))-
(meanV*squareproduct(X,b[-1])))/((meanV*squareproduct(X,X))-
(meanX*squareproduct(X,b[-1])))
alphachap[i]<-meanV + betachap[i]*meanX
varchap[i]<-((squareproduct(b[-1],b[-1])) +
(2*betachap[i]*squareproduct(X,b[-1])) +
(((betachap[i]^2)*squareproduct(X,X))))/(length(s)-2)

#u<-(alpha/(s+beta))

#varbeta<-var/(1+ (2*var/(alpha^2))*(squareproduct(u,u))/(alpha^2))
#varalpha<-(var/m) + (mean(u)^2)*(varbeta)

}

#print(data)
#print(alphachap)
#print(betachap)
#print(varchap)
#print(varbeta)
#print(varalpha)

biaisbeta[k]<-bq(betachap,beta)
biaisalpha[k]<-bq(alphachap,alpha)
biaisvar[k]<-bq(varchap,var)

par(mfrow=c(3,1))

plot(seq(5,15,0.01),dnorm(seq(5,15,0.01),10,sqrt(variancebeta)),col=2,type="
l")
lines(density(betachap))

plot(seq(2.5,7.5,0.01),dnorm(seq(2.5,7.5,0.01),5,sqrt(variancealpha)),col=2,
type="l")
lines(density(alphachap))

plot(seq(0,1,0.01),dnorm(seq(0,1,0.01),0.5,sqrt(variancevar)),col=2,type="l"
)
lines(density(varchap))

}

Thanks.

steph

```