Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/77.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_Lm - Fatal编程技术网

R 加拟合二次曲线

R 加拟合二次曲线,r,lm,R,Lm,我想在绘图中添加一条拟合的二次曲线 abline(lm(data~factor+I(factor^2))) 显示的回归是线性的,而不是二次的,我得到以下信息: 信息d'avis:在abline(lm(数据~因子+I(因子^2)),col= 调色板[迭代]:利用deux premiers des 3 衰减系数 这意味着: 使用3个回归系数中的前2个 当只运行lm()函数时,我没有收到任何消息 以下是一个示例数据: factor <- 1:7 data <- c(0.1375000,0

我想在绘图中添加一条拟合的二次曲线

abline(lm(data~factor+I(factor^2)))
显示的回归是线性的,而不是二次的,我得到以下信息:

信息d'avis:在abline(lm(数据~因子+I(因子^2)),col= 调色板[迭代]:利用deux premiers des 3 衰减系数

这意味着:

使用3个回归系数中的前2个

当只运行
lm()
函数时,我没有收到任何消息

以下是一个示例数据:

factor <- 1:7
data <- c(0.1375000,0.2500000,0.3416667,0.4583333,0.7250000,0.9166667,1.0000000)
factor使用,而不是使用,它将为您提供与预测输入长度相同的向量:

fitted(lm(data~factor+I(factor^2)))
#         1         2         3         4         5         6         7 
# 0.1248016 0.2395833 0.3699405 0.5158730 0.6773810 0.8544643 1.0471230 
因此,类似于:

plot(factor, fitted(lm(data~factor+I(factor^2))), type="l")

到目前为止,我还无法得到答案,因为我使用的数据集的x值并没有增加(如上面David Robinson所述)。我是这样解决的

require(ISLR)
plot(mpg~horsepower, data=Auto)

# fit the model
glm.fit = glm(mpg~poly(horsepower,2), data=Auto)

# create 100 x-values based on min/max of plotted values
minMax = range(Auto$horsepower)
xVals = seq(minMax[1], minMax[2], len = 100) 

# Use predict based on a dataframe containing 'horsepower'
yVals = predict(glm.fit, newdata = data.frame(horsepower = xVals))

lines(xVals, yVals)

感谢所有这些宝贵的回答。小心:

使用

使用基于包含“马力”的数据帧的预测 使用基于包含“马力”的数据帧的预测
lm.fit
是一项功能

yVals = predict(glm.fit, newdata = data.frame(horsepower=xVals)
yVals = predict(lm.fit, newdata = data.frame(horsepower=xVals)