根据单独数据框中的选择标准创建大量统计模型

时间:2018-12-06 09:26:00

标签: r loops dataframe formula

我想根据数据框中指定的选择标准执行一定数量的统计模型。因此,使用一个基本示例,假设我有2个响应变量和2个解释变量:

#######################Data Input############################
Responses <- as.data.frame(matrix(sample(0:10, 1*100, replace=TRUE), ncol=2))
colnames(Responses) <- c("A","B")

Explanatories <- as.data.frame(matrix(sample(20:30, 1*100, replace=TRUE), ncol=2))
colnames(Explanatories) <- c("x","y")

然后,我定义我要运行的统计模型,其中可以包括响应/解释变量的不同组合以及不同的统计功能:

###################Model selection#########################
Function <- c("LIN","LOG","EXP") ##Linear, Logarithmic (base 10) and exponential - see the formula for these below
Respo <- c("A","B","B")
Explan <- c("x","x","y")
Model_selection <- data.frame(Function,Respo,Explan)

然后如何根据这些选择标准执行模型列表?这是我想根据Model_selection数据帧的输入创建的模型的示例。

####################Model creation#########################
Models <- list(
lm(Responses$A ~ Explanatories$x),
lm(Responses$B ~ log10(Explanatories$x)),
lm(Responses$B ~ exp(Explanatories$y))
)

我猜想将需要某种循环功能,并且环顾四周之后也可能粘贴吗?在此先感谢您的任何帮助

2 个答案:

答案 0 :(得分:1)

这不是最漂亮的解决方案,但它似乎适用于您的示例:

/*  collection-date.js
    A script designed to find x number of working days prior to any given date  
*/

// A data endpoint for bank holidays
let bankHolidaysData
// Bank holidays for England and Wales
let england
// An array of bank holidays
let bankHolidays = []

// A boolean specifying whether or not a date is a holiday
let isHoliday = false

// Bool for finding 4 days in the past
let foundFour = false
let fourDays

// Used to subtract the dates
let counter = 1.
// Used as a index for number of days passed
let workingDays = 0
let oneDay = 24 * 60 * 60 * 1000;

// Used to format our dates to match that of gov.uk
let startDateY, startDateM, startDateD, fourdaysY, fourDaysM, fourDaysD, startDateStr, fourDaysStr

// Get the current UK bank holidays - We use the Gov.UK API to account for substitute days
fetch('https://www.gov.uk/bank-holidays.json')
    .then(blob => blob.json())
    // Send the data to getBankHolidays
    .then(data => getBankHolidays(data))
    // log any errors for the IT monkeys
    .catch(err => {console.log(err)});


function getBankHolidays(data) {
    // Parse the data
    bankHolidaysData = data
    // Capture the events from the API
    england = bankHolidaysData["england-and-wales"].events

    // We need to add leading zeros where a day or month is represented by a single number
    zeroPad = function(num, pad) {
      return ((Math.pow(10, pad) + num) + '').slice(1);
    };

    // Add individual dates to bankHolidays array
    for (let index = 0, length = england.length; index < length; index ++) {
        bankHolidays.push(england[index].date)
    }

    // Instantiate a date object from the dateString variable 
    var startDate = new Date('2018-12-6');
    // Get the day of a week as a number, 0 Sunday
    var dayOfWeek = startDate.getDay();

    startDateY = startDate.getFullYear()
    startDateM = zeroPad(startDate.getMonth() + 1, 2)
    startDateD = zeroPad(startDate.getDate(), 2)

    dateString =  startDateY + '-' + startDateM + '-' + startDateD
    while (!foundFour) {
        if (dayOfWeek === 0 || dayOfWeek === 6 || bankHolidays.includes(dateString)) {
            alert('The day you\'ve selected falls on either a weekend or a bank holiday. Please select a working day.');
        } else {
                    while(!foundFour) {
                        if (!foundFour) {
                            fourDays = new Date(startDate - counter * oneDay);
                            if (fourDaysDow !== 0 || fourDaysDow !== 6 || bankHolidays.includes(fourDaysStr)) {
                                workingDays++;
                                var fourDaysDow = fourDays.getDay()

                                fourDaysY = startDate.getFullYear()
                                fourDaysM = zeroPad(startDate.getMonth() + 1, 2)
                                fourDaysD = zeroPad(startDate.getDate(), 2)
                                fourDaysStr =  fourDaysY + '-' + fourDaysM + '-' + fourDaysD

                                if (workingDays === 4) {
                                    foundFour = true;
                                }
                            } else {
                                fourDays.setDate(fourDays.getDate() -1);
                            }
                }
                counter++
                console.log(fourDays)
            } 
        }
    }
}

答案 1 :(得分:1)

这是PI 18 17 17 f>str-rdp type \ 3.14159265358979E0 ok PI 18 17 17 f.rdp \ 3.14159265358979E0 ok PI f. \ 3.14159265358979 ok

的完美用例
tidyverse