Search code examples
rloess

LOESS model in R


I am trying to do a LOESS model for predicting Y based on the independent variable X

data sample is,

x <- c(10, 20, 25, 32, 40)    
y <- c(1200, 1400, 1460, 1620, 1800)

steps i use is as following,

lw1 <- loess(y ~ x,data=data)            
plot(y ~ x, data=data,pch=19,cex=0.1)               
j <- order(data$x)              
lines(data$x[j],lw1$fitted[j])                 

In above data sample , we have only 1 independent variable x.

Now what if we have 2 independent variable?? How to get a model for the following sample data,

x1 <- c(10,20,25,32,40)                   
x2 <- c(1.2,1.4,1.5,2.1,2.8)                
y <- c(1200,1400,1460,1620,1800)

Please help me with an R sample, how can we deal with X1 and X2 in a LOESS model???


Solution

  • Here is an example with loess(y ~ x1 + x2) and predict:

    fit <- loess(y ~ x1 + x2);
    
    pred <- data.frame(ypred = predict(fit, data.frame(x1 = x1, x2 = x2)));
    pred$x1 <- x1;
    pred$x2 <- x2;
    pred$y <- y;
    
    pred;
    #      ypred x1  x2    y
    #1 1199.8667 10 1.2 1200
    #2 1016.4015 20 1.4 1400
    #3  728.8215 25 1.5 1460
    #4 1620.0000 32 2.1 1620
    #5 1799.6245 40 2.8 1800
    

    Sample data

    x1 <- c(10,20,25,32,40)                   
    x2 <- c(1.2,1.4,1.5,2.1,2.8)                
    y <- c(1200,1400,1460,1620,1800);