I have a matrix M[i,j] with a large number of NA representing the population stock in a number of urban areas [i,] over the period 2000-2013 [,j]. I would like to complete missing data assuming that population grows at a constant rate.
I would like to run a loop which, for each row i of the matrix M 1. Computes the average growth rate using all the non-missing observations 2. Fills in the gaps using imputed values
Generating an example dataset:
city1=c(10,40,NA,NA,320,640);
city2=c(NA,300,NA,1200,2400,NA);
city3=c(NA,NA,4000,8000,16000,32000);
mat=rbind(city1,city2,city3)
Since the average growth rate is 4 for city1, 3 for city2, and 2 for city3, the corresponding result matrix should be:
r.city1=c(10,40,80,160,320,640);
r.city2=c(100,300,600,1200,2400,4800);
r.city3=c(1000,2000,4000,8000,16000,32000);
r.mat=rbind(city1,city2,city3)
Do you have any idea of how I could go about this?
Best,
Clément