计算线性回归线的斜率

时间:2020-09-11 10:49:14

标签: r apply

我在数据框中计算斜率,但在返回不同面积大小的原始值时遇到问题。我的Dput格式的数据框如下。

我目前的做法是:

coefLM <- function(x) {
  coef(lm(log(mean) ~ log(area), tar.un_sap.10[-1,]))[2]
}

这段代码给出了以下问题:

tar.un_sap.10$slope=apply(tar.un_sap.10[,1:n],1,coefLM)
> head(tar.un_sap.10$slope)
[1] 0.02201471 0.02201471 0.02201471 0.02201471 0.02201471 0.02201471

这是我的数据框:

> dput(tar.un_sap.10)
structure(list(X1 = c(0, 209, 249, 127, 275, 204, 254, 121, 303, 
275, 231, 200, 293, 261, 196, 287, 212, 261, 300, 260, 249, 297, 
297, 270, 297, 248, 260, 262, 279, 288, 233, 232, 229, 252, 271, 
264, 260, 257, 255, 246, 265, 252, 259, 251, 258, 255, 254, 256, 
254, 320), X2 = c(0, 267, 266, 100, 300, 219, 300, 202, 114, 284, 
264, 247, 271, 249, 287, 290, 244, 268, 231, 264, 282, 222, 276, 
282, 297, 285, 246, 290, 262, 280, 233, 243, 238, 266, 271, 263, 
267, 258, 264, 249, 258, 260, 260, 259, 259, 254, 256, 254, 254, 
320), X3 = c(0, 179, 300, 278, 232, 224, 57, 268, 289, 264, 291, 
288, 178, 289, 187, 258, 287, 241, 259, 268, 242, 265, 260, 268, 
280, 277, 278, 249, 277, 251, 238, 242, 234, 268, 265, 280, 267, 
277, 273, 246, 266, 252, 256, 259, 263, 254, 254, 256, 254, 320
), X4 = c(0, 250, 117, 186, 283, 284, 280, 205, 281, 260, 285, 
243, 294, 214, 246, 252, 193, 231, 296, 300, 279, 264, 289, 262, 
265, 270, 283, 225, 278, 265, 285, 262, 272, 277, 265, 264, 280, 
259, 270, 271, 265, 262, 252, 260, 259, 254, 255, 257, 254, 320
), X5 = c(0, 190, 285, 201, 275, 136, 255, 159, 245, 281, 223, 
231, 297, 267, 211, 300, 262, 299, 221, 286, 297, 275, 252, 262, 
283, 267, 255, 248, 276, 223, 267, 242, 247, 242, 266, 282, 267, 
276, 274, 267, 258, 261, 261, 300, 260, 256, 255, 258, 253, 320
), X7 = c(0, 292, 51, 208, 263, 270, 180, 266, 270, 296, 188, 
295, 125, 223, 287, 237, 299, 223, 242, 268, 281, 240, 288, 297, 
265, 247, 229, 251, 241, 271, 266, 241, 240, 266, 281, 280, 260, 
251, 249, 246, 258, 259, 260, 262, 260, 254, 254, 256, 254, 320
), X8 = c(0, 280, 280, 100, 285, 220, 293, 277, 298, 305, 298, 
292, 269, 255, 223, 259, 296, 248, 277, 265, 297, 288, 259, 291, 
259, 255, 234, 292, 283, 279, 215, 272, 277, 266, 248, 274, 276, 
263, 249, 270, 251, 246, 253, 259, 258, 255, 257, 256, 254, 320
), X9 = c(0, 249, 290, 143, 252, 116, 217, 291, 280, 255, 188, 
273, 240, 254, 253, 280, 282, 285, 282, 249, 286, 252, 260, 234, 
239, 273, 269, 281, 235, 261, 250, 247, 265, 246, 273, 280, 257, 
267, 264, 273, 258, 232, 260, 259, 262, 254, 254, 254, 255, 320
), X10 = c(0, 307, 73, 19, 303, 174, 164, 237, 244, 247, 249, 
286, 188, 301, 210, 295, 281, 268, 230, 281, 274, 245, 257, 248, 
258, 267, 278, 263, 260, 262, 229, 281, 227, 265, 266, 251, 276, 
276, 247, 271, 262, 262, 300, 262, 260, 255, 256, 258, 254, 320
), X11 = c(0, 53, 170, 278, 233, 269, 219, 249, 298, 259, 283, 
303, 182, 255, 243, 282, 282, 232, 282, 221, 282, 280, 279, 297, 
283, 279, 282, 240, 248, 277, 235, 250, 248, 259, 281, 276, 261, 
272, 269, 246, 239, 241, 253, 249, 260, 255, 254, 256, 254, 320
), X12 = c(0, 223, 295, 210, 164, 269, 268, 286, 226, 307, 293, 
211, 195, 240, 264, 243, 249, 232, 206, 252, 241, 242, 250, 269, 
260, 280, 281, 233, 262, 246, 248, 265, 280, 247, 258, 276, 254, 
251, 255, 247, 260, 260, 260, 251, 259, 254, 254, 257, 255, 320
), X14 = c(0, 300, 295, 273, 308, 290, 263, 196, 261, 213, 302, 
243, 281, 298, 273, 241, 300, 291, 281, 261, 228, 196, 279, 278, 
278, 235, 234, 265, 274, 269, 241, 277, 247, 248, 277, 276, 267, 
272, 248, 274, 239, 246, 250, 256, 259, 254, 256, 257, 254, 320
), X15 = c(0, 235, 259, 70, 303, 295, 82, 280, 101, 293, 286, 
278, 291, 302, 276, 281, 281, 288, 260, 287, 252, 295, 260, 258, 
219, 284, 269, 248, 260, 266, 267, 277, 238, 238, 242, 271, 280, 
257, 276, 271, 258, 261, 259, 260, 259, 254, 258, 257, 254, 320
), X16 = c(0, 161, 283, 237, 178, 253, 292, 188, 228, 305, 296, 
244, 210, 214, 262, 193, 283, 278, 287, 268, 235, 221, 293, 270, 
289, 260, 256, 283, 259, 279, 270, 269, 283, 271, 248, 271, 266, 
275, 264, 270, 258, 260, 252, 260, 260, 253, 254, 255, 254, 320
), X17 = c(0, 304, 278, 249, 186, 286, 279, 301, 288, 246, 284, 
188, 254, 283, 300, 262, 242, 276, 255, 294, 282, 253, 262, 274, 
273, 292, 249, 290, 274, 252, 243, 277, 234, 271, 277, 242, 276, 
248, 264, 274, 268, 252, 246, 260, 258, 254, 254, 254, 257, 320
), X18 = c(0, 211, 268, 263, 226, 180, 115, 258, 308, 295, 292, 
243, 258, 264, 249, 231, 298, 278, 276, 275, 269, 227, 277, 267, 
298, 250, 265, 283, 222, 256, 245, 269, 242, 266, 261, 243, 269, 
242, 248, 267, 264, 235, 259, 263, 261, 259, 256, 256, 255, 320
), X19 = c(0, 242, 88, 278, 263, 304, 204, 183, 277, 247, 231, 
282, 195, 242, 242, 222, 256, 203, 264, 280, 263, 262, 299, 260, 
250, 241, 280, 271, 206, 230, 256, 283, 243, 277, 280, 267, 274, 
264, 269, 271, 250, 243, 254, 253, 261, 254, 259, 255, 255, 320
), X20 = c(0, 400, 286, 306, 284, 204, 217, 202, 226, 226, 266, 
288, 239, 246, 301, 287, 277, 263, 284, 280, 299, 288, 270, 279, 
259, 244, 240, 279, 280, 256, 300, 263, 281, 281, 251, 234, 267, 
251, 275, 253, 265, 262, 261, 250, 259, 254, 256, 257, 253, 320
), area = c(0, 84.298625572678, 333.194502290712, 749.687630154103, 
1332.77800916285, 2082.46563931695, 3000.75052061641, 4081.63265306123, 
5331.1120366514, 6747.18867138692, 8329.8625572678, 10079.133694294, 
11995.0020824656, 14077.4677217826, 16326.5306122449, 18742.1907538526, 
21324.4481466056, 24073.302790504, 26988.7546855477, 30070.8038317368, 
33319.4502290712, 36734.693877551, 40316.5347771762, 44064.9729279467, 
47980.0083298626, 52061.6409829238, 56309.8708871304, 60724.6980424823, 
65306.1224489796, 70054.1441066222, 74968.7630154102, 80049.9791753436, 
85297.7925864223, 90712.2032486464, 96293.2111620158, 102040.816326531, 
107955.018742191, 114035.818408996, 120283.215326947, 126600.209496043, 
133277.800916285, 140024.989587672, 146938.775510204, 154019.158683882, 
161266.139108705, 168679.716784673, 176259.891711787, 184006.663890046, 
191920.03331945, 2e+05), mean = c(0, 239.95, 233.95, 202.4, 256.25, 
232.1, 213.2, 227.3, 253.5, 272.9, 260.85, 257.55, 237.2, 253.25, 
250.3, 255.1, 268.2, 258, 262.6, 300.95, 270.35, 252.85, 270.35, 
270.55, 270.4, 262.65, 261.25, 263.75, 256.45, 261.1, 260.1, 
262.6, 250, 262.15, 265.7, 265.6, 267.55, 261.65, 260.9, 260.55, 
258.5, 253.15, 256.65, 257.65, 259.75, 254.5, 255.4, 265.1, 254.4, 
335), slope = c(0.0220147145695401, 0.0220147145695401, 
0.0220147145695401, 0.0220147145695401, 0.0220147145695401, 0.0220147145695401, 
0.0220147145695401, 0.0220147145695401, 0.0220147145695401, 0.0220147145695401, 
0.0220147145695401, 0.0220147145695401, 0.0220147145695401, 0.0220147145695401, 
0.0220147145695401, 0.0220147145695401, 0.0220147145695401, 0.0220147145695401, 
0.0220147145695401, 0.0220147145695401, 0.0220147145695401, 0.0220147145695401, 
0.0220147145695401, 0.0220147145695401, 0.0220147145695401, 0.0220147145695401, 
0.0220147145695401, 0.0220147145695401, 0.0220147145695401, 0.0220147145695401, 
0.0220147145695401, 0.0220147145695401, 0.0220147145695401, 0.0220147145695401, 
0.0220147145695401, 0.0220147145695401, 0.0220147145695401, 0.0220147145695401, 
0.0220147145695401, 0.0220147145695401, 0.0220147145695401, 0.0220147145695401, 
0.0220147145695401, 0.0220147145695401, 0.0220147145695401, 0.0220147145695401, 
0.0220147145695401, 0.0220147145695401, 0.0220147145695401, 0.0220147145695401
)), row.names = c(NA, -50L), class = "data.frame")
> 

如何解决此问题? 非常感谢您的帮助!

1 个答案:

答案 0 :(得分:0)

我认为您首先需要对函数进行一些修改:

coefLM <- function(x) {
  coef(lm(log(mean+1) ~ log(area+1), data=x))[2]
}

您在meanarea中拥有0,因此为它们添加一个开始(或使用不同的转换)将使之成为开始,因此您不会丢失这些观察结果。您还需要让x(您要传入的数据)成为模型中的数据。

要进行计算,我建议使用sapply(),这基本上是一种遍历值的方法。您可以遍历值1:nrow(tar.un_sap.10),每次传递coefLM()函数时,直到该行号的数据。

tar.un_sap.10$slope=sapply(1:nrow(tar.un_sap.10), 
                           function(i)coefLM(tar.un_sap.10[1:i, ]))

这将产生以下结果:

tar.un_sap.10$slope
# [1]        NA 1.2368375 1.0122426 0.8672056 0.7885012 0.7211055
# [7] 0.6636478 0.6202517 0.5881208 0.5626241 0.5382451 0.5160296
# [13] 0.4937550 0.4754918 0.4585715 0.4435312 0.4307508 0.4180439
# [19] 0.4065729 0.3964291 0.3868378 0.3766481 0.3681876 0.3601827
# [25] 0.3525755 0.3449165 0.3375712 0.3307222 0.3238242 0.3174924
# [31] 0.3109094 0.3052338 0.2992165 0.2940177 0.2891791 0.2845205
# [37] 0.2801134 0.2756314 0.2712853 0.2670833 0.2629539 0.2587712
# [43] 0.2548675 0.2511334 0.2475967 0.2439944 0.2405411 0.2372216
# [49] 0.2339506 0.2326499