#time in julia prints different time - julia

I started translating some of my Matlab scripts to Julia and trying to compare the performance. Here I came across a seemly simple question, but I did not find the answer by a quick search. When I run the Julia script from console, i.e. type "julia test_FAS2SA.jl", it prints "3.116226 seconds" on the screen, which is almost 30 times the running time of the corresponding matlab script.
Then I entered the julia environment and type "include("test_FAS2SA.jl")", it prints "1.368429 seconds". However, when I just type "#time SA,PF=FAS2SA(FAS1,f_FAS,10.0,f_SA, 0.05);", it prints "0.033920 seconds". I am wondering what is the real running time for function FAS2SA and different time is printed.
My platform is Windows 7 professional, Julia 1.10. The script "test_FAS2SA.jl" and input files are pasted as below.
# test JuliaFunc
using DelimitedFiles
# import Pkg; Pkg.add("time")
using QuadGK
function getH_SDF(D,fn,freq)
# PURPOSE
# get transfer function of a SDOF oscilator
# acc response to acc excitation
# INPUT
# D - damping ratio
# fn - fundamental frequency of the SDOF (Hz)
# freq - frequency array (Hz)
# OUTPUT
# absH - Amplitude of the transfer function
N=length(freq)
H=complex(zeros(N))
for i=1:N
f=freq[i]
H[i]=(-fn^2)/((f^2-fn^2)-(2*f*fn*D*1im))
end
return abs.(H)
end
function trapz(x, y)
# Trapezoidal integration rule
local n = length(x)
if (length(y) != n)
error("Vectors 'x', 'y' must be of same length")
end
r = 0.0
if n == 1; return r; end
for i in 2:n
r += (x[i] - x[i-1]) * (y[i] + y[i-1])
end
#= correction -h^2/12 * (f'(b) - f'(a))
ha = x[2] - x[1]
he = x[end] - x[end-1]
ra = (y[2] - y[1]) / ha
re = (y[end] - y[end-1]) / he
r/2 - ha*he/12 * (re - ra)
=#
return r/2
end
function getmoment(Y,f,n)
N=length(Y)
S=zeros(N)
for i=1:N
S[i]=(2.0*pi*f[i])^n*Y[i]^2
end
return 2.0*trapz(f,S)
end
function getXmax(Y,F,T_rms)
moment0=getmoment(Y,F,0)
moment2=getmoment(Y,F,2)
moment4=getmoment(Y,F,4)
Y_rms=(moment0/T_rms)^0.5 # rms value
Ne=1.0/pi*(moment4/moment2)^0.5*T_rms # number of extrema
xi=(moment2^2/moment0/moment4)^0.5 # number of width
#temp=(2*log(xi*Ne))^0.5
#PF(i)=temp+0.5772/temp
Func(x)=1-(1-xi*exp(-x^2))^Ne
#Fun=#(x) 1-(1-xi.*exp(-x.^2)).^Ne
Tmp,e = QuadGK.quadgk(Func,0,Inf)
PF = sqrt(2.0)*Tmp
Ymax=PF*Y_rms
return Ymax
end
function FAS2SA(FAS,F,T_gm,freq, damping)
# INPUT
# FAS - Fourier Amplitude Spectrum
# F - Frquency array corresponding FAS (Hz)
# T_gm - ground motion duration (sec)
# freq - fundametnal frequency of SDOF oscilator (Hz)
# damping - damping ratio of SDOF oscilator
# OUTPUT
# SA - Spectral acceleration (g)
# PF - Peak Factor
n_T=length(freq)
SA=zeros(n_T)
PF=zeros(n_T)
for i=1:n_T
H=getH_SDF(damping,freq[i],F) # get transfer function of SDOF oscilator
Y=FAS.*H # FAS of acc response of oscilator
Tn=1.0/freq[i] # fundamantal period
T0=Tn/2.0/pi/damping #oscilator duration
gamma=T_gm/Tn
n_val=3.0
alpha=1.0 / 3.0
T_rms=T_gm+T0*(gamma^n_val/(gamma^n_val+alpha)) # total duration
moment0=getmoment(Y,F,0)
moment2=getmoment(Y,F,2)
moment4=getmoment(Y,F,4)
Y_rms=(moment0/T_rms)^0.5 # rms value
Ne=1/pi*(moment4/moment2)^0.5*T_gm # number of extrema
# note that here use T_gm not T_rms (Refer to Boore and Joyer, 1984)
xi=(moment2^2/moment0/moment4)^0.5 # number of width
#temp=(2*log(xi*Ne))^0.5
#PF(i)=temp+0.5772/temp
Fun(x)= 1-(1-xi*exp(-x^2))^Ne
Q_tmp,e_tmp = QuadGK.quadgk(Fun,0,Inf)
PF1 = 2^0.5*Q_tmp
Ymax=PF1*Y_rms
SA[i]=Ymax
PF[i]=PF1
end
return (SA,PF)
end
T=readdlm("period.dat")
f_SA=1.0 ./ T
#println(vs)
FAS=readdlm("FAS.dat")
f_FAS=FAS[:,1]
FAS1=FAS[:,2]
println(#time SA,PF=FAS2SA(FAS1,f_FAS,10.0,f_SA, 0.05))
file "period.dat"
0.01
0.01103674
0.012180962
0.01344381
0.014837583
0.016375854
0.018073604
0.019947366
0.022015388
0.024297811
0.026816861
0.029597071
0.032665516
0.036052079
0.039789741
0.043914901
0.048467732
0.053492574
0.05903836
0.065159101
0.071914402
0.079370053
0.08759866
0.096680359
0.106703594
0.117765978
0.129975243
0.14345029
0.158322349
0.174736253
0.192851851
0.212845565
0.234912106
0.259266373
0.286145543
0.315811383
0.348552798
0.384688645
0.424570838
0.468587776
0.517168124
0.570784988
0.629960525
0.695271024
0.76735252
0.84690699
0.934709187
1.031614184
1.138565706
1.256605315
1.386882557
1.530666156
1.689356369
1.864498624
2.057798569
2.271138684
2.506596612
2.766465394
3.053275801
3.369820977
3.719183643
4.104766118
4.530323453
5
file "FAS.dat"
5.00E-02 7.29E-03
5.03E-02 7.38E-03
5.07E-02 7.46E-03
5.10E-02 7.55E-03
5.14E-02 7.63E-03
5.17E-02 7.72E-03
5.21E-02 7.81E-03
5.24E-02 7.90E-03
5.28E-02 7.99E-03
5.31E-02 8.08E-03
5.35E-02 8.17E-03
5.39E-02 8.27E-03
5.42E-02 8.36E-03
5.46E-02 8.46E-03
5.50E-02 8.55E-03
5.53E-02 8.65E-03
5.57E-02 8.75E-03
5.61E-02 8.84E-03
5.65E-02 8.94E-03
5.68E-02 9.04E-03
5.72E-02 9.14E-03
5.76E-02 9.24E-03
5.80E-02 9.35E-03
5.84E-02 9.45E-03
5.88E-02 9.55E-03
5.92E-02 9.66E-03
5.96E-02 9.76E-03
6.00E-02 9.87E-03
6.04E-02 9.98E-03
6.08E-02 1.01E-02
6.12E-02 1.02E-02
6.16E-02 1.03E-02
6.21E-02 1.04E-02
6.25E-02 1.05E-02
6.29E-02 1.06E-02
6.33E-02 1.08E-02
6.38E-02 1.09E-02
6.42E-02 1.10E-02
6.46E-02 1.11E-02
6.51E-02 1.12E-02
6.55E-02 1.13E-02
6.59E-02 1.15E-02
6.64E-02 1.16E-02
6.68E-02 1.17E-02
6.73E-02 1.18E-02
6.78E-02 1.19E-02
6.82E-02 1.21E-02
6.87E-02 1.22E-02
6.91E-02 1.23E-02
6.96E-02 1.24E-02
7.01E-02 1.26E-02
7.06E-02 1.27E-02
7.10E-02 1.28E-02
7.15E-02 1.30E-02
7.20E-02 1.31E-02
7.25E-02 1.32E-02
7.30E-02 1.33E-02
7.35E-02 1.35E-02
7.40E-02 1.36E-02
7.45E-02 1.37E-02
7.50E-02 1.39E-02
7.55E-02 1.40E-02
7.60E-02 1.42E-02
7.65E-02 1.43E-02
7.70E-02 1.44E-02
7.76E-02 1.46E-02
7.81E-02 1.47E-02
7.86E-02 1.49E-02
7.91E-02 1.50E-02
7.97E-02 1.51E-02
8.02E-02 1.53E-02
8.08E-02 1.54E-02
8.13E-02 1.56E-02
8.19E-02 1.57E-02
8.24E-02 1.59E-02
8.30E-02 1.60E-02
8.35E-02 1.62E-02
8.41E-02 1.63E-02
8.47E-02 1.65E-02
8.52E-02 1.66E-02
8.58E-02 1.68E-02
8.64E-02 1.69E-02
8.70E-02 1.71E-02
8.76E-02 1.72E-02
8.82E-02 1.74E-02
8.88E-02 1.75E-02
8.94E-02 1.77E-02
9.00E-02 1.78E-02
9.06E-02 1.80E-02
9.12E-02 1.81E-02
9.18E-02 1.83E-02
9.24E-02 1.85E-02
9.31E-02 1.86E-02
9.37E-02 1.88E-02
9.43E-02 1.89E-02
9.50E-02 1.91E-02
9.56E-02 1.92E-02
9.63E-02 1.94E-02
9.69E-02 1.96E-02
9.76E-02 1.97E-02
9.82E-02 1.99E-02
9.89E-02 2.01E-02
9.96E-02 2.02E-02
1.00E-01 2.04E-02
1.01E-01 2.05E-02
1.02E-01 2.07E-02
1.02E-01 2.09E-02
1.03E-01 2.10E-02
1.04E-01 2.12E-02
1.04E-01 2.14E-02
1.05E-01 2.15E-02
1.06E-01 2.17E-02
1.07E-01 2.19E-02
1.07E-01 2.20E-02
1.08E-01 2.22E-02
1.09E-01 2.24E-02
1.09E-01 2.25E-02
1.10E-01 2.27E-02
1.11E-01 2.29E-02
1.12E-01 2.30E-02
1.12E-01 2.32E-02
1.13E-01 2.34E-02
1.14E-01 2.36E-02
1.15E-01 2.37E-02
1.16E-01 2.39E-02
1.16E-01 2.41E-02
1.17E-01 2.42E-02
1.18E-01 2.44E-02
1.19E-01 2.46E-02
1.19E-01 2.48E-02
1.20E-01 2.49E-02
1.21E-01 2.51E-02
1.22E-01 2.53E-02
1.23E-01 2.54E-02
1.24E-01 2.56E-02
1.24E-01 2.58E-02
1.25E-01 2.60E-02
1.26E-01 2.61E-02
1.27E-01 2.63E-02
1.28E-01 2.65E-02
1.29E-01 2.66E-02
1.30E-01 2.68E-02
1.30E-01 2.70E-02
1.31E-01 2.72E-02
1.32E-01 2.73E-02
1.33E-01 2.75E-02
1.34E-01 2.77E-02
1.35E-01 2.79E-02
1.36E-01 2.80E-02
1.37E-01 2.82E-02
1.38E-01 2.84E-02
1.39E-01 2.85E-02
1.40E-01 2.87E-02
1.40E-01 2.89E-02
1.41E-01 2.91E-02
1.42E-01 2.92E-02
1.43E-01 2.94E-02
1.44E-01 2.96E-02
1.45E-01 2.97E-02
1.46E-01 2.99E-02
1.47E-01 3.01E-02
1.48E-01 3.03E-02
1.49E-01 3.04E-02
1.50E-01 3.06E-02
1.51E-01 3.08E-02
1.52E-01 3.09E-02
1.53E-01 3.11E-02
1.54E-01 3.13E-02
1.55E-01 3.14E-02
1.57E-01 3.16E-02
1.58E-01 3.18E-02
1.59E-01 3.20E-02
1.60E-01 3.21E-02
1.61E-01 3.23E-02
1.62E-01 3.24E-02
1.63E-01 3.26E-02
1.64E-01 3.27E-02
1.65E-01 3.29E-02
1.66E-01 3.30E-02
1.67E-01 3.32E-02
1.69E-01 3.34E-02
1.70E-01 3.35E-02
1.71E-01 3.37E-02
1.72E-01 3.38E-02
1.73E-01 3.39E-02
1.74E-01 3.41E-02
1.76E-01 3.42E-02
1.77E-01 3.44E-02
1.78E-01 3.45E-02
1.79E-01 3.47E-02
1.80E-01 3.48E-02
1.82E-01 3.50E-02
1.83E-01 3.51E-02
1.84E-01 3.53E-02
1.85E-01 3.54E-02
1.87E-01 3.56E-02
1.88E-01 3.57E-02
1.89E-01 3.58E-02
1.90E-01 3.60E-02
1.92E-01 3.61E-02
1.93E-01 3.63E-02
1.94E-01 3.64E-02
1.96E-01 3.65E-02
1.97E-01 3.67E-02
1.98E-01 3.68E-02
2.00E-01 3.69E-02
2.01E-01 3.71E-02
2.02E-01 3.72E-02
2.04E-01 3.73E-02
2.05E-01 3.75E-02
2.06E-01 3.76E-02
2.08E-01 3.77E-02
2.09E-01 3.79E-02
2.11E-01 3.80E-02
2.12E-01 3.81E-02
2.14E-01 3.83E-02
2.15E-01 3.84E-02
2.16E-01 3.85E-02
2.18E-01 3.87E-02
2.19E-01 3.88E-02
2.21E-01 3.89E-02
2.22E-01 3.90E-02
2.24E-01 3.92E-02
2.25E-01 3.93E-02
2.27E-01 3.94E-02
2.28E-01 3.95E-02
2.30E-01 3.97E-02
2.32E-01 3.98E-02
2.33E-01 3.99E-02
2.35E-01 4.00E-02
2.36E-01 4.01E-02
2.38E-01 4.03E-02
2.40E-01 4.04E-02
2.41E-01 4.05E-02
2.43E-01 4.06E-02
2.44E-01 4.07E-02
2.46E-01 4.09E-02
2.48E-01 4.10E-02
2.49E-01 4.11E-02
2.51E-01 4.12E-02
2.53E-01 4.13E-02
2.55E-01 4.14E-02
2.56E-01 4.16E-02
2.58E-01 4.17E-02
2.60E-01 4.18E-02
2.61E-01 4.19E-02
2.63E-01 4.20E-02
2.65E-01 4.21E-02
2.67E-01 4.22E-02
2.69E-01 4.23E-02
2.70E-01 4.24E-02
2.72E-01 4.25E-02
2.74E-01 4.27E-02
2.76E-01 4.28E-02
2.78E-01 4.29E-02
2.80E-01 4.30E-02
2.82E-01 4.31E-02
2.84E-01 4.32E-02
2.85E-01 4.33E-02
2.87E-01 4.34E-02
2.89E-01 4.35E-02
2.91E-01 4.36E-02
2.93E-01 4.37E-02
2.95E-01 4.38E-02
2.97E-01 4.39E-02
2.99E-01 4.40E-02
3.01E-01 4.41E-02
3.03E-01 4.42E-02
3.05E-01 4.43E-02
3.07E-01 4.44E-02
3.10E-01 4.45E-02
3.12E-01 4.46E-02
3.14E-01 4.47E-02
3.16E-01 4.48E-02
3.18E-01 4.49E-02
3.20E-01 4.50E-02
3.22E-01 4.51E-02
3.25E-01 4.52E-02
3.27E-01 4.53E-02
3.29E-01 4.54E-02
3.31E-01 4.55E-02
3.33E-01 4.56E-02
3.36E-01 4.57E-02
3.38E-01 4.58E-02
3.40E-01 4.59E-02
3.43E-01 4.59E-02
3.45E-01 4.60E-02
3.47E-01 4.61E-02
3.50E-01 4.62E-02
3.52E-01 4.63E-02
3.54E-01 4.64E-02
3.57E-01 4.65E-02
3.59E-01 4.66E-02
3.62E-01 4.67E-02
3.64E-01 4.68E-02
3.66E-01 4.69E-02
3.69E-01 4.69E-02
3.71E-01 4.70E-02
3.74E-01 4.71E-02
3.77E-01 4.72E-02
3.79E-01 4.73E-02
3.82E-01 4.74E-02
3.84E-01 4.75E-02
3.87E-01 4.76E-02
3.89E-01 4.76E-02
3.92E-01 4.77E-02
3.95E-01 4.78E-02
3.97E-01 4.79E-02
4.00E-01 4.80E-02
4.03E-01 4.81E-02
4.06E-01 4.82E-02
4.08E-01 4.82E-02
4.11E-01 4.83E-02
4.14E-01 4.84E-02
4.17E-01 4.85E-02
4.19E-01 4.86E-02
4.22E-01 4.87E-02
4.25E-01 4.88E-02
4.28E-01 4.88E-02
4.31E-01 4.89E-02
4.34E-01 4.90E-02
4.37E-01 4.91E-02
4.40E-01 4.92E-02
4.43E-01 4.93E-02
4.46E-01 4.93E-02
4.49E-01 4.94E-02
4.52E-01 4.95E-02
4.55E-01 4.96E-02
4.58E-01 4.97E-02
4.61E-01 4.97E-02
4.64E-01 4.98E-02
4.67E-01 4.99E-02
4.71E-01 5.00E-02
4.74E-01 5.01E-02
4.77E-01 5.02E-02
4.80E-01 5.02E-02
4.83E-01 5.03E-02
4.87E-01 5.04E-02
4.90E-01 5.05E-02
4.93E-01 5.06E-02
4.97E-01 5.06E-02
5.00E-01 5.07E-02
5.03E-01 5.08E-02
5.07E-01 5.09E-02
5.10E-01 5.10E-02
5.14E-01 5.10E-02
5.17E-01 5.11E-02
5.21E-01 5.11E-02
5.24E-01 5.12E-02
5.28E-01 5.12E-02
5.31E-01 5.13E-02
5.35E-01 5.13E-02
5.39E-01 5.14E-02
5.42E-01 5.15E-02
5.46E-01 5.15E-02
5.50E-01 5.16E-02
5.53E-01 5.16E-02
5.57E-01 5.17E-02
5.61E-01 5.17E-02
5.65E-01 5.18E-02
5.68E-01 5.18E-02
5.72E-01 5.19E-02
5.76E-01 5.19E-02
5.80E-01 5.20E-02
5.84E-01 5.20E-02
5.88E-01 5.21E-02
5.92E-01 5.21E-02
5.96E-01 5.22E-02
6.00E-01 5.22E-02
6.04E-01 5.23E-02
6.08E-01 5.24E-02
6.12E-01 5.24E-02
6.16E-01 5.25E-02
6.21E-01 5.25E-02
6.25E-01 5.26E-02
6.29E-01 5.26E-02
6.33E-01 5.27E-02
6.38E-01 5.27E-02
6.42E-01 5.28E-02
6.46E-01 5.28E-02
6.51E-01 5.29E-02
6.55E-01 5.29E-02
6.59E-01 5.30E-02
6.64E-01 5.30E-02
6.68E-01 5.31E-02
6.73E-01 5.31E-02
6.78E-01 5.32E-02
6.82E-01 5.32E-02
6.87E-01 5.33E-02
6.91E-01 5.33E-02
6.96E-01 5.34E-02
7.01E-01 5.34E-02
7.06E-01 5.35E-02
7.10E-01 5.35E-02
7.15E-01 5.36E-02
7.20E-01 5.36E-02
7.25E-01 5.37E-02
7.30E-01 5.37E-02
7.35E-01 5.38E-02
7.40E-01 5.38E-02
7.45E-01 5.39E-02
7.50E-01 5.39E-02
7.55E-01 5.40E-02
7.60E-01 5.40E-02
7.65E-01 5.41E-02
7.70E-01 5.41E-02
7.76E-01 5.42E-02
7.81E-01 5.42E-02
7.86E-01 5.43E-02
7.91E-01 5.43E-02
7.97E-01 5.44E-02
8.02E-01 5.44E-02
8.08E-01 5.45E-02
8.13E-01 5.45E-02
8.19E-01 5.46E-02
8.24E-01 5.46E-02
8.30E-01 5.47E-02
8.35E-01 5.47E-02
8.41E-01 5.47E-02
8.47E-01 5.48E-02
8.52E-01 5.48E-02
8.58E-01 5.48E-02
8.64E-01 5.49E-02
8.70E-01 5.49E-02
8.76E-01 5.49E-02
8.82E-01 5.50E-02
8.88E-01 5.50E-02
8.94E-01 5.50E-02
9.00E-01 5.51E-02
9.06E-01 5.51E-02
9.12E-01 5.51E-02
9.18E-01 5.51E-02
9.24E-01 5.52E-02
9.31E-01 5.52E-02
9.37E-01 5.52E-02
9.43E-01 5.53E-02
9.50E-01 5.53E-02
9.56E-01 5.53E-02
9.63E-01 5.53E-02
9.69E-01 5.54E-02
9.76E-01 5.54E-02
9.82E-01 5.54E-02
9.89E-01 5.55E-02
9.96E-01 5.55E-02
1.00E+00 5.55E-02
1.01E+00 5.56E-02
1.02E+00 5.56E-02
1.02E+00 5.56E-02
1.03E+00 5.56E-02
1.04E+00 5.57E-02
1.04E+00 5.57E-02
1.05E+00 5.57E-02
1.06E+00 5.58E-02
1.07E+00 5.58E-02
1.07E+00 5.58E-02
1.08E+00 5.58E-02
1.09E+00 5.59E-02
1.09E+00 5.59E-02
1.10E+00 5.59E-02
1.11E+00 5.60E-02
1.12E+00 5.60E-02
1.12E+00 5.60E-02
1.13E+00 5.60E-02
1.14E+00 5.61E-02
1.15E+00 5.61E-02
1.16E+00 5.61E-02
1.16E+00 5.62E-02
1.17E+00 5.62E-02
1.18E+00 5.62E-02
1.19E+00 5.62E-02
1.19E+00 5.63E-02
1.20E+00 5.63E-02
1.21E+00 5.63E-02
1.22E+00 5.64E-02
1.23E+00 5.64E-02
1.24E+00 5.64E-02
1.24E+00 5.64E-02
1.25E+00 5.65E-02
1.26E+00 5.65E-02
1.27E+00 5.65E-02
1.28E+00 5.65E-02
1.29E+00 5.65E-02
1.30E+00 5.65E-02
1.30E+00 5.65E-02
1.31E+00 5.65E-02
1.32E+00 5.65E-02
1.33E+00 5.65E-02
1.34E+00 5.65E-02
1.35E+00 5.65E-02
1.36E+00 5.65E-02
1.37E+00 5.65E-02
1.38E+00 5.65E-02
1.39E+00 5.66E-02
1.40E+00 5.66E-02
1.40E+00 5.66E-02
1.41E+00 5.66E-02
1.42E+00 5.66E-02
1.43E+00 5.66E-02
1.44E+00 5.66E-02
1.45E+00 5.66E-02
1.46E+00 5.66E-02
1.47E+00 5.66E-02
1.48E+00 5.66E-02
1.49E+00 5.66E-02
1.50E+00 5.66E-02
1.51E+00 5.66E-02
1.52E+00 5.66E-02
1.53E+00 5.66E-02
1.54E+00 5.66E-02
1.55E+00 5.66E-02
1.57E+00 5.66E-02
1.58E+00 5.66E-02
1.59E+00 5.66E-02
1.60E+00 5.66E-02
1.61E+00 5.66E-02
1.62E+00 5.66E-02
1.63E+00 5.66E-02
1.64E+00 5.66E-02
1.65E+00 5.66E-02
1.66E+00 5.66E-02
1.67E+00 5.67E-02
1.69E+00 5.67E-02
1.70E+00 5.67E-02
1.71E+00 5.67E-02
1.72E+00 5.67E-02
1.73E+00 5.67E-02
1.74E+00 5.67E-02
1.76E+00 5.67E-02
1.77E+00 5.67E-02
1.78E+00 5.67E-02
1.79E+00 5.67E-02
1.80E+00 5.67E-02
1.82E+00 5.67E-02
1.83E+00 5.67E-02
1.84E+00 5.66E-02
1.85E+00 5.66E-02
1.87E+00 5.66E-02
1.88E+00 5.66E-02
1.89E+00 5.66E-02
1.90E+00 5.66E-02
1.92E+00 5.66E-02
1.93E+00 5.66E-02
1.94E+00 5.66E-02
1.96E+00 5.66E-02
1.97E+00 5.66E-02
1.98E+00 5.66E-02
2.00E+00 5.66E-02
2.01E+00 5.66E-02
2.02E+00 5.66E-02
2.04E+00 5.66E-02
2.05E+00 5.66E-02
2.06E+00 5.66E-02
2.08E+00 5.66E-02
2.09E+00 5.66E-02
2.11E+00 5.66E-02
2.12E+00 5.66E-02
2.14E+00 5.66E-02
2.15E+00 5.66E-02
2.16E+00 5.65E-02
2.18E+00 5.65E-02
2.19E+00 5.65E-02
2.21E+00 5.65E-02
2.22E+00 5.65E-02
2.24E+00 5.65E-02
2.25E+00 5.65E-02
2.27E+00 5.65E-02
2.28E+00 5.64E-02
2.30E+00 5.64E-02
2.32E+00 5.63E-02
2.33E+00 5.62E-02
2.35E+00 5.62E-02
2.36E+00 5.61E-02
2.38E+00 5.61E-02
2.40E+00 5.60E-02
2.41E+00 5.60E-02
2.43E+00 5.59E-02
2.44E+00 5.58E-02
2.46E+00 5.58E-02
2.48E+00 5.57E-02
2.49E+00 5.57E-02
2.51E+00 5.56E-02
2.53E+00 5.55E-02
2.55E+00 5.55E-02
2.56E+00 5.54E-02
2.58E+00 5.54E-02
2.60E+00 5.53E-02
2.61E+00 5.52E-02
2.63E+00 5.52E-02
2.65E+00 5.51E-02
2.67E+00 5.50E-02
2.69E+00 5.50E-02
2.70E+00 5.49E-02
2.72E+00 5.48E-02
2.74E+00 5.48E-02
2.76E+00 5.47E-02
2.78E+00 5.46E-02
2.80E+00 5.46E-02
2.82E+00 5.45E-02
2.84E+00 5.44E-02
2.85E+00 5.44E-02
2.87E+00 5.43E-02
2.89E+00 5.42E-02
2.91E+00 5.42E-02
2.93E+00 5.41E-02
2.95E+00 5.40E-02
2.97E+00 5.39E-02
2.99E+00 5.39E-02
3.01E+00 5.38E-02
3.03E+00 5.37E-02
3.05E+00 5.37E-02
3.07E+00 5.36E-02
3.10E+00 5.35E-02
3.12E+00 5.34E-02
3.14E+00 5.34E-02
3.16E+00 5.33E-02
3.18E+00 5.32E-02
3.20E+00 5.31E-02
3.22E+00 5.29E-02
3.25E+00 5.28E-02
3.27E+00 5.27E-02
3.29E+00 5.26E-02
3.31E+00 5.24E-02
3.33E+00 5.23E-02
3.36E+00 5.22E-02
3.38E+00 5.20E-02
3.40E+00 5.19E-02
3.43E+00 5.18E-02
3.45E+00 5.16E-02
3.47E+00 5.15E-02
3.50E+00 5.14E-02
3.52E+00 5.13E-02
3.54E+00 5.11E-02
3.57E+00 5.10E-02
3.59E+00 5.09E-02
3.62E+00 5.07E-02
3.64E+00 5.06E-02
3.66E+00 5.05E-02
3.69E+00 5.03E-02
3.71E+00 5.02E-02
3.74E+00 5.00E-02
3.77E+00 4.99E-02
3.79E+00 4.98E-02
3.82E+00 4.96E-02
3.84E+00 4.95E-02
3.87E+00 4.94E-02
3.89E+00 4.92E-02
3.92E+00 4.91E-02
3.95E+00 4.89E-02
3.97E+00 4.88E-02
4.00E+00 4.86E-02
4.03E+00 4.85E-02
4.06E+00 4.84E-02
4.08E+00 4.82E-02
4.11E+00 4.81E-02
4.14E+00 4.79E-02
4.17E+00 4.78E-02
4.19E+00 4.76E-02
4.22E+00 4.75E-02
4.25E+00 4.73E-02
4.28E+00 4.72E-02
4.31E+00 4.71E-02
4.34E+00 4.69E-02
4.37E+00 4.68E-02
4.40E+00 4.66E-02
4.43E+00 4.65E-02
4.46E+00 4.63E-02
4.49E+00 4.62E-02
4.52E+00 4.60E-02
4.55E+00 4.59E-02
4.58E+00 4.57E-02
4.61E+00 4.56E-02
4.64E+00 4.54E-02
4.67E+00 4.52E-02
4.71E+00 4.51E-02
4.74E+00 4.49E-02
4.77E+00 4.48E-02
4.80E+00 4.46E-02
4.83E+00 4.45E-02
4.87E+00 4.43E-02
4.90E+00 4.42E-02
4.93E+00 4.40E-02
4.97E+00 4.38E-02
5.00E+00 4.37E-02
5.03E+00 4.35E-02
5.07E+00 4.34E-02
5.10E+00 4.32E-02
5.14E+00 4.30E-02
5.17E+00 4.29E-02
5.21E+00 4.27E-02
5.24E+00 4.26E-02
5.28E+00 4.24E-02
5.31E+00 4.22E-02
5.35E+00 4.21E-02
5.39E+00 4.19E-02
5.42E+00 4.17E-02
5.46E+00 4.16E-02
5.50E+00 4.14E-02
5.53E+00 4.13E-02
5.57E+00 4.11E-02
5.61E+00 4.09E-02
5.65E+00 4.08E-02
5.68E+00 4.06E-02
5.72E+00 4.04E-02
5.76E+00 4.03E-02
5.80E+00 4.01E-02
5.84E+00 3.99E-02
5.88E+00 3.97E-02
5.92E+00 3.96E-02
5.96E+00 3.94E-02
6.00E+00 3.92E-02
6.04E+00 3.91E-02
6.08E+00 3.89E-02
6.12E+00 3.87E-02
6.16E+00 3.84E-02
6.21E+00 3.82E-02
6.25E+00 3.80E-02
6.29E+00 3.78E-02
6.33E+00 3.76E-02
6.38E+00 3.74E-02
6.42E+00 3.72E-02
6.46E+00 3.70E-02
6.51E+00 3.68E-02
6.55E+00 3.65E-02
6.59E+00 3.63E-02
6.64E+00 3.61E-02
6.68E+00 3.59E-02
6.73E+00 3.57E-02
6.78E+00 3.55E-02
6.82E+00 3.53E-02
6.87E+00 3.51E-02
6.91E+00 3.48E-02
6.96E+00 3.46E-02
7.01E+00 3.44E-02
7.06E+00 3.42E-02
7.10E+00 3.40E-02
7.15E+00 3.38E-02
7.20E+00 3.36E-02
7.25E+00 3.34E-02
7.30E+00 3.31E-02
7.35E+00 3.29E-02
7.40E+00 3.27E-02
7.45E+00 3.25E-02
7.50E+00 3.23E-02
7.55E+00 3.21E-02
7.60E+00 3.19E-02
7.65E+00 3.17E-02
7.70E+00 3.15E-02
7.76E+00 3.12E-02
7.81E+00 3.10E-02
7.86E+00 3.08E-02
7.91E+00 3.06E-02
7.97E+00 3.04E-02
8.02E+00 3.02E-02
8.08E+00 3.00E-02
8.13E+00 2.98E-02
8.19E+00 2.95E-02
8.24E+00 2.93E-02
8.30E+00 2.91E-02
8.35E+00 2.89E-02
8.41E+00 2.87E-02
8.47E+00 2.85E-02
8.52E+00 2.83E-02
8.58E+00 2.81E-02
8.64E+00 2.79E-02
8.70E+00 2.77E-02
8.76E+00 2.74E-02
8.82E+00 2.72E-02
8.88E+00 2.70E-02
8.94E+00 2.68E-02
9.00E+00 2.66E-02
9.06E+00 2.64E-02
9.12E+00 2.62E-02
9.18E+00 2.60E-02
9.24E+00 2.58E-02
9.31E+00 2.56E-02
9.37E+00 2.54E-02
9.43E+00 2.52E-02
9.50E+00 2.50E-02
9.56E+00 2.48E-02
9.63E+00 2.46E-02
9.69E+00 2.43E-02
9.76E+00 2.41E-02
9.82E+00 2.39E-02
9.89E+00 2.37E-02
9.96E+00 2.35E-02
1.00E+01 2.33E-02
1.01E+01 2.31E-02
1.02E+01 2.29E-02
1.02E+01 2.27E-02
1.03E+01 2.25E-02
1.04E+01 2.23E-02
1.04E+01 2.21E-02
1.05E+01 2.19E-02
1.06E+01 2.17E-02
1.07E+01 2.15E-02
1.07E+01 2.13E-02
1.08E+01 2.11E-02
1.09E+01 2.09E-02
1.09E+01 2.07E-02
1.10E+01 2.06E-02
1.11E+01 2.04E-02
1.12E+01 2.02E-02
1.12E+01 2.00E-02
1.13E+01 1.98E-02
1.14E+01 1.96E-02
1.15E+01 1.94E-02
1.16E+01 1.92E-02
1.16E+01 1.90E-02
1.17E+01 1.88E-02
1.18E+01 1.86E-02
1.19E+01 1.84E-02
1.19E+01 1.83E-02
1.20E+01 1.81E-02
1.21E+01 1.79E-02
1.22E+01 1.77E-02
1.23E+01 1.75E-02
1.24E+01 1.73E-02
1.24E+01 1.72E-02
1.25E+01 1.70E-02
1.26E+01 1.68E-02
1.27E+01 1.66E-02
1.28E+01 1.64E-02
1.29E+01 1.62E-02
1.30E+01 1.61E-02
1.30E+01 1.59E-02
1.31E+01 1.57E-02
1.32E+01 1.55E-02
1.33E+01 1.54E-02
1.34E+01 1.52E-02
1.35E+01 1.50E-02
1.36E+01 1.48E-02
1.37E+01 1.47E-02
1.38E+01 1.45E-02
1.39E+01 1.43E-02
1.40E+01 1.42E-02
1.40E+01 1.40E-02
1.41E+01 1.38E-02
1.42E+01 1.37E-02
1.43E+01 1.35E-02
1.44E+01 1.33E-02
1.45E+01 1.32E-02
1.46E+01 1.30E-02
1.47E+01 1.28E-02
1.48E+01 1.27E-02
1.49E+01 1.25E-02
1.50E+01 1.24E-02
1.51E+01 1.22E-02
1.52E+01 1.21E-02
1.53E+01 1.19E-02
1.54E+01 1.17E-02
1.55E+01 1.16E-02
1.57E+01 1.14E-02
1.58E+01 1.13E-02
1.59E+01 1.11E-02
1.60E+01 1.10E-02
1.61E+01 1.08E-02
1.62E+01 1.07E-02
1.63E+01 1.05E-02
1.64E+01 1.04E-02
1.65E+01 1.03E-02
1.66E+01 1.01E-02
1.67E+01 9.95E-03
1.69E+01 9.80E-03
1.70E+01 9.65E-03
1.71E+01 9.50E-03
1.72E+01 9.35E-03
1.73E+01 9.20E-03
1.74E+01 9.06E-03
1.76E+01 8.91E-03
1.77E+01 8.77E-03
1.78E+01 8.63E-03
1.79E+01 8.49E-03
1.80E+01 8.35E-03
1.82E+01 8.21E-03
1.83E+01 8.08E-03
1.84E+01 7.94E-03
1.85E+01 7.81E-03
1.87E+01 7.68E-03
1.88E+01 7.55E-03
1.89E+01 7.42E-03
1.90E+01 7.29E-03
1.92E+01 7.17E-03
1.93E+01 7.04E-03
1.94E+01 6.92E-03
1.96E+01 6.80E-03
1.97E+01 6.68E-03
1.98E+01 6.56E-03
2.00E+01 6.44E-03
2.01E+01 6.32E-03
2.02E+01 6.21E-03
2.04E+01 6.09E-03
2.05E+01 5.98E-03
2.06E+01 5.87E-03
2.08E+01 5.76E-03
2.09E+01 5.65E-03
2.11E+01 5.55E-03
2.12E+01 5.44E-03
2.14E+01 5.34E-03
2.15E+01 5.23E-03
2.16E+01 5.13E-03
2.18E+01 5.03E-03
2.19E+01 4.93E-03
2.21E+01 4.84E-03
2.22E+01 4.74E-03
2.24E+01 4.64E-03
2.25E+01 4.55E-03
2.27E+01 4.46E-03
2.28E+01 4.37E-03
2.30E+01 4.28E-03
2.32E+01 4.19E-03
2.33E+01 4.10E-03
2.35E+01 4.02E-03
2.36E+01 3.93E-03
2.38E+01 3.85E-03
2.40E+01 3.77E-03
2.41E+01 3.69E-03
2.43E+01 3.61E-03
2.44E+01 3.53E-03
2.46E+01 3.45E-03
2.48E+01 3.37E-03
2.49E+01 3.30E-03
2.51E+01 3.23E-03
2.53E+01 3.15E-03
2.55E+01 3.08E-03
2.56E+01 3.01E-03
2.58E+01 2.94E-03
2.60E+01 2.87E-03
2.61E+01 2.81E-03
2.63E+01 2.74E-03
2.65E+01 2.68E-03
2.67E+01 2.61E-03
2.69E+01 2.55E-03
2.70E+01 2.49E-03
2.72E+01 2.43E-03
2.74E+01 2.37E-03
2.76E+01 2.31E-03
2.78E+01 2.26E-03
2.80E+01 2.20E-03
2.82E+01 2.15E-03
2.84E+01 2.09E-03
2.85E+01 2.04E-03
2.87E+01 1.99E-03
2.89E+01 1.94E-03
2.91E+01 1.89E-03
2.93E+01 1.84E-03
2.95E+01 1.79E-03
2.97E+01 1.74E-03
2.99E+01 1.70E-03
3.01E+01 1.65E-03
3.03E+01 1.61E-03
3.05E+01 1.57E-03
3.07E+01 1.52E-03
3.10E+01 1.48E-03
3.12E+01 1.44E-03
3.14E+01 1.40E-03
3.16E+01 1.36E-03
3.18E+01 1.32E-03
3.20E+01 1.29E-03
3.22E+01 1.25E-03
3.25E+01 1.21E-03
3.27E+01 1.18E-03
3.29E+01 1.15E-03
3.31E+01 1.11E-03
3.33E+01 1.08E-03
3.36E+01 1.05E-03
3.38E+01 1.02E-03
3.40E+01 9.87E-04
3.43E+01 9.57E-04
3.45E+01 9.29E-04
3.47E+01 9.00E-04
3.50E+01 8.73E-04
3.52E+01 8.46E-04
3.54E+01 8.20E-04
3.57E+01 7.94E-04
3.59E+01 7.69E-04
3.62E+01 7.45E-04
3.64E+01 7.21E-04
3.66E+01 6.98E-04
3.69E+01 6.76E-04
3.71E+01 6.54E-04
3.74E+01 6.32E-04
3.77E+01 6.12E-04
3.79E+01 5.92E-04
3.82E+01 5.72E-04
3.84E+01 5.53E-04
3.87E+01 5.34E-04
3.89E+01 5.16E-04
3.92E+01 4.98E-04
3.95E+01 4.81E-04
3.97E+01 4.65E-04
4.00E+01 4.48E-04
4.03E+01 4.33E-04
4.06E+01 4.18E-04
4.08E+01 4.03E-04
4.11E+01 3.88E-04
4.14E+01 3.74E-04
4.17E+01 3.61E-04
4.19E+01 3.48E-04
4.22E+01 3.35E-04
4.25E+01 3.23E-04
4.28E+01 3.11E-04
4.31E+01 2.99E-04
4.34E+01 2.88E-04
4.37E+01 2.77E-04
4.40E+01 2.66E-04
4.43E+01 2.56E-04
4.46E+01 2.46E-04
4.49E+01 2.37E-04
4.52E+01 2.28E-04
4.55E+01 2.19E-04
4.58E+01 2.10E-04
4.61E+01 2.02E-04
4.64E+01 1.94E-04
4.67E+01 1.86E-04
4.71E+01 1.78E-04
4.74E+01 1.71E-04
4.77E+01 1.64E-04
4.80E+01 1.57E-04
4.83E+01 1.51E-04
4.87E+01 1.44E-04
4.90E+01 1.38E-04
4.93E+01 1.32E-04
4.97E+01 1.27E-04
5.00E+01 1.21E-04

The first time you run Julia, it compiles everything, this takes a little bit of time. The second time anything gets called, it is already compiled and lightening fast.
Call your function twice, the second call will be much faster.
When I run it my first call is 2.750975 seconds and second call is 0.013358 seconds
What I mean by two call is this:
#time SA,PF=FAS2SA(FAS1,f_FAS,10.0,f_SA, 0.05) <---2.750975 seconds
#time SA,PF=FAS2SA(FAS1,f_FAS,10.0,f_SA, 0.05) <---0.013358 seconds
For timing, using BenchmarkTools is better, it calls it several times for you. replace
println(#time SA,PF=FAS2SA(FAS1,f_FAS,10.0,f_SA, 0.05))
with
using BenchmarkTools
#btime FAS2SA($FAS1,$f_FAS,10.0,$f_SA, 0.05)
the output is 10.669 ms (125187 allocations: 6.17 MiB)
Notice how many allocations and memory you are using. this means your code could still be much faster. I am not going to optimize it, it is too long. But I guarantee you can make your code considerably faster if you get rid of all those allocations.
With practice, you will be able to write code that is as fast as C and Fortran (I have seen it several times now on non-trivial code), but it does take practice. You need to avoid making temporary arrays.
I recommend reading these tips at the very least:
https://docs.julialang.org/performance-tips

Related

Why does Julia throw a premature end of input error when i write and if statement?

This is my code:
using Printf
using Statistics
age = 12
if age < 10
println("$age")
This is the error:
ERROR: LoadError: syntax: incomplete: premature end of input
Stacktrace:
[1] top-level scope at D:\julia\trial.jl:5
[2] include(::Module, ::String) at .\Base.jl:377
[3] exec_options(::Base.JLOptions) at .\client.jl:288
[4] _start() at .\client.jl:484
in expression starting at D:\julia\trial.jl:5
It works well if I remove the if statement and just print the value of age.
Julia terminates its blocks with an end keyword (and does not rely on whitespace to define its blocks).
age = 12
if age < 10
println("$age")
end

Reformat output to one line per interval of a command in batch mode

I am using perf stat to log some hardware counters, it has a batch mode in which it prints the counter values every 1s (or whatever the interval). I want the output of one interval in one single line instead of multiple lines as shown below:
# time counts events
1.000650887 4015.442880 task-clock (msec) [100.00%]
1.000650887 214 context-switches [100.00%]
1.000650887 2 cpu-migrations [100.00%]
1.000650887 14 page-faults
1.000650887 58,447,833 cycles [83.19%]
1.000650887 50,476,562 stalled-cycles-frontend [83.26%]
1.000650887 18,469,093 stalled-cycles-backend [66.85%]
1.000650887 13,861,731 instructions [83.56%]
1.000650887 3,963,967 branches [83.60%]
1.000650887 180,104 branch-misses [83.21%]
2.004854486 4003.706096 task-clock (msec)
2.004854486 245 context-switches
2.004854486 0 cpu-migrations
2.004854486 30 page-faults
2.004854486 60,750,234 cycles [83.27%]
2.004854486 38,491,129 stalled-cycles-frontend [83.26%]
2.004854486 20,561,260 stalled-cycles-backend [66.95%]
2.004854486 15,651,369 instructions [83.36%]
2.004854486 3,826,936 branches [83.25%]
2.004854486 183,319 branch-misses [83.27%]
So that it can be saved as a csv file with each counter value for one interval as a single row. Is there a simple way to do this?
something like:
task-clock, context-switches, page-faults, cycles, instructions, branches
4105, 214, 14, 58447833, 13861, 3963967
4003, 245, 30, 60750234, 15651369, 3826936
my perf version is: 3.13.11.10
I would pass the output of perf stat through something like
awk '/task-clock/{printf("%d ", $2);} /page-faults/{printf("%d ", $2);} /branch-misses/{printf("%d\n", $2);} '
I did not add here all the fields and the header. The additions are pretty obvious

Getting date to appear on x axis of plot.stl method in R

I was wondering if anyone knows of a way to get the dates to appear on the x axis of an stl plot.
res<- (stl(ts(data[,variable],frequency=52,start=as.Date(data[1,date]) ),s.window="per",robust=TRUE) )
plot(res)
However this is just producing some unicode labels at the bottom of the chart, I have also tried chaning as.Date to as.character however this didn't work.
dput of some data:
structure(c("2007-01-01", "2007-01-08", "2007-01-15", "2007-01-22",
"2007-01-29", "2007-02-05", "2007-02-12", "2007-02-19", "2007-02-26",
"2007-03-05", "2007-03-12", "2007-03-19", "2007-03-26", "2007-04-02",
"2007-04-09", "2007-04-16", "2007-04-23", "2007-04-30", "2007-05-07",
"2007-05-14", "2007-05-21", "2007-05-28", "2007-06-04", "2007-06-11",
"2007-06-18", "2007-06-25", "2007-07-02", "2007-07-09", "2007-07-16",
"2007-07-23", "2007-07-30", "2007-08-06", "2007-08-13", "2007-08-20",
"2007-08-27", "2007-09-03", "2007-09-10", "2007-09-17", "2007-09-24",
"2007-10-01", "2007-10-08", "2007-10-15", "2007-10-22", "2007-10-29",
"2007-11-05", "2007-11-12", "2007-11-19", "2007-11-26", "2007-12-03",
"2007-12-10", "2007-12-17", "2007-12-24", "2007-12-31", "2008-01-07",
"2008-01-14", "2008-01-21", "2008-01-28", "2008-02-04", "2008-02-11",
"2008-02-18", "2008-02-25", "2008-03-03", "2008-03-10", "2008-03-17",
"2008-03-24", "2008-03-31", "2008-04-07", "2008-04-14", "2008-04-21",
"2008-04-28", "2008-05-05", "2008-05-12", "2008-05-19", "2008-05-26",
"2008-06-02", "2008-06-09", "2008-06-16", "2008-06-23", "2008-06-30",
"2008-07-07", "2008-07-14", "2008-07-21", "2008-07-28", "2008-08-04",
"2008-08-11", "2008-08-18", "2008-08-25", "2008-09-01", "2008-09-08",
"2008-09-15", "2008-09-22", "2008-09-29", "2008-10-06", "2008-10-13",
"2008-10-20", "2008-10-27", "2008-11-03", "2008-11-10", "2008-11-17",
"2008-11-24", "2008-12-01", "2008-12-08", "2008-12-15", "2008-12-22",
"2008-12-29", "2009-01-05", "2009-01-12", "2009-01-19", "2009-01-26",
"2009-02-02", "2009-02-09", "2009-02-16", "2009-02-23", "2009-03-02",
"2009-03-09", "2009-03-16", "2009-03-23", "2009-03-30", "2009-04-06",
"2009-04-13", "2009-04-20", "2009-04-27", "2009-05-04", "2009-05-11",
"2009-05-18", "2009-05-25", "2009-06-01", "2009-06-08", "2009-06-15",
"2009-06-22", "2009-06-29", "2009-07-06", "2009-07-13", "2009-07-20",
"2009-07-27", "2009-08-03", "2009-08-10", "2009-08-17", "2009-08-24",
"2009-08-31", "2009-09-07", "2009-09-14", "2009-09-21", "2009-09-28",
"2009-10-05", "2009-10-12", "2009-10-19", "2009-10-26", "2009-11-02",
"2009-11-09", "2009-11-16", "2009-11-23", "2009-11-30", "2009-12-07",
"2009-12-14", "2009-12-21", "2009-12-28", "2010-01-04", "2010-01-11",
"2010-01-18", "2010-01-25", "2010-02-01", "2010-02-08", "2010-02-15",
"2010-02-22", "2010-03-01", "2010-03-08", "2010-03-15", "2010-03-22",
"2010-03-29", "2010-04-05", "2010-04-12", "2010-04-19", "2010-04-26",
"2010-05-03", "2010-05-10", "2010-05-17", "2010-05-24", "2010-05-31",
"2010-06-07", "2010-06-14", "2010-06-21", "2010-06-28", "2010-07-05",
"2010-07-12", "2010-07-19", "2010-07-26", "2010-08-02", "2010-08-09",
"2010-08-16", "2010-08-23", "2010-08-30", "2010-09-06", "2010-09-13",
"2010-09-20", "2010-09-27", "2010-10-04", "2010-10-11", "2010-10-18",
"2010-10-25", "2010-11-01", "2010-11-08", "2010-11-15", "2010-11-22",
"2010-11-29", "2010-12-06", "2010-12-13", "2010-12-20", "2010-12-27",
"2011-01-03", "2011-01-10", "2011-01-17", "2011-01-24", "2011-01-31",
"2011-02-07", "2011-02-14", "2011-02-21", "2011-02-28", "2011-03-07",
"2011-03-14", "2011-03-21", "2011-03-28", "2011-04-04", "2011-04-11",
"2011-04-18", "2011-04-25", "2011-05-02", "2011-05-09", "2011-05-16",
"2011-05-23", "2011-05-30", "2011-06-06", "2011-06-13", "2011-06-20",
"2011-06-27", "2011-07-04", "2011-07-11", "2011-07-18", "2011-07-25",
"2011-08-01", "2011-08-08", "2011-08-15", "2011-08-22", "2011-08-29",
"2011-09-05", "2011-09-12", "2011-09-19", "2011-09-26", "2011-10-03",
"2011-10-10", "2011-10-17", "2011-10-24", "2011-10-31", "2011-11-07",
"2011-11-14", "2011-11-21", "2011-11-28", "2011-12-05", "2011-12-12",
"2011-12-19", "2011-12-26", "442573", "452832", "452785", "459228",
"479509", "477631", "465619", "462001", "485567", "462381", "456059",
"457401", "474094", "468766", "456945", "539126", "545640", "511801",
"486619", "484430", "481428", "470622", "479677", "486755", "477617",
"483656", "497479", "493436", "480080", "481527", "516029", "532349",
"503939", "472171", "461550", "504532", "489715", "480032", "488376",
"470772", "467395", "488155", "455712", "474456", "471237", "482943",
"459320", "456956", "465056", "461930", "441201", "451255", "464508",
"449199", "455411", "476323", "521761", "513416", "521070", "497596",
"485461", "485593", "461148", "429938", "441207", "459484", "462099",
"469285", "454395", "456729", "469251", "517727", "526719", "477985",
"484538", "469766", "472399", "481162", "479039", "481071", "485068",
"462108", "459079", "452410", "488574", "502166", "504323", "514529",
"524206", "504315", "462935", "461899", "461551", "455491", "456857",
"443314", "454586", "458943", "450555", "454311", "442808", "440126",
"414876", "415787", "413352", "423864", "448319", "415440", "431948",
"433313", "448509", "436400", "454154", "454183", "447735", "452220",
"451433", "472808", "446767", "426595", "463693", "435673", "452704",
"456828", "465069", "448685", "457353", "443859", "463972", "480139",
"493342", "500982", "529602", "527365", "512005", "484585", "455007",
"470006", "477522", "443956", "459038", "488877", "466669", "476242",
"470862", "457298", "466438", "454062", "460216", "466645", "445113",
"457255", "451553", "451504", "447991", "435100", "416140", "481390",
"489041", "496984", "486245", "478191", "455201", "448085", "459097",
"480859", "490248", "462523", "489755", "468391", "460229", "481276",
"472845", "481099", "476435", "487314", "475043", "476847", "461928",
"483488", "479379", "456732", "461538", "480773", "471101", "459898",
"482129", "464356", "459420", "457850", "456764", "438152", "467928",
"464732", "458767", "470256", "449488", "433263", "428548", "435099",
"429163", "424251", "434723", "425841", "399498", "418791", "405051",
"385037", "425144", "418296", "397644", "414283", "431907", "429117",
"424862", "439664", "432791", "443588", "434985", "442418", "445760",
"449290", "451412", "456247", "444372", "441390", "458192", "456435",
"450670", "447609", "439083", "464513", "462784", "439423", "450857",
"442374", "447753", "440207", "435254", "430841", "437233", "426523",
"430127", "431305", "470244", "508878", "511064", "504182", "462076",
"452218", "426535", "436892", "459008", "441449", "438783", "427497",
"432275", "436745", "423068", "429574", "416074"), .Dim = c(261L,
2L))
The problem is with how you have created the time series object.
?ts will give you
start : the time of the first observation. Either a single number or a
vector of two integers, which specify a natural time unit and a
(1-based) number of samples into the time unit.
Since your data starts at 2007-01-01 and is a weekly data, you have to specify c(2007,1), Note that the "1" here denotes that data start from the 1st week of 2007, as the data is weekly. Here the frequency should be set to 52 because, after 52 cycles/weeks a year will change from 2007 to 2008.
This should work
res<- (stl(ts(data[,2],frequency=52,start=c(2007,1 )),s.window="per",robust=TRUE) )

R - Color or shade area between lines

I'm trying to replicate with R a chart I made on Excel, which should represent a 95% Confidence Interval (CI) around a time series forecast. The Excel chart looks like this:
So, basically, the original historical time series and from a certain point in time the forecast of what it could be with its respective CI.
They way it's done on Excel is a bit inefficient:
I have four time series which overlap much of the time;
The actual/historical time series (blue line above) simply stops when the forecast begins;
The forecast (dotted red above) is simply hidden below the blue one until the forecast period begins;
Then I have a time series representing the difference between the the upper bound and the lower bound of the CI, which playing around with Excel Stacked Areas charts, becomes the shaded area in the chart above.
Obviously, the computation to generate the forecast and the CIs is much faster and easier to generalize and use with R, and while I could complete the task on R and then simply copy the output on Excel to draw the chart, doing everything in R would be much nicer.
At the end of the question I provided the raw data with dput() as suggested by #MLavoie.
Here the packages I loaded (not sure you need them all here, but they are the ones I usually work with):
require(zoo)
require(xts)
require(lattice)
require(latticeExtra)
My data looks like this for the first 100 rows:
> head(data)
fifth_percentile Median nintyfifth_percentile
2017-06-18 1.146267 1.146267 1.146267
2017-06-19 1.134643 1.134643 1.134643
2017-06-20 1.125664 1.125664 1.125664
2017-06-21 1.129037 1.129037 1.129037
2017-06-22 1.147542 1.147542 1.147542
2017-06-23 1.159989 1.159989 1.159989
Then after the 100 data point, the time series start to diverge and at the end they look like this:
> tail(data)
fifth_percentile Median nintyfifth_percentile
2017-12-30 0.9430930 1.125844 1.341603
2017-12-31 0.9435227 1.127391 1.354928
2018-01-01 0.9417235 1.124625 1.355527
2018-01-02 0.9470077 1.124088 1.361420
2018-01-03 0.9571596 1.127299 1.364005
2018-01-04 0.9515535 1.127978 1.369536
Solution provided by DaveTurek
Thanks to DaveTurek I've found the answer. However, only difference is that for my xts dataframe, apparently, I need first to convert each column to numbers (with as.numeric()). No idea if that stems from me doing something wrong with xts and lattice, or it is the only way to achieve it using DaveTurek suggestion. Will try to investigate it further.
Here is the code to generate the chart:
x = index(data[1:100,2])
y = as.numeric(data[1:100,2])
ex.x = index(data[101:200,2])
ex.y = as.numeric(data[101:200,2])
ex.lo = as.numeric(data[101:200,1])
ex.hi = as.numeric(data[101:200,3])
xyplot(y~x, ylim = c(0.9,1.4),
panel=function(x,y,...) {
panel.lines(x,y,lwd=2,col=4)
panel.polygon(c(ex.x,rev(ex.x)),c(ex.lo,rev(ex.hi)),border=NA,col=5)
panel.lines(ex.x,ex.y,lwd=2,col=2)
})
And here the final result:
Here is the final dataset, from dput(), that I'm trying to plot:
> dput(data)
structure(c(1.14626724930899, 1.13464279067717, 1.12566420479952,
1.12903662366847, 1.14754211999921, 1.15998855701439, 1.15274364578958,
1.16226441955745, 1.16169992687419, 1.16520028734587, 1.16823402018407,
1.19832130049664, 1.18411773220697, 1.18531274215286, 1.16421444455115,
1.17108139956539, 1.18392357740377, 1.20103911352579, 1.17791736605905,
1.18277944964829, 1.20162550199013, 1.19665058179752, 1.19411188122108,
1.19367558590966, 1.19803272562951, 1.20600155861871, 1.22189449901607,
1.22072774140118, 1.22312376195254, 1.25355505518571, 1.25895911759195,
1.2613354420716, 1.24440525381363, 1.24444079462029, 1.24168652168112,
1.24154936710117, 1.23440527301777, 1.22592718438811, 1.21709102449773,
1.21448030929365, 1.23109601090898, 1.24401127451953, 1.23953314346685,
1.21863565024168, 1.20834325548551, 1.20281193695583, 1.20405850724191,
1.19608032796923, 1.22008184095742, 1.21675995421116, 1.20198916403093,
1.20029121301547, 1.18822375424598, 1.19007923345344, 1.19285965857709,
1.1971013197471, 1.1776860331227, 1.18028531916998, 1.18394951589397,
1.16712430930941, 1.17827461393349, 1.18751430033172, 1.21482260909863,
1.2167262724184, 1.21729489152574, 1.21847062594996, 1.21932070698031,
1.19678189566773, 1.17678214957629, 1.17586968485613, 1.16903708967946,
1.16967697995898, 1.14498266161799, 1.12782282645368, 1.11540004479973,
1.12639853863918, 1.11402516325222, 1.10511837662567, 1.10600107687395,
1.10243149863659, 1.10404564773364, 1.12949458422398, 1.11679224666313,
1.11338078540871, 1.10762728498848, 1.12437898939299, 1.11572706259347,
1.1148111967932, 1.12358625045939, 1.11169207274881, 1.13009253108247,
1.13772927166761, 1.12550770863279, 1.13062401691547, 1.12821231512428,
1.13174620070443, 1.13072790983063, 1.1428325334377, 1.12739171867048,
1.1214997813059, 1.11870510839984, 1.096148222775, 1.08805136310032,
1.08701594286129, 1.08047984136855, 1.07939438148434, 1.0684082570972,
1.06497159411023, 1.05820047926833, 1.06322519359802, 1.06234781015662,
1.05431808916504, 1.054405104791, 1.05330182895869, 1.04787681441803,
1.041698698458, 1.03870702538097, 1.03300007904201, 1.02741553353049,
1.03525701392318, 1.0339774223954, 1.0328464056954, 1.03100871401712,
1.03348765946373, 1.03473218333386, 1.02942612874379, 1.02109481188296,
1.02301597272716, 1.01553904377803, 1.0031650628692, 1.00779708136199,
1.01322764666693, 1.01964272925677, 1.02125480865504, 1.02300342204156,
1.02563993245866, 1.02972111884963, 1.02048756192688, 1.00481457379443,
1.00512607721887, 1.01094340128446, 1.01377432300649, 1.01170553705668,
1.00551128145228, 1.00612634442438, 1.00735643866839, 1.0080606590012,
0.985706701720841, 0.982234200010558, 0.975314534071082, 0.973611418201841,
0.968118612511537, 0.973092829667201, 0.975599110408158, 0.967214930243667,
0.968569928969912, 0.963572085616274, 0.964901787179726, 0.957782708788541,
0.951868416101986, 0.956694066411684, 0.956937537219092, 0.956303331651844,
0.947880835881923, 0.956308493824626, 0.948146077843001, 0.945939091828748,
0.945082701640947, 0.937222489932819, 0.937989843132858, 0.948712728941467,
0.939050882255992, 0.946264846068344, 0.944926693194716, 0.946825914432391,
0.939070104432721, 0.950666108330947, 0.949365988007735, 0.943616625744159,
0.946600795357699, 0.941276090147603, 0.939957902451166, 0.941523527816784,
0.946611480333791, 0.959236316317354, 0.96165367272139, 0.957508302724503,
0.954774123925477, 0.960811125123549, 0.956525507301749, 0.948237690612711,
0.951299123137395, 0.945212566792479, 0.94507842203255, 0.942735006048921,
0.943093032220433, 0.943522672031737, 0.941723495992432, 0.947007713852018,
0.95715960245335, 0.951553478810637, 1.14626724930899, 1.13464279067717,
1.12566420479952, 1.12903662366847, 1.14754211999921, 1.15998855701439,
1.15274364578958, 1.16226441955745, 1.16169992687419, 1.16520028734587,
1.16823402018407, 1.19832130049664, 1.18411773220697, 1.18531274215286,
1.16421444455115, 1.17108139956539, 1.18392357740377, 1.20103911352579,
1.17791736605905, 1.18277944964829, 1.20162550199013, 1.19665058179752,
1.19411188122108, 1.19367558590966, 1.19803272562951, 1.20600155861871,
1.22189449901607, 1.22072774140118, 1.22312376195254, 1.25355505518571,
1.25895911759195, 1.2613354420716, 1.24440525381363, 1.24444079462029,
1.24168652168112, 1.24154936710117, 1.23440527301777, 1.22592718438811,
1.21709102449773, 1.21448030929365, 1.23109601090898, 1.24401127451953,
1.23953314346685, 1.21863565024168, 1.20834325548551, 1.20281193695583,
1.20405850724191, 1.19608032796923, 1.22008184095742, 1.21675995421116,
1.20198916403093, 1.20029121301547, 1.18822375424598, 1.19007923345344,
1.19285965857709, 1.1971013197471, 1.1776860331227, 1.18028531916998,
1.18394951589397, 1.16712430930941, 1.17827461393349, 1.18751430033172,
1.21482260909863, 1.2167262724184, 1.21729489152574, 1.21847062594996,
1.21932070698031, 1.19678189566773, 1.17678214957629, 1.17586968485613,
1.16903708967946, 1.16967697995898, 1.14498266161799, 1.12782282645368,
1.11540004479973, 1.12639853863918, 1.11402516325222, 1.10511837662567,
1.10600107687395, 1.10243149863659, 1.10404564773364, 1.12949458422398,
1.11679224666313, 1.11338078540871, 1.10762728498848, 1.12437898939299,
1.11572706259347, 1.1148111967932, 1.12358625045939, 1.11169207274881,
1.13009253108247, 1.13772927166761, 1.12550770863279, 1.13062401691547,
1.12821231512428, 1.13174620070443, 1.13072790983063, 1.1428325334377,
1.12739171867048, 1.1214997813059, 1.11870510839984, 1.11811303551412,
1.11855383782522, 1.11981261957516, 1.12096887905804, 1.12162710713999,
1.12015553029278, 1.12189306008921, 1.1236834173899, 1.12204149206779,
1.12075809542535, 1.12116672935174, 1.12216772364685, 1.11821915571021,
1.12117719223463, 1.11896003906963, 1.11563621625852, 1.1183625095638,
1.12053072892388, 1.1216348268255, 1.12317377733957, 1.11873136428952,
1.12267083202989, 1.12642930089215, 1.13027646770951, 1.13129632891931,
1.12700346009603, 1.12060488827701, 1.12390899402613, 1.13129350591169,
1.12786650327192, 1.1274201121913, 1.13101906643359, 1.12727135093377,
1.12458327192256, 1.12259738972645, 1.12097982776572, 1.12073621452193,
1.12364872830763, 1.12644326299714, 1.12556263098661, 1.12797963752343,
1.12734519199847, 1.1261793072762, 1.12911407446825, 1.12754878937943,
1.12777579027467, 1.12554965831588, 1.12324469267853, 1.12231558194992,
1.12135908710208, 1.11923353817423, 1.12345300992675, 1.12186883237389,
1.12173652640663, 1.12488148969114, 1.12664301925369, 1.12294230775256,
1.12393650688095, 1.13038044949978, 1.12822226676967, 1.12934384230215,
1.1217648908055, 1.12218158739803, 1.12302651609468, 1.12682187689922,
1.13537701046932, 1.13172108462183, 1.1374053505525, 1.13498257452656,
1.12692005654471, 1.13210629725645, 1.12868775509168, 1.13073909215368,
1.13098804355869, 1.13353301668386, 1.13336476594698, 1.13233873705211,
1.12667020676157, 1.12133152301322, 1.12418759586717, 1.12048022460741,
1.12798162212357, 1.13053093896994, 1.12019367019997, 1.12422483586498,
1.11303086301782, 1.11986711815552, 1.12504718249418, 1.11341517044014,
1.12495096618792, 1.12995127061511, 1.13538401552385, 1.13145536081928,
1.1264465959783, 1.12584386458867, 1.1273908895838, 1.12462482614994,
1.1240880626286, 1.12729907535003, 1.12797751377714, 1.14626724930899,
1.13464279067717, 1.12566420479952, 1.12903662366847, 1.14754211999921,
1.15998855701439, 1.15274364578958, 1.16226441955745, 1.16169992687419,
1.16520028734587, 1.16823402018407, 1.19832130049664, 1.18411773220697,
1.18531274215286, 1.16421444455115, 1.17108139956539, 1.18392357740377,
1.20103911352579, 1.17791736605905, 1.18277944964829, 1.20162550199013,
1.19665058179752, 1.19411188122108, 1.19367558590966, 1.19803272562951,
1.20600155861871, 1.22189449901607, 1.22072774140118, 1.22312376195254,
1.25355505518571, 1.25895911759195, 1.2613354420716, 1.24440525381363,
1.24444079462029, 1.24168652168112, 1.24154936710117, 1.23440527301777,
1.22592718438811, 1.21709102449773, 1.21448030929365, 1.23109601090898,
1.24401127451953, 1.23953314346685, 1.21863565024168, 1.20834325548551,
1.20281193695583, 1.20405850724191, 1.19608032796923, 1.22008184095742,
1.21675995421116, 1.20198916403093, 1.20029121301547, 1.18822375424598,
1.19007923345344, 1.19285965857709, 1.1971013197471, 1.1776860331227,
1.18028531916998, 1.18394951589397, 1.16712430930941, 1.17827461393349,
1.18751430033172, 1.21482260909863, 1.2167262724184, 1.21729489152574,
1.21847062594996, 1.21932070698031, 1.19678189566773, 1.17678214957629,
1.17586968485613, 1.16903708967946, 1.16967697995898, 1.14498266161799,
1.12782282645368, 1.11540004479973, 1.12639853863918, 1.11402516325222,
1.10511837662567, 1.10600107687395, 1.10243149863659, 1.10404564773364,
1.12949458422398, 1.11679224666313, 1.11338078540871, 1.10762728498848,
1.12437898939299, 1.11572706259347, 1.1148111967932, 1.12358625045939,
1.11169207274881, 1.13009253108247, 1.13772927166761, 1.12550770863279,
1.13062401691547, 1.12821231512428, 1.13174620070443, 1.13072790983063,
1.1428325334377, 1.12739171867048, 1.1214997813059, 1.11870510839984,
1.14162401974592, 1.15630966411729, 1.15992199767135, 1.16683144867851,
1.16928280999155, 1.17287782220285, 1.18184525262982, 1.17555305757354,
1.18031492211593, 1.18142628277888, 1.18307577052783, 1.18257404220722,
1.19421117710041, 1.19403330560815, 1.19510080390052, 1.2058940348108,
1.19848571699109, 1.20138771250604, 1.20660682710938, 1.20790011589089,
1.20963951875753, 1.21572259411602, 1.21379678812156, 1.220302087399,
1.22062959185172, 1.22743877731977, 1.23135277550334, 1.24075667733246,
1.24169498945046, 1.23529301399753, 1.2399941777708, 1.24823732280171,
1.23861121958778, 1.24816319854615, 1.25252933549084, 1.25133386983018,
1.24512546001264, 1.2617641352045, 1.25486018976211, 1.25424601859098,
1.25820538036104, 1.25968528498312, 1.26939611029084, 1.27883933177157,
1.27926882841012, 1.27951234203094, 1.28997494816278, 1.29391898267335,
1.2971442938215, 1.29733541086814, 1.30376525837809, 1.31025722802128,
1.29718190520268, 1.27919305871102, 1.28685138548374, 1.28594279969497,
1.28695233433419, 1.30277136510213, 1.29178316107299, 1.29586799884087,
1.30076586308517, 1.30881154838964, 1.32171887794143, 1.3197588324899,
1.3121332301804, 1.31744410759858, 1.31402945919721, 1.30926303329755,
1.32019231597949, 1.31449633135152, 1.31730801686101, 1.31834557852015,
1.3175761022299, 1.33430488507454, 1.34091614601639, 1.33606628597812,
1.33180446732765, 1.33630738683041, 1.33449101077219, 1.32521028784732,
1.32241490851887, 1.31488015995544, 1.31913131799656, 1.32901121011698,
1.33177659436063, 1.32577077582349, 1.31960627618725, 1.31307169067904,
1.32148403094167, 1.33104893196281, 1.33491831741272, 1.3386091981919,
1.35730874062825, 1.3460340606746, 1.34160318929376, 1.35492848895938,
1.35552729646417, 1.36141957863605, 1.36400538435282, 1.369536167295),
.indexCLASS = "Date", tclass = "Date", .indexTZ = "UTC", tzone = "UTC",
class = c("xts", "zoo"), index = structure(c(1497744000, 1497830400, 1497916800,
1498003200, 1498089600, 1498176000, 1498262400, 1498348800, 1498435200,
1498521600, 1498608000, 1498694400, 1498780800, 1498867200, 1498953600,
1499040000, 1499126400, 1499212800, 1499299200, 1499385600, 1499472000,
1499558400, 1499644800, 1499731200, 1499817600, 1499904000, 1499990400,
1500076800, 1500163200, 1500249600, 1500336000, 1500422400, 1500508800,
1500595200, 1500681600, 1500768000, 1500854400, 1500940800, 1501027200,
1501113600, 1501200000, 1501286400, 1501372800, 1501459200, 1501545600,
1501632000, 1501718400, 1501804800, 1501891200, 1501977600, 1502064000,
1502150400, 1502236800, 1502323200, 1502409600, 1502496000, 1502582400,
1502668800, 1502755200, 1502841600, 1502928000, 1503014400, 1503100800,
1503187200, 1503273600, 1503360000, 1503446400, 1503532800, 1503619200,
1503705600, 1503792000, 1503878400, 1503964800, 1504051200, 1504137600,
1504224000, 1504310400, 1504396800, 1504483200, 1504569600, 1504656000,
1504742400, 1504828800, 1504915200, 1505001600, 1505088000, 1505174400,
1505260800, 1505347200, 1505433600, 1505520000, 1505606400, 1505692800,
1505779200, 1505865600, 1505952000, 1506038400, 1506124800, 1506211200,
1506297600, 1506384000, 1506470400, 1506556800, 1506643200, 1506729600,
1506816000, 1506902400, 1506988800, 1507075200, 1507161600, 1507248000,
1507334400, 1507420800, 1507507200, 1507593600, 1507680000, 1507766400,
1507852800, 1507939200, 1508025600, 1508112000, 1508198400, 1508284800,
1508371200, 1508457600, 1508544000, 1508630400, 1508716800, 1508803200,
1508889600, 1508976000, 1509062400, 1509148800, 1509235200, 1509321600,
1509408000, 1509494400, 1509580800, 1509667200, 1509753600, 1509840000,
1509926400, 1510012800, 1510099200, 1510185600, 1510272000, 1510358400,
1510444800, 1510531200, 1510617600, 1510704000, 1510790400, 1510876800,
1510963200, 1511049600, 1511136000, 1511222400, 1511308800, 1511395200,
1511481600, 1511568000, 1511654400, 1511740800, 1511827200, 1511913600,
1.512e+09, 1512086400, 1512172800, 1512259200, 1512345600, 1512432000,
1512518400, 1512604800, 1512691200, 1512777600, 1512864000, 1512950400,
1513036800, 1513123200, 1513209600, 1513296000, 1513382400, 1513468800,
1513555200, 1513641600, 1513728000, 1513814400, 1513900800, 1513987200,
1514073600, 1514160000, 1514246400, 1514332800, 1514419200, 1514505600,
1514592000, 1514678400, 1514764800, 1514851200, 1514937600, 1515024000
), tzone = "UTC", tclass = "Date"), .Dim = c(201L, 3L), .Dimnames = list(
NULL, c("fifth_percentile", "Median", "nintyfifth_percentile"
)))
I haven't tried with your data, but if the question is how to shade the forecast area, maybe this simple example will help.
library(lattice)
x = 1:12 # base data
y = x
ex.x = 12:16 # extrapolated data
ex.y = 12:16
ex.lo = 12+0:4*.3 # lower bound
ex.hi = 12+0:4*1.6 # upper bound
xyplot(y~x,xlim=c(0:18),ylim=c(0:20),
panel=function(x,y,...) {
panel.lines(x,y,lwd=2,col=4)
panel.polygon(c(ex.x,rev(ex.x)),c(ex.lo,rev(ex.hi)),border=NA,col=5)
panel.lines(ex.x,ex.y,lwd=2,col=2)
})
You can add the shaded polygon to the lattice plot in a panel function. I used c(ex.x,rev(ex.x)) and c(ex.lo,rev(ex.hi)) to construct the polygon boundary.

Scraping from aspx website using R

I am trying to accomplish a task using R to scrape data on a website.
I would like to go through each link on the following page:
http://capitol.hawaii.gov/advreports/advreport.aspx?year=2013&report=deadline&rpt_type=&measuretype=hb&title=House Bills
Select only items with Current Status showing "transmitted to the governor". For example, http://capitol.hawaii.gov/measure_indiv.aspx?billtype=HB&billnumber=17&year=2013
And then scrapping the cells within STATUS TEXT for the following clause" Passed Final Reading". For example: Passed Final Reading as amended in SD 2 with Representative(s) Fale, Jordan, Tsuji voting aye with reservations; Representative(s) Cabanilla, Morikawa, Oshiro, Tokioka voting no (4) and none excused (0).
I have tried using previous examples with packages Rcurl and XML (in R), but I don't know how to use them correctly for aspx sites. So what I would love to have is: 1. Some suggestion on how to build such a code. 2. And recommendation for how to learn the knowledge needed for performing such a task.
Thanks for any help,
Tom
require(httr)
require(XML)
basePage <- "http://capitol.hawaii.gov"
h <- handle(basePage)
GET(handle = h)
res <- GET(handle = h, path = "/advreports/advreport.aspx?year=2013&report=deadline&rpt_type=&measuretype=hb&title=House")
# parse content for "Transmitted to Governor" text
resXML <- htmlParse(content(res, as = "text"))
resTable <- getNodeSet(resXML, '//*/table[#id ="GridViewReports"]/tr/td[3]')
appRows <-sapply(resTable, xmlValue)
include <- grepl("Transmitted to Governor", appRows)
resUrls <- xpathSApply(resXML, '//*/table[#id ="GridViewReports"]/tr/td[2]//#href')
appUrls <- resUrls[include]
# look at just the first
res <- GET(handle = h, path = appUrls[1])
resXML <- htmlParse(content(res, as = "text"))
xpathSApply(resXML, '//*[text()[contains(.,"Passed Final Reading")]]', xmlValue)
[1] "Passed Final Reading as amended in SD 2 with Representative(s) Fale, Jordan,
Tsuji voting aye with reservations; Representative(s) Cabanilla, Morikawa, Oshiro,
Tokioka voting no (4) and none excused (0)."
Let package httr handle all the background work by setting up a handle.
If you want to run over all 92 links:
# get all the links returned as a list (will take sometime)
# print statement included for sanity
res <- lapply(appUrls, function(x){print(sprintf("Got url no. %d",which(appUrls%in%x)));
GET(handle = h, path = x)})
resXML <- lapply(res, function(x){htmlParse(content(x, as = "text"))})
appString <- sapply(resXML, function(x){
xpathSApply(x, '//*[text()[contains(.,"Passed Final Reading")]]', xmlValue)
})
head(appString)
> head(appString)
$href
[1] "Passed Final Reading as amended in SD 2 with Representative(s) Fale, Jordan, Tsuji voting aye with reservations; Representative(s) Cabanilla, Morikawa, Oshiro, Tokioka voting no (4) and none excused (0)."
$href
[1] "Passed Final Reading, as amended (CD 1). 25 Aye(s); Aye(s) with reservations: none . 0 No(es): none. 0 Excused: none."
[2] "Passed Final Reading as amended in CD 1 with Representative(s) Cullen, Har voting aye with reservations; Representative(s) McDermott voting no (1) and none excused (0)."
$href
[1] "Passed Final Reading, as amended (CD 1). 25 Aye(s); Aye(s) with reservations: none . 0 No(es): none. 0 Excused: none."
[2] "Passed Final Reading as amended in CD 1 with none voting aye with reservations; Representative(s) Hashem, McDermott voting no (2) and none excused (0)."
$href
[1] "Passed Final Reading, as amended (CD 1). 24 Aye(s); Aye(s) with reservations: none . 0 No(es): none. 1 Excused: Ige."
[2] "Passed Final Reading as amended in CD 1 with none voting aye with reservations; none voting no (0) and Representative(s) Say excused (1)."
$href
[1] "Passed Final Reading, as amended (CD 1). 25 Aye(s); Aye(s) with reservations: none . 0 No(es): none. 0 Excused: none."
[2] "Passed Final Reading as amended in CD 1 with Representative(s) Johanson voting aye with reservations; none voting no (0) and none excused (0)."
$href
[1] "Passed Final Reading, as amended (CD 1). 25 Aye(s); Aye(s) with reservations: none . 0 No(es): none. 0 Excused: none."
[2] "Passed Final Reading as amended in CD 1 with none voting aye with reservations; none voting no (0) and none excused (0)."

Resources