[R] Unreasonable syntax error

Daniel Malter daniel at umd.edu
Tue Jun 21 09:52:35 CEST 2011


I find this sign in the code multiple times: ¨C; I replaced those with *;
there is also a MS-Word flexible hyphenation sign in there somewhere,
probably standing for a minus sign. Other than that, there are a couple of
output calls within loops that do not do anything anyway, but which seem to
be misplaced in those loops. The below code, removing those unnecessary
calls and MS symbols gives you code that runs through smoothly:

NdeTirage<-1  
Longueur<-40 


par<-function(N){
  s<- array(0,c(4,8,N));
  for( k in 1:N){
     s[1,3,k]<- (0.1) * runif(1)+ 0.1; }
     s
}
#
# Matrice des param?tres
para<-par(NdeTirage); #para<-(i,n?param?tre,n?tirage)

# Donn?es sur i (i(p,t) o? t est le num?ro de la semaine)
# p<-1 -> Plowed 2006 ; p<-2 -> Plowed 2007 ; p<-3 -> Unplowed 2006 ; p<-4
-> Unplowed 2007
i <- array(1, c(4,Longueur));
i[1,1]<-2;
i[1,2]<-1;
i[1,3]<-2;
i[1,32]<-1; #p<-1 -> Plowed 2006  
for (m in 4:11)
    i[1,m]<-1;
NdeTirage<-1  # NdeTirage d?signe le nombre de tirages
Longueur<-40  # L'exp?rience s'?tale sur 40 semaines

# Ecriture de la matrice des param?tres
par<-function(N){
  s<- array(0,c(4,8,N));
  for( k in 1:N){
     s[1,3,k]<- (0.1) * runif(1)+ 0.1; 
     s[1,1,k]<- (0.02) *  runif(1)+ 0.98 * s[1,3,k];
     s[1,5,k]<- (0.04) * runif(1) + 0.04;
     s[1,4,k]<- (0.01)* runif(1) + 0.99 * s[1,5,k];
     s[1,7,k]<- (0.26)* runif(1)+ 0.14; 
     s[1,6,k]<- (0.01) * runif(1) + 0.99 * s[1,7,k];
     s[1,8,k]<- (0.01)* runif(1) + 0.99; s[1,2,k]<- (1.31)* runif(1) + 0.19;
     s[2,3,k]<- (s[1,3,k]-max(0.8-s[1,1,k],0))*runif(1)+max(0.8-s[1,1,k],0);
     s[2,1,k]<- (s[1,1,k]-(0.8-s[2,3,k]))*runif(1)+(0.8-s[2,3,k]);
     s[2,5,k]<- (s[1,5,k]-max(0.4-s[1,4,k],0))*runif(1)+max(0.4-s[1,4,k],0);
     s[2,4,k]<- (s[1,4,k]-(0.4-s[2,5,k]))*runif(1)+(0.4-s[2,5,k]);
     s[2,7,k]<- (s[1,7,k]-max(0.7-s[1,6,k],0))*runif(1)+max(0.7-s[1,6,k],0);
     s[2,6,k]<- (s[1,6,k]-(0.7-s[2,7,k]))*runif(1)+(0.7-s[2,7,k]);
     s[2,8,k]<- (s[1,8,k]- 0.7)* runif(1) + 0.7;
     s[2,2,k]<- s[1,2,k]* runif(1);
     s[3,3,k]<- (s[2,3,k]-max(0.8-s[2,1,k],0))*runif(1)+max(0.8-s[2,1,k],0);
     s[3,1,k]<- (s[2,1,k]-(0.8-s[3,3,k]))*runif(1)+(0.8-s[3,3,k]);
     s[3,5,k]<- (s[2,5,k]-max(0.4-s[2,4,k],0))*runif(1)+max(0.4-s[2,4,k],0);
     s[3,4,k]<- (s[2,4,k]-(0.4-s[3,5,k]))*runif(1)+(0.4-s[3,5,k]);
     s[3,7,k]<- (s[2,7,k]-max(0.7-s[2,6,k],0))*runif(1)+max(0.7-s[2,6,k],0);
     s[3,6,k]<- (s[2,6,k]-(0.7-s[3,7,k]))*runif(1)+(0.7-s[3,7,k]);
     s[3,8,k]<- (s[2,8,k]- 0.7)* runif(1) + 0.7;
     s[3,2,k]<- s[2,2,k]* runif(1);
     s[4,3,k]<- (s[3,3,k]-max(0.8-s[3,1,k],0))*runif(1)+max(0.8-s[3,1,k],0);
     s[4,1,k]<- (s[3,1,k]-(0.8-s[4,3,k]))*runif(1)+(0.8-s[4,3,k]);
     s[4,5,k]<- (s[3,5,k]-max(0.4-s[3,4,k],0))*runif(1)+max(0.4-s[3,4,k],0);
     s[4,4,k]<- (s[3,4,k]-(0.4-s[4,5,k]))*runif(1)+(0.4-s[4,5,k]);
     s[4,7,k]<- (s[3,7,k]-max(0.7-s[3,6,k],0))*runif(1)+max(0.7-s[3,6,k],0);
     s[4,6,k]<- (s[3,6,k]-(0.7-s[4,7,k]))*runif(1)+(0.7-s[4,7,k]);
     s[4,8,k]<- (s[3,8,k]- 0.7)* runif(1) + 0.7;
     s[4,2,k]<- s[3,2,k]* runif(1);  }
     s}
