我在处理R中的时间序列时遇到问题
#--------------read data
wb = loadWorkbook("Countries_Europe_Prices.xlsx")
df = readWorksheet(wb, sheet="Sheet2")
x <- df$Year
y <- df$Index1
y <- lag(y, 1, na.pad = TRUE)
cbind(x, y)
Run Code Online (Sandbox Code Playgroud)
它给了我以下输出:
x y
[1,] 1974 NA
[2,] 1975 50.8
[3,] 1976 51.9
[4,] 1977 54.8
[5,] 1978 58.8
[6,] 1979 64.0
[7,] 1980 68.8
[8,] 1981 73.6
[9,] 1982 74.3
[10,] 1983 74.5
[11,] 1984 72.9
[12,] 1985 72.1
[13,] 1986 72.3
[14,] 1987 71.7
[15,] 1988 72.9
[16,] 1989 75.3
[17,] 1990 81.2
[18,] …Run Code Online (Sandbox Code Playgroud) 我想对人口数据执行(立方)样条插值,以将年度数据"转换"为季度数据.我知道有相当多的缺陷,但我需要这样做.
这是我的代码示例(使用通用输入数据):
#--------------spline interpolation
x = c(1973:2014)
population = seq(500000, 600000, length.out = 42)
list = spline(x, population, n = 4*length(x), method = "fmm",
xmin = min(x), xmax = max(x), ties = mean)
x_spline = list$x
pop_spline = list$y
Run Code Online (Sandbox Code Playgroud)
如何定义样条曲线是"每季度"计算的,换句话说是1973.25,1973.5,1973.75,1974等?很抱歉不是统计专家:将年度数据"转换"成季度数据的最佳方法是什么:"fmm","natural","periodic","monoH.FC"或"hyman"?假设人口增长在一年中均匀分布.
致以最诚挚的问候和许多感谢!
虽然我发现了几个类似的问题,但我找不到基本R中我的问题的简单解决方案.我想从一组数据(这里是y值)计算同比百分比变化,并将此"Delta"系列添加为我的数据框的新列.
例如:
>x = c(2000,2001,2002,2003,2004,2005,2006)
>y = c(100,104,106,108,112,115,121)
>df = data.frame(x,y)
Run Code Online (Sandbox Code Playgroud)
如果我通过读取.csv文件加载数据该怎么办?我必须将此数据转换为数据框吗?
在Windows 8系统上使用RStudio会显示以下错误消息:
Error in savePlot(filename = "123", type = c("png"), device = dev.cur()) :
can only copy from 'windows' devices
Run Code Online (Sandbox Code Playgroud)
如果我windows()在savePlot之前的行中写入,错误消息消失,但该图为“空”。如果我使用R代替RStudio,则不存在该问题。
除了“不使用RStudio”之外,还有其他解决方案吗?最好的祝福
编辑:这是更多的原始代码:
#--------------create plot
x <- df$Year
y <- df$Index1970
par(family="serif", font=1, cex=1)
xrange <- range(x, na.rm=TRUE)
yrange <- range(y, na.rm=TRUE)
plot(xrange, yrange, type="n", xlab="Year",
ylab="Price index, 1970=100" )
lines(x, y, col="black", lwd=3)
title("Belgium Property Prices from 1970-2013")
grid(nx = NULL, ny = NULL, col = "lightgray", lty = "dotted",
lwd = par("lwd"), equilogs = TRUE)
savePlot(filename="D:/...RPlots/Belgium_Prices_from_1970-2013", …Run Code Online (Sandbox Code Playgroud)