weighted

Aggregating weekly (7 day) data to monthly in R

余生颓废 提交于 2020-01-01 07:11:11
问题 I have data measured over a 7 day period. Part of the data looks as follows: start wk end wk X1 2/1/2004 2/7/2004 89 2/8/2004 2/14/2004 65 2/15/2004 2/21/2004 64 2/22/2004 2/28/2004 95 2/29/2004 3/6/2004 79 3/7/2004 3/13/2004 79 I want to convert this weekly (7 day) data into monthly data using weighted averages of X1. Notice that some of the 7 day X1 data will overlap from one month to the other (X1=79 for the period 2/29 to 3/6 of 2004). Specifically I would obtain the February 2004 monthly

Frequency tables with weighted data in R

孤街醉人 提交于 2020-01-01 04:25:10
问题 I need to calculate the frequency of individuals by age and marital status so normally I'd use: table(age, marital_status) However each individual has a different weight after the sampling of the data. How do I incorporate this into my frequency table? 回答1: You can use function svytable from package survey , or wtd.table from rgrs . EDIT : rgrs is now called questionr : df <- data.frame(var = c("A", "A", "B", "B"), wt = c(30, 10, 20, 40)) library(questionr) wtd.table(x = df$var, weights = df

Weighted Pearson's Correlation?

会有一股神秘感。 提交于 2019-12-28 13:21:12
问题 I have a 2396x34 double matrix named y wherein each row (2396) represents a separate situation consisting of 34 consecutive time segments. I also have a numeric[34] named x that represents a single situation of 34 consecutive time segments. Currently I am calculating the correlation between each row in y and x like this: crs[,2] <- cor(t(y),x) What I need now is to replace the cor function in the above statement with a weighted correlation. The weight vector xy.wt is 34 elements long so that

Google Foobar, maximum unique visits under a resource limit, negative weights in graph

点点圈 提交于 2019-12-25 08:26:22
问题 I'm having trouble figuring out the type of problem this is. I'm still a student and haven't taken a graph theory/linear optimization class yet. The only thing I know for sure is to check for negative cycles, as this means you can rack the resource limit up to infinity, allowing for you to pick up each rabbit. I don't know the "reason" to pick the next path. I also don't know when to terminate, as you could keep using all of the edges and make the resource limit drop below 0 forever, but

Weighted Shuffle of an Array or Arrays?

烂漫一生 提交于 2019-12-22 08:26:27
问题 What is a good algorithm that shuffles an array or arrays using weights from the nested arrays? Example: $array = array( array("name"=>"John", "rank"=>3), array("name"=>"Bob", "rank"=>1), array("name"=>"Todd", "rank"=>8), array("name"=>"Todd", "rank"=>14), array("name"=>"Todd", "rank"=>4) ); I want the array randomly shuffled but I want the rank value to be a weight. So those with a low number rank are more likely to be at the top of the list. I've experimented with a few things, like

Adding a weighted least squares trendline in ggplot2

☆樱花仙子☆ 提交于 2019-12-22 04:48:07
问题 I am preparing a plot using ggplot2, and I want to add a trendline that is based on a weighted least squares estimation. In base graphics this can be done by sending a WLS model to abline : mod0 <- lm(ds$dMNP~ds$MNP) mod1 <- lm(ds$dMNP~ds$MNP, weights = ds$Asset) symbols(ds$dMNP~ds$MNP, circles=ds$r, inches=0.35) #abline(mod0) abline(mod1) in ggplot2 I set the argument weight in geom_smooth but nothing changes: ggplot(ds, aes(x=MNP, y=dMNP, size=Asset) + geom_point(shape=21) + geom_smooth

Finding all paths in directed graph with specific cost

感情迁移 提交于 2019-12-21 20:22:37
问题 Suppose we have the directed, weighted graph. Our task is to find all paths beetween two vertices (source and destination) which cost is less or equal =< N. We visit every vertex only once. In later version I'd like to add a condition that the source can be the destination (we just make a loop). I think it can be done with modified Dijkstra's algorithm, but I have no idea how implement such thing. Thanks for any help. 回答1: You could use recursive backtracking to solve this problem. Terminate

Weighted sum of variables by groups with data.table

和自甴很熟 提交于 2019-12-19 08:12:58
问题 I am looking for a solution to compute weighted sum of some variables by groups with data.table. I hope the example is clear enough. require(data.table) dt <- data.table(matrix(1:200, nrow = 10)) dt[, gr := c(rep(1,5), rep(2,5))] dt[, w := 2] # Error: object 'w' not found dt[, lapply(.SD, function(x) sum(x * w)), .SDcols = paste0("V", 1:4)] # Error: object 'w' not found dt[, lapply(.SD * w, sum), .SDcols = paste0("V", 1:4)] # This works with out groups dt[, lapply(.SD, function(x) sum(x * dt

Calculating weighted mean and standard deviation

跟風遠走 提交于 2019-12-17 18:22:22
问题 I have a time series x_0 ... x_t . I would like to compute the exponentially weighted variance of the data. That is: V = SUM{w_i*(x_i - x_bar)^2, i=1 to T} where SUM{w_i} = 1 and x_bar=SUM{w_i*x_i} ref: http://en.wikipedia.org/wiki/Weighted_mean#Weighted_sample_variance The goal is to basically weight observations that are further back in time less. This is very simple to implement but I would like to use as much built in funcitonality as possible. Does anyone know what this corresponds to in

Weighted standard deviation in NumPy

守給你的承諾、 提交于 2019-12-17 04:17:24
问题 numpy.average() has a weights option, but numpy.std() does not. Does anyone have suggestions for a workaround? 回答1: How about the following short "manual calculation"? def weighted_avg_and_std(values, weights): """ Return the weighted average and standard deviation. values, weights -- Numpy ndarrays with the same shape. """ average = numpy.average(values, weights=weights) # Fast and numerically precise: variance = numpy.average((values-average)**2, weights=weights) return (average, math.sqrt