[R] handling missing values

R. Michael Weylandt michael.weylandt at gmail.com
Fri Mar 16 03:08:15 CET 2012


Here you go:

library(mgcv)
y.list <- list(y1=runif(10), y2 <- runif(10), y3 <- runif(10))
y.list <- list(y1=runif(20))
x1 <- 1:20
x2 <- c(11, 15, 17, 2, 18, 6, 7, NA, 12, 10,21, 25, 27, 12, 28, 16,
17, NA, 12, 10)
y.gam <-lapply(y.list, function(y)gam(y~s(x1,x2, k=10), na.action = na.exclude))
y.fitted <- lapply(y.gam, fitted) # list of fitted values

sum(is.na(y.fitted$y1)) == sum(is.na(x2)) ## Success!!

Michael


On Thu, Mar 15, 2012 at 6:48 PM, Mintewab Bezabih
<Mintewab.Bezabih at economics.gu.se> wrote:
> Dear Michael,
>
> Thanks. I tried it and I was not able to run it on the gam line (sorry my programming is not that great). Since I am ussing the fitted vallues for another program I actually want a blank value in place of the missing data.
>
> Many thanks in advance
> Regards
> Minti
>
>
> ________________________________________
> Från: R. Michael Weylandt [michael.weylandt at gmail.com]
> Skickat: den 15 mars 2012 19:45
> Till: Mintewab Bezabih
> Kopia: r-help at R-project.org
> Ämne: Re: [R] handling missing values
>
> Try adding na.action = na.exclude to your call to gam.
>
> Michael
>
> On Thu, Mar 15, 2012 at 1:00 PM, Mintewab Bezabih
> <Mintewab.Bezabih at economics.gu.se> wrote:
>> Dear R users,
>>
>> I have problems handling missing values. THe problem is that after running my codes the result I get just skips the missing values. What I want is for the missing values to appear in my results as well. I have put a workable example below and as you could see the missing value in x1 (the 18thh value) does not appear in the list y.fitted.
>>
>> Any help appreciated
>>
>> Regards,
>> minti
>>
>> library(mgcv)
>> y.list <- list(y1=runif(10), y2 <- runif(10), y3 <- runif(10))
>> y.list <- list(y1=runif(20))
>> x1 <- 1:20
>> x2 <- c(11, 15, 17, 2, 18, 6, 7, NA, 12, 10,21, 25, 27, 12, 28, 16, 17, NA, 12, 10)
>> y.gam <-lapply(y.list, function(y)gam(y~s(x1,x2, k=10)))
>> y.fitted <- lapply(y.gam, fitted) # list of fitted values
>>
>> ______________________________________________
>> R-help at 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.



More information about the R-help mailing list