要查看年龄和某个变量的四分位数之间是否存在线性趋势,我使用lm
拟合了线性模型。残差相对于拟合值的图以及残差相对于四分位数的图表示方差的异质性。
此图像是通过以下方式创建的:
m1 <- lm(age ~ quartile, data = DF) #DF = dataframe
op <- par(mfrow = c(1,3))
plot(resid(m1) ~ fitted(m1)) #Homogeneity of variances: graphical
plot(resid(m1) ~ DF$quartile)
qqnorm(resid(m1));qqline(resid(m1))
par(op)
在GLS框架中,我想使用varFunc
包中nlme
中的类之一,使剩余方差取决于四分位数。我尝试了多种功能,但没有成功。
下面的样本数据大致产生相同的模式:
reconstruct <- structure(list(quartile = structure(c(2L, 1L, 4L, 3L, 1L, 1L,
3L, 4L, 3L, 2L, 2L, 3L, 3L, 1L, 2L, 4L, 2L, 2L, 2L, 1L, 1L, 3L,
1L, 1L, 1L, 3L, 3L, 1L, 4L, 3L, 3L, 3L, 2L, 4L, 1L, 1L, 3L, 1L,
3L, 2L, 2L, 4L, 3L, 4L, 1L, 4L, 1L, 4L, 3L, 1L, 1L, 2L, 4L, 2L,
2L, 2L, 1L, 1L, 4L, 1L, 4L, 4L, 3L, 3L, 4L, 4L, 1L, 1L, 2L, 1L,
4L, 3L, 4L, 2L, 3L, 3L, 3L, 1L, 1L, 4L, 1L, 2L, 1L, 2L, 1L, 1L,
2L, 4L, 1L, 3L, 4L, 2L, 4L, 1L, 4L, 4L, 1L, 3L, 4L, 2L, 2L, 1L,
1L, 4L, 2L, 4L, 3L, 4L, 4L, 4L, 3L, 3L, 3L, 3L, 2L, 2L, 4L, 2L,
4L, 1L, 4L, 3L, 4L, 1L, 2L, 1L, 4L, 2L, 1L, 3L, 1L, 4L, 1L, 4L,
4L, 4L, 1L, 1L, 4L, 2L, 4L, 3L, 2L, 2L, 1L, 3L, 1L, 4L, 2L, 3L,
4L, 3L, 4L, 1L, 1L, 2L, 2L, 4L, 1L, 2L, 4L, 2L, 1L, 2L, 1L, 1L,
4L, 3L, 2L, 3L, 2L, 4L, 3L, 4L, 1L, 4L, 1L, 3L, 4L, 4L, 4L, 1L,
4L, 3L, 2L, 4L, 3L, 3L, 2L, 1L, 1L, 4L, 1L, 4L, 2L, 2L, 2L, 4L,
2L, 3L), .Label = c("1", "2", "3", "4"), class = c("ordered",
"factor")), age = c(40.45, 33.49, 41.02, 53.06, 63.46, 47.17,
39.45, 60.71, 67.13, 53.12, 62.78, 70.39, 56.14, 50.55, 35.64,
38.5, 68.53, 53.69, 50.84, 38.66, 35.31, 57.03, 37.84, 35.82,
50.68, 56.44, 65.36, 58.64, 55.98, 56.13, 42.09, 54.91, 35.16,
63.68, 44.5, 51.79, 69.56, 59.11, 55.39, 43.87, 58.12, 65.59,
52.58, 60.17, 48.57, 52.09, 40.04, 35.61, 77.14, 43.82, 48.98,
36.26, 44.63, 62.13, 69.59, 41.22, 47.85, 53.5, 42.08, 49.08,
75.49, 52.39, 41.21, 58.25, 74.37, 64.28, 34.01, 42.99, 34.05,
60.99, 68.82, 41.3, 71.07, 55.21, 52.01, 37.76, 64.54, 57.43,
45.78, 62.9, 67.73, 49.25, 69.68, 51.85, 37.32, 47.37, 53.41,
68.55, 35.31, 63.59, 69.04, 48.03, 50.74, 42.93, 79.23, 72.22,
35.42, 43.26, 45.81, 37.92, 39.26, 60.97, 47.36, 50.19, 43.52,
41.82, 40.42, 54.87, 55.32, 75.74, 69.54, 56.44, 59.85, 50.02,
49.23, 48.38, 34.07, 38.57, 46.57, 35.29, 42.04, 63.35, 34.68,
50.34, 72.5, 40.27, 58.41, 37.79, 34.62, 75.47, 38.91, 46.21,
49.72, 40.55, 66.98, 59.07, 55.8, 38.86, 47.76, 59.16, 74.79,
57.87, 54.82, 43.58, 66.15, 34.55, 50.12, 67.68, 61.1, 40.29,
54.1, 69.8, 60.68, 36.7, 38.31, 46.15, 34.68, 41.92, 38.97, 50.67,
68.53, 40.06, 46.5, 44.38, 47.6, 37.95, 78.39, 54.73, 79.07,
40.05, 48.67, 58.71, 73.07, 75.65, 43.07, 48.25, 44.03, 51.37,
62.16, 54.78, 66.27, 50.25, 60.56, 32.77, 68.41, 37.74, 38.46,
46.33, 41.59, 64.52, 53.66, 71.04, 64.55, 53.25, 40.58, 52.33,
39.64, 52.76, 43.52, 48.45)), row.names = c(1:200), class = "data.frame")
要获取图像:
m2 <- lm(age ~ quartile, data = reconstruct)
op <- par(mfrow = c(1,3))
plot(resid(m2) ~ fitted(m2))
plot(resid(m2) ~ reconstruct$quartile)
qqnorm(resid(m2));qqline(resid(m2))
par(op)
有什么建议吗?