# R how to find the top key parameters contribute to the response var change in two tables?

For example, if I create two tables, both contain multiple kinds of data: numeric (integer), numeric (continuous), and factor (character) like below:

a = c('red','yellow','blue','blue','red','blue','yellow','blue','red','red')
b = c(1,2,3,2,3,2,2,2,1,2)
c = c(1023,432.34,775.33,342.78,3241.45,1029,938,837.32, 739.43,649)
d = c(17313,23523.32,89790,98790.45,98792,498792,23984.87,29739,69198,917638.48)
data.t1 = data.frame(a=a, b=b, c=c, response=d)

a = c('blue','blue','yellow','blue','red','red','yellow','red','blue','red')
b = c(2,1,1,3,2,1,3,1,3,2)
c = c(1775.33,8342.78,649,241.45,29,938,1083,4432.34, 3837.32, 2739.43)
d = c(27313,2423.32,8990,18790.45,27792,4982,2384.87,9739,6198,91638.48)
data.t2 = data.frame(a=a,b=b,c=c, response = d)


I will have two tables like below:

> data.t1
a b       c  response
1     red 1 1023.00  17313.00
2  yellow 2  432.34  23523.32
3    blue 3  775.33  89790.00
4    blue 2  342.78  98790.45
5     red 3 3241.45  98792.00
6    blue 2 1029.00 498792.00
7  yellow 2  938.00  23984.87
8    blue 2  837.32  29739.00
9     red 1  739.43  69198.00
10    red 2  649.00 917638.48
> data.t2
a b       c response
1    blue 2 1775.33 27313.00
2    blue 1 8342.78  2423.32
3  yellow 1  649.00  8990.00
4    blue 3  241.45 18790.45
5     red 2   29.00 27792.00
6     red 1  938.00  4982.00
7  yellow 3 1083.00  2384.87
8     red 1 4432.34  9739.00
9    blue 3 3837.32  6198.00
10    red 2 2739.43 91638.48


data.t1 is the data collected at time 1, and data.t2 is data collected at time 2.

so I want to know, which are the key parameter(s) that contributed the most to the change of the "response" var (or vars, if I can scale it that would be nice as well) from data.t1 to data.t2. for example, if the change in variable a & b contributes most to the increasing (or decreasing) trend in the response var from table at t1 to table at t2, i'd like the code to return var a & b.

note: the data i created are completely random, so may not actually display a "trending" but this is more just for my illustration purpose.

The rows correspond to each other here; ie row 6 of data.t1 corresponds to row 6 of data.t2, and I am interested in the change in response in that row "caused by" in some sense the change in the a, b and c variables

• Do the rows correspond to each other here? ie row 6 of data.t1 corresponds to row 6 of data.t2, and you are interested in the change in response in that row "caused by" in some sense the change in the a, b and c variables? Could you give a clearer made up example where the "key parameters" are obvious? Otherwise this isn't particularly clear. Mar 30, 2016 at 7:55
• yes that is exactly it. i will add that note to my post. thanks for pointing it out @Spacedman Mar 30, 2016 at 16:29

Construct a new data set with differences in the numeric variables and new factors in the categorical data containing the first and second levels to represent change. Then fit a linear model:

data = cbind(data.t1, data.t2)
names(data)=c("a1","b1","c1","response1","a2","b2","c2","response2")
data$da = paste(data$a1,"-",data$a2) data$db = data$b2 - data$b1
data$dc = data$c2 - data$c1 data$dresponse = data$response2 - data$response1


So now the data columns we are interested in look like this:

> data[,9:12]
da db       dc dresponse
1       red - blue  1   752.33     10000
2    yellow - blue -1  7910.44    -21100
3    blue - yellow -2  -126.33    -80800
4      blue - blue  1  -101.33    -80000
5        red - red -1 -3212.45    -71000
6       blue - red -1   -91.00   -493810
7  yellow - yellow  1   145.00    -21600
8       blue - red -1  3595.02    -20000
9       red - blue  2  3097.89    -63000
10       red - red  0  2090.43   -826000


So now see how the change in response from data.t1 to data.t2 depends on the change in the (numeric or factor) variables:

> m = glm(dresponse ~ da + db + dc,data=data)


The summary(m) will give you a coefficients table:

Coefficients:
Estimate Std. Error t value Pr(>|t|)
(Intercept)        4.953e+05  8.904e+05   0.556    0.677
dablue - red      -1.395e+06  1.822e+06  -0.766    0.584
dablue - yellow   -1.713e+06  2.337e+06  -0.733    0.597
dared - blue       2.558e+05  6.113e+05   0.418    0.748
dared - red       -1.206e+06  1.209e+06  -0.998    0.501
dayellow - blue   -1.413e+06  2.666e+06  -0.530    0.690
dayellow - yellow  4.828e+04  6.543e+05   0.074    0.953
db                -5.712e+05  7.489e+05  -0.763    0.585
dc                 4.109e+01  1.541e+02   0.267    0.834


which you can interpret in the usual way. The process you describe about choosing which variables are most influential sounds like you just want the variables with the largest point estimate, but you may want to do some standardisation. For example, in the above, a unit change in b from 1 to 2, say, decreases the dresponse by -570000, but a unit change in c changes the response by +41.09 in the fitted model. But c has a much bigger range, so perhaps you want to standardise your numeric variables to have mean 0 and sd=1 first.

The factor variables have a similar interpretation - rows with "yellow-yellow" increase response by 48280 compared to the average change in response. I'm not sure how you standardise categorical variables, since they are just a bunch of step-changes...

Statistically none of these variables make any difference to the response (none of the t-values are big enough and there's no stars in the P column).