#
# Matrice des param?tres
para<-par(NdeTirage); #para<-(i,n?param?tre,n?tirage)

# Donn?es sur i (i(p,t) o? t est le num?ro de la semaine)
# p<-1 -> Plowed 2006 ; p<-2 -> Plowed 2007 ; p<-3 -> Unplowed 2006 ; p<-4
-> Unplowed 2007
i <- array(1, c(4,Longueur));
i[1,1]<-2;
i[1,2]<-1;
i[1,3]<-2;
i[1,32]<-1; #p<-1 -> Plowed 2006  
for (m in 4:11)
    i[1,m]<-1;

for( m in 12:31)
    i[1,m]<-3;

for( m in 33:38)
    i[1,m]<-2;

for (m in 1:3) # p<-2 ->Plowed 2007
    i[2,m]<-2;

for (m in 30:40)
    i[2,m]<-2;

for (m in 4:29)
    i[2,m]<-1;

i[3,1]<-2;
i[3,3]<-2;
i[3,4]<-2;
i[3,33]<-2;
i[3,34]<-2;
i[3,36]<-2;
i[3,37]<-2;
i[3,38]<-2;#p<-3 -> Unplowed 2006
i[3,2]<-1;
for (m in 5:14)
    i[3,m]<-1;

for (m in 15:22)
    i[3,m]<-3;

for (m in 23:25)
    i[3,m]<-1;

for (m in 26:29)
    i[3,m]<-3;

for (m in 30:32)
    i[3,m]<-1;

i[3,35]<-1;

for (m in 1:3)#p<-4 -> Unplowed 2007
    i[4,m]<-2;

for (m in 4:31)
    i[4,m]<-1;

for (m in 32:40)
    i[4,m]<-2;

#

# Ecriture des autres param?tres

para2<- array(0,c(4,8, NdeTirage, 40));

# On va donner les valeurs de CS et CJcs
for ( k in 1:NdeTirage){
  para2[1,6,k,1]<- 0.7 *13* para[2,3,k]; para2[2,6,k,1]<- 0.7 *13*
para[2,3,k]; para2[3,6,k,1]<- 9.5 *13* para[2,3,k]; para2[4,6,k,1]<- 9.5
*13* para[2,3,k];

for ( p in 1:4){
    for (t in 1:12){
      para2[p,6,k,t+1]<- para[i[p,t],1,k]*para2[p,6,k,t];
    }
    para2[p,5,k,13]<- para[i[p,13],3,k];
    for (t in 13:39){
      para2[p,5,k,t+1]<- para[i[p,t+1],3,k];
      para2[p,6,k,t+1]<- para[i[p,t],1,k]*para2[p,6,k,t] - para2[p,5,k,t-1]
*para2[p,6,k,t-1];    }}}
#       



