I have three matrices named a_b
, pop
and event
df<- data_frame(a=c(-5.8221,-6.4678,-6.1213,-5.4518,-5.3527,-5.3517,-5.3169),
b= c(.0234,.0199,.0162,.0151,.0148,.0138,.0123))
a_b<-as.matrix(df[1:7, 1:2])
df1<- data_frame("2000" =c(716209.27,642504.69,644640.11,632239.35,552437.34,554395.34,539358.87),
"2005" = c(653040.76,637752.95,651635.40,647572.55,641183.99,570938.83,554092.75),
"2010" =c(5643.6,876123,665412.5,98643,89012,776321,8863))
pop<-as.matrix(df1[1:7, 1:3])
df2<- data_frame("2000"=c(9968.50418,2605.16992,2937.01261,4286.23376,5718.80904,4843.19769,5077.52440),
"2005"= c(6037.16994,2336.72681,2391.55031,4085.62207,5000.77218,4425.02331,5097.25052),
"2010" =c(19123.45,87765.54,102345.44,98000,12396,90076,10133))
event <-as.matrix(df2[1:7, 1:3])
Using following function, I want to apply uniroot
to find root of each row of event
and pop
. a
and b
in a_b
are constant for each column of event
and pop
.
fun<-function(x) (event - (exp(a b* x )* pop))
u1 <- uniroot(fun, c(-10000,10000),extendInt="yes", trace=1)$root
My problem is how should I apply my function to the corresponding value in a
, b
, event
and pop
. Because I have three years, my expected output would be like this:
2000 2005 2010
66.1349 48.6493 300.9612
48.2373 43.1457 209.395
45.0617 31.7133 262.299
30.3271 25.5657 360.6132
52.8459 33.7145 228.4664
44.3041 35.6299 231.7237
52.9546 51.0787 443.1554
CodePudding user response:
This is probably not the most efficient implementation, but it only took my computer about 0.03 seconds for the seven rows. I loop through all the different parameter options and use uniroot()
each time. The output matches your example output reasonably well.
df<- data.frame(a=c(-5.8221,-6.4678,-6.1213,-5.4518,-5.3527,-5.3517,-5.3169),
b= c(.0234,.0199,.0162,.0151,.0148,.0138,.0123))
a_b<-as.matrix(df[1:7, 1:2])
df1<- data.frame("2000" =c(716209.27,642504.69,644640.11,632239.35,552437.34,554395.34,539358.87),
"2005" = c(653040.76,637752.95,651635.40,647572.55,641183.99,570938.83,554092.75),
"2010" =c(5643.6,876123,665412.5,98643,89012,776321,8863))
pop<-as.matrix(df1[1:7, 1:3])
df2<- data.frame("2000"=c(9968.50418,2605.16992,2937.01261,4286.23376,5718.80904,4843.19769,5077.52440),
"2005"= c(6037.16994,2336.72681,2391.55031,4085.62207,5000.77218,4425.02331,5097.25052),
"2010" =c(19123.45,87765.54,102345.44,98000,12396,90076,10133))
event <-as.matrix(df2[1:7, 1:3])
fun<-function(x) (event_t - (exp(a_t b_t* x )* pop_t))
output <- data.frame(matrix(ncol=3,nrow=0, dimnames=list(NULL, c("2000", "2005", "2010"))))
start_time <- Sys.time()
for(i in 1:nrow(pop)){
row_vals = rep(NA,3)
for(j in 1:ncol(pop)){
a_t=a_b[i,1]
b_t=a_b[i,2]
event_t=event[i,j]
pop_t=pop[i,j]
u1 <- uniroot(fun,interval=c(-10000,10000),extendInt="yes", trace=1)$root
row_vals[j] <- u1
}
output <- rbind(output, row_vals)
}
end_time <- Sys.time()
print(end_time - start_time)
names(output) <- c("2000", "2005", "2010")
output
The output is:
2000 2005 2010
1 66.13496 48.64939 300.9612
2 48.23738 43.14574 209.3951
3 45.06177 31.71337 262.2992
4 30.32718 25.56577 360.6133
5 52.84598 33.71459 228.4664
6 44.30414 35.62997 231.7237
7 52.95467 51.07877 443.1555