Re: [R] array problem and for looping

2004-10-29 Thread Petr Pikal
Hi

Beter not to give a same name your values (variables) as is 
function name. R is quite clever and

sample - rnorm(10)
sample - sample(sample,3)

works as expected, but it is not a rule.

Cheers
Petr

On 28 Oct 2004 at 17:54, Kunal Shetty wrote:

 Dear R- users and Helpers
 
 Is there some way to re initialise or clear the array elements? 
 Pardon me for being vague but the problem itself quite vague. I have
 attached the code along with email. When I run the saved r- code using
 source(random1.txt) , command.
 
 The program runs fine..but at times there is an error see below # ; 
 but again after the error if re-excuted it would work fine?I  probably
 missed some array detail in my program any suggestions
 
 #Error in var(parrX[1, ]) : missing observations in cov/cor
 
 parrX[] is an array .
 
  2) Also pardon me for the lengthy procedural code
 but as you could see in it..i have used the for loop for
 finding the positions (indexes) of the missing values and
 later carry out updating the new array element values at those
 particular positions/
  So how can I escape the for loop in this case ?  i.e get the
  missing position indexes and save another object ay vector or
  array ?
 
 And also later wanted to use matrix or vector multiplication
 (%*%)  for the updating statement
 newy[i]- u2 + covXY/varX * (sample$x[i] - u1)
 
  is any of the apply function good out here ?
 
   I really feel that I am doing something very routine and donkey work
   and I am most certain that powerful R ? functions could just execute
   the same 10 liner for loop condition to mere 4 lines ? but how?I am
   getting lost in the sea of functions here?
 
 Thank u for reading
 Regards
 Kunal
 

Petr Pikal
[EMAIL PROTECTED]

__
[EMAIL PROTECTED] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html


[R] array problem and for looping

2004-10-28 Thread Kunal Shetty
Dear R- users and Helpers

Is there some way to re initialise or clear the array elements?  Pardon me for being 
vague but the problem itself quite vague. I have attached the code along with email. 
When I run the saved r- code using source(random1.txt) , command.

The program runs fine..but at times there is an error see below # ;  but again after 
the error if re-excuted it would work fine…I  probably missed some array detail in my 
program any suggestions

#Error in var(parrX[1, ]) : missing observations in cov/cor

parrX[] is an array .

 2) Also pardon me for the lengthy procedural code
but as you could see in it..i have used the for loop for finding the positions 
(indexes) of the missing values and later carry out updating the new array element 
values at those particular positions/
 So how can I escape the for loop in this case ?  i.e get the missing position 
indexes and save another object ay vector or array ?

And also later wanted to use matrix or vector multiplication (%*%)  for the 
updating statement
newy[i]- u2 + covXY/varX * (sample$x[i] - u1)

 is any of the apply function good out here ?

  I really feel that I am doing something very routine and donkey work and I am most 
certain that powerful R – functions could just execute the same 10 liner for loop 
condition to mere 4 lines ? but how…I am getting lost in the sea of functions here…

Thank u for reading
Regards
Kunal

# creation for random data set
x - rnorm(100,17,24)
y - rnorm(100,7,11)
A - matrix(c(10,25,8,40),nrow=2,ncol=2)

z - rbind(x, y) # now 2 x 100
w - A %*% z # 2 x 100

x - w[1,]
y - w[2,]


 print(meanX  meanY   varX   varY   covar) 


 # mean of the variates

 u1 - mean(x)
 u2 - mean(y)
  
 # Variances of the variates
 varX - var(x)
 varY - var(y)
 
 # coVariances of the variates
 covXY - cov(x,y)
 

 # printing mean , var , covar


 print(c(u1,u2,varX,varY,covXY))



# now replace randomly with NA
x[sample(1:length(x), 10)] - NA
y[sample(1:length(y), 10)] - NA

# just a variable
sample - data.frame(x=x,y=y)


# Vector for missing values
missing - is.na(sample$x) | is.na(sample$y)
xNA - array(sample$x[missing], dim=c(1,length(sample$x[missing])))
yNA - array(sample$y[missing], dim=c(1,length(sample$y[missing])))


# find the  mean of the non missing values in the array

mX - mean(sample$x,na.rm=TRUE)
mY - mean(sample$y,na.rm=TRUE)


# Imputing the missing values with current estimated means

 x-array(ifelse(is.na(sample$x), mX, sample$x),dim=c(1,length(sample$x)))
 y-array(ifelse(is.na(sample$y), mY, sample$y),dim=c(1,length(sample$y)))


 print(  meanX  meanY   varX   varY   covar) 

