I created a for-loop to form a linear model with features and remove one feature every one cycle of the for-loop. Using this for loop, I want to take-out pvalues of each features.
Below is my fake data and the for loop:
#fake data
z <- c(0.91629, 1.32580, -1.09940, 0.33601, 0.28795, 1.59050, 0.68193, -1.5944, 0.32160, -1.30810, -3.8569)
x <- c(0.35391, -0.84966, 0.17623, 2.39960, -0.50007, 0.70983, 0.61825, -1.7655, -0.44426, 0.01343, 3.0556)
c <- c(1.32060, -0.29756, 0.60097, 1.91580, -0.13838, -1.77920, -0.625670, -0.30979, 0.86718, -0.27289, 2.9607)
v <- c(-0.49864, -0.61754, 0.17977, 1.17100, 0.77713, -0.50157, 0.021347, 1.32660, 0.11048, 0.79202, -1.3138)
b <- c(-0.39620, 1.30740, 1.09030, 0.45662, 0.48579, -0.17219, 0.387560, -0.98518, -0.47283, 0.27918, 8.7742)
n <- c(-0.79369, -0.050101, 0.89823, 0.80320, 0.32670, 0.39969, -0.547320, -0.23154, -0.46198, -0.25495, 1.1776)
L <- data.frame(rbind(z,x,c,v,b,n))
names(L) <- c("P1", "P2", "P3", "P4", "P5", "P6", "P7", "P8", "P9","P10", "SysResponse")
L
#my function
Remove <- c("P1", "P5", "P2", "P8", "P9", "P4", "P3", "P6", "P7", "P10")
for (i in 1:length(Remove)){
fit3 <- lm(as.formula(paste('SysResponse~',paste(Remove, collapse="+"))), data=L);
b <- summary(fit3)$coefficients[,4]; # p-value
print(b)
Remove = Remove[-1];
}
This will give you the pvalues of individual features in each steps. (Ignore the Nans. I just failed to make a good fake data.)
What I am trying to do here is instead of throw the bunch of numbers as my output, I would like to create a table with the pvalues of each features in each step.
so my output should look like something like this:
D1 D2 D3 ..... D10
P1 1 . .
P2 2 6 22
P3 3 8 45
P4 4 15 64
P5 5 17 .
P6 6 15 353
P7 7 11 12
P8 8 4 45
P9 9 96 23
P10 10 12 12
because P1 and P5 will be removed.
What can I try next?
Is this what you want?
Remove <- c("P1", "P5", "P2", "P8", "P9", "P4", "P3", "P6", "P7", "P10")
b <- matrix(rep(NA, 100), nrow = 10)
for (i in 1:length(Remove)){
fit3 <- lm(as.formula(paste('SysResponse~',paste(Remove, collapse="+"))), data=L)
b[i,] <- if(length(summary(fit3)$coefficients[,4]) == 10){summary(fit3)$coefficients[,4]}else{
c(rep(NA, 10-length(summary(fit3)$coefficients[,4])), summary(fit3)$coefficients[,4])
}
#print(b)
Remove = Remove[-1]
}
> b
[,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
[1,] NA NA NA NA NaN NaN NaN NaN NaN NaN
[2,] NA NA NA NA NaN NaN NaN NaN NaN NaN
[3,] NA NA NA NA NaN NaN NaN NaN NaN NaN
[4,] NA NA NA NA NaN NaN NaN NaN NaN NaN
[5,] NA NA NA NA NaN NaN NaN NaN NaN NaN
[6,] NA NA NA NA NaN NaN NaN NaN NaN NaN
[7,] NA NA NA NA NA 0.05965568 0.01732815 0.04548212 0.02498853 0.04486418
[8,] NA NA NA NA NA NA 0.53777567 0.65398542 0.73020052 0.77532750
[9,] NA NA NA NA NA NA NA 0.38472799 0.97510236 0.45831503
[10,] NA NA NA NA NA NA NA NA 0.30062420 0.37796597