如何编写一个多项式函数,其中我的自变量对数在给定系数的情况下进行了变换?
How do I write a polynomial function with my independent variable log transformed given the coefficients?
预警:我是一个彻头彻尾的菜鸟,所以我很抱歉这个愚蠢的问题。我已经尝试了一切,试图弄清楚如何根据这些系数写出实际的多项式函数。
Coefficients:
Estimate Std. Error t value Pr(>|t|)
(Intercept) 89.6131 0.8525 105.119 < 2e-16
poly(log(x), 3, raw = TRUE)1 -36.8351 2.3636 -15.584 1.13e-10
poly(log(x), 3, raw = TRUE)2 6.9735 1.6968 4.110 0.000928
poly(log(x), 3, raw = TRUE)3 -0.7105 0.3124 -2.274 0.038063
我认为它只是 f(x) = 89.6131 - 36.8351log(x) + 6.9735log(x^2) - 0.7105*log(x^3 ).
我已经尝试了很多变体,但似乎没有任何效果。我正在尝试将我的多项式函数和我的 x 值插入到 Desmos 中并将其变为 return 我在 R 中得到的是:
1 2 3 4 5 6
9.806469 15.028672 20.317227 25.669588 28.757896 35.816853
7 8 9 10 11 12
41.334623 43.919057 49.267966 53.880519 60.862101 63.830004
13 14 15 16 17 18
70.390727 79.412081 80.416065 85.214063 86.165068 98.187744
19
96.723278
我的 x 值为:
x = c(49.64,34.61,23.76,16.31,13.23,8.47,6.19,5.4,4.15,3.37,2.53,2.26,1.79,1.34,1.3,1.13,1.1,0.8,0.83)
建模代码:
#data
x = c(49.64,34.61,23.76,16.31,13.23,8.47,6.19,5.4,4.15,3.37,2.53,2.26,1.79,1.34,1.3,1.13,1.1,0.8,0.83)
y = c(10,15,20,25,30,35,40,45,50,55,60,65,70,75,80,85,90,95,100)
#fitting the model
model1 <- lm(y~poly(log(x),3,raw=TRUE))
new.distance <- data.frame(
distance = c(49.64,34.61,23.76,16.31,13.23,8.47,6.19,5.4,4.15,3.37,2.53,2.26,1.79,1.34,1.3,1.13,1.1,0.83,0.8)
)
predict(model1, newdata = new.distance)
summary(model1)
图书馆
library(tidyverse)
示例数据
x <- c(49.64,34.61,23.76,16.31,13.23,8.47,6.19,5.4,4.15,3.37,2.53,2.26,1.79,1.34,1.3,1.13,1.1,0.8,0.83)
y <- c(10,15,20,25,30,35,40,45,50,55,60,65,70,75,80,85,90,95,100)
df <-
tibble(
x = x,
y = y
) %>%
mutate(
lx = log(x)
)
拟合模型
model1 <- lm(y~poly(log(x),3,raw=TRUE))
预测数据
df_to_pred <-
data.frame(
x_to_pred = c(49.64,34.61,23.76,16.31,13.23,8.47,6.19,5.4,4.15,3.37,2.53,2.26,1.79,1.34,1.3,1.13,1.1,0.83,0.8)
)
原始数据 x 预测值
预测数据-函数x手册
df %>%
cbind(y_pred_model = predict(model1, newdata = new.distance)) %>%
mutate(y_pred_manual = 89.6131 - 36.8351*log(x) + 6.9735*log(x)^2 - 0.7105*log(x)^3) %>%
ggplot(aes(y_pred_manual,y_pred_model))+
geom_abline(intercept = 0,slope = 1,size = 1, col = "red")+
geom_point()
预警:我是一个彻头彻尾的菜鸟,所以我很抱歉这个愚蠢的问题。我已经尝试了一切,试图弄清楚如何根据这些系数写出实际的多项式函数。
Coefficients:
Estimate Std. Error t value Pr(>|t|)
(Intercept) 89.6131 0.8525 105.119 < 2e-16
poly(log(x), 3, raw = TRUE)1 -36.8351 2.3636 -15.584 1.13e-10
poly(log(x), 3, raw = TRUE)2 6.9735 1.6968 4.110 0.000928
poly(log(x), 3, raw = TRUE)3 -0.7105 0.3124 -2.274 0.038063
我认为它只是 f(x) = 89.6131 - 36.8351log(x) + 6.9735log(x^2) - 0.7105*log(x^3 ).
我已经尝试了很多变体,但似乎没有任何效果。我正在尝试将我的多项式函数和我的 x 值插入到 Desmos 中并将其变为 return 我在 R 中得到的是:
1 2 3 4 5 6
9.806469 15.028672 20.317227 25.669588 28.757896 35.816853
7 8 9 10 11 12
41.334623 43.919057 49.267966 53.880519 60.862101 63.830004
13 14 15 16 17 18
70.390727 79.412081 80.416065 85.214063 86.165068 98.187744
19
96.723278
我的 x 值为:
x = c(49.64,34.61,23.76,16.31,13.23,8.47,6.19,5.4,4.15,3.37,2.53,2.26,1.79,1.34,1.3,1.13,1.1,0.8,0.83)
建模代码:
#data
x = c(49.64,34.61,23.76,16.31,13.23,8.47,6.19,5.4,4.15,3.37,2.53,2.26,1.79,1.34,1.3,1.13,1.1,0.8,0.83)
y = c(10,15,20,25,30,35,40,45,50,55,60,65,70,75,80,85,90,95,100)
#fitting the model
model1 <- lm(y~poly(log(x),3,raw=TRUE))
new.distance <- data.frame(
distance = c(49.64,34.61,23.76,16.31,13.23,8.47,6.19,5.4,4.15,3.37,2.53,2.26,1.79,1.34,1.3,1.13,1.1,0.83,0.8)
)
predict(model1, newdata = new.distance)
summary(model1)
图书馆
library(tidyverse)
示例数据
x <- c(49.64,34.61,23.76,16.31,13.23,8.47,6.19,5.4,4.15,3.37,2.53,2.26,1.79,1.34,1.3,1.13,1.1,0.8,0.83)
y <- c(10,15,20,25,30,35,40,45,50,55,60,65,70,75,80,85,90,95,100)
df <-
tibble(
x = x,
y = y
) %>%
mutate(
lx = log(x)
)
拟合模型
model1 <- lm(y~poly(log(x),3,raw=TRUE))
预测数据
df_to_pred <-
data.frame(
x_to_pred = c(49.64,34.61,23.76,16.31,13.23,8.47,6.19,5.4,4.15,3.37,2.53,2.26,1.79,1.34,1.3,1.13,1.1,0.83,0.8)
)
原始数据 x 预测值
预测数据-函数x手册
df %>%
cbind(y_pred_model = predict(model1, newdata = new.distance)) %>%
mutate(y_pred_manual = 89.6131 - 36.8351*log(x) + 6.9735*log(x)^2 - 0.7105*log(x)^3) %>%
ggplot(aes(y_pred_manual,y_pred_model))+
geom_abline(intercept = 0,slope = 1,size = 1, col = "red")+
geom_point()