# On renvoie para2 (t+1) en prenant comme arguments pat<- para2(t) et
rt<-resultat(t)
par2 <-function(t, pat, rt){
  g<- array(0,c(4,8,NdeTirage));
  for ( p in 1:4){
    for( k in 1:NdeTirage){
      if (t==1){
        if (i[p,1]<3){
          g[p,1,k]<- 0;
          g[p,2,k]<- 0;
        }else{
          g[p,1,k]<- para[i[p,1],6,k]*rt[p,k,3]+ para[i[p,1],5,k]*
rt[p,k,2];
          g[p,2,k]<- para[i[p,1],8,k]*rt[p,k,4]+ para[i[p,1],7,k]*
rt[p,k,3];
        }
        g[p,7,k]<- para[1,6,k]*pat[p,7,k]+ g[p,1,k]-g[p,3,k];
        g[p,8,k]<- para[1,8,k]*pat[p,8,k]+ g[p,2,k]-g[p,4,k];
        g[p,6,k]<- para[i[p,1],1,k]*pat[p,6,k];
      }else{
        if (i[p,t]<3){
          g[p,1,k]<- 0;
          g[p,2,k]<- 0;
          if (i[p,t-1]>3){
            g[p,3,k]<- pat[p,7,k]* para[1,6,k];
            g[p,4,k]<- pat[p,8,k]* para[1,8,k];
          }else{
            g[p,3,k]<-0;
            g[p,4,k]<-0;
          }
        }else{
          g[p,1,k]<- para[i[p,t],6,k]*rt[p,k,3]+ para[i[p,t],5,k]*
rt[p,k,2];
          g[p,2,k]<- para[i[p,t],8,k]*rt[p,k,4]+ para[i[p,t],7,k]*
rt[p,k,3];
          g[p,3,k]<-0;
          g[p,4,k]<-0;
        }
        g[p,7,k]<- para[1,6,k]*pat[p,7,k]+ g[p,1,k]-g[p,3,k];
        g[p,8,k]<- para[1,8,k]*pat[p,8,k]+ g[p,2,k]-g[p,4,k];
      }
    }
  }
}
#


# On veut trouver le vecteur final en fonction de t
 
resultat<-array(0,c(4, NdeTirage, Longueur, 4)); #Initialisation de resultat
o? resultat est le vecteur final. resultat(p,n?tirage,t,n?variable) ,
variables<- C,J,S,A

# On rentre les valeurs initiales
for (k in 1:NdeTirage){
  resultat[3,k,1,2]<- 43.13;
  resultat[3,k,1,3]<- 10.63;
  resultat[3,k,1,4]<- 3.75;
  resultat[4,k,1,2]<- 55.005;
  resultat[4,k,1,3]<- 1.88;
  resultat[4,k,1,4]<- 7.5;
  resultat[1,k,1,2]<- 11.25;
  resultat[1,k,1,3]<- 0;
  resultat[1,k,1,4]<- 0;
  resultat[2,k,1,2]<- 3.125;
  resultat[2,k,1,3]<- 0;
  resultat[2,k,1,4]<- 1.88;
}

