R 中的线性回归(正态数据和对数数据)
我想在 R 中对正态图和双对数图中的数据进行线性回归。
对于正常数据,数据集可能如下:
lin <- data.frame(x = c(0:6), y = c(0.3, 0.1, 0.9, 3.1, 5, 4.9, 6.2))
plot (lin$x, lin$y)
我想计算仅对数据点 2、3 和 4 的线性回归绘制一条线。
对于双对数数据数据集可能如下:
data = data.frame(
x=c(1:15),
y=c(
1.000, 0.742, 0.623, 0.550, 0.500, 0.462, 0.433,
0.051, 0.043, 0.037, 0.032, 0.028, 0.025, 0.022, 0.020
)
)
plot (data$x, data$y, log="xy")
这里我想绘制数据集 1:7 和 8:15 的回归线。
我可以计算斜率和y偏移以及拟合参数(R^2,p值)?
对于正态数据和对数数据如何完成?
谢谢你的帮助,
斯文
I want to carry out a linear regression in R for data in a normal and in a double logarithmic plot.
For normal data the dataset might be the follwing:
lin <- data.frame(x = c(0:6), y = c(0.3, 0.1, 0.9, 3.1, 5, 4.9, 6.2))
plot (lin$x, lin$y)
There I want to calculate draw a line for the linear regression only of the datapoints 2, 3 and 4.
For double logarithmic data the dataset might be the following:
data = data.frame(
x=c(1:15),
y=c(
1.000, 0.742, 0.623, 0.550, 0.500, 0.462, 0.433,
0.051, 0.043, 0.037, 0.032, 0.028, 0.025, 0.022, 0.020
)
)
plot (data$x, data$y, log="xy")
Here I want to draw the regression line for the datasets 1:7 and for 8:15.
Ho can I calculate the slope and the y-offset als well as parameters for the fit (R^2, p-value)?
How is it done for normal and for logarithmic data?
Thanks for you help,
Sven
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在 R 中,线性最小二乘模型通过 lm() 函数进行拟合。使用公式接口,我们可以使用
subset
参数来选择用于拟合实际模型的数据点,例如:给出:
至于双对数,我猜你有两种选择; i) 按照我们上面的方法估计两个单独的模型,或者 ii) 通过 ANCOVA 估计。对数转换是使用
log()
在公式中完成的。通过两个单独的模型:
或者通过 ANCOVA,我们需要一个指示变量
您可能会问这两种方法是否等效?确实如此,我们可以通过模型系数来证明这一点。
因此,单独模型的两个斜率分别为 -0.4306 和 -1.4967。 ANCOVA 模型的系数为:
我们如何协调两者?嗯,我设置 ind 的方式,对 logm3 进行了参数化,以给出从 logm2 估计的更直接的值;
logm2
和logm3
的截距相同,log(x)
的系数也相同。获得相当于系数的值对于
logm1
,我们需要进行操作,首先是截距:其中
indTRUE
的系数是第 1 组平均值与第 2 组平均值的差值。对于斜率:与我们获得的
logm1
相同,并且基于组 2 (coefs[2]
) 的斜率,并根据斜率差异进行修改第1组(coefs[4]
)。至于绘图,一个简单的方法是通过
abline()
来绘制简单模型。例如,对于普通数据示例:对于日志数据,我们可能需要更有创意,这里的一般解决方案是在数据范围内进行预测并绘制预测结果:
可以通过对 < code>yhat
或对数刻度:
例如...
这个通用解决方案也适用于更复杂的 ANCOVA 模型。在这里,我像以前一样创建一个新的 pdat 并添加一个指标。
请注意,由于使用了 ANCOVA 来拟合
logm3<,因此我们如何从对
predict()
的一次调用中获得我们想要的所有预测。 /代码>。我们现在可以像以前一样绘制:In R, linear least squares models are fitted via the
lm()
function. Using the formula interface we can use thesubset
argument to select the data points used to fit the actual model, for example:giving:
As for the double log, you have two choices I guess; i) estimate two separate models as we did above, or ii) estimate via ANCOVA. The log transformation is done in the formula using
log()
.Via two separate models:
Or via ANCOVA, where we need an indicator variable
You might ask if these two approaches are equivalent? Well they are and we can show this via the model coefficients.
So the two slopes are -0.4306 and -1.4967 for the separate models. The coefficients for the ANCOVA model are:
How do we reconcile the two? Well the way I set up
ind
,logm3
is parametrised to give more directly values estimated fromlogm2
; the intercepts oflogm2
andlogm3
are the same, as are the coefficients forlog(x)
. To get the values equivalent to the coefficientsof
logm1
, we need to do a manipulation, first for the intercept:where the coefficient for
indTRUE
is the difference in the mean of group 1 over the mean of group 2. And for the slope:which is the same as we got for
logm1
and is based on the slope for group 2 (coefs[2]
) modified by the difference in slope for group 1 (coefs[4]
).As for plotting, an easy way is via
abline()
for simple models. E.g. for the normal data example:For the log data we might need to be a bit more creative, and the general solution here is to predict over the range of data and plot the predictions:
Which can plot on the original scale, by exponentiating
yhat
or on the log scale:
For example...
This general solution works well for the more complex ANCOVA model too. Here I create a new pdat as before and add in an indicator
Notice how we get all the predictions we want from the single call to
predict()
because of the use of ANCOVA to fitlogm3
. We can now plot as before:一般来说,将数据分成不同的组并在不同的子集上运行不同的模型是不寻常的,而且可能是不好的形式。您可能需要考虑添加分组变量
并在整个数据集上运行某种模型,例如,
In general, splitting the data into different groups and running different models on different subsets is unusual, and probably bad form. You may want to consider adding a grouping variable
and running some sort of model on the whole dataset, e.g.,