Calculates a poll aggregation and stores it in the popPolls object, where
it is picked up by plot.popPolls().
Arguments
- data
A
popPollsobject.- name
Name of the trend. Defaults to a name built from
typeand the applied interpolations.- type
Name of a trend function (see details) or a function.
- args
Arguments passed on to the trend function.
- interpolations
Named list of interpolations that should be applied to the trend, see details.
- houseEffects
Whether the polls should be corrected by the house effects of their firms (see
popHouseEffects()) before the trend is calculated. The correction removes the differences between the firms, not the errors they share, so it does not necessarily bring the trend closer to election results; seepopAccuracy().
Details
Available trend functions are:
kalmanKalman filter, arguments:
sd = 0.003, the daily standard deviation of the true support on the share scale,smoothing = FALSEandmissingSampleSize = "firm". Withsmoothing = TRUEevery estimate takes the later polls into account as well (Rauch-Tung-Striebel smoother). The estimates are calculated for the dates with polls only; together withlinearInterpolationthe smoothed trend reproduces POLITICO's dailykalmanSmoothtrend. Polls are weighted by their sample size; polls without one get the median sample size of their firm (or of all polls), or the number given asmissingSampleSize. POLITICO uses 400, somissingSampleSize = 400reproduces its trends exactly.kalmanKFASKalman filter based on the KFAS package, arguments:
sd = 0.003,smoothing = TRUE,missingSampleSize = "firm".weightedMeanLastDaysLinearly weighted rolling mean, arguments:
days = 30,maxObs = Inf.identPlain mean of all polls published on the same day, no arguments.
Instead of a name, type can be a function. It is called with the
popPolls object as argument data and the elements of args, and has to
return a data.frame with the columns date, party (the codes of
data$parties) and value, plus optionally variance, which is used for
the uncertainty bands.
Available interpolations are:
lastInterpolationCarries the last value forward, no arguments.
linearInterpolationLinear interpolation, no arguments.
bernoulliConvInterpolationBinomial smoothing over consecutive trend values, arguments:
n = 20,k = 6.
Interpolations keep the variance of a trend, which is interpolated (and
smoothed) like the values; between two dates with polls it is therefore an
approximation.
Examples
if (FALSE) { # \dontrun{
de = popRead('DE-parliament')
de = popAddTrend(de, name = 'Kalman 0.003', type = 'kalman', args = list(sd = 0.003))
de = popAddTrend(de, name = 'Kalman smoothed', type = 'kalman',
args = list(smoothing = TRUE),
interpolations = list('linearInterpolation' = list()))
plot(de)
# a custom trend: the median of the polls of the last 14 days
rollingMedian = function(data, days = 14) {
polls = popLong(data)
dates = seq(min(polls$date), max(polls$date), by = 'day')
polls[, .(date = dates,
value = vapply(dates, function(d) median(value[date > d - days & date <= d]),
numeric(1))), by = party]
}
de = popAddTrend(de, type = rollingMedian, args = list(days = 21))
} # }