我尝试在 R 中实现对数似然函数。这是我使用过的函数(我是 R 新手)
f <- function(t)
{
s=0
x=d
l = dim(x)[1]
for (i in 1:l)
{
vector = d[i,]
lin_res = t[1] + t[2] * vector[2] + t[3] * vector[3]
yi = vector[1]
s = s + yi*lin_res - log(1 + exp(lin_res))
}
return (s[1,1])
}
而 d 是具有以下数据的小矩阵:
y x1 x2 x3 x4
1 0 1 0.29944294 5.0 0.71049142
2 0 2 0.12521669 6.0 0.20554934
3 1 3 0.97254701 3.0 0.43665094
4 0 4 0.79952796 1.0 0.64749898
5 0 5 0.77358425 9.0 0.57564913
6 0 6 0.09983754 5.0 0.32164782
7 1 7 0.46133893 10.0 0.86437213
8 0 8 0.59833493 20.0 0.72545982
9 0 9 0.80005524 80.0 0.35782812
10 0 10 0.02979412 115.0 0.76707371
11 1 11 0.70576655 1.5 0.96908006
12 0 12 0.67138962 2.0 0.37169164
13 0 13 0.33446510 8.0 0.23591223
14 1 14 0.72187427 2.0 0.98578941
15 0 15 0.28193852 200.0 0.87076869
16 1 16 0.11258881 3.0 0.05566943
17 0 17 0.22001868 100.0 0.98197495
18 1 18 0.54681964 4.0 0.53437931
19 0 19 0.03336023 5.0 0.26451825
20 1 20 0.47007378 10.0 0.28463580
由于某种原因,这个函数需要很多时间(运行这个函数 100 次需要大约 7 秒)。
d <- structure(list(y = c(0L, 0L, 1L, 0L, 0L, 0L, 1L, 0L, 0L, 0L,
1L, 0L, 0L, 1L, 0L, 1L, 0L, 1L, 0L, 1L), x1 = 1:20, x2 = c(0.299442944,
0.125216695, 0.972547007, 0.799527959, 0.773584254, 0.099837539,
0.461338927, 0.59833493, 0.800055241, 0.029794123, 0.705766552,
0.671389622, 0.334465098, 0.721874271, 0.281938515, 0.112588815,
0.220018683, 0.546819639, 0.033360232, 0.470073781), x3 = c(5,
6, 3, 1, 9, 5, 10, 20, 80, 115, 1.5, 2, 8, 2, 200, 3, 100, 4,
5, 10), x4 = c(0.710491422, 0.20554934, 0.436650943, 0.647498983,
0.575649134, 0.321647815, 0.864372135, 0.725459824, 0.357828117,
0.767073707, 0.969080057, 0.371691641, 0.23591223, 0.985789413,
0.870768686, 0.055669431, 0.981974949, 0.534379314, 0.26451825,
0.284635804)), .Names = c("y", "x1", "x2", "x3", "x4"), class = "data.frame", row.names = c(NA,
-20L))
有人可以帮助我加速此功能或了解我做错了什么吗?
谢谢!