LOWESS Regression analysis in R
1
0
Entering edit mode
2.3 years ago
anshulmbi ▴ 40

Hello Everyone, Below is the table each id column contains 7 visits and their value is in AC107c column. I want to use lowess function in R to get a smooth fitted value (one out of 7) for each ID. Please help me how I can do it in R. Thanks!!

VisitID ID  AC107c
V5  A   70
V6  A   67
V1  A   75
V8  A   62
V4  A   78
V7  A   74
V3  A   80
V6  B   101
V3  B   109
V4  B   100
V7  B   103
V1  B   94
V8  B   100
V5  B   103
V1  C   81
V8  C   86
V5  C   84
V6  C   83
V4  C   88
V3  C   86
V7  C   85

R Regression LOWESS • 595 views
2
Entering edit mode
2.3 years ago

You could try something like this, this example just for ID 'A':

options(scipen = 9)
data
VisitID ID AC107c
4       V8  A     62
2       V6  A     67
1       V5  A     70
6       V7  A     74
3       V1  A     75
5       V4  A     78
7       V3  A     80
15      V1  C     81
18      V6  C     83
17      V5  C     84
21      V7  C     85
16      V8  C     86
20      V3  C     86
19      V4  C     88
12      V1  B     94
10      V4  B    100
13      V8  B    100
8       V6  B    101
11      V7  B    103
14      V5  B    103
9       V3  B    109

data <- data[order(data$AC107c),] data_A <- data.frame(Visit = 1:7, subset(data, ID == 'A')) data_A Visit VisitID ID AC107c 4 1 V8 A 62 2 2 V6 A 67 1 3 V5 A 70 6 4 V7 A 74 3 5 V1 A 75 5 6 V4 A 78 7 7 V3 A 80 loessfit <- loess(Visit ~ AC107c, data = data_A) plot(data_A$Visit ~ data_A$AC107c, pch = 20, las = 1, xlab = 'AC107c', ylab = 'VisitID') lines(data_A$AC107c, predict(loessfit), col = "red2", lwd = 2)

# add correlation and p-value
r <- round(cor(data_A$AC107c, data_A$Visit, use = "complete.obs", method = "spearman"), 3)
pval <- cor.test(data_A$AC107c, data_A$Visit, use = "complete.obs", method = "spearman")\$p.value
pval <- ifelse(pval < 0.0001, "< 0.0001", format(pval, digits = 2))
text(70, 7, paste("r, ", r, ",\np, ", pval, sep=""), adj=c(0, 1), cex=0.8)


There are not many data-points here; so, it does not look too hot.

Kevin

0
Entering edit mode

Thank you so much for your kind help. But my actual problem is I want to predict one value for each id from 7 visits with the help of the LOWESS function. Also if possible, please let me know how to incorporate the loop because I have more than 500 ids data. Thank you!!

0
Entering edit mode

Take a look at the predict() function - you will also require the loessfit object

For the loop, I may ask that you have a think about that in your own time.

1
Entering edit mode

Thank you so much for your kind suggestions.