Re: [R] Making a function and applying it over a list(?)

2013-10-24 Thread Christoph Häni
You could store your first approach in a function and lapply it to
your by_hour variable:

  df <- data.frame(
hour = factor(rep(1:5,4)),
  id   = factor(rep(c("supply", "demand"), each = 10)),
  price= c(5,7,9,11,13,15,17,19,21,23,
20,18,16,14,12,10,8,6,4,2 ),
  quantity = c(3,5,7,13,19,31,37,53,61,67,6,18,20,24,40,46,66,70,76,78)
)

myfu <- function(x){

df <- x # for simplicity

quantity_points <- with(
  df,
  seq(min(quantity), max(quantity), length.out = 500)
)

by_id <- split(df[, c("price", "quantity")], df$id)

interpolated_price <- lapply(
  by_id,
  function(x)
  {
with(
  x,
  approx(
quantity,
price,
xout = quantity_points
  )
)$y
  }
)

index_of_equality <- with(interpolated_price, which.min(abs(supply - demand)))
quantity_points[index_of_equality]

}

by_hour <- split(df,df$hour)

lapply(by_hour,myfu)


Was that what you were looking for?

Cheers,
Christoph


2013/10/24 Lasse Thorst :
> Hi All
>
> I've gotten some awesome help getting a formular that finds the intersection 
> of two vectors. This works brilliantly, but I can't figure out how to make it 
> run over another factor. A simple example looks likes this:
>
>   df <- data.frame(
>   id   = factor(rep(c("supply", "demand"), each = 10)),
>   price= c(5,7,9,11,13,15,17,19,21,23,20,18,16,14,12,10,8,6,4,2 ),
>   quantity = c(3,5,7,13,19,31,37,53,61,67,6,18,20,24,40,46,66,70,76,78)
> )
>
> quantity_points <- with(
>   df,
>   seq(min(quantity), max(quantity), length.out = 500)
> )
>
> by_id <- split(df[, c("price", "quantity")], df$id)
>
> interpolated_price <- lapply(
>   by_id,
>   function(x)
>   {
> with(
>   x,
>   approx(
> quantity,
> price,
> xout = quantity_points
>   )
> )$y
>   }
> )
>
> index_of_equality <- with(interpolated_price, which.min(abs(supply - demand)))
> quantity_points[index_of_equality]
>
> Question: I need to run this over a larger data frame, where I have the same 
> data, but also a new factor variable (called hour). So if you have the 
> original data frame and add:
>
>   df <- data.frame(
> hour = factor(seq(1:20)),
>   id   = factor(rep(c("supply", "demand"), each = 10)),
>   price= c(5,7,9,11,13,15,17,19,21,23,20,18,16,14,12,10,8,6,4,2 ),
>   quantity = c(3,5,7,13,19,31,37,53,61,67,6,18,20,24,40,46,66,70,76,78)
> )
>
> How can I run it for each hour? I tried using:
> by_hour <- split(df[, c("price", "quantity")], df$hour)
> mapply(fx, by_hour)
>
> And gathering the above into a fx <- function(){"the neat code"}, but I can't 
> get it to work.
>
> Kind Regards,
> Lasse
>
> [[alternative HTML version deleted]]
>
> __
> R-help@r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.

__
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


Re: [R] matrix values linked to vector index

2013-10-13 Thread Christoph Häni
Just a further suggestion:

vec <- c(3,2,5,0,1)

mat <- t(sapply(vec,">=",1:max(vec)))

ifelse(mat,1,0)


Cheers,
Christoph

2013/10/11 arun :
> Hi,
>
> In the example you showed:
>
> m1<- matrix(0,length(vec),max(vec))
> 1*!upper.tri(m1)
>
> #or
>  m1[!upper.tri(m1)] <-  rep(rep(1,length(vec)),vec)
>
> #But, in a case like below, perhaps:
> vec1<- c(3,4,5)
>
>  m2<- matrix(0,length(vec1),max(vec1))
>  indx <- 
> cbind(rep(seq_along(vec1),vec1),unlist(tapply(vec1,list(vec1),FUN=seq),use.names=FALSE))
> m2[indx]<- 1
>  m2
> # [,1] [,2] [,3] [,4] [,5]
> #[1,]11100
> #[2,]11110
> #[3,]11111
>
>
>
>
> A.K.
>
>
> Hi-
>
> I'd like to create a matrix of 0's and 1's where the number of
> 1's in each row defined by the value indexed in another vector, and
> where the (value-1) is back-filled by 0's.
>
> For example, given the following vector:
> vec= c(1,2,3)
>
> I'd like to produce a matrix with dimensions (length(vec), max(vec)):
>
> 1,0,0
> 1,1,0
> 1,1,1
>
> Thank you!
>
> __
> R-help@r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.

__
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


[R] GLM: Defining non-constant variance for (gaussian) family

2013-10-07 Thread Christoph Häni
Dear all,

I want to fit some observations Y to a set of predictor variables X_i. (and
proceed with model selection with support of the second-order AIC
(AICc)...)
I (think I) "know" that the distribution of Y[i] is Gaussian and has a
variance, which is proportional to its value Y[i]. Say:

x1 <- rnorm(1:100)*2
x2 <- runif(1:100)*0.5
etc...

Y <- exp(3.2 + 0.45*x1 - 0.5*x2) + rnorm(100,0,(exp(3.2 + 0.45*x1 -
0.5*x2))^0.5)

So, I thought, I could change the family object as following:

GaussVar <- gaussian("log")

GaussVar$variance <- function(mu) mu
GaussVar$dev.resids <- function(y, mu, wt) wt * ((y - mu)^2/mu)
GaussVar$aic <- function (y, n, mu, wt, dev){
nobs <- length(y);-2 * sum(dnorm(y,mu,sqrt(dev*mu/nobs), log = TRUE) * wt)
+ 2
}

And the resulting Regression:

reg <- glm(Y~x1+x2,family=GaussVar)

Is this a valid modification of the "family" object?
Am I missing something?
Is the calculation of the AIC complete rubbish?

I have to admit, I'm very new to the topic of regression, so please be
kind! :-)

Thanks in advance,
Christoph

[[alternative HTML version deleted]]

__
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.