我正在处理的数据集如下所示:
Var1 = c(3402,5206,4820,4608)
Var2 = c(0,122,126,0)
Data = cbind(Var1,var2)
现在我想分别对遵循逻辑的列变量应用Adstock,power和lag转换:
Y1 = x1
Y2 = (x2+Y1*adstock)^power
Y3=(X3+y2*adstock)^power
…… so on
为实现这一点,我使用了以下代码 -
data=read.csv("APL_Test.csv",header=T)
ind<-names(data)
ind
ad<-10
pwr<-60
lg<-1
data[1] = data[1]
for(i in 2:nrow(data))
{
data[i] = data[i] + adstock_rate * data[i-1]
ap = adstocked_advertising*power
data[paste(ind,"_",ad,".",pwr,".",lg,sep="")]<-lag(ap,lg)
data[,paste(ind,"_",ad,".",pwr,".",lg,sep="")]<- ifelse(is.na(data[,paste(ind,"_",ad,".",pwr,".",lg,sep="")]),data[,paste(ind,"_",ad,".",pwr,".",lg,sep="")][1],data[,paste(ind,"_",ad,".",pwr,".",lg,sep="")])
}
在这方面,我面临以下问题: