如何为lme4中的模型编码预测.merMod循环,其中前一行的结果是下一行的预测子?

时间:2019-02-15 17:36:38

标签: r lme4

我需要使用predict.merMod来预测一个模型,其中结果将充当后续行的预测器。我该怎么办?

我将不得不使用模型进行预测(由于目前尚无所有变量或有关嵌套结构的信息,因此我尚无法确定是否为lm / glm / lmer / glmer ),其中预测变量“ Prev_Week_Share”是预测结果变量“ Share_Outcome”的地方。每行表示一个特定的星期。在使用历史数据创建和测试模型时,这应该很容易做到,但是我还想在未来的13周内运行模型(在那里我拥有除“ Prev_Week_Share”之外的所有变量),因此,我最好选择模型要预测当前一周的结果,请在第二周的“ Prev_Week_Share”列中输入结果,在第二周运行,等等。

我包括了一些虚假数据,并使用线性模型作为代表来表达我的意思:

data <- structure(list(Week = c(1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 
11L, 12L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 
1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 1L, 2L, 3L, 
4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 1L, 2L, 3L, 4L, 5L, 6L, 
7L, 8L, 9L, 10L, 11L, 12L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 
10L, 11L, 12L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 
12L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 1L, 2L, 
3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 1L, 2L, 3L, 4L, 5L, 
6L, 7L, 8L, 9L, 10L, 11L, 12L), Month = c(1L, 1L, 1L, 1L, 2L, 
2L, 2L, 2L, 3L, 3L, 3L, 3L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 3L, 
3L, 3L, 3L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 1L, 
1L, 1L, 1L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 1L, 1L, 1L, 1L, 2L, 
2L, 2L, 2L, 3L, 3L, 3L, 3L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 3L, 
3L, 3L, 3L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 1L, 
1L, 1L, 1L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 1L, 1L, 1L, 1L, 2L, 
2L, 2L, 2L, 3L, 3L, 3L, 3L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 3L, 
3L, 3L, 3L), Prev_Week_Share = c(0.3, 0.303936667, 0.273786667, 
0.306816667, 0.30575, 0.325383333, 0.33843, 0.312856667, 0.293873333, 
0.291823333, 0.313623333, 0.32478, 0.26565, 0.274876667, 0.233856667, 
0.23039, 0.219736667, 0.23674, 0.267043333, 0.321296667, 0.327726667, 
0.338473333, 0.327476667, 0.328093333, 0.32226, 0.351813333, 
0.36878, 0.38063, 0.389546667, 0.37392, 0.36738, 0.387893333, 
0.366956667, 0.383786667, 0.396803333, 0.421943333, 0.4398, 0.3905, 
0.414966667, 0.433166667, 0.413223333, 0.40131, 0.357253333, 
0.39222, 0.41099, 0.432733333, 0.401896667, 0.37905, 0.379313333, 
0.366893333, 0.396366667, 0.415226667, 0.435476667, 0.435296667, 
0.41514, 0.409953333, 0.407093333, 0.426343333, 0.45566, 0.485513333, 
0.467543333, 0.504633333, 0.501383333, 0.45577, 0.46573, 0.464056667, 
0.432013333, 0.416973333, 0.397246667, 0.414503333, 0.410733333, 
0.383263333, 0.326556667, 0.337626667, 0.313836667, 0.310403333, 
0.294576667, 0.32822, 0.30475, 0.3349, 0.38117, 0.407446667, 
0.397463333, 0.444763333, 0.468953333, 0.504086667, 0.5506, 0.520243333, 
0.481673333, 0.50075, 0.492156667, 0.510876667, 0.517183333, 
0.52981, 0.493726667, 0.50577, 0.533003333, 0.52743, 0.556453333, 
0.532443333, 0.537556667, 0.56994, 0.559286667, 0.59399, 0.61469, 
0.668476667, 0.675053333, 0.64556, 0.65703, 0.632163333, 0.615773333, 
0.612706667, 0.653636667, 0.631013333, 0.641616667, 0.644456667, 
0.63567, 0.655903333, 0.64686, 0.645506667), price_gap = c(1.02, 
-0.48, 1.66, -1.19, 0.01, 0.89, 1.19, 0.71, 0.95, 0.74, -1.54, 
-1.61, -0.23, -0.86, 0.4, -0.01, -1.71, 0.18, 1.95, 0.7, 1.94, 
0.99, 0.07, 1.07, -0.9, 1.04, 0.67, -0.47, -1.56, -0.31, 0.09, 
1.37, -0.01, -0.76, 1.71, -1.11, -1.8, 0.15, -1.8, 0.93, -0.01, 
-1.26, 1.88, 1.31, 0.46, -1.33, 0.15, -0.98, 0.23, -0.48, 1.3, 
1.13, 0.22, -1.83, 0.68, 0.37, -0.47, 0.62, -0.11, -1, 0.81, 
0.71, 0.16, 1.76, 1.18, 0.8, 1.48, -0.26, 1.99, 0.98, -0.38, 
-0.5, 0.88, 0.56, 0.81, -1.56, 0.88, 0.38, 0.07, 0, 1.89, 0.81, 
0.52, 1.86, 0.75, 0.52, -0.66, -1.14, 1.89, -1.7, 0.54, 0.68, 
0.6, -0.17, -0.09, 0.37, 1.6, 1.29, -0.74, 0.59, 0.79, -1.81, 
0.86, 1.5, 1.3, -1.45, -1.49, 1.19, -1.49, 0.12, 0.14, -0.28, 
-0.12, -0.54, -0.43, -0.95, 1.07, -0.72, -0.16, -0.33), POR = c(0.182, 
0.135, 0.188, 0.1, 0.145, 0.188, 0.106, 0.115, 0.125, 0.12, 0.184, 
0.172, 0.156, 0.128, 0.12, 0.138, 0.157, 0.102, 0.107, 0.178, 
0.163, 0.192, 0.18, 0.105, 0.157, 0.19, 0.15, 0.17, 0.174, 0.156, 
0.118, 0.153, 0.168, 0.19, 0.154, 0.134, 0.11, 0.155, 0.13, 0.134, 
0.192, 0.151, 0.165, 0.197, 0.106, 0.103, 0.142, 0.178, 0.188, 
0.144, 0.171, 0.19, 0.112, 0.146, 0.133, 0.139, 0.145, 0.19, 
0.177, 0.103, 0.159, 0.115, 0.182, 0.166, 0.106, 0.144, 0.156, 
0.139, 0.199, 0.163, 0.108, 0.111, 0.102, 0.171, 0.2, 0.169, 
0.171, 0.153, 0.105, 0.127, 0.156, 0.145, 0.13, 0.124, 0.165, 
0.168, 0.131, 0.153, 0.186, 0.124, 0.162, 0.179, 0.171, 0.165, 
0.101, 0.105, 0.186, 0.114, 0.184, 0.118, 0.185, 0.118, 0.122, 
0.2, 0.102, 0.161, 0.179, 0.167, 0.145, 0.116, 0.165, 0.148, 
0.131, 0.137, 0.129, 0.108, 0.165, 0.119, 0.163, 0.155), random_noise_variable = c(0.65, 
0.83, 0.62, 0.27, 0.59, 0.85, 0.75, 0.51, 0.81, 0.6, 0.65, 0.32, 
0.4, 0.16, 0.58, 0.58, 0.4, 0.63, 0.91, 0.11, 0.96, 0.26, 0.51, 
0.78, 0.93, 0.35, 0.5, 0.39, 0.5, 0.3, 0.13, 0.81, 0.78, 0.78, 
0.97, 0, 0.18, 0.37, 0.74, 0.69, 0.86, 0.71, 0.12, 0.01, 0.1, 
0.21, 0.95, 0.13, 0.16, 0.48, 0.61, 0.58, 0.24, 0.17, 0.88, 0.66, 
0.94, 0.37, 0.35, 0.86, 0.34, 0.93, 0.48, 0.84, 0.12, 0.66, 0.46, 
0.12, 0.67, 0.4, 0.87, 0.99, 0.17, 0.67, 0.9, 0.39, 0.66, 0.03, 
0.42, 0.84, 0.76, 0.9, 0.2, 0.04, 0.25, 0.67, 0.44, 0.8, 0.78, 
0.22, 0.92, 0.56, 0.61, 0.19, 0.34, 0.59, 0.26, 0.72, 0.37, 0.09, 
0.14, 0.41, 0.25, 0.31, 0.59, 0.88, 0.73, 0.23, 0.63, 1, 0.14, 
0.15, 0.66, 0.83, 0.59, 0.67, 0.19, 0.82, 1, 0.49), Share_Outcome = c(0.303936667, 
0.273786667, 0.306816667, 0.30575, 0.325383333, 0.33843, 0.312856667, 
0.293873333, 0.291823333, 0.313623333, 0.32478, 0.26565, 0.274876667, 
0.233856667, 0.23039, 0.219736667, 0.23674, 0.267043333, 0.321296667, 
0.327726667, 0.338473333, 0.327476667, 0.328093333, 0.32226, 
0.351813333, 0.36878, 0.38063, 0.389546667, 0.37392, 0.36738, 
0.387893333, 0.366956667, 0.383786667, 0.396803333, 0.421943333, 
0.4398, 0.3905, 0.414966667, 0.433166667, 0.413223333, 0.40131, 
0.357253333, 0.39222, 0.41099, 0.432733333, 0.401896667, 0.37905, 
0.379313333, 0.366893333, 0.396366667, 0.415226667, 0.435476667, 
0.435296667, 0.41514, 0.409953333, 0.407093333, 0.426343333, 
0.45566, 0.485513333, 0.467543333, 0.504633333, 0.501383333, 
0.45577, 0.46573, 0.464056667, 0.432013333, 0.416973333, 0.397246667, 
0.414503333, 0.410733333, 0.383263333, 0.326556667, 0.337626667, 
0.313836667, 0.310403333, 0.294576667, 0.32822, 0.30475, 0.3349, 
0.38117, 0.407446667, 0.397463333, 0.444763333, 0.468953333, 
0.504086667, 0.5506, 0.520243333, 0.481673333, 0.50075, 0.492156667, 
0.510876667, 0.517183333, 0.52981, 0.493726667, 0.50577, 0.533003333, 
0.52743, 0.556453333, 0.532443333, 0.537556667, 0.56994, 0.559286667, 
0.59399, 0.61469, 0.668476667, 0.675053333, 0.64556, 0.65703, 
0.632163333, 0.615773333, 0.612706667, 0.653636667, 0.631013333, 
0.641616667, 0.644456667, 0.63567, 0.655903333, 0.64686, 0.645506667, 
0.642623333), ï..Region = c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), 
    Wholesaler.number = c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
    1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
    3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 4L, 4L, 4L, 
    4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 5L, 5L, 5L, 5L, 5L, 5L, 
    5L, 5L, 5L, 5L, 5L, 5L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 
    6L, 6L, 6L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 
    8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 9L, 9L, 9L, 
    9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 10L, 10L, 10L, 10L, 10L, 
    10L, 10L, 10L, 10L, 10L, 10L, 10L)), class = "data.frame", row.names = c(NA, 
-120L))

lmtest <- lm(Share_Outcome ~ Prev_Week_Share  + Month  + price_gap + POR + random_noise_variable,
                 data=data)

summary(lmtest)

该模型用于描述重大影响,但是鉴于一行的结果是下一行的预测因子,我不确定如何用它来预测未来数据。

我感觉我需要使用循环来做到这一点,但是我对循环的经验非常少,而对predict.merMod的经验很少。请问有人可以提供任何建议或模板代码吗?

0 个答案:

没有答案