Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/64.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R 拟合线,不同权重的数据点_R - Fatal编程技术网

R 拟合线,不同权重的数据点

R 拟合线,不同权重的数据点,r,R,我想尝试将R中的“最佳拟合线”拟合到一组点上。但我希望每个点都有一个特定的权重,基于该点的精度 我的数据是: x y precision 4 4 2 16 18 5 17 39 4 29 30 20 38 38 11 因此,我希望直线拟合精度较高的点,而不是拟合精度较低的点 我猜这大概是以下几点: abline(lm(y~x+precision)) 但这似乎不起作用 非常感谢然后您需要使用lm函数中的weights参数来执行此操作: df <- read.table

我想尝试将R中的“最佳拟合线”拟合到一组点上。但我希望每个点都有一个特定的权重,基于该点的精度

我的数据是:

x  y   precision
4  4   2
16 18  5
17 39  4
29 30  20
38 38  11
因此,我希望直线拟合精度较高的点,而不是拟合精度较低的点

我猜这大概是以下几点:

abline(lm(y~x+precision))
但这似乎不起作用
非常感谢

然后您需要使用
lm
函数中的
weights
参数来执行此操作:

df <- read.table(header=T, text='x  y   precision
4  4   2
16 18  5
17 39  4
29 30  20
38 38  11')

a <- lm( y ~ x , data=df , weights=precision)

> a

Call:
lm(formula = y ~ x, data = df, weights = precision)

Coefficients:
(Intercept)            x  
    10.7895       0.7096  
这样,根据精度值为每个点指定特定权重

为了让您确切地了解
权重
的工作原理,它复制每一行的次数与
权重
向量指定的次数相同(在本例中为精度)。i、 e


df2然后您需要使用
lm
函数中的
weights
参数来执行以下操作:

df <- read.table(header=T, text='x  y   precision
4  4   2
16 18  5
17 39  4
29 30  20
38 38  11')

a <- lm( y ~ x , data=df , weights=precision)

> a

Call:
lm(formula = y ~ x, data = df, weights = precision)

Coefficients:
(Intercept)            x  
    10.7895       0.7096  
这样,根据精度值为每个点指定特定权重

为了让您确切地了解
权重
的工作原理,它复制每一行的次数与
权重
向量指定的次数相同(在本例中为精度)。i、 e


df2因为您也在询问如何可视化这些

abline()
需要事先调用
plot()
。这应该是x和y的散点图,否则仅直线图不包含任何有用信息

为了可视化回归线是基于加权回归的,您可以为模型输入另一条线,该线具有相等的权重和/或使点大小取决于精度

下面是一个简单的例子:

df <- read.table(header=T, text='x  y   precision
4  4   2
16 18  5
17 39  4
29 30  20
38 38  11')

u <- lm( y ~ x , data=df)
w <- lm( y ~ x , data=df, weights=precision)

plot(df$x, df$y, cex=df$precision/max(df$x)*10)
abline(u, lty=2)
abline(w)

df因为您也在询问如何可视化这些

abline()
需要事先调用
plot()
。这应该是x和y的散点图,否则仅直线图不包含任何有用信息

为了可视化回归线是基于加权回归的,您可以为模型输入另一条线,该线具有相等的权重和/或使点大小取决于精度

下面是一个简单的例子:

df <- read.table(header=T, text='x  y   precision
4  4   2
16 18  5
17 39  4
29 30  20
38 38  11')

u <- lm( y ~ x , data=df)
w <- lm( y ~ x , data=df, weights=precision)

plot(df$x, df$y, cex=df$precision/max(df$x)*10)
abline(u, lty=2)
abline(w)
df