将变量输入回归函数

发布于 2024-09-19 20:50:19 字数 1512 浏览 6 评论 0原文

我有这个 feature_list,其中包含几个可能的值,例如“A”、“B”、“C”等。并且 time_list 中有时间。

因此,我将有一个循环,我将在其中遍历每个不同的值并将其放入公式中。

类似 for(i in ...) ,然后 my_feature <- feature_list[i]my_time <- time_list[i]

然后我将时间和所选特征放入数据帧中以用于回归

feature_list<- c("GPRS")
time_list<-c("time")
calc<-0

feature_dim <- length(feature_list)
time_dim <- length(time_list)

data <- read.csv("data.csv", header = TRUE, sep = ";")
result <- matrix(nrow=0, ncol=5)
errors<-matrix(nrow=0, ncol=3)

for(i in 1:feature_dim) {
    my_feature <- feature_list[i]
    my_time <- time_list[i]

    fitdata <- data.frame(data[my_feature], data[my_time])

    for(j in 1:60) {

        my_b <- 0.0001 * (2^j)

        for(k in 1:60) {
            my_c <- 0.0001 * (2^k)
            cat("Feature: ", my_feature, "\t")
            cat("b: ", my_b, "\t")
            cat("c: ", my_c, "\n")

            err <- try(nlsfit <- nls(GPRS ~ 53E5*exp(-1*b*exp(-1*c*time)), data=fitdata, start=list(b=my_b, c=my_c)), silent=TRUE)
            calc<-calc+1

            if(class(err) == "try-error") {
                next
            }

            else {
                coefs<-coef(nlsfit)
                ess<-deviance(nlsfit)
                result<-rbind(result, c(coefs[1], coefs[2], ess, my_b, my_c))
            }
    }
} 
}

现在在 nls() 调用中我希望能够调用 my_feature 而不仅仅是“A”或“B”或某件事,然后到列表中的下一个。但我在那里遇到错误。我做错了什么?

I have this feature_list that contains several possible values, say "A", "B", "C" etc. And there is time in time_list.

So I will have a loop where I will want to go through each of these different values and put it in a formula.

something like for(i in ...) and then my_feature <- feature_list[i] and my_time <- time_list[i].

Then I put the time and the chosen feature to a dataframe to be used for regression

feature_list<- c("GPRS")
time_list<-c("time")
calc<-0

feature_dim <- length(feature_list)
time_dim <- length(time_list)

data <- read.csv("data.csv", header = TRUE, sep = ";")
result <- matrix(nrow=0, ncol=5)
errors<-matrix(nrow=0, ncol=3)

for(i in 1:feature_dim) {
    my_feature <- feature_list[i]
    my_time <- time_list[i]

    fitdata <- data.frame(data[my_feature], data[my_time])

    for(j in 1:60) {

        my_b <- 0.0001 * (2^j)

        for(k in 1:60) {
            my_c <- 0.0001 * (2^k)
            cat("Feature: ", my_feature, "\t")
            cat("b: ", my_b, "\t")
            cat("c: ", my_c, "\n")

            err <- try(nlsfit <- nls(GPRS ~ 53E5*exp(-1*b*exp(-1*c*time)), data=fitdata, start=list(b=my_b, c=my_c)), silent=TRUE)
            calc<-calc+1

            if(class(err) == "try-error") {
                next
            }

            else {
                coefs<-coef(nlsfit)
                ess<-deviance(nlsfit)
                result<-rbind(result, c(coefs[1], coefs[2], ess, my_b, my_c))
            }
    }
} 
}

Now in the nls() call I want to be able to call my_feature instead of just "A" or "B" or something and then to the next one on the list. But I get an error there. What am I doing wrong?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

凉月流沐 2024-09-26 20:50:19

您可以使用粘贴创建公式的字符串版本,包括所需的变量名称,然后使用 as.formulaformula 函数将其转换为要传递的公式到 nls。

as.formula(paste(my_feature, "~ 53E5*exp(-1*b*exp(-1*c*time))"))

另一种选择是使用 bquote 函数将变量名称插入到函数调用中,然后评估函数调用。

You can use paste to create a string version of your formula including the variable name you want, then use either as.formula or formula functions to convert this to a formula to pass to nls.

as.formula(paste(my_feature, "~ 53E5*exp(-1*b*exp(-1*c*time))"))

Another option is to use the bquote function to insert the variable names into a function call, then eval the function call.

小草泠泠 2024-09-26 20:50:19

我不久前使用过 R,也许你可以尝试一下:

你想要的是创建一个包含变量列表的公式,对吧?

因此,如果响应变量是列表的第一个元素,其他元素是解释变量,您可以这样创建公式:

my_feature[0] ~ reduce("+",my_feature[1:]) 。这可能有效。

这样您就可以创建取决于 my_features 中的变量的公式。

I worked with R a while ago, maybe you can give this a try:

What you want is create a formula with a list of variables right?

so if the response variable is the first element of your list and the others are the explanatory variables you could create your formula this way:

my_feature[0] ~ reduce("+",my_feature[1:]) . This might work.

this way you can create formulae that depends on the variables in my_features.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文