# Ici on veut resultat (t+1) en fonction de resultat (t) et des param?tres ?
t
# la fonction renvoie resultat (t+1) en  prenant comme arguments 
ff<-function(t, rt, pat){
  y<- array(0, c(4, NdeTirage, 4));
  for (p in 1:4){     for ( k in 1:NdeTirage ){       y[p,k,1]<-
para[i[p,t],1,k] * rt[p,k,1]+ para[i[p,t],2,k]* rt[p,k,4];      y[p,k,2]<-
para[i[p,t],3,k] * rt[p,k,1]+ para[i[p,t],4,k]* rt[p,k,2] +
pat[p,5,k]*pat[p,6,k];      y[p,k,3]<- para[i[p,t],5,k] * rt[p,k,2]+
para[i[p,t],6,k]* rt[p,k,3] *pat[p,1,k] + pat[p,3,k];      y[p,k,4]<-
para[i[p,t],7,k] * rt[p,k,3]+ para[i[p,t],8,k]* rt[p,k,4]- pat[p,2,k] +
pat[p,4,k];      if(y[p,k,1]<0) y[p,k,1]<-0;       if(y[p,k,2]<0) y[p,k,2]
<- 0;       if(y[p,k,3]<0) y[p,k,3] <- 0;       if(y[p,k,4]<0) y[p,k,4] <-
0; }}}
#

# On calcule maintenant resultat(t) et para2(t) pour tout t

for ( t in 1:39){
    resultat [,,t+1,]  <-  ff(t,resultat[ , ,t,],para2 [ , , , t]);
    para2 [ , , ,t+1]  <-  par2(t, para2 [ , , ,t], resultat[ , ,t, ]);
}


#On fait maintenant la moyenne des r?sultats
resultatfinal<- array(0, c( 4, Longueur, 4 ))
for (p in 1:4){
  for (t in 1:40){
    for (n in 1:4){
      somme<-0;
      for (k in 1:NdeTirage){
        somme<- somme+ resultat[p,k,t,n];
      }
      resultatfinal[p,t,n]<- somme/NdeTirage;
    }
  }
}
#

 


#On va maintenant construire les graphiques

#En vert: 2007 , en bleu: 2006

# Juveniles pour "plowed plots"
x1<-seq(1,40)
plot(x1, resultatfinal[1, ,2])
plot(x1, resultatfinal[2, ,2])


# Sous-adultes pour "plowed plots"
plot(x1, resultatfinal[1, ,3])
plot(x1, resultatfinal[2, ,3])


# Adultes pour "plowed plots"
plot(x1, resultatfinal[1, ,4])
plot(x1, resultatfinal[2, ,4])

# Juveniles pour "unplowed plots"
plot(x1, resultatfinal[3, ,2])
plot(x1, resultatfinal[4, ,2])

# Sous-adultes pour "unplowed plots"
plot(x1, resultatfinal[3, ,3])
plot(x1, resultatfinal[4, ,3])


# Adultes pour "unplowed plots"
plot(x1, resultatfinal[3, ,4])
plot(x1, resultatfinal[4, ,4])

#




for( m in 12:31)
    i[1,m]<-3;

for( m in 33:38)
    i[1,m]<-2;

for (m in 1:3) # p<-2 ->Plowed 2007
    i[2,m]<-2;

for (m in 30:40)
    i[2,m]<-2;

for (m in 4:29)
    i[2,m]<-1;

i[3,1]<-2;
i[3,3]<-2;
i[3,4]<-2;
i[3,33]<-2;
i[3,34]<-2;
i[3,36]<-2;
i[3,37]<-2;
i[3,38]<-2;#p<-3 -> Unplowed 2006
i[3,2]<-1;
for (m in 5:14)
    i[3,m]<-1;

for (m in 15:22)
    i[3,m]<-3;

for (m in 23:25)
    i[3,m]<-1;

for (m in 26:29)
    i[3,m]<-3;

for (m in 30:32)
    i[3,m]<-1;

i[3,35]<-1;

for (m in 1:3)#p<-4 -> Unplowed 2007
    i[4,m]<-2;

for (m in 4:31)
    i[4,m]<-1;

for (m in 32:40)
    i[4,m]<-2;

#

# Ecriture des autres param?tres

para2<- array(0,c(4,8, NdeTirage, 40));

# On va donner les valeurs de CS et CJcs
for ( k in 1:NdeTirage){
  para2[1,6,k,1]<- 0.7 *13* para[2,3,k]; para2[2,6,k,1]<- 0.7 *13*
para[2,3,k]; para2[3,6,k,1]<- 9.5 *13* para[2,3,k]; para2[4,6,k,1]<- 9.5
*13* para[2,3,k];

for ( p in 1:4){
    for (t in 1:12){
      para2[p,6,k,t+1]<- para[i[p,t],1,k]*para2[p,6,k,t];
    }
    para2[p,5,k,13]<- para[i[p,13],3,k];
    for (t in 13:39){
      para2[p,5,k,t+1]<- para[i[p,t+1],3,k];
      para2[p,6,k,t+1]<- para[i[p,t],1,k]*para2[p,6,k,t] - para2[p,5,k,t-1]
*para2[p,6,k,t-1];    }}}
#       



# On renvoie para2 (t+1) en prenant comme arguments pat<- para2(t) et
rt<-resultat(t)
par2 <-function(t, pat, rt){
  g<- array(0,c(4,8,NdeTirage));
  for ( p in 1:4){
    for( k in 1:NdeTirage){
      if (t==1){
        if (i[p,1]<3){
          g[p,1,k]<- 0;
          g[p,2,k]<- 0;
        }else{
          g[p,1,k]<- para[i[p,1],6,k]*rt[p,k,3]+ para[i[p,1],5,k]*
rt[p,k,2];
          g[p,2,k]<- para[i[p,1],8,k]*rt[p,k,4]+ para[i[p,1],7,k]*
rt[p,k,3];
        }
        g[p,7,k]<- para[1,6,k]*pat[p,7,k]+ g[p,1,k]-g[p,3,k];
        g[p,8,k]<- para[1,8,k]*pat[p,8,k]+ g[p,2,k]-g[p,4,k];
        g[p,6,k]<- para[i[p,1],1,k]*pat[p,6,k];
      }else{
        if (i[p,t]<3){
          g[p,1,k]<- 0;
          g[p,2,k]<- 0;
          if (i[p,t-1]>3){
            g[p,3,k]<- pat[p,7,k]* para[1,6,k];
            g[p,4,k]<- pat[p,8,k]* para[1,8,k];
          }else{
            g[p,3,k]<-0;
            g[p,4,k]<-0;
          }
        }else{
          g[p,1,k]<- para[i[p,t],6,k]*rt[p,k,3]+ para[i[p,t],5,k]*
rt[p,k,2];
          g[p,2,k]<- para[i[p,t],8,k]*rt[p,k,4]+ para[i[p,t],7,k]*
rt[p,k,3];
          g[p,3,k]<-0;
          g[p,4,k]<-0;
        }
        g[p,7,k]<- para[1,6,k]*pat[p,7,k]+ g[p,1,k]-g[p,3,k];
        g[p,8,k]<- para[1,8,k]*pat[p,8,k]+ g[p,2,k]-g[p,4,k];
      }
    }
  }
}
#


# On veut trouver le vecteur final en fonction de t
 
resultat<-array(0,c(4, NdeTirage, Longueur, 4)); #Initialisation de resultat
o? resultat est le vecteur final. resultat(p,n?tirage,t,n?variable) ,
variables<- C,J,S,A

# On rentre les valeurs initiales
for (k in 1:NdeTirage){
  resultat[3,k,1,2]<- 43.13;
  resultat[3,k,1,3]<- 10.63;
  resultat[3,k,1,4]<- 3.75;
  resultat[4,k,1,2]<- 55.005;
  resultat[4,k,1,3]<- 1.88;
  resultat[4,k,1,4]<- 7.5;
  resultat[1,k,1,2]<- 11.25;
  resultat[1,k,1,3]<- 0;
  resultat[1,k,1,4]<- 0;
  resultat[2,k,1,2]<- 3.125;
  resultat[2,k,1,3]<- 0;
  resultat[2,k,1,4]<- 1.88;
}

