# Linear regression on data with multiple levels?

Here is a table with values that depend on two variables (VAR1=column names, VAR2=rownames).

> df <- data.frame("A"=c(1,1,2,2,2,3,3,3,3), "B"=c(2,2,3,3,3,4,4,4,4), "C"=c(3,3,4,4,4,5,5,5,5), "D"=c(5,5,6,6,6,10,10,10,10), "E"=c(10,10,15,15,15,20,20,20,20))

> df
A B C  D  E
1 1 2 3  5 10
2 1 2 3  5 10
3 2 3 4  6 15
4 2 3 4  6 15
5 2 3 4  6 15
6 3 4 5 10 20
7 3 4 5 10 20
8 3 4 5 10 20
9 3 4 5 10 20


I want to find out how much the values depend on VAR1 and VAR2.

As you can see, the data have levels: the rows (VAR2) 1-2, 3-5 and 6-9 all have the same values.

Looking at the data, what is the best approach to take for finding out the dependence of values on VAR1 and VAR2? Can this be done with linear regressions (values ~ VAR1, values ~ VAR2)?

• What are these values? You show integer values in your example, so are these count data? Usually you should restructure your data by making row names an actual column and then melting the data (with the reshape2 or tidyr package). Do you have any replicates? Are the row names a continuous variable or are they categorical? – Roland Jan 25 '16 at 10:08
• What do you mean by replicates? The row names and the column names (think E, F, G... AA, AB,...) are continuous variables. – R-obert Jan 25 '16 at 18:42
• Your column names are not a continuous variable. – Roland Jan 25 '16 at 18:50

I think you need to format your data set first. Please the following data.

data = expand.grid(rownames(df), colnames(df))
data\$value = unlist(df)
View(data)


If you think the row names of df and column names of df are uncorrelated to each other, then I guess you can

boxplot(value~Var1, data = data)
boxplot(value~Var2, data = data)
summary(lm(value~Var1, data = data))
summary(lm(value~Var2, data = data))


However, if you think row names of df and column names of df are correlated, namely, comparing column name (for example) B, the value may increase more when row names change from 1 - 9 than values in column name (for example) A, then I guess you need to increase your sample size. Because, it seems to me you only have one sample per group (like group A1).

• Even with a larger dataset, there will be only one sample per group (F10, F11, etc.). What about the levels in the data? For VAR2, the rows 1-2, 3-5 and 6-9 all have the same values. Can we use linear regression for such data? – R-obert Jan 25 '16 at 6:30
• As far as I know, sample per group is actually the sample size. If you only have one sample in a group, I think for that group you won't make inference on it. But for VAR2, I don't think it will be a problem if you have identical values. – WCMC Jan 25 '16 at 17:22