如何存储大结果 R?
How to store large outcomes R?
我正在使用 Bass 扩散模型,通过执行 for 循环,我找到了大约 5000 种产品的参数 m、p 和 q。手动将这些参数放入一个 excel sheet 中是很多工作。有人知道在 R 中存储这些值的简单方法吗?例如在 table 中?这是我的代码:
PID=unique(TotalBass4$ProdID)
BassStored=NULL
k=0
pidlistNAs=NULL
for(pid in PID){
k=k+1
out <- lm(Sales ~ Cumsaleslag + Cumsalessqrt, data=subset(TotalBass4,subset=ProdID==pid))
P1a <- out$coef[1]
P1b <- out$coef[2]
P1c <- out$coef[3]
P1mplus <- (-P1b+sqrt(P1b**2-4*P1a*P1c))/(2*P1c)
P1mminus <- (-P1b-sqrt(P1b**2-4*P1a*P1c))/(2*P1c)
m <- P1mminus
p <- 1/m
q <- P1b+p
cmsl=subset(TotalBass4,subset=ProdID==pid)$Cumsaleslag
Spred <- Bassmodel(p, q, m, cmsl,T=30)$Sales
Spred <- ts(Spred)
BassStored[[k]]=list(parm=c(m,p,q),ProdID=pid) ## Spred=Spred
names(BassStored[[k]]$parm)=c("m","p","q")
if(is.na(P1c))
pidlistNAs=c(pidlistNAs,pid)
if((k%%10)==0)
print(k)
}
BassStored[1]
示例的一些可重现数据:
set.seed(2017-12-20)
p <- runif(10, min=0.006293, max=0.00689)
m <- runif(10, min=67380.15, max=68980.95)
q <- runif(10, min=0.61809, max=0.65804)
lapply(1:10, function(i) {
list(parm=c(m=m[i], p=p[i], q=q[i]), ProdID=i)
}) -> BassStored
您已经创建了这样的东西,并且可能仍将其加载到 R 的内存中:
str(BassStored)
## List of 10
## $ :List of 2
## ..$ parm : Named num [1:3] 6.79e+04 6.69e-03 6.47e-01
## .. ..- attr(*, "names")= chr [1:3] "m" "p" "q"
## ..$ ProdID: int 1
## $ :List of 2
## ..$ parm : Named num [1:3] 6.81e+04 6.45e-03 6.29e-01
## .. ..- attr(*, "names")= chr [1:3] "m" "p" "q"
## ..$ ProdID: int 2
## $ :List of 2
## ..$ parm : Named num [1:3] 6.86e+04 6.37e-03 6.18e-01
## .. ..- attr(*, "names")= chr [1:3] "m" "p" "q"
## ..$ ProdID: int 3
## $ :List of 2
## ..$ parm : Named num [1:3] 6.78e+04 6.54e-03 6.55e-01
## .. ..- attr(*, "names")= chr [1:3] "m" "p" "q"
## ..$ ProdID: int 4
## $ :List of 2
## ..$ parm : Named num [1:3] 6.88e+04 6.52e-03 6.45e-01
## .. ..- attr(*, "names")= chr [1:3] "m" "p" "q"
## ..$ ProdID: int 5
## $ :List of 2
## ..$ parm : Named num [1:3] 6.79e+04 6.38e-03 6.19e-01
## .. ..- attr(*, "names")= chr [1:3] "m" "p" "q"
## ..$ ProdID: int 6
## $ :List of 2
## ..$ parm : Named num [1:3] 6.75e+04 6.63e-03 6.21e-01
## .. ..- attr(*, "names")= chr [1:3] "m" "p" "q"
## ..$ ProdID: int 7
## $ :List of 2
## ..$ parm : Named num [1:3] 6.76e+04 6.57e-03 6.24e-01
## .. ..- attr(*, "names")= chr [1:3] "m" "p" "q"
## ..$ ProdID: int 8
## $ :List of 2
## ..$ parm : Named num [1:3] 6.79e+04 6.83e-03 6.55e-01
## .. ..- attr(*, "names")= chr [1:3] "m" "p" "q"
## ..$ ProdID: int 9
## $ :List of 2
## ..$ parm : Named num [1:3] 6.88e+04 6.61e-03 6.29e-01
## .. ..- attr(*, "names")= chr [1:3] "m" "p" "q"
## ..$ ProdID: int 10
我们可以把它变成一个数据框:
do.call(
rbind.data.frame,
lapply(1:length(BassStored), function(i){
as.list(unlist(BassStored[i]))
})
) -> xdf
xdf
## parm.m parm.p parm.q ProdID
## 2 67860.06 0.006689309 0.6468014 1
## 21 68054.35 0.006451261 0.6286121 2
## 3 68640.19 0.006372309 0.6181186 3
## 4 67829.24 0.006541486 0.6551225 4
## 5 68807.85 0.006517481 0.6454875 5
## 6 67886.29 0.006382578 0.6194927 6
## 7 67542.34 0.006625390 0.6212089 7
## 8 67635.12 0.006566107 0.6239669 8
## 9 67878.34 0.006826642 0.6545225 9
## 10 68778.44 0.006609701 0.6287901 10
清理名称:
xdf <- setNames(xdf, c("m", "p", "q", "ProdID"))
xdf
## m p q ProdID
## 2 67860.06 0.006689309 0.6468014 1
## 21 68054.35 0.006451261 0.6286121 2
## 3 68640.19 0.006372309 0.6181186 3
## 4 67829.24 0.006541486 0.6551225 4
## 5 68807.85 0.006517481 0.6454875 5
## 6 67886.29 0.006382578 0.6194927 6
## 7 67542.34 0.006625390 0.6212089 7
## 8 67635.12 0.006566107 0.6239669 8
## 9 67878.34 0.006826642 0.6545225 9
## 10 68778.44 0.006609701 0.6287901 10
然后,写出来:
write.csv(xdf, "bassmodel.csv")
我正在使用 Bass 扩散模型,通过执行 for 循环,我找到了大约 5000 种产品的参数 m、p 和 q。手动将这些参数放入一个 excel sheet 中是很多工作。有人知道在 R 中存储这些值的简单方法吗?例如在 table 中?这是我的代码:
PID=unique(TotalBass4$ProdID)
BassStored=NULL
k=0
pidlistNAs=NULL
for(pid in PID){
k=k+1
out <- lm(Sales ~ Cumsaleslag + Cumsalessqrt, data=subset(TotalBass4,subset=ProdID==pid))
P1a <- out$coef[1]
P1b <- out$coef[2]
P1c <- out$coef[3]
P1mplus <- (-P1b+sqrt(P1b**2-4*P1a*P1c))/(2*P1c)
P1mminus <- (-P1b-sqrt(P1b**2-4*P1a*P1c))/(2*P1c)
m <- P1mminus
p <- 1/m
q <- P1b+p
cmsl=subset(TotalBass4,subset=ProdID==pid)$Cumsaleslag
Spred <- Bassmodel(p, q, m, cmsl,T=30)$Sales
Spred <- ts(Spred)
BassStored[[k]]=list(parm=c(m,p,q),ProdID=pid) ## Spred=Spred
names(BassStored[[k]]$parm)=c("m","p","q")
if(is.na(P1c))
pidlistNAs=c(pidlistNAs,pid)
if((k%%10)==0)
print(k)
}
BassStored[1]
示例的一些可重现数据:
set.seed(2017-12-20)
p <- runif(10, min=0.006293, max=0.00689)
m <- runif(10, min=67380.15, max=68980.95)
q <- runif(10, min=0.61809, max=0.65804)
lapply(1:10, function(i) {
list(parm=c(m=m[i], p=p[i], q=q[i]), ProdID=i)
}) -> BassStored
您已经创建了这样的东西,并且可能仍将其加载到 R 的内存中:
str(BassStored)
## List of 10
## $ :List of 2
## ..$ parm : Named num [1:3] 6.79e+04 6.69e-03 6.47e-01
## .. ..- attr(*, "names")= chr [1:3] "m" "p" "q"
## ..$ ProdID: int 1
## $ :List of 2
## ..$ parm : Named num [1:3] 6.81e+04 6.45e-03 6.29e-01
## .. ..- attr(*, "names")= chr [1:3] "m" "p" "q"
## ..$ ProdID: int 2
## $ :List of 2
## ..$ parm : Named num [1:3] 6.86e+04 6.37e-03 6.18e-01
## .. ..- attr(*, "names")= chr [1:3] "m" "p" "q"
## ..$ ProdID: int 3
## $ :List of 2
## ..$ parm : Named num [1:3] 6.78e+04 6.54e-03 6.55e-01
## .. ..- attr(*, "names")= chr [1:3] "m" "p" "q"
## ..$ ProdID: int 4
## $ :List of 2
## ..$ parm : Named num [1:3] 6.88e+04 6.52e-03 6.45e-01
## .. ..- attr(*, "names")= chr [1:3] "m" "p" "q"
## ..$ ProdID: int 5
## $ :List of 2
## ..$ parm : Named num [1:3] 6.79e+04 6.38e-03 6.19e-01
## .. ..- attr(*, "names")= chr [1:3] "m" "p" "q"
## ..$ ProdID: int 6
## $ :List of 2
## ..$ parm : Named num [1:3] 6.75e+04 6.63e-03 6.21e-01
## .. ..- attr(*, "names")= chr [1:3] "m" "p" "q"
## ..$ ProdID: int 7
## $ :List of 2
## ..$ parm : Named num [1:3] 6.76e+04 6.57e-03 6.24e-01
## .. ..- attr(*, "names")= chr [1:3] "m" "p" "q"
## ..$ ProdID: int 8
## $ :List of 2
## ..$ parm : Named num [1:3] 6.79e+04 6.83e-03 6.55e-01
## .. ..- attr(*, "names")= chr [1:3] "m" "p" "q"
## ..$ ProdID: int 9
## $ :List of 2
## ..$ parm : Named num [1:3] 6.88e+04 6.61e-03 6.29e-01
## .. ..- attr(*, "names")= chr [1:3] "m" "p" "q"
## ..$ ProdID: int 10
我们可以把它变成一个数据框:
do.call(
rbind.data.frame,
lapply(1:length(BassStored), function(i){
as.list(unlist(BassStored[i]))
})
) -> xdf
xdf
## parm.m parm.p parm.q ProdID
## 2 67860.06 0.006689309 0.6468014 1
## 21 68054.35 0.006451261 0.6286121 2
## 3 68640.19 0.006372309 0.6181186 3
## 4 67829.24 0.006541486 0.6551225 4
## 5 68807.85 0.006517481 0.6454875 5
## 6 67886.29 0.006382578 0.6194927 6
## 7 67542.34 0.006625390 0.6212089 7
## 8 67635.12 0.006566107 0.6239669 8
## 9 67878.34 0.006826642 0.6545225 9
## 10 68778.44 0.006609701 0.6287901 10
清理名称:
xdf <- setNames(xdf, c("m", "p", "q", "ProdID"))
xdf
## m p q ProdID
## 2 67860.06 0.006689309 0.6468014 1
## 21 68054.35 0.006451261 0.6286121 2
## 3 68640.19 0.006372309 0.6181186 3
## 4 67829.24 0.006541486 0.6551225 4
## 5 68807.85 0.006517481 0.6454875 5
## 6 67886.29 0.006382578 0.6194927 6
## 7 67542.34 0.006625390 0.6212089 7
## 8 67635.12 0.006566107 0.6239669 8
## 9 67878.34 0.006826642 0.6545225 9
## 10 68778.44 0.006609701 0.6287901 10
然后,写出来:
write.csv(xdf, "bassmodel.csv")