# Ici on veut resultat (t+1) en fonction de resultat (t) et des param?tres ?
t
# la fonction renvoie resultat (t+1) en  prenant comme arguments 
ff<-function(t, rt, pat){
  y<- array(0, c(4, NdeTirage, 4));
  for (p in 1:4){     for ( k in 1:NdeTirage ){       y[p,k,1]<-
para[i[p,t],1,k] * rt[p,k,1]+ para[i[p,t],2,k]* rt[p,k,4];      y[p,k,2]<-
para[i[p,t],3,k] * rt[p,k,1]+ para[i[p,t],4,k]* rt[p,k,2] +
pat[p,5,k]*pat[p,6,k];      y[p,k,3]<- para[i[p,t],5,k] * rt[p,k,2]+
para[i[p,t],6,k]* rt[p,k,3] *pat[p,1,k] + pat[p,3,k];      y[p,k,4]<-
para[i[p,t],7,k] * rt[p,k,3]+ para[i[p,t],8,k]* rt[p,k,4]- pat[p,2,k] +
pat[p,4,k];      if(y[p,k,1]<0) y[p,k,1]<-0;       if(y[p,k,2]<0) y[p,k,2]
<- 0;       if(y[p,k,3]<0) y[p,k,3] <- 0;       if(y[p,k,4]<0) y[p,k,4] <-
0; }}}
#

# On calcule maintenant resultat(t) et para2(t) pour tout t

for ( t in 1:39){
    resultat [,,t+1,]  <-  ff(t,resultat[ , ,t,],para2 [ , , , t]);
    para2 [ , , ,t+1]  <-  par2(t, para2 [ , , ,t], resultat[ , ,t, ]);
}


#On fait maintenant la moyenne des r?sultats
resultatfinal<- array(0, c( 4, Longueur, 4 ))
for (p in 1:4){
  for (t in 1:40){
    for (n in 1:4){
      somme<-0;
      for (k in 1:NdeTirage){
        somme<- somme+ resultat[p,k,t,n];
      }
      resultatfinal[p,t,n]<- somme/NdeTirage;
    }
  }
}
#

 


#On va maintenant construire les graphiques

#En vert: 2007 , en bleu: 2006

# Juveniles pour "plowed plots"
x1<-seq(1,40)
plot(x1, resultatfinal[1, ,2])
plot(x1, resultatfinal[2, ,2])


# Sous-adultes pour "plowed plots"
plot(x1, resultatfinal[1, ,3])
plot(x1, resultatfinal[2, ,3])


# Adultes pour "plowed plots"
plot(x1, resultatfinal[1, ,4])
plot(x1, resultatfinal[2, ,4])

# Juveniles pour "unplowed plots"
plot(x1, resultatfinal[3, ,2])
plot(x1, resultatfinal[4, ,2])

# Sous-adultes pour "unplowed plots"
plot(x1, resultatfinal[3, ,3])
plot(x1, resultatfinal[4, ,3])


# Adultes pour "unplowed plots"
plot(x1, resultatfinal[3, ,4])
plot(x1, resultatfinal[4, ,4])

#













james198877 wrote:
> 
>  http://r.789695.n4.nabble.com/file/n3612530/PSC.r PSC.r 
> 
> Hi all,
> 
> I just wrote a program in R by editing it in Microsoft Word and then
> pasting into the text editor of R. The above is the file.
> 
> And below is what the console complains.... Why doesn't it recognise 'r'??
> 
> I have to mention that at least when I typed this first several lines into
> the console, the first error didn't appear. I don't try the next errors as
> there would be too many lines to type...I'm not sure if this is something
> about Word
> 
>  http://r.789695.n4.nabble.com/file/n3612530/lastsave.txt lastsave.txt 
> 
> Thanks a lot for your help!!!
> 

--
View this message in context: http://r.789695.n4.nabble.com/Unreasonable-syntax-error-tp3612530p3613414.html
Sent from the R help mailing list archive at Nabble.com.



More information about the R-help mailing list