简体   繁体   English

使用 by() 从模型中提取预测值

[英]Extract predicted values from a model using by()

I'm fitting an exponential model to population data for 208 springsheds to back-calculate population for years 1975-2015 in 5-year intervals, ie seq(1975,2015,5) .我正在为 208 个泉域的人口数据拟合指数模型,以 5 年为间隔反算 1975-2015 年的人口,即seq(1975,2015,5) Here's the first 5 springs in my dataset and the code I'm using to fit the model and plot it (I want to have the figures):这是我的数据集中的前 5 个弹簧以及我用来拟合模型并绘制它的代码(我想要这些数字):

springsheds <- 
structure(list(spring = c("alexander", "alexander", "alexander", "alexander", 
"blue hole", "blue hole", "blue hole", "blue hole", "cedar head", "cedar head", 
"cedar head", "cedar head", "charles", "charles", "charles", "charles", 
"columbia", "columbia", "columbia", "columbia"), year = c(2000L, 2005L, 2010L, 
2015L, 2000L, 2005L, 2010L, 2015L, 2000L, 2005L, 2010L, 2015L, 2000L, 2005L, 
2010L, 2015L, 2000L, 2005L, 2010L, 2015L), pop = c(527L, 620L, 732L, 867L, 
3071L, 3356L, 3669L, 4007L, 3038L, 3320L, 3630L, 3965L, 1311L, 1446L, 1592L, 
1747L, 7550L, 8130L, 8706L, 9332L)), .Names = c("spring", "year", "pop"), 
class = "data.frame", row.names = c(NA, -20L))

models.spsh <- by(springsheds, springsheds$spring, function(x) {
        fm <- lm(log(pop) ~ year, data = x)
        timevalues <- seq(1970, 2020, 10)
        predict <- exp(predict(fm,list(year=timevalues)))
        plot(pop ~ year, x, main = spring[1], xlim = c(1970, 2020), ylim=c(0,15000))
        lines(timevalues, predict,lwd=1, col = "blue", xlab = "Year", ylab = "Population")
})

Can I also use by() to extract the predicted values for each spring?我还可以使用 by() 来提取每个弹簧的预测值吗? My current workaround is to create an object for each spring separately and iteratively add the predicted values to an object:我目前的解决方法是分别为每个弹簧创建一个对象,并迭代地将预测值添加到一个对象中:

fm <- lm(log(pop) ~ year, data = alex)
timevalues <- seq(1975,2015,5)
alex <- exp(predict(fm,list(year=timevalues)))
old<-cbind(timevalues,alex)
fm <- lm(log(pop) ~ year, data = blue)
blue <- exp(predict(fm,list(year=timevalues)))
old<-cbind(old,blue)

This seems really inefficient and I'm assuming there's a more elegant way of doing this, is there a way I can just add to my initial code to also extract the predicted population values?这似乎非常低效,我假设有一种更优雅的方法可以做到这一点,有没有一种方法可以添加到我的初始代码中以提取预测的人口值?

You can split the data and then use lapply for each desired output:您可以split数据,然后对每个所需的输出使用lapply

#Split the data grouped by spring
sdata <- split(springsheds, springsheds$spring)

#Fit the models
fit.spsh <- lapply(sdata, function(x) {
  lm(log(pop) ~ year, data = x)
})

#Get the predicted values
timevalues <- seq(1970, 2020, 10)
predictList <- lapply(fit.spsh, function(m) exp(predict(m,list(year=timevalues))))

#Generate plots
lapply(names(sdata), function(n) {
  plot(pop ~ year,sdata[[n]] , main = n, xlim = c(1970, 2020), ylim=c(0,15000))
  lines(timevalues, predictList[[n]],lwd=1, col = "blue", xlab = "Year", ylab = "Population")

})

 #Combine the predict values
 do.call(cbind,predictList)
 #alexander blue hole cedar head   charles  columbia  
 #1  194.3679  1803.470   1783.068  738.9545  4955.633
 #2  270.8778  2153.663   2129.682  894.9253  5705.076
 #3  377.5048  2571.856   2543.676 1083.8167  6567.857
 #4  526.1037  3071.253   3038.146 1312.5774  7561.118
 #5  733.1965  3667.621   3628.738 1589.6225  8704.590
 #6 1021.8081  4379.790   4334.137 1925.1434 10020.989

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM