Linear regression of time series over multiple columns

試著忘記壹切 提交于 2019-12-11 17:54:08

问题


I have the following problem. I want to compute the regression of an annual time series in matrix form. In total, I have 56 time series I extracted from gridpoints of an area I want to examine, so that I've got 56 values per year. I've plotted all values as points in a figure. Now I want to add a regression line to this figure, which contains all data.

My goal is to compute the regression for the whole matrix.

library(zoo)

pdf(file="/home/user/name.pdf", pointsize=20, onefile = FALSE, width=18, height=11, paper = "special")
plot(mat.zoo[,1], pch=20, type="p", ylim=c(8,max(mat.zoo)),
     yaxt = "n", xaxt = "n", lwd = 1.5, main = "Some title", 
     ylab = "ylabtext", xlab ="", col = "black")

tt <- time(mat.zoo)
ix <- seq(1, length(tt), by=1) #every year a tick
labs <- format(tt[ix])
axis(side = 1, at = tt[ix], labels = labs,  tcl = -0.7, cex.axis = 1)

for (i in 2:ncol(mat.zoo)) {  
  #plot every column
  points(mat.zoo[,i], pch=20, lwd = 1.5)
}

## create ticks at every first y value 
axis(side = 2, at = seq(0, max(mat.zoo), by = 1), labels = FALSE)
iy <- seq(0, max(mat.zoo), 2)
axis(side = 2, at = iy, cex.axis = 1)

#this line doesn't work
abline( lm(mat.zoo ~ tt), col="light blue", lwd=3 )

dev.off()

figure:

http://i.imgur.com/Ny5ERj1.png

Some sample data, if I use dput()

structure(c(14.6108611110572, 15.0943707315979, 16.4246753285039, 
15.4777258564571, 15.3910647660091, 14.9576052728563, 14.577379912167, 
15.6818364395762, 15.3935454316438, 14.6986382632628, 14.9616178291156, 
14.6208764396762, 17.4073263088521, 16.3932907105236, 16.4711871055354, 
15.7165524844793, 15.910687798697, 15.2800531253961, 16.2585353059321, 
14.9642915613775, 15.5682258772038, 15.7581733353644, 16.3600126905042, 
15.9906231843285, 16.4740591781654, 16.6207709477207, 16.7107736486755, 
15.3495937400046, 15.081738134456, 17.8213361743775, 17.0073514277019, 
16.0639354869614, 15.7564229038361, 16.4711872385234, 16.1474456418556, 
16.1012429675788, 14.935862417968, 14.649232718741, 14.7248073786802, 
16.3713171174875, 16.5047383689279, 15.6553509485205, 15.8069612127912, 
15.0880755914505, 15.9605131388024, 15.1647608142339, 15.0206531342878, 
15.8533914806642, 16.1936611693424, 14.4341552680467, 15.0030002589802, 
16.2373036559464, 15.4563912060316, 15.7540478676699, 14.4544119112367, 
14.1481450642128, 15.8808048538232, 15.3109864936677, 14.6184823877101, 
14.759740997088, 14.4554473653311, 17.1869089559961, 16.032779242263, 
15.9154018617995, 15.7003191635601, 16.1782858717824, 15.005330870126, 
16.1074524252519, 15.387333324397, 15.4238444378858, 15.7384875972114, 
16.3306448173221, 15.8050630623362, 16.5357139417134, 15.7318155157117, 
16.6027108391727, 15.3521994865507, 14.6028494060288, 17.0695642066462, 
16.5601941440799, 16.0704699986853, 15.9527367313925, 15.8492898967367, 
15.8094909404139, 15.9223122951851, 14.7427484210632, 14.3087395573591, 
14.9164340340289, 16.5109060631933, 16.1756705822203, 15.6869363317253, 
15.302941446409, 14.7871569748782, 16.2405108282472, 14.9030204259848, 
15.1076128392841, 15.7835364136346, 16.2406871099921, 13.9434587358454, 
14.8761562136977, 16.5604955686145, 15.3055531556642, 15.528200122034, 
14.3683664247369, 14.8660671257497, 16.2483828855783, 15.5912163679296, 
14.5206758668367, 15.0572249827849, 14.2126710362867, 16.7430589790551, 
15.913830135814, 15.5309377608968, 15.4301657033962, 16.1024796689616, 
14.9412190564665, 15.5415580911515, 15.6185795702858, 15.246965832492, 
15.5331896889331, 16.0527261022428, 16.2496153707101, 16.1013003488606, 
15.4012992267683, 16.6433171425044, 15.3443805149379, 15.0832591147848, 
17.2409394600713, 16.3670395392329, 15.8028463074112, 16.230362038712, 
15.8533914346074, 16.0962730847646, 15.4780493166121, 14.7644838005869, 
14.0160611132642, 14.7363498686371, 16.5339052116905, 16.1142787861115, 
15.1343982378726, 15.0479243093561, 14.8394739356758, 16.2015436792666, 
14.8852279610404, 15.171354759099, 15.8823805835669, 16.082598536468, 
13.6882801770178, 15.1822273858009, 16.7314060285488, 15.1822255101789, 
15.6470428935629, 14.6219009419668, 14.5344414346855, 17.0856674074961, 
15.6276761713817, 14.9656277726849, 15.0416098763217, 14.8660691394921, 
16.8350823196938, 15.7276830387531, 15.6464050524098, 15.7889210440969, 
15.8260661780512, 15.0685110014866, 15.5003231376182, 15.1818971179834, 
15.2523764253926, 15.2397513974873, 16.4076206985996, 16.2609962527472, 
15.9563455712026, 14.6758308266033, 15.9928106586864, 15.3388404382473, 
15.3352069271315, 17.491711796634, 16.3110401122382, 15.6722694212894, 
16.0979740581832, 15.9314161173117, 15.5309368794019, 15.5425227514293, 
14.8653903137068, 14.3680198631293, 14.6030824713595, 16.7764724794758, 
15.7590262768357, 14.9562687841841, 14.7258278360439, 15.00733114536, 
16.1086825085102, 14.8246425174662, 15.6697167018262, 15.5235314139726, 
15.810753562246), .Dim = c(49L, 4L), .Dimnames = list(c("1", 
"367", "732", "1097", "1462", "1828", "2193", "2558", "2923", 
"3289", "3654", "4019", "4384", "4750", "5115", "5480", "5845", 
"6211", "6576", "6941", "7306", "7672", "8037", "8402", "8767", 
"9133", "9498", "9863", "10228", "10594", "10959", "11324", "11689", 
"12055", "12420", "12785", "13150", "13516", "13881", "14246", 
"14611", "14977", "15342", "15707", "16072", "16438", "16803", 
"17168", "17533"), c("GB.1", "GB.2", "GB.3", "GB.4")), index = c(1960, 
1961, 1962, 1963, 1964, 1965, 1966, 1967, 1968, 1969, 1970, 1971, 
1972, 1973, 1974, 1975, 1976, 1977, 1978, 1979, 1980, 1981, 1982, 
1983, 1984, 1985, 1986, 1987, 1988, 1989, 1990, 1991, 1992, 1993, 
1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 
2005, 2006, 2007, 2008), class = "zoo")

回答1:


Try modifying your lm statement to

lm(as.vector(mat.zoo) ~ rep(tt, length.out = length(mat.zoo)))


来源:https://stackoverflow.com/questions/24656647/linear-regression-of-time-series-over-multiple-columns

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!