# algorithm function to find out new estimate values

algoResult - function(parrX,parrY,parrXNA,parrYNA,ictr) {

# Variables for New X and Y
newx - array(parrX, dim=c(1,length(parrX)))
newy - array(parrY, dim=c(1,length(parrY)))

 # mean of the variates

 u1 - mean(parrX)
 u2 - mean(parrY)
  
 # Variances of the variates
 varX - var(parrX[1,])
 varY - var(parrY[1,])
 
 # coVariances of the variates
 covXY - cov(parrX[1,],parrY[1,])
 

 # printing mean , var , covar


 print(c(u1,u2,varX,varY,covXY))


  # Expected or updated values for the missing values 

   
  # loop for finding positon of missing vectors
  for (i in 1:length(missing)) {


 if (missing[i]==TRUE) 


{
  
 if (is.na(sample$x[i]== TRUE))
  {
newx[i] - u1 + covXY/varY * (sample$y[i] -u2)

  } 
 if (is.na(sample$y[i]== TRUE))
  {

newy[i]- u2 + covXY/varX * (sample$x[i] - u1)

 
  } 

}
 }

l - list(c(newx),c(newy)) 
names(l)-c(X1,Y1); as.data.frame(l)  
return(l)  

   }


# the iteration loop to do the algorithm
for (ctr in 1:50) {

   Output - algoResult(x,y,xNA,yNA,ctr); Output

   x - array(Output$X1, dim=c(1,length(Output$X1)))
   y - array(Output$Y1, dim=c(1,length(Output$Y1)))


   }



















__
[EMAIL PROTECTED] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html

RE: [R] array problem and for looping

2004-10-28 Thread Huntsinger, Reid
First, the condition 

  if (is.na(sample$x[i]== TRUE))

asks if sample$x[i] is equal to TRUE, and then checks whether the result of
this comparison is NA. Because the comparison returns NA when one or the
other argument is NA, this works, but note that it would work as well with
FALSE in place of TRUE. I suppose you meant to say if (is.na(sample$x[i]) ==
TRUE) which is the same as if(is.na(sample$x[i])). 

Second, your code could generate data with *both* x and y missing
simultaneously. That would produce missing results in your regression
imputation. You probably want to check for these and drop them or otherwise
handle them.

Third, you might gain some insight by running your function algoResult on
known data, and examining the results. The missing values have to come from
somewhere, but because you run the entire script which generates random data
each time you never get this check. 

Fourth, you function doesn't use its last 3 arguments, but does use the
dataframe sample and the vector missing. As these are not passed as
arguments, they are searched for in the enclosing environment. Do you want
that? 

Fifth, yes, you don't need the loop. Just use subscripting for example, as
you did to insert NAs randomly, or ifelse as you used to impute by means. 

Reid Huntsinger
 
-Original Message-
From: [EMAIL PROTECTED]
[mailto:[EMAIL PROTECTED] On Behalf Of Kunal Shetty
Sent: Thursday, October 28, 2004 1:55 PM
To: [EMAIL PROTECTED]
Subject: [R] array problem and for looping


Dear R- users and Helpers



Is there some way to re initialise or clear the array elements?  Pardon me
for being vague but the problem itself quite vague. I have attached the code
along with email. 

When I run the saved r- code using source(random1.txt) , command.



The program runs fine..but at times there is an error see below # ;  but
again after the error if re-excuted it would work fine...I  probably missed
some array detail in my program any suggestions



#Error in var(parrX[1, ]) : missing observations in cov/cor



parrX[] is an array .



 2) Also pardon me for the lengthy procedural code

but as you could see in it..i have used the for loop for finding the
positions (indexes) of the missing values and later carry out updating the
new array element values at those particular positions/

 So how can I escape the for loop in this case ?  i.e get the
missing position indexes and save another object ay vector or array ?



And also later wanted to use matrix or vector multiplication (%*%)
for the updating statement

newy[i]- u2 + covXY/varX * (sample$x[i] - u1)



 is any of the apply function good out here ?



  I really feel that I am doing something very routine and donkey work and I
am most certain that powerful R - functions could just execute the same 10
liner for loop condition to mere 4 lines ? but how...I am getting lost in
the sea of functions here...



Thank u for reading

Regards

Kunal

__
[EMAIL PROTECTED] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html